╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1161 .{ 1162 .title = "Edit a conflicting line in alpha.txt", 1163 .description = "Both source and squash merges conflict with a different edit to the same line on master.", 1164 .tags = "merge", 1165 .status = .open, 1166 .alpha_edit = .{ .line = 3, .text = "replace alpha with a conflicting edit" }, 1167 }, 1168 }; 1169 1170 const repos_path = try std.fs.path.join(allocator, &.{ server_path, "repos" }); 1171 defer allocator.free(repos_path); 1172 const patch_author = evt.CommitAuthor{ .name = "admin", .email = "admin@example.test" }; 1173 var patch_ids: [patch_data.len][evt.event_id_size]u8 = undefined; 1174 for (patch_data, 0..) |patch, i| { 1175 const timestamp: u64 = @intCast(500 + i * 10); 1176 patch_ids[i] = evt.EventWithId.randomId(random); 1177 const patch_hex = std.fmt.bytesToHex(patch_ids[i], .lower); 1178 const path = try fork.create(.{}, io, allocator, repos_path, admin_repo, .{ 1179 .id = patch_hex, 1180 .user_id = user_id.*, 1181 .repo_id = repo_id.*, 1182 .title = patch.title, 1183 .description = patch.description, 1184 .tags = patch.tags, 1185 .target_branch = "master", 1186 .author = patch_author, 1187 .timestamp = timestamp, 1188 }); 1189 allocator.free(path); 1190 1191 // prepare the patch's base and file contents, then seed its commits and revision 1192 { 1193 var arena = std.heap.ArenaAllocator.init(allocator); 1194 defer arena.deinit(); 1195 const aa = arena.allocator(); 1196 var base_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1197 var contents: [3][]const u8 = undefined; 1198 const file_path: []const u8, const count: usize = if (patch.alpha_edit) |edit| blk: { 1199 // v30 precedes the last scatter commit, even after the sample merge 1200 const tag_oid = (try target_repo.readRef(io, .{ .kind = .tag, .name = "v30" })) orelse return error.NotFound; 1201 var moment = try target_repo.core.latestMoment(); 1202 const state = rp.Repo(.xit, .{}).State(.read_only){ .core = &target_repo.core, .extra = .{ .moment = &moment } }; 1203 var tag = try obj.Object(.xit, .{}).init(state, io, allocator, &tag_oid); 1204 defer tag.deinit(); 1205 base_oid = switch (tag.content) { 1206 .tag => |value| value.target, 1207 else => return error.InvalidObject, 1208 }; 1209 // rev 29 first changes line 4; edit that line or the one above it 1210 contents[0] = try scatterContent(aa, 0, 28, edit); 1211 break :blk .{ "src/alpha.txt", 1 }; 1212 } else blk: { 1213 var writer = std.Io.Writer.Allocating.init(allocator); 1214 defer writer.deinit(); 1215 for (&contents, 0..) |*content, part| { 1216 try writer.writer.print("{s}: part {d}\n", .{ patch.title, part + 1 }); 1217 content.* = try aa.dupe(u8, writer.written()); 1218 } 1219 break :blk .{ "patch.txt", contents.len }; 1220 }; 1221 try seedPatchRevision(io, allocator, repos_path, &patch_ids[i], patch.title, patch_author, timestamp + 1, random, base_oid, file_path, contents[0..count]); 1222 } 1223 const status = patch.status orelse continue; 1224 const fork_path = try fork.forkPath(allocator, repos_path, &patch_hex); 1225 defer allocator.free(fork_path); 1226 try pch.publish(.{}, io, allocator, admin_repo, target_repo, fork_path, .{ 1227 .id = patch_hex, 1228 .user_id = user_id.*, 1229 .repo_id = repo_id.*, 1230 .author = patch_author, 1231 .timestamp = timestamp + 6, 1232 }); 1233 1234 if (status == .merged) { 1235 try pch.mergeAndRemoveFork(.{}, io, allocator, repos_path, admin_repo, target_repo, .{ 1236 .id = patch_hex, 1237 .revision = .source, 1238 .author = patch_author, 1239 .timestamp = timestamp + 7, 1240 }); 1241 } else if (status != .open) { 1242 try evt.Patch.update(.server, .xit, .{}, io, allocator, target_repo, &patch_ids[i], .{ .status = status }, patch_author); 1243 } 1244 } 1245 1246 // seed a small comment tree on the newest patch 1247 var comment_ids: [5][evt.event_id_size]u8 = undefined; 1248 for (&comment_ids) |*id| id.* = evt.EventWithId.randomId(random); 1249 const comment_bodies = [_][]const u8{ 1250 "This also needs to preserve the executable bit for files nested inside generated directories.", 1251 "I tested the archive path and the direct-copy path; both currently lose the mode.", 1252 "The archive writer is the right place to centralize it, since every exporter already goes through that layer.", 1253 "Please include a regression fixture with a mix of executable and ordinary files.", 1254 "I'll add that fixture and cover both supported archive formats.", 1255 }; 1256 var comments: [comment_ids.len]evt.EventWithId = undefined; 1257 for (&comments, 0..) |*comment, i| { 1258 comment.* = .{ 1259 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 1260 .timestamp = @intCast(700 + i), 1261 .author = .{ .name = if (i % 2 == 0) "alice" else "bob", .email = if (i % 2 == 0) "alice@example.test" else "bob@example.test" }, 1262 .event = .{ .comment = .{ 1263 .thread_id = std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1264 .parent_id = switch (i) { 1265 0, 1 => std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1266 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 1267 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 1268 else => unreachable, 1269 }, 1270 .body = comment_bodies[i], 1271 } }, 1272 }; 1273 } 1274 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &comments); 1275 1276 // create divergent metadata edits on the next three patches 1277 var patch_arena = std.heap.ArenaAllocator.init(allocator); 1278 defer patch_arena.deinit(); 1279 const moment = try evt.currentMoment(.{}, target_repo); 1280 var values: [3]evt.Patch = undefined; 1281 for (1..4) |i| { 1282 const record = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &patch_arena, &patch_ids[i])) orelse return error.NotFound; 1283 values[i - 1] = record.event; 1284 } 1285 var ours_values = values; 1286 ours_values[0].title = "Drop the legacy configuration loader"; 1287 ours_values[0].tags = "cleanup config breaking"; 1288 const closed_description = try longDescription( 1289 patch_arena.allocator(), 1290 "Cache parsed manifests for the lifetime of a command invocation and invalidate entries when their files change.", 1291 ); 1292 ours_values[1].description = closed_description; 1293 ours_values[2].description = "Add JSON output to inspect with versioned field names and deterministic object ordering."; 1294 var theirs_values = values; 1295 theirs_values[0].title = "Delete compatibility configuration support"; 1296 theirs_values[0].tags = "config maintenance"; 1297 theirs_values[1].description = "Keep a process-wide manifest cache shared by every command and refresh it after writes."; 1298 theirs_values[2].description = "Expose inspect results as newline-delimited JSON so callers can stream large repositories."; 1299 1300 const seed_tip = (try target_repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 1301 const other_ref = xit.ref.Ref{ .kind = .head, .name = "haxy/patch-other" }; 1302 var ours = [_]evt.EventWithId{ 1303 .{ 1304 .id = std.fmt.bytesToHex(patch_ids[1], .lower), 1305 .timestamp = 800, 1306 .author = .{ .name = "alice", .email = "alice@example.test" }, 1307 .event = .{ .patch = ours_values[0] }, 1308 }, 1309 .{ 1310 .id = std.fmt.bytesToHex(patch_ids[2], .lower), 1311 .timestamp = 801, 1312 .author = .{ .name = "alice", .email = "alice@example.test" }, 1313 .event = .{ .patch = ours_values[1] }, 1314 }, 1315 .{ 1316 .id = std.fmt.bytesToHex(patch_ids[3], .lower), 1317 .timestamp = 802, 1318 .author = .{ .name = "alice", .email = "alice@example.test" }, 1319 .event = .{ .patch = ours_values[2] }, 1320 }, 1321 }; 1322 1323 var theirs = ours; 1324 theirs[0].timestamp = 810; 1325 theirs[0].author = .{ .name = "bob", .email = "bob@example.test" }; 1326 theirs[0].event.patch = theirs_values[0]; 1327 theirs[1].timestamp = 811; 1328 theirs[1].author = .{ .name = "bob", .email = "bob@example.test" }; 1329 theirs[1].event.patch = theirs_values[1]; 1330 theirs[2].timestamp = 812; 1331 theirs[2].author = .{ .name = "bob", .email = "bob@example.test" }; 1332 theirs[2].event.patch = theirs_values[2]; 1333 1334 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &ours); 1335 try commitEventsAtRef(io, allocator, target_repo, other_ref, &theirs, seed_tip); 1336 { 1337 var merge = try target_repo.mergeAtRef(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, evt.events_ref, null); 1338 defer merge.deinit(); 1339 if (merge.result != .success) return error.MergeFailed; 1340 } 1341 try target_repo.removeBranch(io, .{ .name = other_ref.name }); 1342 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &.{}); 1343 try pch.writeBranchPatch(.server, .xit, .{}, io, allocator, target_repo, std.fmt.bytesToHex(evt.EventWithId.randomId(random), .lower), .{ 1344 .title = "Merge the existing feature branch", 1345 .description = "This patch tracks the feature branch in this repo without creating a fork.", 1346 .tags = "feature", 1347 .source_branch = "feature", 1348 .target_branch = "master", 1349 }, null, patch_author); 1350 1351 // a push onto the branch that patch tracks, so the undo tab shows the 1352 // action and the patch refresh it sets off 1353 try seedPush(io, allocator, target_repo, patch_author); 1354 try pch.refreshBranches(.server, .xit, .{}, io, allocator, target_repo, null, null); 1355 } 1356 1357 // stand in for a client push, which the fixture has no server to receive: one 1358 // transaction moves the branch, refreshes the patches tracking it and records 1359 // the action, the way receive-pack does 1360 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), author: evt.CommitAuthor) !void { 1361 const Repo = rp.Repo(.xit, .{}); 1362 const DB = Repo.DB; 1363 const Ctx = struct { 1364 core: *Repo.Core, 1365 io: std.Io, 1366 allocator: std.mem.Allocator, 1367 author: evt.CommitAuthor, 1368 1369 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1370 const opts: rp.RepoOpts(.xit) = .{}; 1371 var moment = try DB.HashMap(.read_write).init(cursor.*); 1372 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1373 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1374 defer arena.deinit(); 1375 const aa = arena.allocator(); 1376 1377 // a null tree keeps the parent's content, so the branch advances 1378 // without the fixture having to build one 1379 const author_line = try std.fmt.allocPrint(aa, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1380 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1381 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1382 .message = "Revise the feature branch", 1383 .author = author_line, 1384 .timestamp = 820, 1385 }, null, .{ .kind = .head, .name = "feature" }); 1386 1387 // a branch the push creates has no old tip, so its range is open 1388 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1389 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1390 1391 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1392 .{ 1393 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1394 .old_oid = try aa.dupe(u8, &feature_old), 1395 .new_oid = try aa.dupe(u8, &feature_new), 1396 }, 1397 .{ 1398 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1399 .old_oid = try aa.dupe(u8, "0" ** hash.hexLen(opts.hash)), 1400 .new_oid = try aa.dupe(u8, &docs_new), 1401 }, 1402 }; 1403 1404 _ = try pch.refreshBranchesInTransaction(.server, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1405 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1406 } 1407 }; 1408 1409 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .author = author }); 1410 } 1411 1412 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1413 // consume always roots a new branch at nothing, so the divergent fixtures 1414 // write their side branch here instead. nothing is consumed, so it records 1415 // xit's own commit action rather than one of haxy's event actions 1416 fn commitEventsAtRef( 1417 io: std.Io, 1418 allocator: std.mem.Allocator, 1419 repo: *rp.Repo(.xit, .{}), 1420 ref: xit.ref.Ref, 1421 events: []const evt.EventWithId, 1422 parent: [hash.hexLen(.sha1)]u8, 1423 ) !void { 1424 const Repo = rp.Repo(.xit, .{}); 1425 const DB = Repo.DB; 1426 const Ctx = struct { 1427 core: *Repo.Core, 1428 io: std.Io, 1429 allocator: std.mem.Allocator, 1430 ref: xit.ref.Ref, 1431 events: []const evt.EventWithId, 1432 parent: [hash.hexLen(.sha1)]u8, 1433 1434 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1435 const opts: rp.RepoOpts(.xit) = .{}; 1436 var moment = try DB.HashMap(.read_write).init(cursor.*); 1437 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1438 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1439 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1440 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1441 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1442 }); 1443 } 1444 }; 1445 1446 try transaction(io, repo, Ctx{ 1447 .core = &repo.core, 1448 .io = io, 1449 .allocator = allocator, 1450 .ref = ref, 1451 .events = events, 1452 .parent = parent, 1453 }); 1454 } 1455 1456 // append `ctx` to the repo's history as one transaction. the fixtures write a 1457 // few states the normal paths have no way to produce 1458 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1459 const DB = rp.Repo(.xit, .{}).DB; 1460 try repo.core.db_file.lock(io, .exclusive); 1461 defer repo.core.db_file.unlock(io); 1462 1463 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1464 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1465 } 1466 1467 // recursively copy the contents of src_dir into dest_dir 1468 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1469 var iter = src_dir.iterate(); 1470 while (try iter.next(io)) |entry| { 1471 switch (entry.kind) { 1472 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1473 .directory => { 1474 try dest_dir.createDirPath(io, entry.name); 1475 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1476 defer dest_entry_dir.close(io); 1477 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1478 defer src_entry_dir.close(io); 1479 try copyDir(io, src_entry_dir, dest_entry_dir); 1480 }, 1481 else => {}, 1482 } 1483 } 1484 } 1485