monochromatic-git-policy-cli 0.1.2

Repository-owned Git policy wrapper: forwards Git commands and checks commits against configured policies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! What:
//!  The linked-worktree ignored-state copy through the built executable:
//!  `git worktree add` from the main checkout and from a linked worktree brings the generated forbidden-strings
//!  rules file into the new worktree, and the scanner there finds its rules.
//! Why:
//!  The user decision of 2026-10-12 is that a fresh worktree must get `.cache/forbidden-strings.rules.txt` when the
//!  wrapper creates it; a source without the file copies nothing, so the scanner fails closed instead of scanning
//!  with fewer rules.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // run(wrappedGit, ['worktree', 'add', ...]); expect(run(wrappedGit, ['cli-git', 'check', '--all']).status).toBe(1);
//! ```

/// Import the shared fixtures and the bounded process helpers.
use super::support::{
    Fixture, Observed, fixture, git, observe, remove, repository, run_wrapped, stderr_of, wrapped,
};
use git_policy_cli::worktree_copy_journal::{create_journal, read_pending_journals};
use git_policy_cli::worktree_copy_selection::CopySelection;
use git_policy_cli::worktree_copy_stage::{StagedSnapshot, stage_ignored_snapshot};
use git_policy_cli::transaction_git::GitContext;
use std::ffi::OsStr;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

/// Where the generated rules file lives, as the configuration's `rulesFile` names it.
const RULES_FILE: &str = ".cache/forbidden-strings.rules.txt";

/// The text the rules file forbids and the tracked file `a.txt` contains.
fn needle() -> String {
    return ["PLANTED", "COPY", "NEEDLE"].join("_");
}

/// A repository whose committed configuration names the rules file, with `a.txt` holding the forbidden text.
/// The rules file and a second ignored directory exist only when `with_rules` is set.
fn repository_with_rules(fixture: &Fixture, with_rules: bool) -> PathBuf {
    let repo: PathBuf = repository(fixture, OsStr::new("repo"));
    std::fs::write(
        repo.join("cli-git.config.jsonc"),
        format!(
            "{{ \"policies\": {{ \"security/forbidden-strings\": [\"error\", {{ \"builtinRules\": false, \"rulesFile\": \"{RULES_FILE}\" }}] }} }}\n"
        ),
    )
    .expect("configuration");
    std::fs::write(repo.join(".gitignore"), b".cache/\nheavy/\n").expect("gitignore");
    std::fs::write(repo.join("a.txt"), format!("first\n{}\n", needle())).expect("needle file");
    git(
        fixture,
        repo.as_path(),
        &["add", "--", "cli-git.config.jsonc", ".gitignore", "a.txt"],
    );
    git(
        fixture,
        repo.as_path(),
        &["commit", "--quiet", "--message=configuration"],
    );
    if with_rules {
        std::fs::create_dir_all(repo.join(".cache")).expect("cache directory");
        std::fs::write(repo.join(RULES_FILE), format!("{}\n", needle())).expect("rules");
        std::fs::write(repo.join(".cache/other.txt"), b"other\n").expect("other cache file");
        std::fs::create_dir_all(repo.join("heavy")).expect("heavy directory");
        std::fs::write(repo.join("heavy/data"), b"payload\n").expect("heavy data");
    }
    return repo;
}

/// Run the forbidden-strings check over every file of a worktree.
fn check_all(fixture: &Fixture, worktree: &Path) -> Observed {
    return run_wrapped(
        fixture,
        worktree,
        &["cli-git", "check", "--policy", "security/forbidden-strings", "--all"],
    );
}

/// The check found the forbidden text with the copied rules.
fn assert_scanner_has_rules(observed: &Observed) {
    let stdout: String = String::from_utf8_lossy(&observed.stdout).into_owned();
    assert_eq!(observed.code, Some(1), "{stdout} {}", stderr_of(observed));
    assert!(
        stdout.contains("security/forbidden-strings/forbidden-string"),
        "{stdout}"
    );
}

/// The check could not load rules and scanned nothing; the message names the missing path.
fn assert_scanner_failed_closed(observed: &Observed, worktree: &Path) {
    let stdout: String = String::from_utf8_lossy(&observed.stdout).into_owned();
    assert_eq!(observed.code, Some(2), "{stdout}");
    assert!(stdout.contains("\"code\":\"policy-incomplete\""), "{stdout}");
    assert!(
        stdout.contains(worktree.join(RULES_FILE).to_string_lossy().as_ref()),
        "{stdout}"
    );
}

/// A worktree added from the main checkout receives the rules file and nothing else the checkout ignores,
/// and the scanner there finds its rules;
///  the same add with the opt-out leaves the scanner without rules.
#[test]
fn worktree_added_from_the_main_checkout_receives_the_rules_file() {
    let fixture: Fixture = fixture("copy-main");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    let created: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--quiet", "-b", "one", "../one"],
    );
    assert_eq!(created.code, Some(0), "{}", stderr_of(&created));
    assert_eq!(
        stderr_of(&created),
        format!(
            "cli-git: copied 1 ignored filesystem entry from \"{}\" into 1 new worktree.\n",
            repo.display()
        )
    );
    let one: PathBuf = fixture.root.join("one");
    assert_eq!(
        std::fs::read(one.join(RULES_FILE)).expect("copied rules"),
        format!("{}\n", needle()).into_bytes()
    );
    assert!(!one.join(".cache/other.txt").exists(), "siblings stay behind");
    assert!(!one.join("heavy").exists(), "other ignored trees stay behind");
    assert_scanner_has_rules(&check_all(&fixture, one.as_path()));
    // Control: the opt-out copies nothing, and then the scanner has no rules and fails closed.
    let opted_out: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--no-worktree-copy", "--quiet", "-b", "two", "../two"],
    );
    assert_eq!(opted_out.code, Some(0), "{}", stderr_of(&opted_out));
    assert!(opted_out.stderr.is_empty(), "{}", stderr_of(&opted_out));
    let two: PathBuf = fixture.root.join("two");
    assert!(!two.join(".cache").exists());
    assert_scanner_failed_closed(&check_all(&fixture, two.as_path()), two.as_path());
    remove(&fixture);
}

/// A worktree added from a linked worktree receives every ignored path of that worktree.
#[test]
fn worktree_added_from_a_linked_worktree_receives_all_ignored_state() {
    let fixture: Fixture = fixture("copy-linked");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    assert_eq!(
        run_wrapped(
            &fixture,
            repo.as_path(),
            &["worktree", "add", "--quiet", "-b", "one", "../one"],
        )
        .code,
        Some(0)
    );
    let one: PathBuf = fixture.root.join("one");
    std::fs::create_dir_all(one.join("heavy")).expect("heavy in the source");
    std::fs::write(one.join("heavy/data"), b"payload\n").expect("heavy data");
    let created: Observed = run_wrapped(
        &fixture,
        one.as_path(),
        &["worktree", "add", "--quiet", "-b", "two", "../two"],
    );
    assert_eq!(created.code, Some(0), "{}", stderr_of(&created));
    assert_eq!(
        stderr_of(&created),
        format!(
            "cli-git: copied 4 ignored filesystem entries from \"{}\" into 1 new worktree.\n",
            one.display()
        )
    );
    let two: PathBuf = fixture.root.join("two");
    assert!(two.join(RULES_FILE).exists());
    assert_eq!(
        std::fs::read(two.join("heavy/data")).expect("heavy copied"),
        b"payload\n"
    );
    assert_scanner_has_rules(&check_all(&fixture, two.as_path()));
    remove(&fixture);
}

/// A main checkout without a rules file copies nothing, and the scanner in the new worktree fails closed.
#[test]
fn a_source_without_the_rules_file_copies_nothing_and_the_scanner_fails_closed() {
    let fixture: Fixture = fixture("copy-absent");
    let repo: PathBuf = repository_with_rules(&fixture, false);
    let created: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--quiet", "-b", "one", "../one"],
    );
    assert_eq!(created.code, Some(0), "{}", stderr_of(&created));
    let one: PathBuf = fixture.root.join("one");
    assert!(!one.join(".cache").exists());
    assert_scanner_failed_closed(&check_all(&fixture, one.as_path()), one.as_path());
    remove(&fixture);
}

/// A main checkout whose configuration names no rules file stays a bypass: nothing is copied and nothing is printed.
#[test]
fn a_main_checkout_without_a_configured_rules_file_is_forwarded_plainly() {
    let fixture: Fixture = fixture("copy-unconfigured");
    let repo: PathBuf = repository(&fixture, OsStr::new("repo"));
    std::fs::write(repo.join(".gitignore"), b".cache/\n").expect("gitignore");
    std::fs::create_dir_all(repo.join(".cache")).expect("cache");
    std::fs::write(repo.join(RULES_FILE), b"rules\n").expect("rules");
    let created: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--quiet", "-b", "one", "../one"],
    );
    assert_eq!(created.code, Some(0), "{}", stderr_of(&created));
    assert!(created.stderr.is_empty(), "{}", stderr_of(&created));
    assert!(!fixture.root.join("one/.cache").exists());
    remove(&fixture);
}

/// A command Git expands from an alias into `worktree add` is copied too, and a failed Git keeps its own status.
#[test]
fn an_aliased_worktree_add_is_copied_and_git_failures_keep_their_status() {
    let fixture: Fixture = fixture("copy-alias");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    let aliased: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["-c", "alias.wta=worktree add", "wta", "--quiet", "-b", "one", "../one"],
    );
    assert_eq!(aliased.code, Some(0), "{}", stderr_of(&aliased));
    assert!(fixture.root.join("one").join(RULES_FILE).exists());
    // Git refuses a branch that exists: its status is kept and nothing is copied or summarized.
    let refused: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--quiet", "-b", "one", "../again"],
    );
    assert_eq!(refused.code, Some(255), "{}", stderr_of(&refused));
    assert!(!stderr_of(&refused).contains("cli-git: copied"));
    assert!(!fixture.root.join("again").exists());
    remove(&fixture);
}

/// A copy interrupted before it finished is recovered by the next command run from a linked worktree:
/// the staged files are installed, the recovery is reported, and the scanner then finds its rules.
#[test]
fn an_interrupted_copy_is_recovered_by_the_next_command() {
    let fixture: Fixture = fixture("copy-recovery");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    let opted_out: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--no-worktree-copy", "--quiet", "-b", "one", "../one"],
    );
    assert_eq!(opted_out.code, Some(0));
    let one: PathBuf = std::fs::canonicalize(fixture.root.join("one")).expect("canonical worktree");
    assert!(!one.join(RULES_FILE).exists());
    // The state an interrupted copy leaves: a staged journal for the registered worktree, nothing installed.
    let common: PathBuf = std::fs::canonicalize(repo.join(".git")).expect("common directory");
    let context: GitContext = GitContext {
        real_git: PathBuf::from(super::support::REAL_GIT),
        overlay: Vec::new(),
        global_prefix: Vec::new(),
    };
    let snapshot: StagedSnapshot = stage_ignored_snapshot(
        &context,
        std::fs::canonicalize(repo.as_path()).expect("canonical source").as_path(),
        one.as_path(),
        &[],
        &CopySelection::Paths(vec![String::from(RULES_FILE)]),
    )
    .expect("staged");
    create_journal(common.as_path(), one.as_path(), &snapshot).expect("journal");
    assert_eq!(read_pending_journals(common.as_path()).expect("pending").len(), 1);
    // A read-only command from the linked worktree recovers it before Git runs.
    let status: Observed = run_wrapped(&fixture, one.as_path(), &["rev-parse", "--is-inside-work-tree"]);
    assert_eq!(status.code, Some(0), "{}", stderr_of(&status));
    assert_eq!(status.stdout, b"true\n");
    assert_eq!(
        stderr_of(&status),
        "cli-git: recovered ignored-state copies for 1 worktree transaction.\n"
    );
    assert!(read_pending_journals(common.as_path()).expect("pending").is_empty());
    assert!(one.join(RULES_FILE).exists());
    assert_scanner_has_rules(&check_all(&fixture, one.as_path()));
    // Recovery is not repeated.
    let again: Observed = run_wrapped(&fixture, one.as_path(), &["rev-parse", "--is-inside-work-tree"]);
    assert!(again.stderr.is_empty(), "{}", stderr_of(&again));
    remove(&fixture);
}

/// A nested Git command that inherits a live settlement lease is forwarded without settling again,
/// while a token that does not belong to the lock is not trusted.
#[test]
fn a_nested_command_with_a_foreign_lease_still_runs() {
    let fixture: Fixture = fixture("copy-lease");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    let created: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--quiet", "-b", "one", "../one"],
    );
    assert_eq!(created.code, Some(0));
    let one: PathBuf = fixture.root.join("one");
    let observed: Observed = observe(
        wrapped(&fixture)
            .current_dir(&one)
            .env("CLI_GIT_WORKTREE_COPY_LEASE", "not-the-lock-token")
            .args(["rev-parse", "--is-inside-work-tree"]),
        b"",
    );
    assert_eq!(observed.code, Some(0), "{}", stderr_of(&observed));
    assert_eq!(observed.stdout, b"true\n");
    remove(&fixture);
}

/// Git runs a hook of the new worktree while the settlement lock is held;
///  a worktree that hook creates through the wrapper inherits the lease instead of waiting for the lock it is inside.
#[test]
fn a_hook_that_creates_a_worktree_runs_under_the_inherited_lease() {
    let fixture: Fixture = fixture("copy-hook-lease");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    let hook: PathBuf = repo.join(".git/hooks/post-checkout");
    super::support::executable(
        hook.as_path(),
        b"#!/bin/sh\nif [ -n \"$CLI_GIT_WORKTREE_COPY_LEASE\" ] && [ ! -e ../nested ]; then\n  printf 'leased' > ../hook-saw-lease\n  \"$CLI_GIT_TEST_WRAPPER\" worktree add --quiet -b nested ../nested || exit 1\nfi\n",
    );
    // Git puts its own directory first on a hook's PATH, so the hook names the wrapper explicitly.
    let created: Observed = observe(
        wrapped(&fixture)
            .current_dir(&repo)
            .env("CLI_GIT_TEST_WRAPPER", fixture.root.join("bin/git"))
            .args(["worktree", "add", "--quiet", "-b", "one", "../one"]),
        b"",
    );
    assert_eq!(created.code, Some(0), "{}", stderr_of(&created));
    assert_eq!(
        std::fs::read(fixture.root.join("hook-saw-lease")).expect("the hook saw the lease"),
        b"leased"
    );
    assert!(fixture.root.join("nested/.git").exists(), "the nested creation ran");
    assert!(fixture.root.join("one").join(RULES_FILE).exists(), "the outer copy finished");
    // The outer settlement sees both registrations, so it copies into both worktrees.
    assert!(fixture.root.join("nested").join(RULES_FILE).exists());
    assert!(stderr_of(&created).ends_with(" into 2 new worktrees.\n"), "{}", stderr_of(&created));
    remove(&fixture);
}

/// A copy interrupted from the main checkout is recovered by the next worktree creation from the main checkout,
/// before Git creates the next worktree.
#[test]
fn an_interrupted_main_checkout_copy_is_recovered_by_the_next_creation() {
    let fixture: Fixture = fixture("copy-recovery-main");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    assert_eq!(
        run_wrapped(
            &fixture,
            repo.as_path(),
            &["worktree", "add", "--no-worktree-copy", "--quiet", "-b", "one", "../one"],
        )
        .code,
        Some(0)
    );
    let one: PathBuf = std::fs::canonicalize(fixture.root.join("one")).expect("canonical worktree");
    let common: PathBuf = std::fs::canonicalize(repo.join(".git")).expect("common directory");
    let context: GitContext = GitContext {
        real_git: PathBuf::from(super::support::REAL_GIT),
        overlay: Vec::new(),
        global_prefix: Vec::new(),
    };
    let snapshot: StagedSnapshot = stage_ignored_snapshot(
        &context,
        std::fs::canonicalize(repo.as_path()).expect("canonical source").as_path(),
        one.as_path(),
        &[],
        &CopySelection::Paths(vec![String::from(RULES_FILE)]),
    )
    .expect("staged");
    create_journal(common.as_path(), one.as_path(), &snapshot).expect("journal");
    let next: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--quiet", "-b", "two", "../two"],
    );
    assert_eq!(next.code, Some(0), "{}", stderr_of(&next));
    assert!(
        stderr_of(&next).starts_with("cli-git: recovered ignored-state copies for 1 worktree transaction.\n"),
        "{}",
        stderr_of(&next)
    );
    assert!(one.join(RULES_FILE).exists());
    assert!(fixture.root.join("two").join(RULES_FILE).exists());
    assert!(read_pending_journals(common.as_path()).expect("pending").is_empty());
    remove(&fixture);
}

/// Moving a worktree registers nothing new, so it succeeds from either kind of source and prints no summary.
#[test]
fn moving_a_worktree_is_forwarded_and_prints_no_summary() {
    let fixture: Fixture = fixture("copy-move");
    let repo: PathBuf = repository_with_rules(&fixture, true);
    assert_eq!(
        run_wrapped(
            &fixture,
            repo.as_path(),
            &["worktree", "add", "--quiet", "-b", "one", "../one"],
        )
        .code,
        Some(0)
    );
    let moved: Observed = run_wrapped(&fixture, repo.as_path(), &["worktree", "move", "../one", "../moved"]);
    assert_eq!(moved.code, Some(0), "{}", stderr_of(&moved));
    assert!(moved.stderr.is_empty(), "{}", stderr_of(&moved));
    assert!(fixture.root.join("moved").join(RULES_FILE).exists());
    let moved_again: Observed = run_wrapped(
        &fixture,
        fixture.root.join("moved").as_path(),
        &["worktree", "move", "../moved", "../final"],
    );
    assert_eq!(moved_again.code, Some(0), "{}", stderr_of(&moved_again));
    assert!(moved_again.stderr.is_empty(), "{}", stderr_of(&moved_again));
    remove(&fixture);
}

/// A copy that fails after Git created the worktree ends with status 2 and the copy's own message,
///  leaves the created worktree in place, and leaves no lock or journal behind.
#[test]
fn a_failed_copy_ends_with_status_two_and_its_message() {
    let fixture: Fixture = fixture("copy-failure");
    let repo: PathBuf = repository_with_rules(&fixture, false);
    // A file the caller cannot read cannot be copied, and Git still lists it as an ignored file of the checkout.
    std::fs::create_dir_all(repo.join(".cache")).expect("cache directory");
    std::fs::write(repo.join(RULES_FILE), b"rules\n").expect("rules");
    std::fs::set_permissions(repo.join(RULES_FILE), std::fs::Permissions::from_mode(0o000))
        .expect("unreadable");
    let created: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--quiet", "-b", "one", "../one"],
    );
    assert_eq!(created.code, Some(2), "{}", stderr_of(&created));
    assert!(
        stderr_of(&created).starts_with("cli-git: could not stage ignored state from "),
        "{}",
        stderr_of(&created)
    );
    assert!(!stderr_of(&created).contains("cli-git: copied"), "{}", stderr_of(&created));
    assert!(fixture.root.join("one/.git").exists(), "Git created the worktree");
    assert!(!fixture.root.join("one").join(RULES_FILE).exists());
    let common: PathBuf = std::fs::canonicalize(repo.join(".git")).expect("common directory");
    assert!(read_pending_journals(common.as_path()).expect("pending").is_empty());
    std::fs::set_permissions(repo.join(RULES_FILE), std::fs::Permissions::from_mode(0o600))
        .expect("readable again");
    // The failed copy released the lock, so the next creation is not held up.
    let next: Observed = run_wrapped(
        &fixture,
        repo.as_path(),
        &["worktree", "add", "--no-worktree-copy", "--quiet", "-b", "two", "../two"],
    );
    assert_eq!(next.code, Some(0), "{}", stderr_of(&next));
    remove(&fixture);
}