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