╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1191 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 1192 return .{ .name = name, .email = line[open + 1 .. close] }; 1193 } 1194 1195 // import a revision after detecting a client-side merge 1196 fn importMerged( 1197 comptime repo_opts: rp.RepoOpts(.xit), 1198 state: rp.Repo(.xit, repo_opts).State(.read_write), 1199 db: *evt.EventDB(repo_opts.hash), 1200 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1201 io: std.Io, 1202 allocator: std.mem.Allocator, 1203 repo_root_path: []const u8, 1204 patch_id: *const [evt.event_id_size]u8, 1205 matched_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1206 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1207 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1208 ) !void { 1209 var arena = std.heap.ArenaAllocator.init(allocator); 1210 defer arena.deinit(); 1211 1212 // identify which selected revision was merged by the client. 1213 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 1214 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 1215 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1216 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 1217 const selected = record.event.revision orelse return; 1218 const revision: evt.Patch.MergeRevision = if (std.mem.eql(u8, matched_oid, selected.source_oid)) .source else if (std.mem.eql(u8, matched_oid, selected.squash_oid)) .squash else return; 1219 1220 // open the fork when the revision is not stored in the target repository. 1221 var fork_repo_maybe: ?rp.Repo(.xit, repo_opts) = if (record.event.source_branch != null) null else try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 1222 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 1223 1224 // import the revision and record the client-side merge. 1225 _ = try importMergedRevision(repo_opts, state, db, moment, io, allocator, if (fork_repo_maybe) |*repo| repo else null, patch_id, .{ .revision = revision, .before_oid = before_oid, .after_oid = after_oid }, null, null, 0); 1226 } 1227 1228 // import the revision and mark its patch merged in the current transaction 1229 fn importMergedRevision( 1230 comptime repo_opts: rp.RepoOpts(.xit), 1231 state: rp.Repo(.xit, repo_opts).State(.read_write), 1232 db: *evt.EventDB(repo_opts.hash), 1233 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1234 io: std.Io, 1235 allocator: std.mem.Allocator, 1236 fork_repo: ?*rp.Repo(.xit, repo_opts), 1237 patch_id: *const [evt.event_id_size]u8, 1238 merged: struct { 1239 revision: evt.Patch.MergeRevision, 1240 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1241 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1242 }, 1243 expected_patch: ?evt.Patch, 1244 merged_author: ?evt.CommitAuthor, 1245 merged_timestamp: u64, 1246 ) !bool { 1247 const DB = evt.EventDB(repo_opts.hash); 1248 var arena = std.heap.ArenaAllocator.init(allocator); 1249 defer arena.deinit(); 1250 1251 // read the patch and reject outdated or conflicting merge requests. 1252 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1253 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 1254 const selected = patch_record.event.revision orelse return false; 1255 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 1256 if (expected_patch) |expected| { 1257 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 1258 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 1259 const conflicts = try DB.SortedMap(.read_only).init(cursor); 1260 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 1261 } 1262 } 1263 const revision_id = try evt.parseEventId(&selected.id); 1264 1265 // resolve the selected revision from the target or its fork. 1266 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 1267 const revision = (if (fork_repo) |repo| 1268 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 1269 else 1270 current_revision) orelse return false; 1271 if (!selected.matches(revision)) return false; 1272 1273 // read the original revision commit and author. 1274 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1275 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = if (fork_repo) |repo| &repo.core else state.core, .extra = .{ .moment = &fork_repo_moment } }; 1276 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1277 if (revision.event_oid.len != event_oid.len) return false; 1278 @memcpy(&event_oid, revision.event_oid); 1279 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1280 defer event_object.deinit(); 1281 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1282 1283 // retain the submitted revision if the target has not imported it yet. 1284 var events_buffer: [3]evt.EventWithId = undefined; 1285 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1286 var tree_entries: [2]evt.EventTreeEntry = undefined; 1287 const needs_import = if (current_revision) |existing| existing.removed else true; 1288 if (needs_import) { 1289 tree_entries = .{ 1290 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1291 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1292 }; 1293 events.appendAssumeCapacity(.{ 1294 .id = selected.id, 1295 .timestamp = event_object.content.commit.metadata.timestamp, 1296 .author = original_author, 1297 .tree_entries = &tree_entries, 1298 .event = .{ .patchrev = revision.event }, 1299 }); 1300 } else { 1301 const existing = current_revision orelse unreachable; 1302 if (!selected.matches(existing)) return error.InvalidPatch; 1303 } 1304 1305 // snapshot the merge result and mark the patch as merged. 1306 { 1307 const author = merged_author orelse original_author; 1308 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1309 var patch = patch_record.event; 1310 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1311 events.appendAssumeCapacity(result.event); 1312 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1313 events.appendAssumeCapacity(.{ 1314 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1315 .timestamp = timestamp, 1316 .author = author, 1317 .event = .{ .patch = patch }, 1318 }); 1319 } 1320 1321 // commit and consume the revision and patch events together. 1322 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1323 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 1324 return true; 1325 } 1326 1327 // find patches merged by commits received in a push 1328 pub fn detectMerged( 1329 comptime repo_opts: rp.RepoOpts(.xit), 1330 state: rp.Repo(.xit, repo_opts).State(.read_write), 1331 db: *evt.EventDB(repo_opts.hash), 1332 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1333 io: std.Io, 1334 allocator: std.mem.Allocator, 1335 repo_root_path: []const u8, 1336 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1337 error_writer: *std.Io.Writer, 1338 ) !void { 1339 const DB = evt.EventDB(repo_opts.hash); 1340 var arena = std.heap.ArenaAllocator.init(allocator); 1341 defer arena.deinit(); 1342 1343 // read the revision index used to identify merged patches. 1344 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1345 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1346 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1347 1348 // ignore event refs and inspect only changed branch tips. 1349 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1350 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1351 const heads_prefix = "refs/heads/"; 1352 for (updates) |update| { 1353 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1354 std.mem.eql(u8, update.ref_name, events_ref_path) or 1355 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1356 1357 // validate the ref tips and skip branch deletions. 1358 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1359 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1360 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1361 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1362 @memcpy(&old_oid, update.old_oid); 1363 @memcpy(&new_oid, update.new_oid); 1364 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1365 1366 // traverse commits introduced by this branch update. 1367 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1368 defer commits.deinit(); 1369 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1370 var targets: std.ArrayList(struct { 1371 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1372 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1373 }) = .empty; 1374 1375 // collect merge boundaries along the target's first-parent history. 1376 { 1377 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1378 defer first_parents.deinit(); 1379 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1380 try first_parents.include(&new_oid); 1381 while (try first_parents.next(arena.allocator())) |commit| { 1382 defer commit.deinit(); 1383 try targets.append(arena.allocator(), .{ 1384 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1385 .after_oid = commit.oid, 1386 }); 1387 } 1388 } 1389 1390 // visit target commits oldest first, attributing each revision to the 1391 // commit that introduced it; the shared iterator visits each ancestor once 1392 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1393 while (targets.pop()) |target| { 1394 try commits.include(&target.after_oid); 1395 while (try commits.next(arena.allocator())) |commit| { 1396 defer commit.deinit(); 1397 1398 // find patches whose selected revision matches this commit. 1399 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1400 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1401 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1402 var ids_iter = try ids.iterator(); 1403 while (try ids_iter.next()) |cursor| { 1404 const pair = try cursor.readKeyValuePair(); 1405 var patch_id: [evt.event_id_size]u8 = undefined; 1406 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1407 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1408 1409 // a fast-forward has no recorded merge boundary; use the old ref tip 1410 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1411 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1412 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1413 continue; 1414 }; 1415 } 1416 } 1417 } 1418 } 1419 } 1420