╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1184 .status = .open, 1185 }, 1186 .{ 1187 .title = "Edit an adjacent line in alpha.txt", 1188 .description = "This patch can be cleanly merged by haxy, while git throws a merge conflict!", 1189 .labels = "merge", 1190 .status = .open, 1191 .alpha_edit = .{ .line = 2, .text = "adjust alpha beside the latest scatter edit" }, 1192 }, 1193 .{ 1194 .title = "Edit a conflicting line in alpha.txt", 1195 .description = "Both source and squash merges conflict with a different edit to the same line on master.", 1196 .labels = "merge", 1197 .status = .open, 1198 .alpha_edit = .{ .line = 3, .text = "replace alpha with a conflicting edit" }, 1199 }, 1200 }; 1201 1202 const repos_path = try std.fs.path.join(allocator, &.{ server_path, "repos" }); 1203 defer allocator.free(repos_path); 1204 const patch_author = evt.CommitAuthor{ .name = "admin", .email = "admin@example.test" }; 1205 var user_repo = (try evt.openUserRepo(io, allocator, repos_path, user_id)) orelse return error.NotFound; 1206 defer user_repo.deinit(io, allocator); 1207 var patch_ids: [patch_data.len][evt.event_id_size]u8 = undefined; 1208 for (patch_data, 0..) |patch, i| { 1209 const timestamp: u64 = @intCast(500 + i * 10); 1210 patch_ids[i] = evt.EventWithId.randomId(random); 1211 const patch_hex = std.fmt.bytesToHex(patch_ids[i], .lower); 1212 const path = try fork.create(.{}, io, allocator, repos_path, &user_repo, .{ 1213 .id = patch_hex, 1214 .user_id = user_id.*, 1215 .repo_id = repo_id.*, 1216 .repo_user_id = repo_user_id.*, 1217 .title = patch.title, 1218 .description = patch.description, 1219 .labels = patch.labels, 1220 .target_branch = "master", 1221 .author = patch_author, 1222 .timestamp = timestamp, 1223 }); 1224 allocator.free(path); 1225 1226 // prepare the patch's base and file contents, then seed its commits and revision 1227 { 1228 var arena = std.heap.ArenaAllocator.init(allocator); 1229 defer arena.deinit(); 1230 const aa = arena.allocator(); 1231 var base_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1232 if (patch.alpha_edit != null or patch.behind_master) { 1233 // v30 precedes the last scatter commit, even after the sample merge 1234 const tag_oid = (try target_repo.readRef(io, .{ .kind = .tag, .name = "v30" })) orelse return error.NotFound; 1235 var moment = try target_repo.core.latestMoment(); 1236 const state = rp.Repo(.xit, .{}).State(.read_only){ .core = &target_repo.core, .extra = .{ .moment = &moment } }; 1237 var tag = try obj.Object(.xit, .{}).init(state, io, allocator, &tag_oid); 1238 defer tag.deinit(); 1239 base_oid = switch (tag.content) { 1240 .tag => |value| value.target, 1241 else => return error.InvalidObject, 1242 }; 1243 } 1244 var contents: [3][]const u8 = undefined; 1245 const file_path: []const u8, const count: usize = if (patch.alpha_edit) |edit| blk: { 1246 // rev 29 first changes line 4; edit that line or the one above it 1247 contents[0] = try scatterContent(aa, 0, 28, edit); 1248 break :blk .{ "src/alpha.txt", 1 }; 1249 } else blk: { 1250 var writer = std.Io.Writer.Allocating.init(allocator); 1251 defer writer.deinit(); 1252 for (&contents, 0..) |*content, part| { 1253 try writer.writer.print("{s}: part {d}\n", .{ patch.title, part + 1 }); 1254 content.* = try aa.dupe(u8, writer.written()); 1255 } 1256 break :blk .{ "patch.txt", contents.len }; 1257 }; 1258 try seedPatchRevision(io, allocator, repos_path, &patch_ids[i], patch.title, patch_author, timestamp + 1, random, base_oid, file_path, contents[0..count]); 1259 } 1260 const status = patch.status orelse continue; 1261 const fork_path = try fork.forkPath(allocator, repos_path, &patch_hex); 1262 defer allocator.free(fork_path); 1263 try pch.publish(.{}, io, allocator, admin_repo, &user_repo, target_repo, fork_path, .{ 1264 .id = patch_hex, 1265 .user_id = user_id.*, 1266 .repo_id = repo_id.*, 1267 .author = patch_author, 1268 .timestamp = timestamp + 6, 1269 }); 1270 1271 if (status == .merged) { 1272 try pch.mergeAndRemoveFork(.{}, io, allocator, repos_path, target_repo, .{ 1273 .id = patch_hex, 1274 .revision = .source, 1275 .author = patch_author, 1276 .timestamp = timestamp + 7, 1277 }); 1278 } else if (status != .open) { 1279 try evt.Patch.update(.server, .xit, .{}, io, allocator, target_repo, &patch_ids[i], .{ .status = status }, patch_author); 1280 } 1281 } 1282 1283 // seed a small comment tree on the newest patch 1284 var comment_ids: [5][evt.event_id_size]u8 = undefined; 1285 for (&comment_ids) |*id| id.* = evt.EventWithId.randomId(random); 1286 const comment_bodies = [_][]const u8{ 1287 "This also needs to preserve the executable bit for files nested inside generated directories.", 1288 "I tested the archive path and the direct-copy path; both currently lose the mode.", 1289 "The archive writer is the right place to centralize it, since every exporter already goes through that layer.", 1290 "Please include a regression fixture with a mix of executable and ordinary files.", 1291 "I'll add that fixture and cover both supported archive formats.", 1292 }; 1293 var comments: [comment_ids.len]evt.EventWithId = undefined; 1294 for (&comments, 0..) |*comment, i| { 1295 comment.* = .{ 1296 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 1297 .timestamp = @intCast(700 + i), 1298 .author = .{ .name = if (i % 2 == 0) "alice" else "bob", .email = if (i % 2 == 0) "alice@example.test" else "bob@example.test" }, 1299 .event = .{ .comment = .{ 1300 .thread_id = std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1301 .parent_id = switch (i) { 1302 0, 1 => std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1303 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 1304 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 1305 else => unreachable, 1306 }, 1307 .body = comment_bodies[i], 1308 } }, 1309 }; 1310 } 1311 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &comments); 1312 1313 // create divergent metadata edits on the next three patches 1314 var patch_arena = std.heap.ArenaAllocator.init(allocator); 1315 defer patch_arena.deinit(); 1316 const moment = try evt.currentMoment(.{}, target_repo); 1317 var values: [3]evt.Patch = undefined; 1318 for (1..4) |i| { 1319 const record = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &patch_arena, &patch_ids[i])) orelse return error.NotFound; 1320 values[i - 1] = record.event; 1321 } 1322 var ours_values = values; 1323 ours_values[0].title = "Drop the legacy configuration loader"; 1324 ours_values[0].labels = "cleanup config breaking"; 1325 const closed_description = try longDescription( 1326 patch_arena.allocator(), 1327 "Cache parsed manifests for the lifetime of a command invocation and invalidate entries when their files change.", 1328 ); 1329 ours_values[1].description = closed_description; 1330 ours_values[2].description = "Add JSON output to inspect with versioned field names and deterministic object ordering."; 1331 var theirs_values = values; 1332 theirs_values[0].title = "Delete compatibility configuration support"; 1333 theirs_values[0].labels = "config maintenance"; 1334 theirs_values[1].description = "Keep a process-wide manifest cache shared by every command and refresh it after writes."; 1335 theirs_values[2].description = "Expose inspect results as newline-delimited JSON so callers can stream large repositories."; 1336 1337 const seed_tip = (try target_repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 1338 const other_ref = xit.ref.Ref{ .kind = .head, .name = "haxy/patch-other" }; 1339 var ours = [_]evt.EventWithId{ 1340 .{ 1341 .id = std.fmt.bytesToHex(patch_ids[1], .lower), 1342 .timestamp = 800, 1343 .author = .{ .name = "alice", .email = "alice@example.test" }, 1344 .event = .{ .patch = ours_values[0] }, 1345 }, 1346 .{ 1347 .id = std.fmt.bytesToHex(patch_ids[2], .lower), 1348 .timestamp = 801, 1349 .author = .{ .name = "alice", .email = "alice@example.test" }, 1350 .event = .{ .patch = ours_values[1] }, 1351 }, 1352 .{ 1353 .id = std.fmt.bytesToHex(patch_ids[3], .lower), 1354 .timestamp = 802, 1355 .author = .{ .name = "alice", .email = "alice@example.test" }, 1356 .event = .{ .patch = ours_values[2] }, 1357 }, 1358 }; 1359 1360 var theirs = ours; 1361 theirs[0].timestamp = 810; 1362 theirs[0].author = .{ .name = "bob", .email = "bob@example.test" }; 1363 theirs[0].event.patch = theirs_values[0]; 1364 theirs[1].timestamp = 811; 1365 theirs[1].author = .{ .name = "bob", .email = "bob@example.test" }; 1366 theirs[1].event.patch = theirs_values[1]; 1367 theirs[2].timestamp = 812; 1368 theirs[2].author = .{ .name = "bob", .email = "bob@example.test" }; 1369 theirs[2].event.patch = theirs_values[2]; 1370 1371 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &ours); 1372 try commitEventsAtRef(io, allocator, target_repo, other_ref, &theirs, seed_tip); 1373 { 1374 var merge = try target_repo.mergeAtRef(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, evt.events_ref, null); 1375 defer merge.deinit(); 1376 if (merge.result != .success) return error.MergeFailed; 1377 } 1378 try target_repo.removeBranch(io, .{ .name = other_ref.name }); 1379 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &.{}); 1380 try pch.writeBranchPatch(.server, .xit, .{}, io, allocator, target_repo, std.fmt.bytesToHex(evt.EventWithId.randomId(random), .lower), .{ 1381 .title = "Merge the existing feature branch", 1382 .description = "This patch tracks the feature branch in this repo without creating a fork.", 1383 .labels = "feature", 1384 .source_branch = "feature", 1385 .target_branch = "master", 1386 }, null, patch_author); 1387 1388 // a push onto the branch that patch tracks, so the undo tab shows the 1389 // action and the patch refresh it sets off 1390 try seedPush(io, allocator, target_repo, patch_author); 1391 try pch.refreshBranches(.server, .xit, .{}, io, allocator, target_repo, null, null); 1392 } 1393 1394 // stand in for a client push, which the fixture has no server to receive: one 1395 // transaction moves the branch, refreshes the patches tracking it and records 1396 // the action, the way receive-pack does 1397 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), author: evt.CommitAuthor) !void { 1398 const Repo = rp.Repo(.xit, .{}); 1399 const DB = Repo.DB; 1400 const Ctx = struct { 1401 core: *Repo.Core, 1402 io: std.Io, 1403 allocator: std.mem.Allocator, 1404 author: evt.CommitAuthor, 1405 1406 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1407 const opts: rp.RepoOpts(.xit) = .{}; 1408 var moment = try DB.HashMap(.read_write).init(cursor.*); 1409 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1410 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1411 defer arena.deinit(); 1412 const aa = arena.allocator(); 1413 1414 // a null tree keeps the parent's content, so the branch advances 1415 // without the fixture having to build one 1416 const author_line = try std.fmt.allocPrint(aa, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1417 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1418 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1419 .message = "Revise the feature branch", 1420 .author = author_line, 1421 .timestamp = 820, 1422 }, null, .{ .kind = .head, .name = "feature" }); 1423 1424 // a branch the push creates has no old tip, so its range is open 1425 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1426 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1427 1428 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1429 .{ 1430 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1431 .old_oid = try aa.dupe(u8, &feature_old), 1432 .new_oid = try aa.dupe(u8, &feature_new), 1433 }, 1434 .{ 1435 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1436 .old_oid = try aa.dupe(u8, "0" ** hash.hexLen(opts.hash)), 1437 .new_oid = try aa.dupe(u8, &docs_new), 1438 }, 1439 }; 1440 1441 _ = try pch.refreshBranchesInTransaction(.server, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1442 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1443 } 1444 }; 1445 1446 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .author = author }); 1447 } 1448 1449 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1450 // consume always roots a new branch at nothing, so the divergent fixtures 1451 // write their side branch here instead. nothing is consumed, so it records 1452 // xit's own commit action rather than one of haxy's event actions 1453 fn commitEventsAtRef( 1454 io: std.Io, 1455 allocator: std.mem.Allocator, 1456 repo: *rp.Repo(.xit, .{}), 1457 ref: xit.ref.Ref, 1458 events: []const evt.EventWithId, 1459 parent: [hash.hexLen(.sha1)]u8, 1460 ) !void { 1461 const Repo = rp.Repo(.xit, .{}); 1462 const DB = Repo.DB; 1463 const Ctx = struct { 1464 core: *Repo.Core, 1465 io: std.Io, 1466 allocator: std.mem.Allocator, 1467 ref: xit.ref.Ref, 1468 events: []const evt.EventWithId, 1469 parent: [hash.hexLen(.sha1)]u8, 1470 1471 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1472 const opts: rp.RepoOpts(.xit) = .{}; 1473 var moment = try DB.HashMap(.read_write).init(cursor.*); 1474 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1475 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1476 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1477 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1478 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1479 }); 1480 } 1481 }; 1482 1483 try transaction(io, repo, Ctx{ 1484 .core = &repo.core, 1485 .io = io, 1486 .allocator = allocator, 1487 .ref = ref, 1488 .events = events, 1489 .parent = parent, 1490 }); 1491 } 1492 1493 // append `ctx` to the repo's history as one transaction. the fixtures write a 1494 // few states the normal paths have no way to produce 1495 // fixtures commit directly, so the ref tips are indexed by hand. a fork gets 1496 // no commit index, as in production. 1497 const Reindex = struct { 1498 io: std.Io, 1499 allocator: std.mem.Allocator, 1500 core: *rp.Repo(.xit, .{}).Core, 1501 commits: bool, 1502 1503 pub fn run(ctx: @This(), cursor: *rp.Repo(.xit, .{}).DB.Cursor(.read_write)) !void { 1504 var moment = try rp.Repo(.xit, .{}).DB.HashMap(.read_write).init(cursor.*); 1505 const state = rp.Repo(.xit, .{}).State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1506 _ = try find.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, null); 1507 if (ctx.commits) _ = try srch_cmmt.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, &.{}, null); 1508 } 1509 }; 1510 1511 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1512 const DB = rp.Repo(.xit, .{}).DB; 1513 try repo.core.db_file.lock(io, .exclusive); 1514 defer repo.core.db_file.unlock(io); 1515 1516 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1517 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1518 } 1519 1520 // recursively copy the contents of src_dir into dest_dir 1521 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1522 var iter = src_dir.iterate(); 1523 while (try iter.next(io)) |entry| { 1524 switch (entry.kind) { 1525 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1526 .directory => { 1527 try dest_dir.createDirPath(io, entry.name); 1528 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1529 defer dest_entry_dir.close(io); 1530 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1531 defer src_entry_dir.close(io); 1532 try copyDir(io, src_entry_dir, dest_entry_dir); 1533 }, 1534 else => {}, 1535 } 1536 } 1537 } 1538