╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1419 // a push onto the branch that patch tracks, so the undo tab shows the 1420 // action and the patch refresh it sets off 1421 try seedPush(io, allocator, target_repo, users_dir, patch_author); 1422 try pch.refreshBranches(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, null, null); 1423 } 1424 1425 // stand in for a client push, which the fixture has no server to receive: one 1426 // transaction moves the branch, refreshes the patches tracking it and records 1427 // the action, the way receive-pack does 1428 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), users_dir: []const u8, author: evt.CommitAuthor) !void { 1429 const Repo = rp.Repo(.xit, .{}); 1430 const DB = Repo.DB; 1431 const Ctx = struct { 1432 core: *Repo.Core, 1433 io: std.Io, 1434 allocator: std.mem.Allocator, 1435 users_dir: []const u8, 1436 author: evt.CommitAuthor, 1437 1438 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1439 const opts: rp.RepoOpts(.xit) = .{}; 1440 var moment = try DB.HashMap(.read_write).init(cursor.*); 1441 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1442 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1443 defer arena.deinit(); 1444 const aa = arena.allocator(); 1445 1446 // a null tree keeps the parent's content, so the branch advances 1447 // without the fixture having to build one 1448 const author_line = try aa.print("{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1449 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1450 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1451 .message = "Revise the feature branch", 1452 .author = author_line, 1453 .timestamp = 820, 1454 }, null, .{ .kind = .head, .name = "feature" }); 1455 1456 // a branch the push creates has no old tip, so its range is open 1457 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1458 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1459 1460 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1461 .{ 1462 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1463 .old_oid = try aa.dupe(u8, &feature_old), 1464 .new_oid = try aa.dupe(u8, &feature_new), 1465 }, 1466 .{ 1467 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1468 .old_oid = try aa.dupe(u8, &@as([hash.hexLen(opts.hash)]u8, @splat('0'))), 1469 .new_oid = try aa.dupe(u8, &docs_new), 1470 }, 1471 }; 1472 1473 _ = try pch.refreshBranchesInTransaction(.{ .server = .{ .users_dir = ctx.users_dir } }, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1474 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1475 } 1476 }; 1477 1478 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .users_dir = users_dir, .author = author }); 1479 } 1480 1481 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1482 // consume always roots a new branch at nothing, so the divergent fixtures 1483 // write their side branch here instead. nothing is consumed, so it records 1484 // xit's own commit action rather than one of haxy's event actions 1485 fn commitEventsAtRef( 1486 io: std.Io, 1487 allocator: std.mem.Allocator, 1488 repo: *rp.Repo(.xit, .{}), 1489 ref: xit.ref.Ref, 1490 events: []const evt.EventWithId, 1491 parent: [hash.hexLen(.sha1)]u8, 1492 ) !void { 1493 const Repo = rp.Repo(.xit, .{}); 1494 const DB = Repo.DB; 1495 const Ctx = struct { 1496 core: *Repo.Core, 1497 io: std.Io, 1498 allocator: std.mem.Allocator, 1499 ref: xit.ref.Ref, 1500 events: []const evt.EventWithId, 1501 parent: [hash.hexLen(.sha1)]u8, 1502 1503 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1504 const opts: rp.RepoOpts(.xit) = .{}; 1505 var moment = try DB.HashMap(.read_write).init(cursor.*); 1506 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1507 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1508 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1509 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1510 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1511 }); 1512 } 1513 }; 1514 1515 try transaction(io, repo, Ctx{ 1516 .core = &repo.core, 1517 .io = io, 1518 .allocator = allocator, 1519 .ref = ref, 1520 .events = events, 1521 .parent = parent, 1522 }); 1523 } 1524 1525 // append `ctx` to the repo's history as one transaction. the fixtures write a 1526 // few states the normal paths have no way to produce 1527 // fixtures commit directly, so the ref tips are indexed by hand. a fork gets 1528 // no commit index, as in production. 1529 const Reindex = struct { 1530 io: std.Io, 1531 allocator: std.mem.Allocator, 1532 core: *rp.Repo(.xit, .{}).Core, 1533 commits: bool, 1534 1535 pub fn run(ctx: @This(), cursor: *rp.Repo(.xit, .{}).DB.Cursor(.read_write)) !void { 1536 var moment = try rp.Repo(.xit, .{}).DB.HashMap(.read_write).init(cursor.*); 1537 const state = rp.Repo(.xit, .{}).State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1538 _ = try find.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, null); 1539 if (ctx.commits) _ = try srch_cmmt.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, &.{}, null); 1540 } 1541 }; 1542 1543 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1544 const DB = rp.Repo(.xit, .{}).DB; 1545 try repo.core.db_file.lock(io, .exclusive); 1546 defer repo.core.db_file.unlock(io); 1547 1548 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1549 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1550 } 1551 1552 // recursively copy the contents of src_dir into dest_dir 1553 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1554 var iter = src_dir.iterate(); 1555 while (try iter.next(io)) |entry| { 1556 switch (entry.kind) { 1557 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1558 .directory => { 1559 try dest_dir.createDirPath(io, entry.name); 1560 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1561 defer dest_entry_dir.close(io); 1562 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1563 defer src_entry_dir.close(io); 1564 try copyDir(io, src_entry_dir, dest_entry_dir); 1565 }, 1566 else => {}, 1567 } 1568 } 1569 } 1570