╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1276 .repo_id = repo_id.*, 1277 .author = patch_author, 1278 .timestamp = timestamp + 6, 1279 }); 1280 1281 if (status == .merged) { 1282 try pch.mergeAndRemoveFork(.{}, io, allocator, users_dir, target_repo, .{ 1283 .id = patch_hex, 1284 .revision = .source, 1285 .author = patch_author, 1286 .timestamp = timestamp + 7, 1287 }); 1288 } else if (status != .open) { 1289 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, &patch_ids[i], .{ .status = status }, patch_author); 1290 } 1291 } 1292 1293 // seed a small comment tree on the newest patch 1294 var comment_ids: [5][evt.event_id_size]u8 = undefined; 1295 for (&comment_ids) |*id| id.* = evt.EventWithId.randomId(random); 1296 const comment_bodies = [_][]const u8{ 1297 "This also needs to preserve the executable bit for files nested inside generated directories.", 1298 "I tested the archive path and the direct-copy path; both currently lose the mode.", 1299 "The archive writer is the right place to centralize it, since every exporter already goes through that layer.", 1300 "Please include a regression fixture with a mix of executable and ordinary files.", 1301 "I'll add that fixture and cover both supported archive formats.", 1302 }; 1303 var comments: [comment_ids.len]evt.EventWithId = undefined; 1304 for (&comments, 0..) |*comment, i| { 1305 comment.* = .{ 1306 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 1307 .timestamp = @intCast(700 + i), 1308 .author = .{ .name = if (i % 2 == 0) "alice" else "bobby", .email = if (i % 2 == 0) "alice@example.test" else "bobby@example.test" }, 1309 .event = .{ .comment = .{ 1310 .thread_id = std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1311 .parent_id = switch (i) { 1312 0, 1 => std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1313 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 1314 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 1315 else => unreachable, 1316 }, 1317 .body = comment_bodies[i], 1318 } }, 1319 }; 1320 } 1321 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &comments); 1322 1323 // create divergent metadata edits on the next three patches 1324 var patch_arena = std.heap.ArenaAllocator.init(allocator); 1325 defer patch_arena.deinit(); 1326 const moment = try evt.currentMoment(.{}, target_repo); 1327 var values: [3]evt.Patch = undefined; 1328 for (1..4) |i| { 1329 const record = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &patch_arena, &patch_ids[i])) orelse return error.NotFound; 1330 values[i - 1] = record.event; 1331 } 1332 var ours_values = values; 1333 ours_values[0].title = "Drop the legacy configuration loader"; 1334 ours_values[0].labels = "cleanup config breaking"; 1335 const closed_description = try longDescription( 1336 patch_arena.allocator(), 1337 "Cache parsed manifests for the lifetime of a command invocation and invalidate entries when their files change.", 1338 ); 1339 ours_values[1].description = closed_description; 1340 ours_values[2].description = "Add JSON output to inspect with versioned field names and deterministic object ordering."; 1341 var theirs_values = values; 1342 theirs_values[0].title = "Delete compatibility configuration support"; 1343 theirs_values[0].labels = "config maintenance"; 1344 theirs_values[1].description = "Keep a process-wide manifest cache shared by every command and refresh it after writes."; 1345 theirs_values[2].description = "Expose inspect results as newline-delimited JSON so callers can stream large repositories."; 1346 1347 const seed_tip = (try target_repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 1348 const other_ref = xit.ref.Ref{ .kind = .head, .name = "haxy/patch-other" }; 1349 var ours = [_]evt.EventWithId{ 1350 .{ 1351 .id = std.fmt.bytesToHex(patch_ids[1], .lower), 1352 .timestamp = 800, 1353 .author = .{ .name = "alice", .email = "alice@example.test" }, 1354 .event = .{ .patch = ours_values[0] }, 1355 }, 1356 .{ 1357 .id = std.fmt.bytesToHex(patch_ids[2], .lower), 1358 .timestamp = 801, 1359 .author = .{ .name = "alice", .email = "alice@example.test" }, 1360 .event = .{ .patch = ours_values[1] }, 1361 }, 1362 .{ 1363 .id = std.fmt.bytesToHex(patch_ids[3], .lower), 1364 .timestamp = 802, 1365 .author = .{ .name = "alice", .email = "alice@example.test" }, 1366 .event = .{ .patch = ours_values[2] }, 1367 }, 1368 }; 1369 1370 var theirs = ours; 1371 theirs[0].timestamp = 810; 1372 theirs[0].author = .{ .name = "bobby", .email = "bobby@example.test" }; 1373 theirs[0].event.patch = theirs_values[0]; 1374 theirs[1].timestamp = 811; 1375 theirs[1].author = .{ .name = "bobby", .email = "bobby@example.test" }; 1376 theirs[1].event.patch = theirs_values[1]; 1377 theirs[2].timestamp = 812; 1378 theirs[2].author = .{ .name = "bobby", .email = "bobby@example.test" }; 1379 theirs[2].event.patch = theirs_values[2]; 1380 1381 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &ours); 1382 try commitEventsAtRef(io, allocator, target_repo, other_ref, &theirs, seed_tip); 1383 { 1384 var merge = try target_repo.mergeAtRef(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, evt.events_ref, null); 1385 defer merge.deinit(); 1386 if (merge.result != .success) return error.MergeFailed; 1387 } 1388 try target_repo.removeBranch(io, .{ .name = other_ref.name }); 1389 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &.{}); 1390 try pch.writeBranchPatch(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, std.fmt.bytesToHex(evt.EventWithId.randomId(random), .lower), .{ 1391 .title = "Merge the existing feature branch", 1392 .description = "This patch tracks the feature branch in this repo without creating a fork.", 1393 .labels = "feature", 1394 .source_branch = "feature", 1395 .target_branch = "master", 1396 }, null, patch_author); 1397 1398 // a push onto the branch that patch tracks, so the undo tab shows the 1399 // action and the patch refresh it sets off 1400 try seedPush(io, allocator, target_repo, users_dir, patch_author); 1401 try pch.refreshBranches(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, null, null); 1402 } 1403 1404 // stand in for a client push, which the fixture has no server to receive: one 1405 // transaction moves the branch, refreshes the patches tracking it and records 1406 // the action, the way receive-pack does 1407 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), users_dir: []const u8, author: evt.CommitAuthor) !void { 1408 const Repo = rp.Repo(.xit, .{}); 1409 const DB = Repo.DB; 1410 const Ctx = struct { 1411 core: *Repo.Core, 1412 io: std.Io, 1413 allocator: std.mem.Allocator, 1414 users_dir: []const u8, 1415 author: evt.CommitAuthor, 1416 1417 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1418 const opts: rp.RepoOpts(.xit) = .{}; 1419 var moment = try DB.HashMap(.read_write).init(cursor.*); 1420 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1421 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1422 defer arena.deinit(); 1423 const aa = arena.allocator(); 1424 1425 // a null tree keeps the parent's content, so the branch advances 1426 // without the fixture having to build one 1427 const author_line = try std.fmt.allocPrint(aa, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1428 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1429 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1430 .message = "Revise the feature branch", 1431 .author = author_line, 1432 .timestamp = 820, 1433 }, null, .{ .kind = .head, .name = "feature" }); 1434 1435 // a branch the push creates has no old tip, so its range is open 1436 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1437 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1438 1439 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1440 .{ 1441 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1442 .old_oid = try aa.dupe(u8, &feature_old), 1443 .new_oid = try aa.dupe(u8, &feature_new), 1444 }, 1445 .{ 1446 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1447 .old_oid = try aa.dupe(u8, "0" ** hash.hexLen(opts.hash)), 1448 .new_oid = try aa.dupe(u8, &docs_new), 1449 }, 1450 }; 1451 1452 _ = try pch.refreshBranchesInTransaction(.{ .server = .{ .users_dir = ctx.users_dir } }, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1453 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1454 } 1455 }; 1456 1457 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .users_dir = users_dir, .author = author }); 1458 } 1459 1460 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1461 // consume always roots a new branch at nothing, so the divergent fixtures 1462 // write their side branch here instead. nothing is consumed, so it records 1463 // xit's own commit action rather than one of haxy's event actions 1464 fn commitEventsAtRef( 1465 io: std.Io, 1466 allocator: std.mem.Allocator, 1467 repo: *rp.Repo(.xit, .{}), 1468 ref: xit.ref.Ref, 1469 events: []const evt.EventWithId, 1470 parent: [hash.hexLen(.sha1)]u8, 1471 ) !void { 1472 const Repo = rp.Repo(.xit, .{}); 1473 const DB = Repo.DB; 1474 const Ctx = struct { 1475 core: *Repo.Core, 1476 io: std.Io, 1477 allocator: std.mem.Allocator, 1478 ref: xit.ref.Ref, 1479 events: []const evt.EventWithId, 1480 parent: [hash.hexLen(.sha1)]u8, 1481 1482 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1483 const opts: rp.RepoOpts(.xit) = .{}; 1484 var moment = try DB.HashMap(.read_write).init(cursor.*); 1485 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1486 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1487 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1488 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1489 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1490 }); 1491 } 1492 }; 1493 1494 try transaction(io, repo, Ctx{ 1495 .core = &repo.core, 1496 .io = io, 1497 .allocator = allocator, 1498 .ref = ref, 1499 .events = events, 1500 .parent = parent, 1501 }); 1502 } 1503 1504 // append `ctx` to the repo's history as one transaction. the fixtures write a 1505 // few states the normal paths have no way to produce 1506 // fixtures commit directly, so the ref tips are indexed by hand. a fork gets 1507 // no commit index, as in production. 1508 const Reindex = struct { 1509 io: std.Io, 1510 allocator: std.mem.Allocator, 1511 core: *rp.Repo(.xit, .{}).Core, 1512 commits: bool, 1513 1514 pub fn run(ctx: @This(), cursor: *rp.Repo(.xit, .{}).DB.Cursor(.read_write)) !void { 1515 var moment = try rp.Repo(.xit, .{}).DB.HashMap(.read_write).init(cursor.*); 1516 const state = rp.Repo(.xit, .{}).State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1517 _ = try find.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, null); 1518 if (ctx.commits) _ = try srch_cmmt.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, &.{}, null); 1519 } 1520 }; 1521 1522 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1523 const DB = rp.Repo(.xit, .{}).DB; 1524 try repo.core.db_file.lock(io, .exclusive); 1525 defer repo.core.db_file.unlock(io); 1526 1527 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1528 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1529 } 1530 1531 // recursively copy the contents of src_dir into dest_dir 1532 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1533 var iter = src_dir.iterate(); 1534 while (try iter.next(io)) |entry| { 1535 switch (entry.kind) { 1536 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1537 .directory => { 1538 try dest_dir.createDirPath(io, entry.name); 1539 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1540 defer dest_entry_dir.close(io); 1541 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1542 defer src_entry_dir.close(io); 1543 try copyDir(io, src_entry_dir, dest_entry_dir); 1544 }, 1545 else => {}, 1546 } 1547 } 1548 } 1549