╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1374 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1375 const opts: rp.RepoOpts(.xit) = .{}; 1376 var moment = try DB.HashMap(.read_write).init(cursor.*); 1377 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1378 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1379 defer arena.deinit(); 1380 const aa = arena.allocator(); 1381 1382 // a null tree keeps the parent's content, so the branch advances 1383 // without the fixture having to build one 1384 const author_line = try std.fmt.allocPrint(aa, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1385 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1386 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1387 .message = "Revise the feature branch", 1388 .author = author_line, 1389 .timestamp = 820, 1390 }, null, .{ .kind = .head, .name = "feature" }); 1391 1392 // a branch the push creates has no old tip, so its range is open 1393 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1394 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1395 1396 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1397 .{ 1398 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1399 .old_oid = try aa.dupe(u8, &feature_old), 1400 .new_oid = try aa.dupe(u8, &feature_new), 1401 }, 1402 .{ 1403 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1404 .old_oid = try aa.dupe(u8, "0" ** hash.hexLen(opts.hash)), 1405 .new_oid = try aa.dupe(u8, &docs_new), 1406 }, 1407 }; 1408 1409 _ = try pch.refreshBranchesInTransaction(.server, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1410 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1411 } 1412 }; 1413 1414 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .author = author }); 1415 } 1416 1417 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1418 // consume always roots a new branch at nothing, so the divergent fixtures 1419 // write their side branch here instead. nothing is consumed, so it records 1420 // xit's own commit action rather than one of haxy's event actions 1421 fn commitEventsAtRef( 1422 io: std.Io, 1423 allocator: std.mem.Allocator, 1424 repo: *rp.Repo(.xit, .{}), 1425 ref: xit.ref.Ref, 1426 events: []const evt.EventWithId, 1427 parent: [hash.hexLen(.sha1)]u8, 1428 ) !void { 1429 const Repo = rp.Repo(.xit, .{}); 1430 const DB = Repo.DB; 1431 const Ctx = struct { 1432 core: *Repo.Core, 1433 io: std.Io, 1434 allocator: std.mem.Allocator, 1435 ref: xit.ref.Ref, 1436 events: []const evt.EventWithId, 1437 parent: [hash.hexLen(.sha1)]u8, 1438 1439 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1440 const opts: rp.RepoOpts(.xit) = .{}; 1441 var moment = try DB.HashMap(.read_write).init(cursor.*); 1442 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1443 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1444 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1445 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1446 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1447 }); 1448 } 1449 }; 1450 1451 try transaction(io, repo, Ctx{ 1452 .core = &repo.core, 1453 .io = io, 1454 .allocator = allocator, 1455 .ref = ref, 1456 .events = events, 1457 .parent = parent, 1458 }); 1459 } 1460 1461 // append `ctx` to the repo's history as one transaction. the fixtures write a 1462 // few states the normal paths have no way to produce 1463 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1464 const DB = rp.Repo(.xit, .{}).DB; 1465 try repo.core.db_file.lock(io, .exclusive); 1466 defer repo.core.db_file.unlock(io); 1467 1468 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1469 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1470 } 1471 1472 // recursively copy the contents of src_dir into dest_dir 1473 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1474 var iter = src_dir.iterate(); 1475 while (try iter.next(io)) |entry| { 1476 switch (entry.kind) { 1477 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1478 .directory => { 1479 try dest_dir.createDirPath(io, entry.name); 1480 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1481 defer dest_entry_dir.close(io); 1482 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1483 defer src_entry_dir.close(io); 1484 try copyDir(io, src_entry_dir, dest_entry_dir); 1485 }, 1486 else => {}, 1487 } 1488 } 1489 } 1490