╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1432 theirs_values[2].description = "Expose `inspect` results as newline-delimited JSON so callers can stream large repositories."; 1433 1434 const seed_tip = (try target_repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 1435 const other_ref = xit.ref.Ref{ .kind = .head, .name = "haxy/patch-other" }; 1436 var ours = [_]evt.EventWithId{ 1437 .{ 1438 .id = std.fmt.bytesToHex(patch_ids[1], .lower), 1439 .timestamp = now - 30 * std.time.s_per_min, 1440 .author = .{ .name = "alice", .email = "alice@example.test" }, 1441 .event = .{ .patch = ours_values[0] }, 1442 }, 1443 .{ 1444 .id = std.fmt.bytesToHex(patch_ids[2], .lower), 1445 .timestamp = now - 29 * std.time.s_per_min, 1446 .author = .{ .name = "alice", .email = "alice@example.test" }, 1447 .event = .{ .patch = ours_values[1] }, 1448 }, 1449 .{ 1450 .id = std.fmt.bytesToHex(patch_ids[3], .lower), 1451 .timestamp = now - 28 * std.time.s_per_min, 1452 .author = .{ .name = "alice", .email = "alice@example.test" }, 1453 .event = .{ .patch = ours_values[2] }, 1454 }, 1455 }; 1456 1457 var theirs = ours; 1458 theirs[0].timestamp = now - 25 * std.time.s_per_min; 1459 theirs[0].author = .{ .name = "bobby", .email = "bobby@example.test" }; 1460 theirs[0].event.patch = theirs_values[0]; 1461 theirs[1].timestamp = now - 24 * std.time.s_per_min; 1462 theirs[1].author = .{ .name = "bobby", .email = "bobby@example.test" }; 1463 theirs[1].event.patch = theirs_values[1]; 1464 theirs[2].timestamp = now - 23 * std.time.s_per_min; 1465 theirs[2].author = .{ .name = "bobby", .email = "bobby@example.test" }; 1466 theirs[2].event.patch = theirs_values[2]; 1467 1468 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &ours); 1469 try commitEventsAtRef(io, allocator, target_repo, other_ref, &theirs, seed_tip); 1470 { 1471 var merge = try target_repo.mergeAtRef(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, evt.events_ref, null); 1472 defer merge.deinit(); 1473 if (merge.result != .success) return error.MergeFailed; 1474 } 1475 try target_repo.removeBranch(io, .{ .name = other_ref.name }); 1476 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &.{}); 1477 try pch.writeBranchPatch(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, std.fmt.bytesToHex(evt.EventWithId.randomId(random), .lower), .{ 1478 .title = "Merge the existing feature branch", 1479 .description = "This patch tracks the `feature` branch in this repo without creating a fork.", 1480 .labels = "feature", 1481 .source_branch = "feature", 1482 .target_branch = "master", 1483 }, null, patch_author); 1484 1485 // a push onto the branch that patch tracks, so the undo tab shows the 1486 // action and the patch refresh it sets off 1487 try seedPush(io, allocator, target_repo, users_dir, patch_author, now - 15); 1488 try pch.refreshBranches(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, null, null); 1489 return patch_ids[patch_ids.len - 1]; 1490 } 1491 1492 // stand in for a client push, which the fixture has no server to receive: one 1493 // transaction moves the branch, refreshes the patches tracking it and records 1494 // the action, the way receive-pack does 1495 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), users_dir: []const u8, author: evt.CommitAuthor, timestamp: u64) !void { 1496 const Repo = rp.Repo(.xit, .{}); 1497 const DB = Repo.DB; 1498 const Ctx = struct { 1499 core: *Repo.Core, 1500 io: std.Io, 1501 allocator: std.mem.Allocator, 1502 users_dir: []const u8, 1503 author: evt.CommitAuthor, 1504 timestamp: u64, 1505 1506 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1507 const opts: rp.RepoOpts(.xit) = .{}; 1508 var moment = try DB.HashMap(.read_write).init(cursor.*); 1509 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 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