╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
586 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 587 if (record.removed) return error.PatchDataUnavailable; 588 589 var patch = record.event; 590 patch.title = input.title; 591 patch.tags = input.tags; 592 patch.description = input.description; 593 if (!std.mem.eql(u8, patch.target_branch, input.target_branch)) patch.revision = null; 594 patch.target_branch = input.target_branch; 595 try evt.consume(.server, .fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 596 .id = input.id, 597 .timestamp = input.timestamp, 598 .author = input.author, 599 .event = .{ .patch = patch }, 600 }}); 601 return true; 602 } 603 604 // merge the selected patch revision into its target branch 605 pub fn merge( 606 comptime repo_opts: rp.RepoOpts(.xit), 607 io: std.Io, 608 allocator: std.mem.Allocator, 609 repo_root_path: []const u8, 610 target_repo: *rp.Repo(.xit, repo_opts), 611 input: MergeInput, 612 ) !void { 613 const patch_id = try evt.parseEventId(&input.id); 614 errdefer |err| { 615 if (err == error.PatchDataUnavailable or err == error.PatchOutOfDate) { 616 refreshMergeability(repo_opts, io, allocator, target_repo, patch_id); 617 } 618 } 619 var arena = std.heap.ArenaAllocator.init(allocator); 620 defer arena.deinit(); 621 622 // validate the target patch 623 const target_moment = try evt.currentMoment(repo_opts, target_repo); 624 const patch_record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_moment, &arena, &patch_id)) orelse return error.InvalidPatch; 625 if (patch_record.removed) return error.InvalidPatch; 626 switch (patch_record.event.status) { 627 .open => {}, 628 .closed => return error.PatchClosed, 629 .merged => return error.PatchAlreadyMerged, 630 } 631 const selected = patch_record.event.revision orelse return error.PatchNotPushed; 632 const target_ref = rf.Ref{ .kind = .head, .name = patch_record.event.target_branch }; 633 634 // open the fork 635 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &input.id); 636 var fork_repo_maybe: ?rp.Repo(.xit, repo_opts) = if (patch_record.event.source_branch != null) null else rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 637 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 638 // select the commit and merge identity 639 const selected_oid = oidArray(repo_opts.hash, switch (input.revision) { 640 .squash => selected.squash_oid, 641 .source => selected.source_oid, 642 }) catch return error.InvalidPatch; 643 const identity = try std.fmt.allocPrint(arena.allocator(), "{s} <{s}>", .{ input.author.name, input.author.email }); 644 645 // merge the code and events in one target transaction 646 { 647 const DB = rp.Repo(.xit, repo_opts).DB; 648 const State = rp.Repo(.xit, repo_opts).State; 649 const Ctx = struct { 650 core: *rp.Repo(.xit, repo_opts).Core, 651 fork_repo: ?*rp.Repo(.xit, repo_opts), 652 io: std.Io, 653 allocator: std.mem.Allocator, 654 patch_id: [evt.event_id_size]u8, 655 expected_patch: evt.Patch, 656 target_ref: rf.Ref, 657 selected_oid: [hash.hexLen(repo_opts.hash)]u8, 658 revision: evt.Patch.MergeRevision, 659 identity: []const u8, 660 author: evt.CommitAuthor, 661 timestamp: u64, 662 663 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 664 var moment = try DB.HashMap(.read_write).init(cursor.*); 665 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 666 667 var check_arena = std.heap.ArenaAllocator.init(ctx.allocator); 668 defer check_arena.deinit(); 669 670 // require the fork's newest revision while both repos are locked 671 const selected_revision = ctx.expected_patch.revision orelse unreachable; 672 const revision_id = try evt.parseEventId(&selected_revision.id); 673 if (ctx.fork_repo) |repo| { 674 const fork_moment = try evt.currentMoment(repo_opts, repo); 675 const newest = (try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &check_arena)) orelse return error.PatchDataUnavailable; 676 if (!std.mem.eql(u8, &newest.id, &revision_id) or !selected_revision.matches(newest.record)) return error.PatchOutOfDate; 677 } else if (!try branchCurrent(repo_opts, state.readOnly(), ctx.io, &check_arena, ctx.expected_patch)) return error.PatchOutOfDate; 678 679 const availability = try readMergeability(repo_opts, state.readOnly(), ctx.io, &check_arena, &ctx.patch_id, ctx.expected_patch); 680 switch (availability.get(ctx.revision)) { 681 .clean => {}, 682 .conflict => return error.MergeConflict, 683 .unknown => return error.MergeCheckUnavailable, 684 } 685 686 // copy the selected commit and its dependencies 687 if (ctx.fork_repo) |repo| { 688 var fork_repo_moment = try repo.core.latestMoment(); 689 const fork_state = State(.read_only){ .core = &repo.core, .extra = .{ .moment = &fork_repo_moment } }; 690 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 691 defer objects.deinit(); 692 try objects.include(&ctx.selected_oid); 693 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 694 } 695 696 // merge the selected commit 697 const before_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 698 { 699 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 700 .kind = .full, 701 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.selected_oid }}, .algo = .patch } }, 702 .commit_metadata = .{ 703 .author = ctx.identity, 704 .committer = ctx.identity, 705 .message = ctx.expected_patch.title, 706 .timestamp = ctx.timestamp, 707 }, 708 }, ctx.target_ref, null); 709 defer merge_result.deinit(); 710 switch (merge_result.result) { 711 .conflict => return error.MergeConflict, 712 .success, .fast_forward, .nothing => {}, 713 .clean => unreachable, 714 } 715 } 716 const after_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 717 718 // retain the submitted revision and snapshot the merge result 719 if (!try importMergedRevision(repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.fork_repo, &ctx.patch_id, .{ .revision = ctx.revision, .before_oid = &before_oid, .after_oid = &after_oid }, ctx.expected_patch, ctx.author, ctx.timestamp)) return error.PatchOutOfDate; 720 721 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "merge patch" }); 722 } 723 }; 724 725 if (fork_repo_maybe) |*repo| try repo.core.db_file.lock(io, .shared); 726 defer if (fork_repo_maybe) |*repo| repo.core.db_file.unlock(io); 727 try target_repo.core.db_file.lock(io, .exclusive); 728 defer target_repo.core.db_file.unlock(io); 729 730 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 731 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 732 .core = &target_repo.core, 733 .fork_repo = if (fork_repo_maybe) |*repo| repo else null, 734 .io = io, 735 .allocator = allocator, 736 .patch_id = patch_id, 737 .expected_patch = patch_record.event, 738 .target_ref = target_ref, 739 .selected_oid = selected_oid, 740 .revision = input.revision, 741 .identity = identity, 742 .author = input.author, 743 .timestamp = input.timestamp, 744 }); 745 } 746 refreshMergeability(repo_opts, io, allocator, target_repo, null); 747 // the merge moved the target branch, so its file index follows 748 find.refresh(repo_opts, io, allocator, target_repo) catch |err| { 749 std.log.warn("failed to refresh file index: {s}", .{@errorName(err)}); 750 }; 751 } 752 753 // merge a published patch and remove its fork 754 pub fn mergeAndRemoveFork( 755 comptime repo_opts: rp.RepoOpts(.xit), 756 io: std.Io, 757 allocator: std.mem.Allocator, 758 repo_root_path: []const u8, 759 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 760 target_repo: *rp.Repo(.xit, repo_opts), 761 input: MergeInput, 762 ) !void { 763 try merge(repo_opts, io, allocator, repo_root_path, target_repo, input); 764 var arena = std.heap.ArenaAllocator.init(allocator); 765 defer arena.deinit(); 766 const record = (try evt.readFromRepo(evt.Patch, .xit, repo_opts, io, allocator, &arena, target_repo, &try evt.parseEventId(&input.id))) orelse return error.InvalidPatch; 767 if (record.event.source_branch == null) try fork.remove(io, allocator, repo_root_path, admin_repo, &input.id, null, input.author); 768 } 769 770 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 771 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 772 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 773 const name = std.mem.trimEnd(u8, line[0..open], " "); 774 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 775 return .{ .name = name, .email = line[open + 1 .. close] }; 776 } 777 778 // import a revision after detecting a client-side merge 779 fn importMerged( 780 comptime repo_opts: rp.RepoOpts(.xit), 781 state: rp.Repo(.xit, repo_opts).State(.read_write), 782 db: *evt.EventDB(repo_opts.hash), 783 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 784 io: std.Io, 785 allocator: std.mem.Allocator, 786 repo_root_path: []const u8, 787 patch_id: *const [evt.event_id_size]u8, 788 matched_oid: *const [hash.hexLen(repo_opts.hash)]u8, 789 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 790 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 791 ) !void { 792 var arena = std.heap.ArenaAllocator.init(allocator); 793 defer arena.deinit(); 794 795 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 796 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 797 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 798 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 799 const selected = record.event.revision orelse return; 800 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; 801 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 }); 802 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 803 804 _ = 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); 805 } 806 807 // import the revision and mark its patch merged in the current transaction 808 fn importMergedRevision( 809 comptime repo_opts: rp.RepoOpts(.xit), 810 state: rp.Repo(.xit, repo_opts).State(.read_write), 811 db: *evt.EventDB(repo_opts.hash), 812 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 813 io: std.Io, 814 allocator: std.mem.Allocator, 815 fork_repo: ?*rp.Repo(.xit, repo_opts), 816 patch_id: *const [evt.event_id_size]u8, 817 merged: struct { 818 revision: evt.Patch.MergeRevision, 819 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 820 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 821 }, 822 expected_patch: ?evt.Patch, 823 merged_author: ?evt.CommitAuthor, 824 merged_timestamp: u64, 825 ) !bool { 826 const DB = evt.EventDB(repo_opts.hash); 827 var arena = std.heap.ArenaAllocator.init(allocator); 828 defer arena.deinit(); 829 830 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 831 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 832 const selected = patch_record.event.revision orelse return false; 833 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 834 if (expected_patch) |expected| { 835 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 836 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 837 const conflicts = try DB.SortedMap(.read_only).init(cursor); 838 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 839 } 840 } 841 const revision_id = try evt.parseEventId(&selected.id); 842 843 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 844 const revision = (if (fork_repo) |repo| 845 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 846 else 847 current_revision) orelse return false; 848 if (!selected.matches(revision)) return false; 849 850 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 851 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 } }; 852 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 853 if (revision.event_oid.len != event_oid.len) return false; 854 @memcpy(&event_oid, revision.event_oid); 855 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 856 defer event_object.deinit(); 857 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 858 859 var events_buffer: [3]evt.EventWithId = undefined; 860 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 861 var tree_entries: [2]evt.EventTreeEntry = undefined; 862 const needs_import = if (current_revision) |existing| existing.removed else true; 863 if (needs_import) { 864 tree_entries = .{ 865 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 866 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 867 }; 868 events.appendAssumeCapacity(.{ 869 .id = selected.id, 870 .timestamp = event_object.content.commit.metadata.timestamp, 871 .author = original_author, 872 .tree_entries = &tree_entries, 873 .event = .{ .patchrev = revision.event }, 874 }); 875 } else { 876 const existing = current_revision orelse unreachable; 877 if (!selected.matches(existing)) return error.InvalidPatch; 878 } 879 880 const author = merged_author orelse original_author; 881 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 882 var patch = patch_record.event; 883 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 884 events.appendAssumeCapacity(result.event); 885 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 886 events.appendAssumeCapacity(.{ 887 .id = std.fmt.bytesToHex(patch_id.*, .lower), 888 .timestamp = timestamp, 889 .author = author, 890 .event = .{ .patch = patch }, 891 }); 892 893 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 894 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 895 return true; 896 } 897 898 // find patches merged by commits received in a push 899 pub fn detectMerged( 900 comptime repo_opts: rp.RepoOpts(.xit), 901 state: rp.Repo(.xit, repo_opts).State(.read_write), 902 db: *evt.EventDB(repo_opts.hash), 903 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 904 io: std.Io, 905 allocator: std.mem.Allocator, 906 repo_root_path: []const u8, 907 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 908 error_writer: *std.Io.Writer, 909 ) !void { 910 const DB = evt.EventDB(repo_opts.hash); 911 var arena = std.heap.ArenaAllocator.init(allocator); 912 defer arena.deinit(); 913 914 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 915 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 916 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 917 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 918 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 919 const heads_prefix = "refs/heads/"; 920 for (updates) |update| { 921 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 922 std.mem.eql(u8, update.ref_name, events_ref_path) or 923 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 924 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 925 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 926 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 927 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 928 @memcpy(&old_oid, update.old_oid); 929 @memcpy(&new_oid, update.new_oid); 930 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 931 932 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 933 defer commits.deinit(); 934 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 935 var targets: std.ArrayList(struct { 936 before_oid: [hash.hexLen(repo_opts.hash)]u8, 937 after_oid: [hash.hexLen(repo_opts.hash)]u8, 938 }) = .empty; 939 { 940 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 941 defer first_parents.deinit(); 942 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 943 try first_parents.include(&new_oid); 944 while (try first_parents.next(arena.allocator())) |commit| { 945 defer commit.deinit(); 946 try targets.append(arena.allocator(), .{ 947 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 948 .after_oid = commit.oid, 949 }); 950 } 951 } 952 953 // visit target commits oldest first, attributing each revision to the 954 // commit that introduced it; the shared iterator visits each ancestor once 955 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 956 while (targets.pop()) |target| { 957 try commits.include(&target.after_oid); 958 while (try commits.next(arena.allocator())) |commit| { 959 defer commit.deinit(); 960 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 961 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 962 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 963 var ids_iter = try ids.iterator(); 964 while (try ids_iter.next()) |cursor| { 965 const pair = try cursor.readKeyValuePair(); 966 var patch_id: [evt.event_id_size]u8 = undefined; 967 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 968 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 969 // a fast-forward has no recorded merge boundary; use the old ref tip 970 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 971 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 972 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 973 continue; 974 }; 975 } 976 } 977 } 978 } 979 } 980