╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1073 1074 // require the fork's newest revision while both repos are locked 1075 const selected_revision = ctx.expected_patch.revision orelse unreachable; 1076 const revision_id = try evt.parseEventId(&selected_revision.id); 1077 if (ctx.fork_repo) |repo| { 1078 const fork_moment = try evt.currentMoment(repo_opts, repo); 1079 const newest = (try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &check_arena)) orelse return error.PatchDataUnavailable; 1080 if (!std.mem.eql(u8, &newest.id, &revision_id) or !selected_revision.matches(newest.record)) return error.PatchOutOfDate; 1081 } else if (!try branchCurrent(repo_opts, state.readOnly(), ctx.io, &check_arena, ctx.expected_patch)) return error.PatchOutOfDate; 1082 1083 // require a clean cached result for the selected merge style. 1084 { 1085 const availability = try readMergeability(repo_opts, state.readOnly(), ctx.io, &check_arena, &ctx.patch_id, ctx.expected_patch); 1086 switch (availability.get(ctx.revision)) { 1087 .clean => {}, 1088 .conflict => return error.MergeConflict, 1089 .unknown => return error.MergeCheckUnavailable, 1090 } 1091 } 1092 1093 // copy the selected commit and its dependencies 1094 if (ctx.fork_repo) |repo| { 1095 var fork_repo_moment = try repo.core.latestMoment(); 1096 const fork_state = State(.read_only){ .core = &repo.core, .extra = .{ .moment = &fork_repo_moment } }; 1097 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 1098 defer objects.deinit(); 1099 try objects.include(&ctx.selected_oid); 1100 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 1101 } 1102 1103 // merge the selected commit 1104 const before_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 1105 { 1106 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 1107 .kind = .full, 1108 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.selected_oid }}, .algo = .patch } }, 1109 .commit_metadata = .{ 1110 .author = ctx.identity, 1111 .committer = ctx.identity, 1112 .message = ctx.expected_patch.title, 1113 .timestamp = ctx.timestamp, 1114 }, 1115 }, ctx.target_ref, null); 1116 defer merge_result.deinit(); 1117 switch (merge_result.result) { 1118 .conflict => return error.MergeConflict, 1119 .success, .fast_forward, .nothing => {}, 1120 .clean => unreachable, 1121 } 1122 } 1123 const after_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 1124 1125 // retain the submitted revision and snapshot the merge result 1126 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; 1127 1128 // the merge moved the target branch, so its indexes follow, 1129 // the commit index deriving from the old tip's version 1130 _ = try find.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, null); 1131 _ = try srch_cmmt.refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, &.{.{ .old_oid = &before_oid, .new_oid = &after_oid }}, null); 1132 1133 // record the merge as one undoable action. 1134 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = merge_undo_action } }); 1135 } 1136 }; 1137 1138 // lock the fork before the target for the full merge transaction. 1139 if (fork_repo_maybe) |*repo| try repo.core.db_file.lock(io, .shared); 1140 defer if (fork_repo_maybe) |*repo| repo.core.db_file.unlock(io); 1141 try target_repo.core.db_file.lock(io, .exclusive); 1142 defer target_repo.core.db_file.unlock(io); 1143 1144 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 1145 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1146 .core = &target_repo.core, 1147 .fork_repo = if (fork_repo_maybe) |*repo| repo else null, 1148 .io = io, 1149 .allocator = allocator, 1150 .patch_id = patch_id, 1151 .expected_patch = patch_record.event, 1152 .target_ref = target_ref, 1153 .selected_oid = selected_oid, 1154 .revision = input.revision, 1155 .identity = identity, 1156 .author = input.author, 1157 .timestamp = input.timestamp, 1158 }); 1159 } 1160 1161 // refresh other patches affected by the updated target branch. 1162 refreshOpenMergeability(repo_opts, io, allocator, target_repo, null, null); 1163 } 1164 1165 // merge a published patch and remove its fork 1166 pub fn mergeAndRemoveFork( 1167 comptime repo_opts: rp.RepoOpts(.xit), 1168 io: std.Io, 1169 allocator: std.mem.Allocator, 1170 repo_root_path: []const u8, 1171 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 1172 target_repo: *rp.Repo(.xit, repo_opts), 1173 input: MergeInput, 1174 ) !void { 1175 try merge(repo_opts, io, allocator, repo_root_path, target_repo, input); 1176 var arena = std.heap.ArenaAllocator.init(allocator); 1177 defer arena.deinit(); 1178 1179 // remove fork data only for patches backed by a fork. 1180 const record = (try evt.readFromRepo(evt.Patch, .xit, repo_opts, io, allocator, &arena, target_repo, &try evt.parseEventId(&input.id))) orelse return error.InvalidPatch; 1181 if (record.event.source_branch == null) try fork.remove(io, allocator, repo_root_path, admin_repo, &input.id, null, input.author); 1182 } 1183 1184 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 1185 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 1186 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 1187 const name = std.mem.trimEnd(u8, line[0..open], " "); 1188 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 1189 return .{ .name = name, .email = line[open + 1 .. close] }; 1190 } 1191 1192 // import a revision after detecting a client-side merge 1193 fn importMerged( 1194 comptime repo_opts: rp.RepoOpts(.xit), 1195 state: rp.Repo(.xit, repo_opts).State(.read_write), 1196 db: *evt.EventDB(repo_opts.hash), 1197 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1198 io: std.Io, 1199 allocator: std.mem.Allocator, 1200 repo_root_path: []const u8, 1201 patch_id: *const [evt.event_id_size]u8, 1202 matched_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1203 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1204 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1205 ) !void { 1206 var arena = std.heap.ArenaAllocator.init(allocator); 1207 defer arena.deinit(); 1208 1209 // identify which selected revision was merged by the client. 1210 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 1211 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 1212 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1213 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 1214 const selected = record.event.revision orelse return; 1215 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; 1216 1217 // open the fork when the revision is not stored in the target repository. 1218 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 }); 1219 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 1220 1221 // import the revision and record the client-side merge. 1222 _ = 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); 1223 } 1224 1225 // import the revision and mark its patch merged in the current transaction 1226 fn importMergedRevision( 1227 comptime repo_opts: rp.RepoOpts(.xit), 1228 state: rp.Repo(.xit, repo_opts).State(.read_write), 1229 db: *evt.EventDB(repo_opts.hash), 1230 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1231 io: std.Io, 1232 allocator: std.mem.Allocator, 1233 fork_repo: ?*rp.Repo(.xit, repo_opts), 1234 patch_id: *const [evt.event_id_size]u8, 1235 merged: struct { 1236 revision: evt.Patch.MergeRevision, 1237 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1238 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1239 }, 1240 expected_patch: ?evt.Patch, 1241 merged_author: ?evt.CommitAuthor, 1242 merged_timestamp: u64, 1243 ) !bool { 1244 const DB = evt.EventDB(repo_opts.hash); 1245 var arena = std.heap.ArenaAllocator.init(allocator); 1246 defer arena.deinit(); 1247 1248 // read the patch and reject outdated or conflicting merge requests. 1249 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1250 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 1251 const selected = patch_record.event.revision orelse return false; 1252 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 1253 if (expected_patch) |expected| { 1254 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 1255 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 1256 const conflicts = try DB.SortedMap(.read_only).init(cursor); 1257 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 1258 } 1259 } 1260 const revision_id = try evt.parseEventId(&selected.id); 1261 1262 // resolve the selected revision from the target or its fork. 1263 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 1264 const revision = (if (fork_repo) |repo| 1265 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 1266 else 1267 current_revision) orelse return false; 1268 if (!selected.matches(revision)) return false; 1269 1270 // read the original revision commit and author. 1271 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1272 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 } }; 1273 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1274 if (revision.event_oid.len != event_oid.len) return false; 1275 @memcpy(&event_oid, revision.event_oid); 1276 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1277 defer event_object.deinit(); 1278 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1279 1280 // retain the submitted revision if the target has not imported it yet. 1281 var events_buffer: [3]evt.EventWithId = undefined; 1282 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1283 var tree_entries: [2]evt.EventTreeEntry = undefined; 1284 const needs_import = if (current_revision) |existing| existing.removed else true; 1285 if (needs_import) { 1286 tree_entries = .{ 1287 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1288 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1289 }; 1290 events.appendAssumeCapacity(.{ 1291 .id = selected.id, 1292 .timestamp = event_object.content.commit.metadata.timestamp, 1293 .author = original_author, 1294 .tree_entries = &tree_entries, 1295 .event = .{ .patchrev = revision.event }, 1296 }); 1297 } else { 1298 const existing = current_revision orelse unreachable; 1299 if (!selected.matches(existing)) return error.InvalidPatch; 1300 } 1301 1302 // snapshot the merge result and mark the patch as merged. 1303 { 1304 const author = merged_author orelse original_author; 1305 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1306 var patch = patch_record.event; 1307 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1308 events.appendAssumeCapacity(result.event); 1309 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1310 events.appendAssumeCapacity(.{ 1311 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1312 .timestamp = timestamp, 1313 .author = author, 1314 .event = .{ .patch = patch }, 1315 }); 1316 } 1317 1318 // commit and consume the revision and patch events together. 1319 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1320 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 1321 return true; 1322 } 1323 1324 // find patches merged by commits received in a push 1325 pub fn detectMerged( 1326 comptime repo_opts: rp.RepoOpts(.xit), 1327 state: rp.Repo(.xit, repo_opts).State(.read_write), 1328 db: *evt.EventDB(repo_opts.hash), 1329 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1330 io: std.Io, 1331 allocator: std.mem.Allocator, 1332 repo_root_path: []const u8, 1333 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1334 error_writer: *std.Io.Writer, 1335 ) !void { 1336 const DB = evt.EventDB(repo_opts.hash); 1337 var arena = std.heap.ArenaAllocator.init(allocator); 1338 defer arena.deinit(); 1339 1340 // read the revision index used to identify merged patches. 1341 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1342 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1343 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1344 1345 // ignore event refs and inspect only changed branch tips. 1346 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1347 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1348 const heads_prefix = "refs/heads/"; 1349 for (updates) |update| { 1350 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1351 std.mem.eql(u8, update.ref_name, events_ref_path) or 1352 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1353 1354 // validate the ref tips and skip branch deletions. 1355 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1356 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1357 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1358 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1359 @memcpy(&old_oid, update.old_oid); 1360 @memcpy(&new_oid, update.new_oid); 1361 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1362 1363 // traverse commits introduced by this branch update. 1364 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1365 defer commits.deinit(); 1366 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1367 var targets: std.ArrayList(struct { 1368 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1369 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1370 }) = .empty; 1371 1372 // collect merge boundaries along the target's first-parent history. 1373 { 1374 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1375 defer first_parents.deinit(); 1376 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1377 try first_parents.include(&new_oid); 1378 while (try first_parents.next(arena.allocator())) |commit| { 1379 defer commit.deinit(); 1380 try targets.append(arena.allocator(), .{ 1381 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1382 .after_oid = commit.oid, 1383 }); 1384 } 1385 } 1386 1387 // visit target commits oldest first, attributing each revision to the 1388 // commit that introduced it; the shared iterator visits each ancestor once 1389 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1390 while (targets.pop()) |target| { 1391 try commits.include(&target.after_oid); 1392 while (try commits.next(arena.allocator())) |commit| { 1393 defer commit.deinit(); 1394 1395 // find patches whose selected revision matches this commit. 1396 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1397 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1398 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1399 var ids_iter = try ids.iterator(); 1400 while (try ids_iter.next()) |cursor| { 1401 const pair = try cursor.readKeyValuePair(); 1402 var patch_id: [evt.event_id_size]u8 = undefined; 1403 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1404 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1405 1406 // a fast-forward has no recorded merge boundary; use the old ref tip 1407 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1408 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1409 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1410 continue; 1411 }; 1412 } 1413 } 1414 } 1415 } 1416 } 1417