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