╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
943 .open => {}, 944 .closed => return error.PatchClosed, 945 .merged => return error.PatchAlreadyMerged, 946 } 947 const selected = patch_record.event.revision orelse return error.PatchNotPushed; 948 const target_ref = rf.Ref{ .kind = .head, .name = patch_record.event.target_branch }; 949 950 // open the fork 951 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &input.id); 952 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; 953 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 954 955 // select the commit and merge identity 956 const selected_oid = oidArray(repo_opts.hash, switch (input.revision) { 957 .squash => selected.squash_oid, 958 .source => selected.source_oid, 959 }) catch return error.InvalidPatch; 960 const identity = try std.fmt.allocPrint(arena.allocator(), "{s} <{s}>", .{ input.author.name, input.author.email }); 961 962 // merge the code and events in one target transaction 963 { 964 const DB = rp.Repo(.xit, repo_opts).DB; 965 const State = rp.Repo(.xit, repo_opts).State; 966 const Ctx = struct { 967 core: *rp.Repo(.xit, repo_opts).Core, 968 fork_repo: ?*rp.Repo(.xit, repo_opts), 969 io: std.Io, 970 allocator: std.mem.Allocator, 971 patch_id: [evt.event_id_size]u8, 972 expected_patch: evt.Patch, 973 target_ref: rf.Ref, 974 selected_oid: [hash.hexLen(repo_opts.hash)]u8, 975 revision: evt.Patch.MergeRevision, 976 identity: []const u8, 977 author: evt.CommitAuthor, 978 timestamp: u64, 979 980 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 981 var moment = try DB.HashMap(.read_write).init(cursor.*); 982 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 983 984 var check_arena = std.heap.ArenaAllocator.init(ctx.allocator); 985 defer check_arena.deinit(); 986 987 // require the fork's newest revision while both repos are locked 988 const selected_revision = ctx.expected_patch.revision orelse unreachable; 989 const revision_id = try evt.parseEventId(&selected_revision.id); 990 if (ctx.fork_repo) |repo| { 991 const fork_moment = try evt.currentMoment(repo_opts, repo); 992 const newest = (try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &check_arena)) orelse return error.PatchDataUnavailable; 993 if (!std.mem.eql(u8, &newest.id, &revision_id) or !selected_revision.matches(newest.record)) return error.PatchOutOfDate; 994 } else if (!try branchCurrent(repo_opts, state.readOnly(), ctx.io, &check_arena, ctx.expected_patch)) return error.PatchOutOfDate; 995 996 // require a clean cached result for the selected merge style. 997 { 998 const availability = try readMergeability(repo_opts, state.readOnly(), ctx.io, &check_arena, &ctx.patch_id, ctx.expected_patch); 999 switch (availability.get(ctx.revision)) { 1000 .clean => {}, 1001 .conflict => return error.MergeConflict, 1002 .unknown => return error.MergeCheckUnavailable, 1003 } 1004 } 1005 1006 // copy the selected commit and its dependencies 1007 if (ctx.fork_repo) |repo| { 1008 var fork_repo_moment = try repo.core.latestMoment(); 1009 const fork_state = State(.read_only){ .core = &repo.core, .extra = .{ .moment = &fork_repo_moment } }; 1010 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 1011 defer objects.deinit(); 1012 try objects.include(&ctx.selected_oid); 1013 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 1014 } 1015 1016 // merge the selected commit 1017 const before_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 1018 { 1019 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 1020 .kind = .full, 1021 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.selected_oid }}, .algo = .patch } }, 1022 .commit_metadata = .{ 1023 .author = ctx.identity, 1024 .committer = ctx.identity, 1025 .message = ctx.expected_patch.title, 1026 .timestamp = ctx.timestamp, 1027 }, 1028 }, ctx.target_ref, null); 1029 defer merge_result.deinit(); 1030 switch (merge_result.result) { 1031 .conflict => return error.MergeConflict, 1032 .success, .fast_forward, .nothing => {}, 1033 .clean => unreachable, 1034 } 1035 } 1036 const after_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 1037 1038 // retain the submitted revision and snapshot the merge result 1039 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; 1040 1041 // record the merge as one undoable action. 1042 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action = merge_undo_action } }); 1043 } 1044 }; 1045 1046 // lock the fork before the target for the full merge transaction. 1047 if (fork_repo_maybe) |*repo| try repo.core.db_file.lock(io, .shared); 1048 defer if (fork_repo_maybe) |*repo| repo.core.db_file.unlock(io); 1049 try target_repo.core.db_file.lock(io, .exclusive); 1050 defer target_repo.core.db_file.unlock(io); 1051 1052 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 1053 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1054 .core = &target_repo.core, 1055 .fork_repo = if (fork_repo_maybe) |*repo| repo else null, 1056 .io = io, 1057 .allocator = allocator, 1058 .patch_id = patch_id, 1059 .expected_patch = patch_record.event, 1060 .target_ref = target_ref, 1061 .selected_oid = selected_oid, 1062 .revision = input.revision, 1063 .identity = identity, 1064 .author = input.author, 1065 .timestamp = input.timestamp, 1066 }); 1067 } 1068 1069 // refresh other patches affected by the updated target branch. 1070 refreshMergeability(repo_opts, io, allocator, target_repo, null); 1071 1072 // the merge moved the target branch, so its indexes follow 1073 find.refresh(repo_opts, io, allocator, target_repo) catch |err| { 1074 std.log.warn("failed to refresh file index: {s}", .{@errorName(err)}); 1075 }; 1076 cms.refresh(repo_opts, io, allocator, target_repo, null) catch |err| { 1077 std.log.warn("failed to refresh commit index: {s}", .{@errorName(err)}); 1078 }; 1079 } 1080 1081 // merge a published patch and remove its fork 1082 pub fn mergeAndRemoveFork( 1083 comptime repo_opts: rp.RepoOpts(.xit), 1084 io: std.Io, 1085 allocator: std.mem.Allocator, 1086 repo_root_path: []const u8, 1087 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 1088 target_repo: *rp.Repo(.xit, repo_opts), 1089 input: MergeInput, 1090 ) !void { 1091 try merge(repo_opts, io, allocator, repo_root_path, target_repo, input); 1092 var arena = std.heap.ArenaAllocator.init(allocator); 1093 defer arena.deinit(); 1094 1095 // remove fork data only for patches backed by a fork. 1096 const record = (try evt.readFromRepo(evt.Patch, .xit, repo_opts, io, allocator, &arena, target_repo, &try evt.parseEventId(&input.id))) orelse return error.InvalidPatch; 1097 if (record.event.source_branch == null) try fork.remove(io, allocator, repo_root_path, admin_repo, &input.id, null, input.author); 1098 } 1099 1100 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 1101 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 1102 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 1103 const name = std.mem.trimEnd(u8, line[0..open], " "); 1104 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 1105 return .{ .name = name, .email = line[open + 1 .. close] }; 1106 } 1107 1108 // import a revision after detecting a client-side merge 1109 fn importMerged( 1110 comptime repo_opts: rp.RepoOpts(.xit), 1111 state: rp.Repo(.xit, repo_opts).State(.read_write), 1112 db: *evt.EventDB(repo_opts.hash), 1113 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1114 io: std.Io, 1115 allocator: std.mem.Allocator, 1116 repo_root_path: []const u8, 1117 patch_id: *const [evt.event_id_size]u8, 1118 matched_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1119 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1120 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1121 ) !void { 1122 var arena = std.heap.ArenaAllocator.init(allocator); 1123 defer arena.deinit(); 1124 1125 // identify which selected revision was merged by the client. 1126 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 1127 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 1128 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1129 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 1130 const selected = record.event.revision orelse return; 1131 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; 1132 1133 // open the fork when the revision is not stored in the target repository. 1134 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 }); 1135 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 1136 1137 // import the revision and record the client-side merge. 1138 _ = 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); 1139 } 1140 1141 // import the revision and mark its patch merged in the current transaction 1142 fn importMergedRevision( 1143 comptime repo_opts: rp.RepoOpts(.xit), 1144 state: rp.Repo(.xit, repo_opts).State(.read_write), 1145 db: *evt.EventDB(repo_opts.hash), 1146 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1147 io: std.Io, 1148 allocator: std.mem.Allocator, 1149 fork_repo: ?*rp.Repo(.xit, repo_opts), 1150 patch_id: *const [evt.event_id_size]u8, 1151 merged: struct { 1152 revision: evt.Patch.MergeRevision, 1153 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1154 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1155 }, 1156 expected_patch: ?evt.Patch, 1157 merged_author: ?evt.CommitAuthor, 1158 merged_timestamp: u64, 1159 ) !bool { 1160 const DB = evt.EventDB(repo_opts.hash); 1161 var arena = std.heap.ArenaAllocator.init(allocator); 1162 defer arena.deinit(); 1163 1164 // read the patch and reject outdated or conflicting merge requests. 1165 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1166 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 1167 const selected = patch_record.event.revision orelse return false; 1168 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 1169 if (expected_patch) |expected| { 1170 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 1171 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 1172 const conflicts = try DB.SortedMap(.read_only).init(cursor); 1173 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 1174 } 1175 } 1176 const revision_id = try evt.parseEventId(&selected.id); 1177 1178 // resolve the selected revision from the target or its fork. 1179 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 1180 const revision = (if (fork_repo) |repo| 1181 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 1182 else 1183 current_revision) orelse return false; 1184 if (!selected.matches(revision)) return false; 1185 1186 // read the original revision commit and author. 1187 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1188 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 } }; 1189 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1190 if (revision.event_oid.len != event_oid.len) return false; 1191 @memcpy(&event_oid, revision.event_oid); 1192 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1193 defer event_object.deinit(); 1194 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1195 1196 // retain the submitted revision if the target has not imported it yet. 1197 var events_buffer: [3]evt.EventWithId = undefined; 1198 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1199 var tree_entries: [2]evt.EventTreeEntry = undefined; 1200 const needs_import = if (current_revision) |existing| existing.removed else true; 1201 if (needs_import) { 1202 tree_entries = .{ 1203 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1204 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1205 }; 1206 events.appendAssumeCapacity(.{ 1207 .id = selected.id, 1208 .timestamp = event_object.content.commit.metadata.timestamp, 1209 .author = original_author, 1210 .tree_entries = &tree_entries, 1211 .event = .{ .patchrev = revision.event }, 1212 }); 1213 } else { 1214 const existing = current_revision orelse unreachable; 1215 if (!selected.matches(existing)) return error.InvalidPatch; 1216 } 1217 1218 // snapshot the merge result and mark the patch as merged. 1219 { 1220 const author = merged_author orelse original_author; 1221 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1222 var patch = patch_record.event; 1223 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1224 events.appendAssumeCapacity(result.event); 1225 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1226 events.appendAssumeCapacity(.{ 1227 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1228 .timestamp = timestamp, 1229 .author = author, 1230 .event = .{ .patch = patch }, 1231 }); 1232 } 1233 1234 // commit and consume the revision and patch events together. 1235 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1236 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 1237 return true; 1238 } 1239 1240 // find patches merged by commits received in a push 1241 pub fn detectMerged( 1242 comptime repo_opts: rp.RepoOpts(.xit), 1243 state: rp.Repo(.xit, repo_opts).State(.read_write), 1244 db: *evt.EventDB(repo_opts.hash), 1245 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1246 io: std.Io, 1247 allocator: std.mem.Allocator, 1248 repo_root_path: []const u8, 1249 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1250 error_writer: *std.Io.Writer, 1251 ) !void { 1252 const DB = evt.EventDB(repo_opts.hash); 1253 var arena = std.heap.ArenaAllocator.init(allocator); 1254 defer arena.deinit(); 1255 1256 // read the revision index used to identify merged patches. 1257 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1258 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1259 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1260 1261 // ignore event refs and inspect only changed branch tips. 1262 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1263 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1264 const heads_prefix = "refs/heads/"; 1265 for (updates) |update| { 1266 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1267 std.mem.eql(u8, update.ref_name, events_ref_path) or 1268 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1269 1270 // validate the ref tips and skip branch deletions. 1271 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1272 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1273 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1274 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1275 @memcpy(&old_oid, update.old_oid); 1276 @memcpy(&new_oid, update.new_oid); 1277 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1278 1279 // traverse commits introduced by this branch update. 1280 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1281 defer commits.deinit(); 1282 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1283 var targets: std.ArrayList(struct { 1284 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1285 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1286 }) = .empty; 1287 1288 // collect merge boundaries along the target's first-parent history. 1289 { 1290 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1291 defer first_parents.deinit(); 1292 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1293 try first_parents.include(&new_oid); 1294 while (try first_parents.next(arena.allocator())) |commit| { 1295 defer commit.deinit(); 1296 try targets.append(arena.allocator(), .{ 1297 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1298 .after_oid = commit.oid, 1299 }); 1300 } 1301 } 1302 1303 // visit target commits oldest first, attributing each revision to the 1304 // commit that introduced it; the shared iterator visits each ancestor once 1305 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1306 while (targets.pop()) |target| { 1307 try commits.include(&target.after_oid); 1308 while (try commits.next(arena.allocator())) |commit| { 1309 defer commit.deinit(); 1310 1311 // find patches whose selected revision matches this commit. 1312 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1313 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1314 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1315 var ids_iter = try ids.iterator(); 1316 while (try ids_iter.next()) |cursor| { 1317 const pair = try cursor.readKeyValuePair(); 1318 var patch_id: [evt.event_id_size]u8 = undefined; 1319 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1320 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1321 1322 // a fast-forward has no recorded merge boundary; use the old ref tip 1323 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1324 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1325 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1326 continue; 1327 }; 1328 } 1329 } 1330 } 1331 } 1332 } 1333