╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
785 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 786 ) !void { 787 var arena = std.heap.ArenaAllocator.init(allocator); 788 defer arena.deinit(); 789 790 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 791 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 792 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 793 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 794 const selected = record.event.revision orelse return; 795 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; 796 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 }); 797 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 798 799 _ = 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); 800 } 801 802 // import the revision and mark its patch merged in the current transaction 803 fn importMergedRevision( 804 comptime repo_opts: rp.RepoOpts(.xit), 805 state: rp.Repo(.xit, repo_opts).State(.read_write), 806 db: *evt.EventDB(repo_opts.hash), 807 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 808 io: std.Io, 809 allocator: std.mem.Allocator, 810 fork_repo: ?*rp.Repo(.xit, repo_opts), 811 patch_id: *const [evt.event_id_size]u8, 812 merged: struct { 813 revision: evt.Patch.MergeRevision, 814 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 815 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 816 }, 817 expected_patch: ?evt.Patch, 818 merged_author: ?evt.CommitAuthor, 819 merged_timestamp: u64, 820 ) !bool { 821 const DB = evt.EventDB(repo_opts.hash); 822 var arena = std.heap.ArenaAllocator.init(allocator); 823 defer arena.deinit(); 824 825 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 826 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 827 const selected = patch_record.event.revision orelse return false; 828 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 829 if (expected_patch) |expected| { 830 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 831 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 832 const conflicts = try DB.SortedMap(.read_only).init(cursor); 833 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 834 } 835 } 836 const revision_id = try evt.parseEventId(&selected.id); 837 838 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 839 const revision = (if (fork_repo) |repo| 840 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 841 else 842 current_revision) orelse return false; 843 if (!selected.matches(revision)) return false; 844 845 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 846 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 } }; 847 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 848 if (revision.event_oid.len != event_oid.len) return false; 849 @memcpy(&event_oid, revision.event_oid); 850 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 851 defer event_object.deinit(); 852 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 853 854 var events_buffer: [3]evt.EventWithId = undefined; 855 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 856 var tree_entries: [2]evt.EventTreeEntry = undefined; 857 const needs_import = if (current_revision) |existing| existing.removed else true; 858 if (needs_import) { 859 tree_entries = .{ 860 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 861 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 862 }; 863 events.appendAssumeCapacity(.{ 864 .id = selected.id, 865 .timestamp = event_object.content.commit.metadata.timestamp, 866 .author = original_author, 867 .tree_entries = &tree_entries, 868 .event = .{ .patchrev = revision.event }, 869 }); 870 } else { 871 const existing = current_revision orelse unreachable; 872 if (!selected.matches(existing)) return error.InvalidPatch; 873 } 874 875 const author = merged_author orelse original_author; 876 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 877 var patch = patch_record.event; 878 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 879 events.appendAssumeCapacity(result.event); 880 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 881 events.appendAssumeCapacity(.{ 882 .id = std.fmt.bytesToHex(patch_id.*, .lower), 883 .timestamp = timestamp, 884 .author = author, 885 .event = .{ .patch = patch }, 886 }); 887 888 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 889 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 890 return true; 891 } 892 893 // find patches merged by commits received in a push 894 pub fn detectMerged( 895 comptime repo_opts: rp.RepoOpts(.xit), 896 state: rp.Repo(.xit, repo_opts).State(.read_write), 897 db: *evt.EventDB(repo_opts.hash), 898 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 899 io: std.Io, 900 allocator: std.mem.Allocator, 901 repo_root_path: []const u8, 902 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 903 error_writer: *std.Io.Writer, 904 ) !void { 905 const DB = evt.EventDB(repo_opts.hash); 906 var arena = std.heap.ArenaAllocator.init(allocator); 907 defer arena.deinit(); 908 909 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 910 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 911 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 912 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 913 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 914 const heads_prefix = "refs/heads/"; 915 for (updates) |update| { 916 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 917 std.mem.eql(u8, update.ref_name, events_ref_path) or 918 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 919 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 920 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 921 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 922 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 923 @memcpy(&old_oid, update.old_oid); 924 @memcpy(&new_oid, update.new_oid); 925 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 926 927 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 928 defer commits.deinit(); 929 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 930 var targets: std.ArrayList(struct { 931 before_oid: [hash.hexLen(repo_opts.hash)]u8, 932 after_oid: [hash.hexLen(repo_opts.hash)]u8, 933 }) = .empty; 934 { 935 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 936 defer first_parents.deinit(); 937 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 938 try first_parents.include(&new_oid); 939 while (try first_parents.next(arena.allocator())) |commit| { 940 defer commit.deinit(); 941 try targets.append(arena.allocator(), .{ 942 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 943 .after_oid = commit.oid, 944 }); 945 } 946 } 947 948 // visit target commits oldest first, attributing each revision to the 949 // commit that introduced it; the shared iterator visits each ancestor once 950 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 951 while (targets.pop()) |target| { 952 try commits.include(&target.after_oid); 953 while (try commits.next(arena.allocator())) |commit| { 954 defer commit.deinit(); 955 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 956 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 957 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 958 var ids_iter = try ids.iterator(); 959 while (try ids_iter.next()) |cursor| { 960 const pair = try cursor.readKeyValuePair(); 961 var patch_id: [evt.event_id_size]u8 = undefined; 962 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 963 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 964 // a fast-forward has no recorded merge boundary; use the old ref tip 965 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 966 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 967 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 968 continue; 969 }; 970 } 971 } 972 } 973 } 974 } 975