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