╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1294 for (&comments, 0..) |*comment, i| { 1295 comment.* = .{ 1296 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 1297 .timestamp = @intCast(700 + i), 1298 .author = .{ .name = if (i % 2 == 0) "alice" else "bob", .email = if (i % 2 == 0) "alice@example.test" else "bob@example.test" }, 1299 .event = .{ .comment = .{ 1300 .thread_id = std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1301 .parent_id = switch (i) { 1302 0, 1 => std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1303 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 1304 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 1305 else => unreachable, 1306 }, 1307 .body = comment_bodies[i], 1308 } }, 1309 }; 1310 } 1311 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &comments); 1312 1313 // create divergent metadata edits on the next three patches 1314 var patch_arena = std.heap.ArenaAllocator.init(allocator); 1315 defer patch_arena.deinit(); 1316 const moment = try evt.currentMoment(.{}, target_repo); 1317 var values: [3]evt.Patch = undefined; 1318 for (1..4) |i| { 1319 const record = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &patch_arena, &patch_ids[i])) orelse return error.NotFound; 1320 values[i - 1] = record.event; 1321 } 1322 var ours_values = values; 1323 ours_values[0].title = "Drop the legacy configuration loader"; 1324 ours_values[0].labels = "cleanup config breaking"; 1325 const closed_description = try longDescription( 1326 patch_arena.allocator(), 1327 "Cache parsed manifests for the lifetime of a command invocation and invalidate entries when their files change.", 1328 ); 1329 ours_values[1].description = closed_description; 1330 ours_values[2].description = "Add JSON output to inspect with versioned field names and deterministic object ordering."; 1331 var theirs_values = values; 1332 theirs_values[0].title = "Delete compatibility configuration support"; 1333 theirs_values[0].labels = "config maintenance"; 1334 theirs_values[1].description = "Keep a process-wide manifest cache shared by every command and refresh it after writes."; 1335 theirs_values[2].description = "Expose inspect results as newline-delimited JSON so callers can stream large repositories."; 1336 1337 const seed_tip = (try target_repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 1338 const other_ref = xit.ref.Ref{ .kind = .head, .name = "haxy/patch-other" }; 1339 var ours = [_]evt.EventWithId{ 1340 .{ 1341 .id = std.fmt.bytesToHex(patch_ids[1], .lower), 1342 .timestamp = 800, 1343 .author = .{ .name = "alice", .email = "alice@example.test" }, 1344 .event = .{ .patch = ours_values[0] }, 1345 }, 1346 .{ 1347 .id = std.fmt.bytesToHex(patch_ids[2], .lower), 1348 .timestamp = 801, 1349 .author = .{ .name = "alice", .email = "alice@example.test" }, 1350 .event = .{ .patch = ours_values[1] }, 1351 }, 1352 .{ 1353 .id = std.fmt.bytesToHex(patch_ids[3], .lower), 1354 .timestamp = 802, 1355 .author = .{ .name = "alice", .email = "alice@example.test" }, 1356 .event = .{ .patch = ours_values[2] }, 1357 }, 1358 }; 1359 1360 var theirs = ours; 1361 theirs[0].timestamp = 810; 1362 theirs[0].author = .{ .name = "bob", .email = "bob@example.test" }; 1363 theirs[0].event.patch = theirs_values[0]; 1364 theirs[1].timestamp = 811; 1365 theirs[1].author = .{ .name = "bob", .email = "bob@example.test" }; 1366 theirs[1].event.patch = theirs_values[1]; 1367 theirs[2].timestamp = 812; 1368 theirs[2].author = .{ .name = "bob", .email = "bob@example.test" }; 1369 theirs[2].event.patch = theirs_values[2]; 1370 1371 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &ours); 1372 try commitEventsAtRef(io, allocator, target_repo, other_ref, &theirs, seed_tip); 1373 { 1374 var merge = try target_repo.mergeAtRef(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, evt.events_ref, null); 1375 defer merge.deinit(); 1376 if (merge.result != .success) return error.MergeFailed; 1377 } 1378 try target_repo.removeBranch(io, .{ .name = other_ref.name }); 1379 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &.{}); 1380 try pch.writeBranchPatch(.server, .xit, .{}, io, allocator, target_repo, std.fmt.bytesToHex(evt.EventWithId.randomId(random), .lower), .{ 1381 .title = "Merge the existing feature branch", 1382 .description = "This patch tracks the feature branch in this repo without creating a fork.", 1383 .labels = "feature", 1384 .source_branch = "feature", 1385 .target_branch = "master", 1386 }, null, patch_author); 1387 1388 // a push onto the branch that patch tracks, so the undo tab shows the 1389 // action and the patch refresh it sets off 1390 try seedPush(io, allocator, target_repo, patch_author); 1391 try pch.refreshBranches(.server, .xit, .{}, io, allocator, target_repo, null, null); 1392 } 1393 1394 // stand in for a client push, which the fixture has no server to receive: one 1395 // transaction moves the branch, refreshes the patches tracking it and records 1396 // the action, the way receive-pack does 1397 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), author: evt.CommitAuthor) !void { 1398 const Repo = rp.Repo(.xit, .{}); 1399 const DB = Repo.DB; 1400 const Ctx = struct { 1401 core: *Repo.Core, 1402 io: std.Io, 1403 allocator: std.mem.Allocator, 1404 author: evt.CommitAuthor, 1405 1406 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1407 const opts: rp.RepoOpts(.xit) = .{}; 1408 var moment = try DB.HashMap(.read_write).init(cursor.*); 1409 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1410 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1411 defer arena.deinit(); 1412 const aa = arena.allocator(); 1413 1414 // a null tree keeps the parent's content, so the branch advances 1415 // without the fixture having to build one 1416 const author_line = try std.fmt.allocPrint(aa, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1417 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1418 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1419 .message = "Revise the feature branch", 1420 .author = author_line, 1421 .timestamp = 820, 1422 }, null, .{ .kind = .head, .name = "feature" }); 1423 1424 // a branch the push creates has no old tip, so its range is open 1425 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1426 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1427 1428 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1429 .{ 1430 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1431 .old_oid = try aa.dupe(u8, &feature_old), 1432 .new_oid = try aa.dupe(u8, &feature_new), 1433 }, 1434 .{ 1435 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1436 .old_oid = try aa.dupe(u8, "0" ** hash.hexLen(opts.hash)), 1437 .new_oid = try aa.dupe(u8, &docs_new), 1438 }, 1439 }; 1440 1441 _ = try pch.refreshBranchesInTransaction(.server, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1442 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1443 } 1444 }; 1445 1446 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .author = author }); 1447 } 1448 1449 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1450 // consume always roots a new branch at nothing, so the divergent fixtures 1451 // write their side branch here instead. nothing is consumed, so it records 1452 // xit's own commit action rather than one of haxy's event actions 1453 fn commitEventsAtRef( 1454 io: std.Io, 1455 allocator: std.mem.Allocator, 1456 repo: *rp.Repo(.xit, .{}), 1457 ref: xit.ref.Ref, 1458 events: []const evt.EventWithId, 1459 parent: [hash.hexLen(.sha1)]u8, 1460 ) !void { 1461 const Repo = rp.Repo(.xit, .{}); 1462 const DB = Repo.DB; 1463 const Ctx = struct { 1464 core: *Repo.Core, 1465 io: std.Io, 1466 allocator: std.mem.Allocator, 1467 ref: xit.ref.Ref, 1468 events: []const evt.EventWithId, 1469 parent: [hash.hexLen(.sha1)]u8, 1470 1471 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1472 const opts: rp.RepoOpts(.xit) = .{}; 1473 var moment = try DB.HashMap(.read_write).init(cursor.*); 1474 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1475 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1476 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1477 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1478 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1479 }); 1480 } 1481 }; 1482 1483 try transaction(io, repo, Ctx{ 1484 .core = &repo.core, 1485 .io = io, 1486 .allocator = allocator, 1487 .ref = ref, 1488 .events = events, 1489 .parent = parent, 1490 }); 1491 } 1492 1493 // append `ctx` to the repo's history as one transaction. the fixtures write a 1494 // few states the normal paths have no way to produce 1495 // fixtures commit directly, so the ref tips are indexed by hand. a fork gets 1496 // no commit index, as in production. 1497 const Reindex = struct { 1498 io: std.Io, 1499 allocator: std.mem.Allocator, 1500 core: *rp.Repo(.xit, .{}).Core, 1501 commits: bool, 1502 1503 pub fn run(ctx: @This(), cursor: *rp.Repo(.xit, .{}).DB.Cursor(.read_write)) !void { 1504 var moment = try rp.Repo(.xit, .{}).DB.HashMap(.read_write).init(cursor.*); 1505 const state = rp.Repo(.xit, .{}).State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1506 _ = try find.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, null); 1507 if (ctx.commits) _ = try srch_cmmt.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, &.{}, null); 1508 } 1509 }; 1510 1511 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1512 const DB = rp.Repo(.xit, .{}).DB; 1513 try repo.core.db_file.lock(io, .exclusive); 1514 defer repo.core.db_file.unlock(io); 1515 1516 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1517 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1518 } 1519 1520 // recursively copy the contents of src_dir into dest_dir 1521 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1522 var iter = src_dir.iterate(); 1523 while (try iter.next(io)) |entry| { 1524 switch (entry.kind) { 1525 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1526 .directory => { 1527 try dest_dir.createDirPath(io, entry.name); 1528 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1529 defer dest_entry_dir.close(io); 1530 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1531 defer src_entry_dir.close(io); 1532 try copyDir(io, src_entry_dir, dest_entry_dir); 1533 }, 1534 else => {}, 1535 } 1536 } 1537 } 1538