const std = @import("std");
const repeat = @import("zit").repeat;
const managed = @import("zit").managed;
const File = std.Io.File;
const support = @import("support.zig");
const addManaged = support.addManaged;
const corruptSection = support.corruptSection;
const expectExit = support.expectExit;
const expectExitDiag = support.expectExitDiag;
const expectManaged = support.expectManaged;
const freeResult = support.freeResult;
const illegal_new_names = support.illegal_new_names;
const initRepo = support.initRepo;
const runZit = support.runZit;
const sectionExists = support.sectionExists;
const touch = support.touch;
const touchTree = support.touchTree;
const writeFile = support.writeFile;
const writeNestedCheckouts = support.writeNestedCheckouts;
// ==========================================================================
// `zit add` end-to-end (contracts/add.md B-add-1..18). Each spawns the REAL
// binary in an isolated, pinned environment and reads `.zit-managed` back
// through the library.
// ==========================================================================
test "cli add B-add-2/-14/-16: happy path marks files managed, prints added, persists the set" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
try touch(io, tmp.dir, "b");
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "a", "b" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("ADDED a\nADDED b\n", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try expectManaged(gpa, io, tmp.dir, &.{ "a", "b" });
}
test "cli add B-add-3: re-adding a managed path is idempotent (notice to stderr, set unchanged)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
try touch(io, tmp.dir, "b");
var first = try runZit(gpa, io, tmp.dir, &.{ "add", "a", "b" });
defer freeResult(gpa, &first);
try expectExit(first.term, 0);
var second = try runZit(gpa, io, tmp.dir, &.{ "add", "a" });
defer freeResult(gpa, &second);
try expectExit(second.term, 0);
try std.testing.expectEqualStrings("", second.stdout);
try std.testing.expectEqualStrings("zit: 'a' is already tracked\n", second.stderr);
try expectManaged(gpa, io, tmp.dir, &.{ "a", "b" });
}
test "cli add B-add-15: ./a, a, and sub/../a denote one managed entry" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
try tmp.dir.createDir(io, "sub", .default_dir);
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "./a", "a", "sub/../a" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// First normalizes-and-adds; the later two are already tracked.
try std.testing.expectEqualStrings("ADDED a\n", res.stdout);
try std.testing.expectEqualStrings(
"zit: 'a' is already tracked\nzit: 'a' is already tracked\n",
res.stderr,
);
try expectManaged(gpa, io, tmp.dir, &.{"a"});
}
test "cli add B-add-4/-18: a missing path is partial — the valid sibling is still added, exit 1" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "a", "nope" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("ADDED a\n", res.stdout);
try std.testing.expectEqualStrings("zit add: 'nope': no such file\n", res.stderr);
try expectManaged(gpa, io, tmp.dir, &.{"a"});
}
test "cli add B-add-5: a named symlink is rejected; a directory recurses; the regular sibling is added" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
try tmp.dir.createDir(io, "d", .default_dir);
try tmp.dir.symLink(io, "a", "s", .{});
// Since P3-F4 a directory argument recurses (here `d` is empty, so it
// contributes nothing and is not an error). Naming a symlink directly is
// still B-add-5's refusal — since F-B3 with the symlink-specific wording,
// so it reads the same as the skip `add <dir>` prints for the same entry
// and does not read like a fifo/socket refusal.
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "a", "d", "s" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("ADDED a\n", res.stdout);
try std.testing.expectEqualStrings("zit add: 's': symlink (not versioned in 0.1.0)\n", res.stderr);
try expectManaged(gpa, io, tmp.dir, &.{"a"});
}
test "cli add B-add-6: a path outside the root is refused, nothing added" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "../x", "/etc/hosts" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit add: '../x' is outside the repository\n" ++
"zit add: '/etc/hosts' is outside the repository\n",
res.stderr,
);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add B-add-7: the three control files are refused" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{ "add", ".zit", ".zit-checkout", ".zit-cache" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit add: '.zit' is a zit control file and cannot be tracked\n" ++
"zit add: '.zit-checkout' is a zit control file and cannot be tracked\n" ++
"zit add: '.zit-cache' is a zit control file and cannot be tracked\n",
res.stderr,
);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add: a named file whose name has illegal characters is refused, exit 1, nothing added" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try writeFile(io, tmp.dir, "ok.txt", "ok\n");
for (illegal_new_names) |name| {
try writeFile(io, tmp.dir, name, "x\n");
var res = try runZit(gpa, io, tmp.dir, &.{ "add", name, "ok.txt" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "zit add: filename contains illegal characters: ") != null);
for (res.stderr) |b| try std.testing.expect(b == '\n' or (b >= ' ' and b != 0x7f));
try expectManaged(gpa, io, tmp.dir, &.{"ok.txt"});
}
}
test "cli add/addremove: a directory holding an illegal name warns, skips it, and adds the rest" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try tmp.dir.createDirPath(io, "d");
try writeFile(io, tmp.dir, "d/ok.txt", "ok\n");
try writeFile(io, tmp.dir, "d/c\x01d", "x\n");
try writeFile(io, tmp.dir, "d/smile\xf0\x9f\x98\x80", "x\n");
// fossil add.c add_one_file: warn, skip, carry on; the exit stays 0.
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "d" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "zit add: filename contains illegal characters: d/c\\x01d\n") != null);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "zit add: filename contains illegal characters: d/smile") != null);
try expectManaged(gpa, io, tmp.dir, &.{"d/ok.txt"});
try writeFile(io, tmp.dir, "d/two.txt", "two\n");
var ar = try runZit(gpa, io, tmp.dir, &.{"addremove"});
defer freeResult(gpa, &ar);
try expectExitDiag(ar.term, 0, ar);
try std.testing.expect(std.mem.indexOf(u8, ar.stderr, "zit addremove: filename contains illegal characters: d/c\\x01d\n") != null);
try expectManaged(gpa, io, tmp.dir, &.{ "d/ok.txt", "d/two.txt" });
}
test "cli add B-add-8: not a zit repository" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try touch(io, tmp.dir, "a"); // a file, but no repo anywhere above
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "a" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit: no zit checkout here (looked for .zit-checkout in this directory and its parents)\n" ++
"if this is a bare repository, name it with -R, or run 'zit open' to create a working tree\n",
res.stderr,
);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add B-add-9: no paths is a usage error, exit 2" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{"add"});
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("zit add: no paths given\ntry 'zit add --help'\n", res.stderr);
}
test "cli add B-add-10: an unknown option is a usage error, exit 2" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "-x" });
defer freeResult(gpa, &res);
try expectExit(res.term, 2);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit add: unknown option '-x'\ntry 'zit add --help'\n",
res.stderr,
);
}
test "cli add B-add-11: --dry-run previews and persists nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "-n", "a" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("would add a\n", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
// No write occurred: `.zit-managed` is absent.
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
// A dry run with a missing sibling still previews the valid one and reports
// the error, exits 1, and writes nothing.
var res2 = try runZit(gpa, io, tmp.dir, &.{ "add", "--dry-run", "a", "nope" });
defer freeResult(gpa, &res2);
try expectExit(res2.term, 1);
try std.testing.expectEqualStrings("would add a\n", res2.stdout);
try std.testing.expectEqualStrings("zit add: 'nope': no such file\n", res2.stderr);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add B-add-12: --quiet suppresses chatter but keeps effect and errors" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "-q", "a", "nope" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
// The `added a` line is suppressed; the error still prints.
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("zit add: 'nope': no such file\n", res.stderr);
// Effect persisted despite quiet.
try expectManaged(gpa, io, tmp.dir, &.{"a"});
// Quiet also suppresses the already-tracked notice.
var res2 = try runZit(gpa, io, tmp.dir, &.{ "add", "-q", "a" });
defer freeResult(gpa, &res2);
try expectExit(res2.term, 0);
try std.testing.expectEqualStrings("", res2.stdout);
try std.testing.expectEqualStrings("", res2.stderr);
}
/// Independent transcription of the B-add-13 help text (contracts/add.md §Help).
/// Hand-copied from the contract — never generated from the binary.
const expected_add_help =
\\usage: zit add [-n|--dry-run] [-q|--quiet] [-f|--force] [--dotfiles]
\\ [--] <path>...
\\
\\Mark one or more working-tree files as managed, so they are included in
\\your next check-in. zit has no staging area: `add` records only *which*
\\files to track — their contents are read from the working tree when you
\\run `zit commit`, not now. Re-adding an already-tracked file does nothing.
\\
\\Files stay tracked until `zit rm` forgets them. You can add files before
\\the first check-in (the default branch 'trunk' is unborn until then).
\\
\\Naming a directory adds every untracked file beneath it, recursively;
\\`zit add .` adds the whole tree. zit's control files are never added, and
\\names beginning with a dot are skipped unless --dotfiles is given (naming
\\one directly always adds it), apart from the files zit reads from the tree:
\\a root .zitignore, .zit-modules, .fossil-settings/ and .zit-settings/.
\\
\\options:
\\ -n, --dry-run show what would be added; change nothing
\\ -q, --quiet do not print the added files
\\ -f, --force add a file named directly even when ignore-glob
\\ matches it (a directory expansion always skips those)
\\ --dotfiles a directory expansion also takes names beginning with a
\\ dot (also settable with `zit settings dotfiles on`)
\\ -h, --help show this help and exit
\\
\\exit status:
\\ 0 all named files are now tracked (or already were)
\\ 1 a file could not be added (missing, not a regular file, outside the
\\ repo), or this is not a zit repository
\\ 2 usage error (no files given, or an unknown option)
\\
;
test "cli add B-add-13: --help/-h prints the verbatim help, exit 0, changes nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Help wins even with no repo, no path, and extra tokens present.
inline for (.{
&[_][]const u8{ "add", "--help" },
&[_][]const u8{ "add", "-h" },
&[_][]const u8{ "add", "--help", "a", "b" },
}) |argv| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var res = try runZit(gpa, io, tmp.dir, argv);
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(expected_add_help, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
}
test "cli add B-add-16: an all-invalid invocation writes nothing" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "nope", "also-nope" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
// No managed file created when nothing valid was added.
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add B-add-17: a corrupt .zit-managed fails cleanly, exit 1" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
// Pre-write garbage: the leading u32 decodes to an impossible entry count.
try corruptSection(io, tmp.dir, .managed);
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "a" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit add: cannot read tracked files: .zit-checkout is corrupt\n",
res.stderr,
);
}
test "cli add B-add-16/-17: a write failure leaves the prior managed set intact, exit 1" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touch(io, tmp.dir, "a");
// Make the checkout root read-only so `ManagedSet.write` cannot create its
// temp file → error.Storage, AFTER the (absent) set reads as empty. Restore
// write permission before cleanup can run (defers are LIFO).
try tmp.dir.setFilePermissions(io, ".", File.Permissions.fromMode(0o555), .{ .follow_symlinks = false });
defer tmp.dir.setFilePermissions(io, ".", File.Permissions.fromMode(0o777), .{ .follow_symlinks = false }) catch {};
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "a" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings(
"zit add: cannot update tracked files: input/output error\n",
res.stderr,
);
// The prior state is intact: no managed file was left behind.
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
// ==========================================================================
// Regression coverage for the three refuted behaviors (deep-cwd crash;
// help-vs-unknown-option precedence; `zit add .` misclassification).
// ==========================================================================
/// Creates `levels` nested subdirectories each named `seg` under `base`, and
/// returns an open handle to the deepest one (caller closes it). Built one
/// component at a time through directory handles, so the total path may exceed
/// the platform PATH_MAX without any single syscall ever seeing an over-long
/// path — the only portable way to place a subprocess's cwd past that limit.
fn makeDeepDir(io: std.Io, base: std.Io.Dir, seg: []const u8, levels: usize) !std.Io.Dir {
var cur = try base.openDir(io, ".", .{});
errdefer cur.close(io);
for (0..levels) |_| {
cur.createDir(io, seg, .default_dir) catch |e| switch (e) {
error.PathAlreadyExists => {},
else => return e,
};
const child = try cur.openDir(io, seg, .{});
cur.close(io);
cur = child;
}
return cur;
}
test "cli add B-add-17/-18: a deep checkout diagnoses instead of crashing (no panic)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// A cwd whose absolute path exceeds the platform PATH_MAX. Running `zit add`
// from there made std's `currentPathAlloc` abort the process (SIGABRT); the
// fix must instead exit cleanly with a diagnostic. 24 × 200-char levels
// ≈ 4800 bytes, past both the 1024 (macOS) and 4096 (Linux) limits.
const seg = repeat("d", 200);
var deep = try makeDeepDir(io, tmp.dir, seg, 24);
defer deep.close(io);
try touch(io, deep, "deep.txt");
var res = try runZit(gpa, io, deep, &.{ "add", "deep.txt" });
defer freeResult(gpa, &res);
// The regression guard: a clean exit 1, never a signal/abort term, and no
// panic trace on stderr.
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expect(std.mem.indexOf(u8, res.stderr, "panic") == null);
// Where getcwd names the deep path, the repo-relative key exceeds
// managed.max_path_len → the honest B-add-17 message; where getcwd refuses
// an over-long path, discovery reports no repository. Both are honest exit-1
// diagnostics; neither is a crash.
try std.testing.expect(
std.mem.indexOf(u8, res.stderr, "path is too long to track") != null or
std.mem.indexOf(u8, res.stderr, "no zit checkout here") != null,
);
}
test "cli add B-add-17: a path longer than managed.max_path_len is rejected as too long" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// A single arg whose normalized repo-relative key exceeds
// managed.max_path_len (4096), built from short "aa/" components so every
// path component is well under NAME_MAX. The file need not exist: the length
// bound is enforced before any filesystem stat, so this is deterministic on
// every platform (cwd stays short).
var buf: [managed.max_path_len + 64]u8 = undefined;
var w: usize = 0;
while (w + 3 <= buf.len) : (w += 3) {
buf[w] = 'a';
buf[w + 1] = 'a';
buf[w + 2] = '/';
}
buf[w] = 'f';
w += 1;
const long_arg = buf[0..w];
try std.testing.expect(long_arg.len > managed.max_path_len);
var res = try runZit(gpa, io, tmp.dir, &.{ "add", long_arg });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
var ebuf: [managed.max_path_len + 128]u8 = undefined;
const want = try std.fmt.bufPrint(&ebuf, "zit add: '{s}': path is too long to track\n", .{long_arg});
try std.testing.expectEqualStrings(want, res.stderr);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add B-add-13/-10: --help wins over an unknown option (exit 0, verbatim help)" {
const gpa = std.testing.allocator;
const io = std.testing.io;
// Both orders print the help and exit 0, never the unknown-option usage
// error — B-add-13's explicit precedence ("help wins over any other token")
// beats B-add-10.
inline for (.{
&[_][]const u8{ "add", "--help", "-x" },
&[_][]const u8{ "add", "-x", "--help" },
&[_][]const u8{ "add", "-x", "-h", "nope.txt" },
}) |argv| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
var res = try runZit(gpa, io, tmp.dir, argv);
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings(expected_add_help, res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
}
test "cli add B-add-5: 'zit add .' at the root sweeps the tree, never 'outside the repository'" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// `.` resolves to the checkout root itself — a directory INSIDE the repo,
// never B-add-6's "outside the repository" (the most likely novice
// invocation). Since P3-F4 it means the whole tree; a tree holding only the
// control files therefore adds nothing and succeeds.
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "." });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add -f: expansion always skips ignore-glob matches; a named match needs --force" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try tmp.dir.createDirPath(io, ".zit-settings");
try tmp.dir.createDirPath(io, "src");
try writeFile(io, tmp.dir, ".zit-settings/ignore-glob", "*.o\n");
try writeFile(io, tmp.dir, ".zit-settings/clean-glob", "*.tmp\n");
try writeFile(io, tmp.dir, "src/m.c", "c\n");
try writeFile(io, tmp.dir, "src/m.o", "o\n");
try writeFile(io, tmp.dir, "src/m.tmp", "t\n");
// Expansion: both glob lists narrow it, forced or not (fossil add.c).
var forced_dir = try runZit(gpa, io, tmp.dir, &.{ "add", "-f", "src" });
defer freeResult(gpa, &forced_dir);
try expectExitDiag(forced_dir.term, 0, forced_dir);
try expectManaged(gpa, io, tmp.dir, &.{"src/m.c"});
// A named ignore-glob match is skipped with a note, exit 0; a named
// clean-glob match is added silently, as fossil does.
var named = try runZit(gpa, io, tmp.dir, &.{ "add", "src/m.o", "src/m.tmp" });
defer freeResult(gpa, &named);
try expectExitDiag(named.term, 0, named);
try std.testing.expect(std.mem.indexOf(u8, named.stderr, "'src/m.o' matches ignore-glob; use --force to add it") != null);
try expectManaged(gpa, io, tmp.dir, &.{ "src/m.c", "src/m.tmp" });
var forced = try runZit(gpa, io, tmp.dir, &.{ "add", "-f", "src/m.o" });
defer freeResult(gpa, &forced);
try expectExitDiag(forced.term, 0, forced);
try std.testing.expectEqualStrings("", forced.stderr);
try expectManaged(gpa, io, tmp.dir, &.{ "src/m.c", "src/m.o", "src/m.tmp" });
}
test "cli add: '.' skips a nested checkout, naming the checkout itself walks into it" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try writeNestedCheckouts(gpa, io, tmp.dir);
{
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "." });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings("ADDED a.txt\nADDED junk/j.c\nADDED sub/s.txt\n", res.stdout);
}
{
// A directory above the nested checkouts prunes them as `.` does.
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "vendor" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings("", res.stdout);
}
{
// Fossil hands a named directory straight to its scan, so the
// nested checkout an argument names is walked.
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "vendor/lib", "vendor/fsl" });
defer freeResult(gpa, &res);
try expectExitDiag(res.term, 0, res);
try std.testing.expectEqualStrings("ADDED vendor/lib/lib.c\nADDED vendor/fsl/f.c\n", res.stdout);
}
}
test "cli add B-extras-5: 'zit add .zit-cache' and 'zit add .zit-tmp-pack' are refused as control files" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// The two transient artifacts join the never-tracked set: add/rm delegate to
// the same predicate `extras` uses, so the boundary cannot drift.
try touch(io, tmp.dir, ".zit-cache");
try touch(io, tmp.dir, ".zit-tmp-pack");
var res = try runZit(gpa, io, tmp.dir, &.{ "add", ".zit-cache", ".zit-tmp-pack" });
defer freeResult(gpa, &res);
try expectExit(res.term, 1);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings(
"zit add: '.zit-cache' is a zit control file and cannot be tracked\n" ++
"zit add: '.zit-tmp-pack' is a zit control file and cannot be tracked\n",
res.stderr,
);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add P3-F4: 'add <dir>' adds every untracked file beneath it, recursively and scoped" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touchTree(io, tmp.dir, &.{ "src/a", "src/deep/b", "src/deep/deeper/c", "other/d", "top" });
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "src" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// Ascending byte order (the sweep's own order), scoped to `src`: the
// sibling tree and the root file are untouched.
try std.testing.expectEqualStrings(
"ADDED src/a\nADDED src/deep/b\nADDED src/deep/deeper/c\n",
res.stdout,
);
try std.testing.expectEqualStrings("", res.stderr);
try expectManaged(gpa, io, tmp.dir, &.{ "src/a", "src/deep/b", "src/deep/deeper/c" });
}
test "cli add P3-F4: a directory prefix never leaks across a shared name boundary" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// `src2/…` shares src's bytes but is NOT under `src/` — a plain
// startsWith would wrongly swallow it.
try touchTree(io, tmp.dir, &.{ "src/a", "src2/b", "srcx" });
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "src" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("ADDED src/a\n", res.stdout);
try expectManaged(gpa, io, tmp.dir, &.{"src/a"});
}
test "cli add P3-F4: 'add .' at the root adds the whole tree; root control names are never added" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touchTree(io, tmp.dir, &.{ "a", "sub/b" });
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "." });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("ADDED a\nADDED sub/b\n", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
// `.zit`, `.zit-checkout` (and any other root control name) are excluded by
// the sweep itself, so `add .` can never track them.
try expectManaged(gpa, io, tmp.dir, &.{ "a", "sub/b" });
}
test "cli add P3-F4: the control-name exclusion is depth-0 only — a nested '.zit-cache' IS added" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
// Below the root zit's OWN names carry no special meaning; --dotfiles is
// what makes them visible at all now that dot-names are hidden by default.
try touchTree(io, tmp.dir, &.{ "sub/.zit-cache", "sub/.zit" });
var res = try runZit(gpa, io, tmp.dir, &.{ "add", ".", "--dotfiles" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("ADDED sub/.zit\nADDED sub/.zit-cache\n", res.stdout);
try expectManaged(gpa, io, tmp.dir, &.{ "sub/.zit", "sub/.zit-cache" });
}
test "cli add P3-F4: 'add .' inside a subdirectory scopes to that subdirectory" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touchTree(io, tmp.dir, &.{ "sub/a", "sub/deep/b", "other/c" });
var sub = try tmp.dir.openDir(io, "sub", .{});
defer sub.close(io);
var res = try runZit(gpa, io, sub, &.{ "add", "." });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// Keys stay root-relative even though the cwd is the subdirectory.
try std.testing.expectEqualStrings("ADDED sub/a\nADDED sub/deep/b\n", res.stdout);
try expectManaged(gpa, io, tmp.dir, &.{ "sub/a", "sub/deep/b" });
}
test "cli add P3-F4: a partially-tracked directory adds exactly the untracked remainder" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touchTree(io, tmp.dir, &.{ "sub/a", "sub/b", "sub/c" });
try addManaged(gpa, io, tmp.dir, &.{"sub/b"});
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "sub" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
// The tracked entry is excluded by the sweep — neither re-inserted nor
// reported as "already tracked" (it was never named).
try std.testing.expectEqualStrings("ADDED sub/a\nADDED sub/c\n", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try expectManaged(gpa, io, tmp.dir, &.{ "sub/a", "sub/b", "sub/c" });
}
test "cli add P3-F4: --dry-run over a directory previews without persisting" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touchTree(io, tmp.dir, &.{ "sub/a", "sub/deep/b" });
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "-n", "sub" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("would add sub/a\nwould add sub/deep/b\n", res.stdout);
try std.testing.expect(!sectionExists(gpa, io, tmp.dir, .managed));
}
test "cli add P3-F4: a directory holding nothing untracked adds nothing and succeeds" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try touchTree(io, tmp.dir, &.{"sub/a"});
try addManaged(gpa, io, tmp.dir, &.{"sub/a"});
var res = try runZit(gpa, io, tmp.dir, &.{ "add", "sub" });
defer freeResult(gpa, &res);
try expectExit(res.term, 0);
try std.testing.expectEqualStrings("", res.stdout);
try std.testing.expectEqualStrings("", res.stderr);
try expectManaged(gpa, io, tmp.dir, &.{"sub/a"});
}
test "cli add: clean-glob matches are not added by expansion but are listed" {
// fossil hands BOTH clean-glob and ignore-glob to the scan that expands a
// directory argument (add.c, addremove; vfile_scan skips on either), while
// `extras`/`status`/`clean` build their scan from ignore-glob alone. So a
// clean-glob match is never swept up by `add .`, and is still reported as
// an extra — the two verbs deliberately disagree.
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try initRepo(gpa, io, tmp.dir);
try tmp.dir.createDir(io, ".fossil-settings", .default_dir);
try writeFile(io, tmp.dir, ".fossil-settings/clean-glob", "*.o\n");
try tmp.dir.createDir(io, "src", .default_dir);
try writeFile(io, tmp.dir, "src/a.c", "int main(void){return 0;}\n");
try writeFile(io, tmp.dir, "src/a.o", "\x00object\n");
var a = try runZit(gpa, io, tmp.dir, &.{ "add", "src" });
defer freeResult(gpa, &a);
try expectExitDiag(a.term, 0, a);
try std.testing.expectEqualStrings("ADDED src/a.c\n", a.stdout);
// Still an extra, in both listings.
var ex = try runZit(gpa, io, tmp.dir, &.{"extras"});
defer freeResult(gpa, &ex);
try expectExitDiag(ex.term, 0, ex);
try std.testing.expectEqualStrings(".fossil-settings/clean-glob\nsrc/a.o\n", ex.stdout);
var st = try runZit(gpa, io, tmp.dir, &.{ "status", "--extra" });
defer freeResult(gpa, &st);
try expectExitDiag(st.term, 0, st);
try std.testing.expect(std.mem.indexOf(u8, st.stdout, "src/a.o") != null);
// Naming it takes it: fossil never consults clean-glob for an explicit path.
var named = try runZit(gpa, io, tmp.dir, &.{ "add", "src/a.o" });
defer freeResult(gpa, &named);
try expectExitDiag(named.term, 0, named);
try std.testing.expectEqualStrings("ADDED src/a.o\n", named.stdout);
}