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