╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1263 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 1264 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 1265 const conflicts = try DB.SortedMap(.read_only).init(cursor); 1266 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 1267 } 1268 } 1269 const revision_id = try evt.parseEventId(&selected.id); 1270 1271 // resolve the selected revision from the target or its fork. 1272 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 1273 const revision = (if (fork_repo) |repo| 1274 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 1275 else 1276 current_revision) orelse return false; 1277 if (!selected.matches(revision)) return false; 1278 1279 // read the original revision commit and author. 1280 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1281 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 } }; 1282 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1283 if (revision.event_oid.len != event_oid.len) return false; 1284 @memcpy(&event_oid, revision.event_oid); 1285 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1286 defer event_object.deinit(); 1287 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1288 1289 // retain the submitted revision if the target has not imported it yet. 1290 var events_buffer: [3]evt.EventWithId = undefined; 1291 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1292 var tree_entries: [2]evt.EventTreeEntry = undefined; 1293 const needs_import = if (current_revision) |existing| existing.removed else true; 1294 if (needs_import) { 1295 tree_entries = .{ 1296 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1297 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1298 }; 1299 events.appendAssumeCapacity(.{ 1300 .id = selected.id, 1301 .timestamp = event_object.content.commit.metadata.timestamp, 1302 .author = original_author, 1303 .tree_entries = &tree_entries, 1304 .event = .{ .patchrev = revision.event }, 1305 }); 1306 } else { 1307 const existing = current_revision orelse unreachable; 1308 if (!selected.matches(existing)) return error.InvalidPatch; 1309 } 1310 1311 // snapshot the merge result and mark the patch as merged. 1312 { 1313 const author = merged_author orelse original_author; 1314 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1315 var patch = patch_record.event; 1316 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1317 events.appendAssumeCapacity(result.event); 1318 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1319 events.appendAssumeCapacity(.{ 1320 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1321 .timestamp = timestamp, 1322 .author = author, 1323 .event = .{ .patch = patch }, 1324 }); 1325 } 1326 1327 // commit and consume the revision and patch events together. 1328 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1329 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 1330 return true; 1331 } 1332 1333 // find patches merged by commits received in a push 1334 pub fn detectMerged( 1335 comptime repo_opts: rp.RepoOpts(.xit), 1336 state: rp.Repo(.xit, repo_opts).State(.read_write), 1337 db: *evt.EventDB(repo_opts.hash), 1338 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1339 io: std.Io, 1340 allocator: std.mem.Allocator, 1341 repo_root_path: []const u8, 1342 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1343 error_writer: *std.Io.Writer, 1344 ) !void { 1345 const DB = evt.EventDB(repo_opts.hash); 1346 var arena = std.heap.ArenaAllocator.init(allocator); 1347 defer arena.deinit(); 1348 1349 // read the revision index used to identify merged patches. 1350 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1351 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1352 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1353 1354 // ignore event refs and inspect only changed branch tips. 1355 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1356 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1357 const heads_prefix = "refs/heads/"; 1358 for (updates) |update| { 1359 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1360 std.mem.eql(u8, update.ref_name, events_ref_path) or 1361 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1362 1363 // validate the ref tips and skip branch deletions. 1364 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1365 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1366 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1367 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1368 @memcpy(&old_oid, update.old_oid); 1369 @memcpy(&new_oid, update.new_oid); 1370 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1371 1372 // traverse commits introduced by this branch update. 1373 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1374 defer commits.deinit(); 1375 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1376 var targets: std.ArrayList(struct { 1377 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1378 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1379 }) = .empty; 1380 1381 // collect merge boundaries along the target's first-parent history. 1382 { 1383 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1384 defer first_parents.deinit(); 1385 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1386 try first_parents.include(&new_oid); 1387 while (try first_parents.next(arena.allocator())) |commit| { 1388 defer commit.deinit(); 1389 try targets.append(arena.allocator(), .{ 1390 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1391 .after_oid = commit.oid, 1392 }); 1393 } 1394 } 1395 1396 // visit target commits oldest first, attributing each revision to the 1397 // commit that introduced it; the shared iterator visits each ancestor once 1398 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1399 while (targets.pop()) |target| { 1400 try commits.include(&target.after_oid); 1401 while (try commits.next(arena.allocator())) |commit| { 1402 defer commit.deinit(); 1403 1404 // find patches whose selected revision matches this commit. 1405 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1406 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1407 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1408 var ids_iter = try ids.iterator(); 1409 while (try ids_iter.next()) |cursor| { 1410 const pair = try cursor.readKeyValuePair(); 1411 var patch_id: [evt.event_id_size]u8 = undefined; 1412 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1413 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1414 1415 // a fast-forward has no recorded merge boundary; use the old ref tip 1416 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1417 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1418 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1419 continue; 1420 }; 1421 } 1422 } 1423 } 1424 } 1425 } 1426