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