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