const std = @import("std");
const support = @import("support.zig");
const checkinHash = support.checkinHash;
const commitHashDup = support.commitHashDup;
const expectExitDiag = support.expectExitDiag;
const no_index = support.no_index;
const renameRun = support.renameRun;
const runCommitEnv = support.runCommitEnv;
const runZitEnv = support.runZitEnv;
const writeFile = support.writeFile;
/// The ids a `log` printed, in order.
fn renameLogIds(arena: std.mem.Allocator, out: []const u8) ![]const []const u8 {
var ids: std.ArrayList([]const u8) = .empty;
var lines = std.mem.splitScalar(u8, out, '\n');
while (lines.next()) |line| {
if (!std.mem.startsWith(u8, line, "checkin ")) continue;
const rest = line["checkin ".len..];
try ids.append(arena, rest[0 .. std.mem.indexOfScalar(u8, rest, ' ') orelse rest.len]);
}
return ids.items;
}
/// The 12-character origin of each `annotate` line, in order.
fn renameOrigins(arena: std.mem.Allocator, out: []const u8) ![]const []const u8 {
var ids: std.ArrayList([]const u8) = .empty;
var lines = std.mem.splitScalar(u8, out, '\n');
while (lines.next()) |line| {
if (line.len < 12) continue;
try ids.append(arena, line[0..12]);
}
return ids.items;
}
fn expectRenameIds(want: []const []const u8, got: []const []const u8, what: []const u8) !void {
var same = want.len == got.len;
if (same) for (want, got) |w, g| {
if (!std.mem.startsWith(u8, w, g) and !std.mem.startsWith(u8, g, w)) same = false;
};
if (same) return;
std.debug.print("\n{s}: want {d} ids, got {d}\n", .{ what, want.len, got.len });
for (want) |w| std.debug.print(" want {s}\n", .{w});
for (got) |g| std.debug.print(" got {s}\n", .{g});
return error.TestExpectedEqual;
}
/// `log tip PATH` with and without `--no-follow`, in text, `--long` and
/// JSON, answered from the file index and by reconstruction: the two must
/// agree byte for byte. Returns the followed ids.
fn renameLogAgreed(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, tip: []const u8, path: []const u8) ![]const []const u8 {
var followed: []const []const u8 = &.{};
for ([_]bool{ true, false }) |follow| {
for ([_][]const []const u8{ &.{}, &.{"--long"}, &.{ "-F", "json" } }) |form| {
var argv: std.ArrayList([]const u8) = .empty;
try argv.append(arena, "log");
try argv.appendSlice(arena, form);
if (!follow) try argv.append(arena, "--no-follow");
try argv.appendSlice(arena, &.{ tip, path });
// The evidence seam proves the index answered: a refused index
// would make the two runs agree vacuously.
const seam = [_][2][]const u8{.{ "ZIT_FILEINDEX_WALK", "1" }};
const run = try runZitEnv(arena, io, dir, &seam, argv.items);
try expectExitDiag(run.term, 0, run);
if (std.mem.indexOf(u8, run.stderr, "fileindex-unknown: none") != null or
std.mem.indexOf(u8, run.stderr, "fileindex-unknown: ") == null)
{
std.debug.print("\nlog {s}: the file index was not consulted\n{s}", .{ path, run.stderr });
return error.IndexNotConsulted;
}
const indexed = run.stdout;
const plain = try renameRun(arena, io, dir, &no_index, argv.items);
if (!std.mem.eql(u8, indexed, plain)) {
std.debug.print("\nlog {s} (follow={}) differs with the index:\n--- reconstructed ---\n{s}--- indexed ---\n{s}", .{ path, follow, plain, indexed });
return error.IndexChangedTheAnswer;
}
if (follow and form.len == 0) followed = try renameLogIds(arena, indexed);
}
}
return followed;
}
/// A store for the rename scenes: flat, or a sub-manifest store whose
/// check-ins are trees (80 padding files force several partitions).
fn renameScene(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, tree: bool) !void {
_ = try renameRun(arena, io, dir, &.{}, if (tree) &.{ "init", "--submanifest", "3" } else &.{"init"});
if (!tree) return;
try dir.createDir(io, "pad", .default_dir);
for (0..80) |i| {
const name = try std.fmt.allocPrint(arena, "pad/f{d:0>2}", .{i});
try writeFile(io, dir, name, name);
}
_ = try renameRun(arena, io, dir, &.{}, &.{ "add", "pad" });
}
/// The S-card keys of check-in `id`'s root, in order (empty for a flat one).
fn renameRootKeys(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, id: []const u8) ![]const []const u8 {
const out = try renameRun(arena, io, dir, &.{}, &.{ "artifact", id });
var keys: std.ArrayList([]const u8) = .empty;
var lines = std.mem.splitScalar(u8, out, '\n');
while (lines.next()) |line| {
if (!std.mem.startsWith(u8, line, "S ")) continue;
const rest = line[2..];
try keys.append(arena, rest[0..std.mem.indexOfScalar(u8, rest, ' ').?]);
}
return keys.items;
}
/// Whether any F card under artifact `id`'s tree ends with `suffix`.
fn renameTreeHolds(arena: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, id: []const u8, suffix: []const u8) !bool {
const out = try renameRun(arena, io, dir, &.{}, &.{ "artifact", id });
var lines = std.mem.splitScalar(u8, out, '\n');
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "F ") and std.mem.endsWith(u8, line, suffix)) return true;
if (std.mem.startsWith(u8, line, "S ")) {
const sub = line[std.mem.lastIndexOfScalar(u8, line, ' ').? + 1 ..];
if (try renameTreeHolds(arena, io, dir, sub, suffix)) return true;
}
}
return false;
}
test "rename: log PATH, annotate and crosslink follow a rename, flat and tree, across partitions" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const io = std.testing.io;
for ([_]bool{ false, true }) |tree| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const d = tmp.dir;
try renameScene(arena, io, d, tree);
// On the tree store the new name sorts after every padding file and
// the old one before them, so the rename crosses partitions.
const new = if (tree) "zzz/moved.md" else "notes/new.md";
try d.createDir(io, "notes", .default_dir);
try writeFile(io, d, "notes/old.md", "line one\nline two\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "notes/old.md" });
const c1 = try commitHashDup(arena, io, d, "c1");
try writeFile(io, d, "notes/old.md", "line one\nline two\nline three\n");
const c2 = try commitHashDup(arena, io, d, "c2");
if (tree) try d.createDir(io, "zzz", .default_dir);
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "notes/old.md", new });
const r = try commitHashDup(arena, io, d, "rename");
try writeFile(io, d, new, "line one\nline two\nline three\nline four\n");
const c4 = try commitHashDup(arena, io, d, "c4");
if (tree) {
const before = try renameRootKeys(arena, io, d, c2);
const after = try renameRootKeys(arena, io, d, r);
try std.testing.expect(before.len >= 2 and after.len >= 2);
// OLD sat in the parent's first partition, NEW sits in the last.
try std.testing.expect(std.mem.order(u8, "notes/old.md", before[1]) == .lt);
try std.testing.expect(std.mem.order(u8, new, after[after.len - 1]) != .lt);
// The leaf records the rename (old-name is legal in a leaf), and
// the next check-in's leaf no longer does.
try std.testing.expect(try renameTreeHolds(arena, io, d, r, "w notes/old.md"));
try std.testing.expect(!try renameTreeHolds(arena, io, d, c4, "w notes/old.md"));
}
// log: c4, the rename, then the history under the old name.
try expectRenameIds(&.{ c4, r, c2, c1 }, try renameLogAgreed(arena, io, d, c4, new), "log NEW");
const literal = try renameRun(arena, io, d, &.{}, &.{ "log", "--no-follow", c4, new });
try expectRenameIds(&.{ c4, r }, try renameLogIds(arena, literal), "log --no-follow NEW");
try expectRenameIds(&.{ r, c2, c1 }, try renameLogAgreed(arena, io, d, c4, "notes/old.md"), "log OLD");
// annotate: lines keep the check-ins that wrote them.
for ([_][]const [2][]const u8{ &.{}, &no_index }) |env| {
const ann = try renameRun(arena, io, d, env, &.{ "annotate", new });
try expectRenameIds(&.{ c1, c1, c2, c4 }, try renameOrigins(arena, ann), "annotate");
const lit = try renameRun(arena, io, d, env, &.{ "annotate", "--no-follow", new });
try expectRenameIds(&.{ r, r, r, c4 }, try renameOrigins(arena, lit), "annotate --no-follow");
}
// With no cache at all, annotate reconstructs every version.
try d.deleteTree(io, ".zit-cache");
const cold = try renameRun(arena, io, d, &.{}, &.{ "annotate", new });
try expectRenameIds(&.{ c1, c1, c2, c4 }, try renameOrigins(arena, cold), "annotate, no cache");
// crosslink: the A and D lines as before, and the rename after the add.
const feed = try renameRun(arena, io, d, &.{}, &.{"crosslink"});
const block = try std.fmt.allocPrint(arena, "file A {s}\nrename notes/old.md {s}\n", .{ new, new });
try std.testing.expect(std.mem.indexOf(u8, feed, block) != null);
try std.testing.expect(std.mem.indexOf(u8, feed, "file D notes/old.md\n") != null);
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, feed, "\nrename "));
const derived = try renameRun(arena, io, d, &.{}, &.{ "crosslink", "--no-cache" });
try std.testing.expectEqualStrings(feed, derived);
const counted = try renameRun(arena, io, d, &.{}, &.{ "crosslink", "--lines" });
const counted_block = try std.fmt.allocPrint(arena, "file A {s}\nlines 3 0\nrename notes/old.md {s}\n", .{ new, new });
try std.testing.expect(std.mem.indexOf(u8, counted, counted_block) != null);
// -F json: each row's own name, and the old name on the rename row.
const json = try renameRun(arena, io, d, &.{}, &.{ "log", "-F", "json", c4, new });
const parsed = try std.json.parseFromSliceLeaky(std.json.Value, arena, json, .{});
try std.testing.expectEqualStrings(new, parsed.object.get("name").?.string);
const rows = parsed.object.get("checkins").?.array.items;
try std.testing.expectEqual(@as(usize, 4), rows.len);
const want_names = [_][]const u8{ new, new, "notes/old.md", "notes/old.md" };
const want_states = [_][]const u8{ "modified", "modified", "modified", "added" };
for (rows, want_names, want_states, 0..) |row, name, state, i| {
try std.testing.expectEqualStrings(name, row.object.get("name").?.string);
try std.testing.expectEqualStrings(state, row.object.get("state").?.string);
const prior = row.object.get("prior").?;
if (i == 1) {
try std.testing.expectEqualStrings("notes/old.md", prior.string);
// A pure rename: the content did not move.
try std.testing.expectEqualStrings(row.object.get("uuid").?.string, row.object.get("parent").?.string);
} else try std.testing.expect(prior == .null);
}
}
}
test "rename: a rename and an edit in one check-in, and a chain of renames" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const io = std.testing.io;
for ([_]bool{ false, true }) |tree| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const d = tmp.dir;
try renameScene(arena, io, d, tree);
try writeFile(io, d, "a.txt", "one\ntwo\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "a.txt" });
const c1 = try commitHashDup(arena, io, d, "c1");
// Renamed and edited at once: the row keeps its new hash.
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "a.txt", "b.txt" });
try writeFile(io, d, "b.txt", "one\ntwo\nthree\n");
const ab = try commitHashDup(arena, io, d, "a to b, edited");
try writeFile(io, d, "b.txt", "one\ntwo\nthree\nfour\n");
const c3 = try commitHashDup(arena, io, d, "c3");
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "b.txt", "c.txt" });
const bc = try commitHashDup(arena, io, d, "b to c");
try expectRenameIds(&.{ bc, c3, ab, c1 }, try renameLogAgreed(arena, io, d, bc, "c.txt"), "log C");
try expectRenameIds(&.{ c3, ab, c1 }, try renameLogAgreed(arena, io, d, c3, "b.txt"), "log B at c3");
try expectRenameIds(&.{ ab, c1 }, try renameLogAgreed(arena, io, d, bc, "a.txt"), "log A");
for ([_][]const [2][]const u8{ &.{}, &no_index }) |env| {
const ann = try renameRun(arena, io, d, env, &.{ "annotate", "c.txt" });
try expectRenameIds(&.{ c1, c1, ab, c3 }, try renameOrigins(arena, ann), "annotate C");
}
const json = try renameRun(arena, io, d, &.{}, &.{ "log", "-F", "json", bc, "c.txt" });
const parsed = try std.json.parseFromSliceLeaky(std.json.Value, arena, json, .{});
const rows = parsed.object.get("checkins").?.array.items;
try std.testing.expectEqualStrings("b.txt", rows[0].object.get("prior").?.string);
try std.testing.expectEqualStrings("a.txt", rows[2].object.get("prior").?.string);
try std.testing.expectEqualStrings("b.txt", rows[2].object.get("name").?.string);
try std.testing.expectEqualStrings("a.txt", rows[3].object.get("name").?.string);
// The edit rode the rename: the content moved as well as the name.
try std.testing.expect(!std.mem.eql(u8, rows[2].object.get("uuid").?.string, rows[2].object.get("parent").?.string));
const feed = try renameRun(arena, io, d, &.{}, &.{"crosslink"});
try std.testing.expect(std.mem.indexOf(u8, feed, "file A b.txt\nrename a.txt b.txt\n") != null);
try std.testing.expect(std.mem.indexOf(u8, feed, "file A c.txt\nrename b.txt c.txt\n") != null);
}
}
test "rename: a new file at the old name has its own history, and the renamed file keeps its own" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const io = std.testing.io;
for ([_]bool{ false, true }) |tree| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const d = tmp.dir;
try renameScene(arena, io, d, tree);
try writeFile(io, d, "old.md", "first\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "old.md" });
const c1 = try commitHashDup(arena, io, d, "c1");
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "old.md", "new.md" });
const r = try commitHashDup(arena, io, d, "rename");
// A different file, created at the name the first one left.
try writeFile(io, d, "old.md", "fresh\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "old.md" });
const x = try commitHashDup(arena, io, d, "new file at old.md");
try writeFile(io, d, "old.md", "fresh\nagain\n");
const y = try commitHashDup(arena, io, d, "edit the new old.md");
try writeFile(io, d, "new.md", "first\nsecond\n");
const z = try commitHashDup(arena, io, d, "edit new.md");
try expectRenameIds(&.{ y, x }, try renameLogAgreed(arena, io, d, z, "old.md"), "log the new old.md");
try expectRenameIds(&.{ z, r, c1 }, try renameLogAgreed(arena, io, d, z, "new.md"), "log new.md");
// By name alone, old.md is both files, as before.
const literal = try renameRun(arena, io, d, &.{}, &.{ "log", "--no-follow", z, "old.md" });
try expectRenameIds(&.{ y, x, r, c1 }, try renameLogIds(arena, literal), "log --no-follow old.md");
// Asked where no file holds the name, the old file's history stands.
try expectRenameIds(&.{ r, c1 }, try renameLogAgreed(arena, io, d, r, "old.md"), "log old.md at the rename");
for ([_][]const [2][]const u8{ &.{}, &no_index }) |env| {
const fresh = try renameRun(arena, io, d, env, &.{ "annotate", "old.md", z });
try expectRenameIds(&.{ x, y }, try renameOrigins(arena, fresh), "annotate the new old.md");
const moved = try renameRun(arena, io, d, env, &.{ "annotate", "new.md", z });
try expectRenameIds(&.{ c1, z }, try renameOrigins(arena, moved), "annotate new.md");
}
}
}
test "rename: a rename on a branch merged back is followed through the merge" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const io = std.testing.io;
for ([_]bool{ false, true }) |tree| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const d = tmp.dir;
try renameScene(arena, io, d, tree);
try writeFile(io, d, "a.txt", "a1\na2\n");
try writeFile(io, d, "other.txt", "o1\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "a.txt", "other.txt" });
const c1 = try commitHashDup(arena, io, d, "c1");
try writeFile(io, d, "a.txt", "a1\na2\na3\n");
const c2 = try commitHashDup(arena, io, d, "c2");
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "a.txt", "b.txt" });
const br = blk: {
const res = try runCommitEnv(arena, io, d, .{ .user = "u" }, &.{ "commit", "-m", "rename on feat", "--branch", "feat" });
try expectExitDiag(res.term, 0, res);
break :blk try arena.dupe(u8, try checkinHash(res.stdout));
};
try writeFile(io, d, "b.txt", "a1\na2\na3\nb4\n");
const be = try commitHashDup(arena, io, d, "edit on feat");
_ = try renameRun(arena, io, d, &.{}, &.{ "update", "trunk" });
try writeFile(io, d, "other.txt", "o1\no2\n");
const t = try commitHashDup(arena, io, d, "trunk work");
_ = try renameRun(arena, io, d, &.{}, &.{ "merge", "feat" });
const m = try commitHashDup(arena, io, d, "merge feat");
_ = t;
try expectRenameIds(&.{ m, be, br, c2, c1 }, try renameLogAgreed(arena, io, d, m, "b.txt"), "log b.txt through the merge");
for ([_][]const [2][]const u8{ &.{}, &no_index }) |env| {
const ann = try renameRun(arena, io, d, env, &.{ "annotate", "b.txt", m });
try expectRenameIds(&.{ c1, c1, c2, be }, try renameOrigins(arena, ann), "annotate b.txt");
}
}
}
test "rename: the index and the reconstruction agree when a merged branch renamed away a name trunk keeps" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const d = tmp.dir;
try renameScene(arena, io, d, false);
try writeFile(io, d, "old.md", "one\n");
try writeFile(io, d, "u.txt", "u1\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "old.md", "u.txt" });
_ = try commitHashDup(arena, io, d, "c1");
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "old.md", "new.md" });
_ = try commitHashDup(arena, io, d, "rename");
// A different file at the old name, which trunk keeps from here on.
try writeFile(io, d, "old.md", "fresh\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "old.md" });
const x = try commitHashDup(arena, io, d, "new file at old.md");
try writeFile(io, d, "u.txt", "u2\n");
_ = try commitHashDup(arena, io, d, "trunk before the fork");
// On a branch, that file is renamed away.
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "old.md", "gone.md" });
{
const res = try runCommitEnv(arena, io, d, .{ .user = "u" }, &.{ "commit", "-m", "rename away on b", "--branch", "b" });
try expectExitDiag(res.term, 0, res);
}
_ = try renameRun(arena, io, d, &.{}, &.{ "update", "trunk" });
try writeFile(io, d, "u.txt", "u3\n");
_ = try commitHashDup(arena, io, d, "trunk after the fork");
// The merge keeps old.md as trunk had it; the next check-in deletes it.
_ = try renameRun(arena, io, d, &.{}, &.{ "merge", "b" });
try writeFile(io, d, "old.md", "fresh\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "old.md" });
_ = try commitHashDup(arena, io, d, "merge b, keeping old.md");
_ = try renameRun(arena, io, d, &.{}, &.{ "rm", "old.md" });
const m = try commitHashDup(arena, io, d, "delete old.md");
// The merge holds old.md without changing it, so only a walk that asks
// whether it holds the name knows the file was met above the branch's
// rename-away; both walks must, or they disagree on that check-in.
try expectRenameIds(&.{ m, x }, try renameLogAgreed(arena, io, d, m, "old.md"), "log old.md");
}
test "rename: annotate follows a rename the derived cache does not cover" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const d = tmp.dir;
try renameScene(arena, io, d, false);
try writeFile(io, d, "a.txt", "l1\nl2\n");
_ = try renameRun(arena, io, d, &.{}, &.{ "add", "a.txt" });
const c1 = try commitHashDup(arena, io, d, "c1");
try writeFile(io, d, "a.txt", "l1\nl2\nl3\n");
const c2 = try commitHashDup(arena, io, d, "c2");
_ = try renameRun(arena, io, d, &.{}, &.{ "log", c2 });
// The cache stops here: later check-ins are never written into it, so
// the rename below is known only from its manifest.
const lock = try std.process.run(arena, io, .{ .argv = &.{ "/bin/chmod", "-R", "a-w", ".zit-cache" }, .cwd = .{ .dir = d } });
try expectExitDiag(lock.term, 0, lock);
defer _ = std.process.run(arena, io, .{ .argv = &.{ "/bin/chmod", "-R", "u+w", ".zit-cache" }, .cwd = .{ .dir = d } }) catch {};
_ = try renameRun(arena, io, d, &.{}, &.{ "mv", "a.txt", "b.txt" });
_ = try commitHashDup(arena, io, d, "rename");
try writeFile(io, d, "b.txt", "l1\nl2\nl3\nl4\n");
const c4 = try commitHashDup(arena, io, d, "c4");
const ann = try renameRun(arena, io, d, &.{}, &.{ "annotate", "b.txt" });
try expectRenameIds(&.{ c1, c1, c2, c4 }, try renameOrigins(arena, ann), "annotate past an uncovered rename");
}