╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1465 1466 // find patches merged by commits received in a push 1467 pub fn detectMerged( 1468 comptime repo_opts: rp.RepoOpts(.xit), 1469 state: rp.Repo(.xit, repo_opts).State(.read_write), 1470 db: *evt.EventDB(repo_opts.hash), 1471 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1472 io: std.Io, 1473 allocator: std.mem.Allocator, 1474 users_dir: []const u8, 1475 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1476 error_writer: *std.Io.Writer, 1477 ) !void { 1478 const DB = evt.EventDB(repo_opts.hash); 1479 var arena = std.heap.ArenaAllocator.init(allocator); 1480 defer arena.deinit(); 1481 1482 // read the revision index used to identify merged patches. 1483 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1484 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1485 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1486 1487 // ignore event refs and inspect only changed branch tips. 1488 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1489 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1490 const heads_prefix = "refs/heads/"; 1491 for (updates) |update| { 1492 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1493 std.mem.eql(u8, update.ref_name, events_ref_path) or 1494 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1495 1496 // validate the ref tips and skip branch deletions. 1497 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1498 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1499 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1500 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1501 @memcpy(&old_oid, update.old_oid); 1502 @memcpy(&new_oid, update.new_oid); 1503 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1504 1505 // traverse commits introduced by this branch update. 1506 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1507 defer commits.deinit(); 1508 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1509 var targets: std.ArrayList(struct { 1510 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1511 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1512 }) = .empty; 1513 1514 // collect merge boundaries along the target's first-parent history. 1515 { 1516 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1517 defer first_parents.deinit(); 1518 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1519 try first_parents.include(&new_oid); 1520 while (try first_parents.next(arena.allocator())) |commit| { 1521 defer commit.deinit(); 1522 try targets.append(arena.allocator(), .{ 1523 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1524 .after_oid = commit.oid, 1525 }); 1526 } 1527 } 1528 1529 // visit target commits oldest first, attributing each revision to the 1530 // commit that introduced it; the shared iterator visits each ancestor once 1531 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1532 while (targets.pop()) |target| { 1533 try commits.include(&target.after_oid); 1534 while (try commits.next(arena.allocator())) |commit| { 1535 defer commit.deinit(); 1536 1537 // find patches whose selected revision matches this commit. 1538 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1539 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1540 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1541 var ids_iter = try ids.iterator(); 1542 while (try ids_iter.next()) |cursor| { 1543 const pair = try cursor.readKeyValuePair(); 1544 var patch_id: [evt.event_id_size]u8 = undefined; 1545 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1546 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1547 1548 // a fast-forward has no recorded merge boundary; use the old ref tip 1549 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1550 importMerged(repo_opts, state, db, moment, io, allocator, users_dir, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1551 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1552 continue; 1553 }; 1554 } 1555 } 1556 } 1557 } 1558 } 1559