╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1215 const status = patch.status orelse continue; 1216 const fork_path = try fork.forkPath(allocator, repos_path, &patch_hex); 1217 defer allocator.free(fork_path); 1218 try pch.publish(.{}, io, allocator, admin_repo, target_repo, fork_path, .{ 1219 .id = patch_hex, 1220 .user_id = user_id.*, 1221 .repo_id = repo_id.*, 1222 .author = patch_author, 1223 .timestamp = timestamp + 6, 1224 }); 1225 1226 if (status == .merged) { 1227 try pch.mergeAndRemoveFork(.{}, io, allocator, repos_path, admin_repo, target_repo, .{ 1228 .id = patch_hex, 1229 .revision = .source, 1230 .author = patch_author, 1231 .timestamp = timestamp + 7, 1232 }); 1233 } else if (status != .open) { 1234 try evt.Patch.update(.server, .xit, .{}, io, allocator, target_repo, &patch_ids[i], .{ .status = status }, patch_author); 1235 } 1236 } 1237 1238 // seed a small comment tree on the newest patch 1239 var comment_ids: [5][evt.event_id_size]u8 = undefined; 1240 for (&comment_ids) |*id| id.* = evt.EventWithId.randomId(random); 1241 const comment_bodies = [_][]const u8{ 1242 "This also needs to preserve the executable bit for files nested inside generated directories.", 1243 "I tested the archive path and the direct-copy path; both currently lose the mode.", 1244 "The archive writer is the right place to centralize it, since every exporter already goes through that layer.", 1245 "Please include a regression fixture with a mix of executable and ordinary files.", 1246 "I'll add that fixture and cover both supported archive formats.", 1247 }; 1248 var comments: [comment_ids.len]evt.EventWithId = undefined; 1249 for (&comments, 0..) |*comment, i| { 1250 comment.* = .{ 1251 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 1252 .timestamp = @intCast(700 + i), 1253 .author = .{ .name = if (i % 2 == 0) "alice" else "bob", .email = if (i % 2 == 0) "alice@example.test" else "bob@example.test" }, 1254 .event = .{ .comment = .{ 1255 .thread_id = std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1256 .parent_id = switch (i) { 1257 0, 1 => std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1258 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 1259 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 1260 else => unreachable, 1261 }, 1262 .body = comment_bodies[i], 1263 } }, 1264 }; 1265 } 1266 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &comments); 1267 1268 // create divergent metadata edits on the next three patches 1269 var patch_arena = std.heap.ArenaAllocator.init(allocator); 1270 defer patch_arena.deinit(); 1271 const moment = try evt.currentMoment(.{}, target_repo); 1272 var values: [3]evt.Patch = undefined; 1273 for (1..4) |i| { 1274 const record = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &patch_arena, &patch_ids[i])) orelse return error.NotFound; 1275 values[i - 1] = record.event; 1276 } 1277 var ours_values = values; 1278 ours_values[0].title = "Drop the legacy configuration loader"; 1279 ours_values[0].tags = "cleanup config breaking"; 1280 const closed_description = try longDescription( 1281 patch_arena.allocator(), 1282 "Cache parsed manifests for the lifetime of a command invocation and invalidate entries when their files change.", 1283 ); 1284 ours_values[1].description = closed_description; 1285 ours_values[2].description = "Add JSON output to inspect with versioned field names and deterministic object ordering."; 1286 var theirs_values = values; 1287 theirs_values[0].title = "Delete compatibility configuration support"; 1288 theirs_values[0].tags = "config maintenance"; 1289 theirs_values[1].description = "Keep a process-wide manifest cache shared by every command and refresh it after writes."; 1290 theirs_values[2].description = "Expose inspect results as newline-delimited JSON so callers can stream large repositories."; 1291 1292 const seed_tip = (try target_repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 1293 const other_ref = xit.ref.Ref{ .kind = .head, .name = "haxy/patch-other" }; 1294 var ours = [_]evt.EventWithId{ 1295 .{ 1296 .id = std.fmt.bytesToHex(patch_ids[1], .lower), 1297 .timestamp = 800, 1298 .author = .{ .name = "alice", .email = "alice@example.test" }, 1299 .event = .{ .patch = ours_values[0] }, 1300 }, 1301 .{ 1302 .id = std.fmt.bytesToHex(patch_ids[2], .lower), 1303 .timestamp = 801, 1304 .author = .{ .name = "alice", .email = "alice@example.test" }, 1305 .event = .{ .patch = ours_values[1] }, 1306 }, 1307 .{ 1308 .id = std.fmt.bytesToHex(patch_ids[3], .lower), 1309 .timestamp = 802, 1310 .author = .{ .name = "alice", .email = "alice@example.test" }, 1311 .event = .{ .patch = ours_values[2] }, 1312 }, 1313 }; 1314 1315 var theirs = ours; 1316 theirs[0].timestamp = 810; 1317 theirs[0].author = .{ .name = "bob", .email = "bob@example.test" }; 1318 theirs[0].event.patch = theirs_values[0]; 1319 theirs[1].timestamp = 811; 1320 theirs[1].author = .{ .name = "bob", .email = "bob@example.test" }; 1321 theirs[1].event.patch = theirs_values[1]; 1322 theirs[2].timestamp = 812; 1323 theirs[2].author = .{ .name = "bob", .email = "bob@example.test" }; 1324 theirs[2].event.patch = theirs_values[2]; 1325 1326 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &ours); 1327 try commitEventsAtRef(io, allocator, target_repo, other_ref, &theirs, seed_tip); 1328 { 1329 var merge = try target_repo.mergeAtRef(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, evt.events_ref, null); 1330 defer merge.deinit(); 1331 if (merge.result != .success) return error.MergeFailed; 1332 } 1333 try target_repo.removeBranch(io, .{ .name = other_ref.name }); 1334 try evt.consume(.server, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &.{}); 1335 try pch.writeBranchPatch(.server, .xit, .{}, io, allocator, target_repo, std.fmt.bytesToHex(evt.EventWithId.randomId(random), .lower), .{ 1336 .title = "Merge the existing feature branch", 1337 .description = "This patch tracks the feature branch in this repo without creating a fork.", 1338 .tags = "feature", 1339 .source_branch = "feature", 1340 .target_branch = "master", 1341 }, null, patch_author); 1342 1343 // a push onto the branch that patch tracks, so the undo tab shows the 1344 // action and the patch refresh it sets off 1345 try seedPush(io, allocator, target_repo, patch_author); 1346 try pch.refreshBranches(.server, .xit, .{}, io, allocator, target_repo, null, null); 1347 } 1348 1349 // stand in for a client push, which the fixture has no server to receive: one 1350 // transaction moves the branch, refreshes the patches tracking it and records 1351 // the action, the way receive-pack does 1352 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), author: evt.CommitAuthor) !void { 1353 const Repo = rp.Repo(.xit, .{}); 1354 const DB = Repo.DB; 1355 const Ctx = struct { 1356 core: *Repo.Core, 1357 io: std.Io, 1358 allocator: std.mem.Allocator, 1359 author: evt.CommitAuthor, 1360 1361 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1362 const opts: rp.RepoOpts(.xit) = .{}; 1363 var moment = try DB.HashMap(.read_write).init(cursor.*); 1364 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1365 1366 // a null tree keeps the parent's content, so the branch advances 1367 // without the fixture having to build one 1368 const author_line = try std.fmt.allocPrint(ctx.allocator, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1369 defer ctx.allocator.free(author_line); 1370 _ = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1371 .message = "Revise the feature branch", 1372 .author = author_line, 1373 .timestamp = 820, 1374 }, null, .{ .kind = .head, .name = "feature" }); 1375 1376 _ = try pch.refreshBranchesInTransaction(.server, opts, state, &moment, ctx.io, ctx.allocator, null, null, null); 1377 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author); 1378 } 1379 }; 1380 1381 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .author = author }); 1382 } 1383 1384 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1385 // consume always roots a new branch at nothing, so the divergent fixtures 1386 // write their side branch here instead. nothing is consumed, so it records 1387 // xit's own commit action rather than one of haxy's event actions 1388 fn commitEventsAtRef( 1389 io: std.Io, 1390 allocator: std.mem.Allocator, 1391 repo: *rp.Repo(.xit, .{}), 1392 ref: xit.ref.Ref, 1393 events: []const evt.EventWithId, 1394 parent: [hash.hexLen(.sha1)]u8, 1395 ) !void { 1396 const Repo = rp.Repo(.xit, .{}); 1397 const DB = Repo.DB; 1398 const Ctx = struct { 1399 core: *Repo.Core, 1400 io: std.Io, 1401 allocator: std.mem.Allocator, 1402 ref: xit.ref.Ref, 1403 events: []const evt.EventWithId, 1404 parent: [hash.hexLen(.sha1)]u8, 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 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1411 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1412 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1413 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1414 }); 1415 } 1416 }; 1417 1418 try transaction(io, repo, Ctx{ 1419 .core = &repo.core, 1420 .io = io, 1421 .allocator = allocator, 1422 .ref = ref, 1423 .events = events, 1424 .parent = parent, 1425 }); 1426 } 1427 1428 // append `ctx` to the repo's history as one transaction. the fixtures write a 1429 // few states the normal paths have no way to produce 1430 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1431 const DB = rp.Repo(.xit, .{}).DB; 1432 try repo.core.db_file.lock(io, .exclusive); 1433 defer repo.core.db_file.unlock(io); 1434 1435 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1436 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1437 } 1438 1439 // recursively copy the contents of src_dir into dest_dir 1440 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1441 var iter = src_dir.iterate(); 1442 while (try iter.next(io)) |entry| { 1443 switch (entry.kind) { 1444 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1445 .directory => { 1446 try dest_dir.createDirPath(io, entry.name); 1447 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1448 defer dest_entry_dir.close(io); 1449 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1450 defer src_entry_dir.close(io); 1451 try copyDir(io, src_entry_dir, dest_entry_dir); 1452 }, 1453 else => {}, 1454 } 1455 } 1456 } 1457