╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1510 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1511 defer arena.deinit(); 1512 const aa = arena.allocator(); 1513 1514 // a null tree keeps the parent's content, so the branch advances 1515 // without the fixture having to build one 1516 const author_line = try aa.print("{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1517 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1518 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1519 .message = "Revise the feature branch", 1520 .author = author_line, 1521 .timestamp = ctx.timestamp, 1522 }, null, .{ .kind = .head, .name = "feature" }); 1523 1524 // a branch the push creates has no old tip, so its range is open 1525 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1526 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1527 1528 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1529 .{ 1530 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1531 .old_oid = try aa.dupe(u8, &feature_old), 1532 .new_oid = try aa.dupe(u8, &feature_new), 1533 }, 1534 .{ 1535 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1536 .old_oid = try aa.dupe(u8, &@as([hash.hexLen(opts.hash)]u8, @splat('0'))), 1537 .new_oid = try aa.dupe(u8, &docs_new), 1538 }, 1539 }; 1540 1541 // receive-pack patches the new tips before the branches refresh 1542 try xit.patch.writePatches(opts, state, ctx.io, ctx.allocator, &.{ feature_new, docs_new }, null); 1543 _ = try pch.refreshBranchesInTransaction(.{ .server = .{ .users_dir = ctx.users_dir } }, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1544 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1545 } 1546 }; 1547 1548 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .users_dir = users_dir, .author = author, .timestamp = timestamp }); 1549 } 1550 1551 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1552 // consume always roots a new branch at nothing, so the divergent fixtures 1553 // write their side branch here instead. nothing is consumed, so it records 1554 // xit's own commit action rather than one of haxy's event actions 1555 fn commitEventsAtRef( 1556 io: std.Io, 1557 allocator: std.mem.Allocator, 1558 repo: *rp.Repo(.xit, .{}), 1559 ref: xit.ref.Ref, 1560 events: []const evt.EventWithId, 1561 parent: [hash.hexLen(.sha1)]u8, 1562 ) !void { 1563 const Repo = rp.Repo(.xit, .{}); 1564 const DB = Repo.DB; 1565 const Ctx = struct { 1566 core: *Repo.Core, 1567 io: std.Io, 1568 allocator: std.mem.Allocator, 1569 ref: xit.ref.Ref, 1570 events: []const evt.EventWithId, 1571 parent: [hash.hexLen(.sha1)]u8, 1572 1573 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1574 const opts: rp.RepoOpts(.xit) = .{}; 1575 var moment = try DB.HashMap(.read_write).init(cursor.*); 1576 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1577 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1578 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1579 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1580 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1581 }); 1582 } 1583 }; 1584 1585 try transaction(io, repo, Ctx{ 1586 .core = &repo.core, 1587 .io = io, 1588 .allocator = allocator, 1589 .ref = ref, 1590 .events = events, 1591 .parent = parent, 1592 }); 1593 } 1594 1595 // append `ctx` to the repo's history as one transaction. the fixtures write a 1596 // few states the normal paths have no way to produce 1597 // fixtures commit directly, so the ref tips are indexed by hand. a fork gets 1598 // no commit index, as in production. 1599 const Reindex = struct { 1600 io: std.Io, 1601 allocator: std.mem.Allocator, 1602 core: *rp.Repo(.xit, .{}).Core, 1603 commits: bool, 1604 1605 pub fn run(ctx: @This(), cursor: *rp.Repo(.xit, .{}).DB.Cursor(.read_write)) !void { 1606 var moment = try rp.Repo(.xit, .{}).DB.HashMap(.read_write).init(cursor.*); 1607 const state = rp.Repo(.xit, .{}).State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1608 _ = try find.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, null); 1609 if (ctx.commits) _ = try srch_cmmt.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, &.{}, null); 1610 } 1611 }; 1612 1613 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1614 const DB = rp.Repo(.xit, .{}).DB; 1615 try repo.core.db_file.lock(io, .exclusive); 1616 defer repo.core.db_file.unlock(io); 1617 1618 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1619 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1620 } 1621 1622 // recursively copy the contents of src_dir into dest_dir 1623 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1624 var iter = src_dir.iterate(); 1625 while (try iter.next(io)) |entry| { 1626 switch (entry.kind) { 1627 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1628 .directory => { 1629 try dest_dir.createDirPath(io, entry.name); 1630 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1631 defer dest_entry_dir.close(io); 1632 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1633 defer src_entry_dir.close(io); 1634 try copyDir(io, src_entry_dir, dest_entry_dir); 1635 }, 1636 else => {}, 1637 } 1638 } 1639 } 1640