monochromatic-git-policy-cli 0.5.31

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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Lifecycle checks against real Git: applicability, bypasses, settlement, the summary line and exit statuses.

use super::*;
use crate::test_support::{fixture, git, git_context, git_output, remove, repository};
use crate::worktree_copy_journal::{create_journal_for, journal_root, read_pending_journals};
use crate::test_support::dead_copy_owner;
use std::process::Output;

/// A repository with an ignored `cache` directory, returned with its canonical common directory.
fn source(root: &Path) -> (PathBuf, PathBuf) {
    let main: PathBuf = repository(root, "main");
    std::fs::write(main.join(".gitignore"), b"cache/\n").expect("gitignore");
    std::fs::create_dir_all(main.join("cache")).expect("cache");
    std::fs::write(main.join("cache/data"), b"payload").expect("data");
    let common: PathBuf = std::fs::canonicalize(main.join(".git")).expect("common");
    return (std::fs::canonicalize(main).expect("canonical main"), common);
}

/// Runs real Git with the given arguments in a directory and reports its status.
fn run_real(directory: &Path, arguments: &[String]) -> GitStatus {
    let output: Output = git_output(directory, arguments);
    return GitStatus {
        code: output.status.code(),
    };
}

/// The identity of the main worktree of a source.
fn main_identity(main: &Path, common: &Path) -> WorktreeIdentity {
    return WorktreeIdentity::MainWorktree {
        common_dir: common.to_path_buf(),
        git_dir: common.to_path_buf(),
        worktree_root: main.to_path_buf(),
    };
}

/// The summary line names the count, the source, and the destinations, with the incumbent's nouns.
#[test]
fn summary_line_uses_exact_nouns() {
    assert_eq!(
        summary_line(1, 1, Some(Path::new("/repo"))),
        "cli-git: copied 1 ignored filesystem entry from \"/repo\" into 1 new worktree.\n"
    );
    assert_eq!(
        summary_line(3, 2, None),
        "cli-git: copied 3 ignored filesystem entries from bare repository with an empty source set into 2 new worktrees.\n"
    );
}

/// The exit status follows the incumbent: a copy-only failure is 2, Git's own status is kept, and a signal is 1.
#[test]
fn exit_status_follows_the_settlement_table() {
    let ok: GitStatus = GitStatus { code: Some(0) };
    let failed: GitStatus = GitStatus { code: Some(5) };
    let signal: GitStatus = GitStatus { code: None };
    assert_eq!(
        exit_code(&Settlement {
            git: ok,
            copy_failure: None
        }),
        0
    );
    assert_eq!(
        exit_code(&Settlement {
            git: failed,
            copy_failure: None
        }),
        5
    );
    assert_eq!(
        exit_code(&Settlement {
            git: signal,
            copy_failure: None
        }),
        1
    );
    let copy_failure: WorktreeCopyError = WorktreeCopyError(String::from("copy failed"));
    assert_eq!(
        exit_code(&Settlement {
            git: ok,
            copy_failure: Some(copy_failure.clone())
        }),
        2
    );
    assert_eq!(
        exit_code(&Settlement {
            git: failed,
            copy_failure: Some(copy_failure.clone())
        }),
        5
    );
    assert_eq!(
        exit_code(&Settlement {
            git: signal,
            copy_failure: Some(copy_failure)
        }),
        1
    );
}

/// A worktree added from a linked worktree gets its ignored files and the summary line, and takes no lock.
#[cfg(unix)]
#[test]
fn added_worktree_receives_ignored_state_without_a_lock() {
    let root: PathBuf = fixture("lifecycle-add");
    let (main, common) = source(root.as_path());
    std::fs::write(common.join("info/exclude"), b"cache/\n").expect("exclude");
    let source_root: PathBuf = root.join("source");
    git(
        main.as_path(),
        &[
            "worktree",
            "add",
            "--quiet",
            "-b",
            "source",
            source_root.to_str().expect("utf8"),
        ],
    );
    let source_canonical: PathBuf = std::fs::canonicalize(source_root).expect("canonical source");
    std::fs::create_dir_all(source_canonical.join("cache")).expect("cache");
    std::fs::write(source_canonical.join("cache/data"), b"payload").expect("data");
    let topic: PathBuf = root.join("topic");
    let identity: WorktreeIdentity = WorktreeIdentity::LinkedWorktree {
        common_dir: common.clone(),
        git_dir: common.join("worktrees").join("source"),
        worktree_root: source_canonical.clone(),
    };
    let mut stderr: Vec<u8> = Vec::new();
    let args: Vec<String> = vec![
        String::from("worktree"),
        String::from("add"),
        String::from("--quiet"),
        String::from("-b"),
        String::from("topic"),
        topic.to_string_lossy().into_owned(),
    ];
    let settlement: Settlement = run_with_worktree_copy(
        &Invocation {
            identity: identity.clone(),
            effective_cwd: source_canonical.clone(),
            creates: true,
            destination: Some(topic.clone()),
            opted_out: false,
            short_circuit: false,
            context: git_context(),
            add_plan: None,
            repair: crate::package_repair::RepairSwitch::default(),
        },
        &mut |_step| return Ok(run_real(source_canonical.as_path(), args.as_slice())),
        &mut stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 0);
    assert_eq!(
        std::fs::read(topic.join("cache/data")).expect("copied"),
        b"payload"
    );
    assert_eq!(
        String::from_utf8(stderr).expect("utf8"),
        format!(
            "cli-git: copied 2 ignored filesystem entries from {} into 1 new worktree.\n",
            quote(source_canonical.to_string_lossy().as_ref())
        )
    );
    assert!(!journal_root(common.as_path()).join("settlement.lock").exists(), "no lock is ever taken");
    assert!(
        read_pending_journals(common.as_path())
            .expect("pending")
            .is_empty()
    );
    remove(root.as_path());
}

/// The opt-out flag skips synchronization and recovery, and the flag never reaches the runner.
#[cfg(unix)]
#[test]
fn opted_out_invocation_skips_synchronization() {
    let root: PathBuf = fixture("lifecycle-opt-out");
    let (main, common) = source(root.as_path());
    let topic: PathBuf = root.join("topic");
    let identity: WorktreeIdentity = main_identity(main.as_path(), common.as_path());
    let mut runs: usize = 0;
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &Invocation {
            identity: identity.clone(),
            effective_cwd: main.clone(),
            creates: true,
            destination: Some(topic.clone()),
            opted_out: true,
            short_circuit: false,
            context: git_context(),
            add_plan: None,
            repair: crate::package_repair::RepairSwitch::default(),
        },
        &mut |_step| {
            runs += 1;
            return Ok(run_real(
                main.as_path(),
                &[
                    String::from("worktree"),
                    String::from("add"),
                    String::from("--quiet"),
                    String::from("-b"),
                    String::from("topic"),
                    topic.to_string_lossy().into_owned(),
                ],
            ));
        },
        &mut stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 0);
    assert_eq!(runs, 1, "Git runs once and untouched");
    assert!(!topic.join("cache").exists());
    assert!(stderr.is_empty());
    remove(root.as_path());
}

/// A main worktree command that creates no worktree runs without any settlement.
#[test]
fn main_worktree_command_that_creates_nothing_is_not_settled() {
    let root: PathBuf = fixture("lifecycle-main");
    let (main, common) = source(root.as_path());
    let identity: WorktreeIdentity = main_identity(main.as_path(), common.as_path());
    let mut runs: usize = 0;
    let settlement: Settlement = run_with_worktree_copy(
        &Invocation {
            identity: identity.clone(),
            effective_cwd: main.clone(),
            creates: false,
            destination: None,
            opted_out: false,
            short_circuit: false,
            context: git_context(),
            add_plan: None,
            repair: crate::package_repair::RepairSwitch::default(),
        },
        &mut |_step| {
            runs += 1;
            return Ok(GitStatus { code: Some(7) });
        },
        &mut Vec::new(),
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 7);
    assert_eq!(runs, 1);
    assert!(!journal_root(common.as_path()).exists(), "nothing is created in the repository");
    remove(root.as_path());
}

/// A command that creates no worktrees runs once, untouched, and creates nothing in the repository.
#[cfg(unix)]
#[test]
fn non_worktree_command_runs_without_settlement() {
    let root: PathBuf = fixture("lifecycle-status");
    let (main, common) = source(root.as_path());
    let identity: WorktreeIdentity = WorktreeIdentity::LinkedWorktree {
        common_dir: common.clone(),
        git_dir: common.clone(),
        worktree_root: main.clone(),
    };
    let mut runs: usize = 0;
    let settlement: Settlement = run_with_worktree_copy(
        &Invocation {
            identity: identity.clone(),
            effective_cwd: main.clone(),
            creates: false,
            destination: None,
            opted_out: false,
            short_circuit: false,
            context: git_context(),
            add_plan: None,
            repair: crate::package_repair::RepairSwitch::default(),
        },
        &mut |_step| {
            runs += 1;
            return Ok(GitStatus { code: Some(0) });
        },
        &mut Vec::new(),
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 0);
    assert_eq!(runs, 1);
    assert!(!journal_root(common.as_path()).exists(), "nothing is created in the repository");
    remove(root.as_path());
}

/// A staged journal of a registered worktree, written for `owner`, as a copy that stopped after staging leaves it.
#[cfg(unix)]
fn journal_for(main: &Path, common: &Path, destination: &Path, owner: &crate::worktree_copy_owner::CopyOwner) -> crate::worktree_copy_journal::PendingJournal {
    use crate::worktree_copy_selection::CopySelection;
    use crate::worktree_copy_stage::{new_stage_container, stage_ignored_snapshot};
    let snapshot = stage_ignored_snapshot(
        &git_context(),
        main,
        new_stage_container(destination).expect("container").as_path(),
        &[],
        &CopySelection::MainCheckout,
    )
    .expect("staged");
    return create_journal_for(common, destination, &snapshot, owner).expect("journal");
}

/// The invocation of a creating `git worktree add` from the main checkout, naming `destination`.
fn creation_from(main: &Path, common: &Path, destination: Option<&Path>) -> Invocation {
    return Invocation {
        identity: main_identity(main, common),
        effective_cwd: main.to_path_buf(),
        creates: true,
        destination: destination.map(Path::to_path_buf),
        opted_out: false,
        short_circuit: false,
        context: git_context(),
        add_plan: None,
        repair: crate::package_repair::RepairSwitch::default(),
    };
}

/// The arguments of a quiet `git worktree add -b <name> <destination>`.
fn add_arguments(name: &str, destination: &Path) -> Vec<String> {
    return ["worktree", "add", "--quiet", "-b", name].iter().map(|word| { return (*word).to_string(); }).chain([destination.to_string_lossy().into_owned()]).collect();
}

/// Another command registers a worktree while this one's Git runs: only the destination this command named is copied into,
/// so two concurrent adds each get exactly their own copy.
#[cfg(unix)]
#[test]
fn a_creation_copies_only_into_the_destination_it_named() {
    let root: PathBuf = fixture("lifecycle-own-destination");
    let (main, common) = source(root.as_path());
    let topic: PathBuf = root.join("topic");
    let other: PathBuf = root.join("other");
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), Some(topic.as_path())),
        &mut |_step| {
            // The other command's registration lands inside this command's Git run, as a concurrent add's would.
            run_real(main.as_path(), add_arguments("other", other.as_path()).as_slice());
            return Ok(run_real(main.as_path(), add_arguments("topic", topic.as_path()).as_slice()));
        },
        &mut stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 0, "{:?}", settlement.copy_failure);
    assert_eq!(std::fs::read(topic.join("cache/data")).expect("own copy"), b"payload");
    assert!(!other.join("cache").exists(), "another command's worktree is not this command's to fill");
    assert!(String::from_utf8(stderr).expect("utf8").ends_with(" into 1 new worktree.\n"));
    assert_nothing_left(root.as_path(), common.as_path());
    remove(root.as_path());
}

/// A journal whose owner still runs belongs to a copy in progress: a creation neither waits for it nor recovers it.
#[cfg(unix)]
#[test]
fn a_creation_leaves_a_live_journal_alone_and_does_not_wait() {
    let root: PathBuf = fixture("lifecycle-live-journal");
    let (main, common) = source(root.as_path());
    let older: PathBuf = std::fs::canonicalize(root.as_path()).expect("canonical root").join("older");
    git(main.as_path(), &["worktree", "add", "--quiet", "-b", "older", older.to_str().expect("utf8")]);
    let live = journal_for(main.as_path(), common.as_path(), older.as_path(), &crate::worktree_copy_owner::current_copy_owner().expect("owner"));
    let topic: PathBuf = root.join("topic");
    let mut stderr: Vec<u8> = Vec::new();
    let started: std::time::Instant = std::time::Instant::now();
    let settlement: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), Some(topic.as_path())),
        &mut |_step| return Ok(run_real(main.as_path(), add_arguments("topic", topic.as_path()).as_slice())),
        &mut stderr,
    )
    .expect("settled");
    assert!(started.elapsed() < std::time::Duration::from_secs(30), "the creation must not wait for the live copy");
    assert_eq!(exit_code(&settlement), 0, "{:?}", settlement.copy_failure);
    assert!(live.path.exists(), "the live journal is whole");
    assert!(Path::new(live.journal.stage_container.as_str()).exists(), "its stage is whole");
    assert!(!older.join("cache").exists(), "its copy is not installed by anyone else");
    assert!(!String::from_utf8(stderr).expect("utf8").contains("recovered"));
    remove(root.as_path());
}

/// A journal whose owner is dead is recovered before the creation runs Git, and reported.
#[cfg(unix)]
#[test]
fn a_creation_recovers_a_dead_journal_first() {
    let root: PathBuf = fixture("lifecycle-dead-journal");
    let (main, common) = source(root.as_path());
    let older: PathBuf = std::fs::canonicalize(root.as_path()).expect("canonical root").join("older");
    git(main.as_path(), &["worktree", "add", "--quiet", "-b", "older", older.to_str().expect("utf8")]);
    let dead = journal_for(main.as_path(), common.as_path(), older.as_path(), &dead_copy_owner());
    let topic: PathBuf = root.join("topic");
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), Some(topic.as_path())),
        &mut |_step| return Ok(run_real(main.as_path(), add_arguments("topic", topic.as_path()).as_slice())),
        &mut stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 0, "{:?}", settlement.copy_failure);
    let text: String = String::from_utf8(stderr).expect("utf8");
    assert!(text.starts_with("cli-git: recovered ignored-state copies for 1 worktree transaction.\n"), "{text}");
    assert!(!dead.path.exists());
    assert_eq!(std::fs::read(older.join("cache/data")).expect("recovered copy"), b"payload");
    assert_eq!(std::fs::read(topic.join("cache/data")).expect("own copy"), b"payload");
    remove(root.as_path());
}

/// A creation whose command line names no readable destination copies nothing and says so once Git succeeded.
#[cfg(unix)]
#[test]
fn a_creation_with_an_unread_destination_copies_nothing_and_says_so() {
    let root: PathBuf = fixture("lifecycle-unread-destination");
    let (main, common) = source(root.as_path());
    let topic: PathBuf = root.join("topic");
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), None),
        &mut |_step| return Ok(run_real(main.as_path(), add_arguments("topic", topic.as_path()).as_slice())),
        &mut stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 0);
    assert_eq!(String::from_utf8(stderr).expect("utf8"), UNKNOWN_DESTINATION_NOTICE);
    assert!(!topic.join("cache").exists());
    // Git's own failure is its own message: the notice is only for a worktree that was made.
    let mut failed_stderr: Vec<u8> = Vec::new();
    let failed: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), None),
        &mut |_step| return Ok(GitStatus { code: Some(128) }),
        &mut failed_stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&failed), 128);
    assert!(failed_stderr.is_empty());
    remove(root.as_path());
}

/// A destination that was a registered worktree before Git ran is not copied into again when Git refuses the add.
#[cfg(unix)]
#[test]
fn a_destination_registered_before_is_not_copied_into_again() {
    let root: PathBuf = fixture("lifecycle-registered-before");
    let (main, common) = source(root.as_path());
    let topic: PathBuf = std::fs::canonicalize(root.as_path()).expect("canonical root").join("topic");
    git(main.as_path(), &["worktree", "add", "--quiet", "-b", "topic", topic.to_str().expect("utf8")]);
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), Some(topic.as_path())),
        &mut |_step| return Ok(run_real(main.as_path(), add_arguments("again", topic.as_path()).as_slice())),
        &mut stderr,
    )
    .expect("settled");
    assert_ne!(exit_code(&settlement), 0, "Git refuses a destination that exists");
    assert!(stderr.is_empty(), "{}", String::from_utf8_lossy(stderr.as_slice()));
    assert!(!topic.join("cache").exists());
    remove(root.as_path());
}

/// Git failing after it registered the worktree keeps Git's status and still copies the ignored state.
#[cfg(unix)]
#[test]
fn a_creation_that_failed_after_registering_still_copies() {
    let root: PathBuf = fixture("lifecycle-failed-after-registering");
    let (main, common) = source(root.as_path());
    let topic: PathBuf = std::fs::canonicalize(root.as_path()).expect("canonical root").join("topic");
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), Some(topic.as_path())),
        &mut |_step| {
            run_real(main.as_path(), add_arguments("topic", topic.as_path()).as_slice());
            return Ok(GitStatus { code: Some(1) });
        },
        &mut stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 1);
    assert_eq!(std::fs::read(topic.join("cache/data")).expect("copied anyway"), b"payload");
    remove(root.as_path());
}

/// The repository path of the generated rules file that sits in the repository root's own `.cache`.
const RULES_PATH: &str = ".cache/forbidden-strings.rules.txt";

/// A main checkout whose ignored state has the shapes the rule must tell apart, returned with its canonical root and common
/// directory: installed dependencies with a pnpm-style link and a hard-linked store file, build output with a cache inside it,
/// caches, run records, a local file, a separate checkout and a registered worktree nested in an ignored directory.
#[cfg(unix)]
fn main_with_state(root: &Path) -> (PathBuf, PathBuf) {
    let main: PathBuf = repository(root, "main");
    std::fs::write(
        main.join(".gitignore"),
        b".cache/\nnode_modules/\ntarget/\n.pnpm-store/\n.claude/\nmutants.out/\nclone/\n*.local.*\n",
    )
    .expect("gitignore");
    let write = |path: &str, bytes: &[u8]| {
        let full: PathBuf = main.join(path);
        std::fs::create_dir_all(full.parent().expect("parent")).expect("directories");
        std::fs::write(full, bytes).expect("file");
    };
    write(RULES_PATH, b"rules");
    write(".cache/test-scratch/keep.txt", b"scratch");
    write("node_modules/.pnpm/dep@1.0.0/node_modules/dep/index.js", b"module");
    write("node_modules/.pnpm/dep@1.0.0/node_modules/dep/data.bin", b"data");
    write("node_modules/.pnpm-store/v11/files/ab", b"stored");
    write("node_modules/.cache/jiti/x.js", b"jiti");
    write("node_modules/.monochromatic/2026-11-01/log.jsonl", b"log");
    write("package/a/node_modules/.monochromatic/log.jsonl", b"log");
    write("package/a/node_modules/.bin/tool", b"#!/bin/sh\n");
    write("package/a/.cache/typescript/tsbuildinfo", b"cache");
    write("target/debug/deps/libx.rlib", b"rlib");
    write("target/debug/.fingerprint/x/fp", b"fp");
    write("target/debug/incremental/x/state", b"incremental");
    write("target/verification/run/out.txt", b"record");
    write("mutants.out/outcomes.json", b"{}");
    write(".pnpm-store/v11/files/cd", b"store");
    write("mise.local.toml", b"[env]\n");
    write("clone/.git/HEAD", b"ref: refs/heads/main\n");
    write("clone/file.txt", b"another checkout");
    // The pnpm layout: a package link to the virtual store, relative, and a store file with a second hard link.
    std::os::unix::fs::symlink(
        ".pnpm/dep@1.0.0/node_modules/dep",
        main.join("node_modules/dep"),
    )
    .expect("link");
    std::os::unix::fs::symlink(
        "../../../node_modules/.pnpm/dep@1.0.0/node_modules/dep",
        main.join("package/a/node_modules/dep"),
    )
    .expect("package link");
    std::fs::hard_link(
        main.join("node_modules/.pnpm/dep@1.0.0/node_modules/dep/data.bin"),
        main.join("target/debug/deps/data.bin"),
    )
    .expect("hard link");
    git(main.as_path(), &["add", ".gitignore"]);
    git(main.as_path(), &["commit", "--message", "ignore"]);
    git(main.as_path(), &["worktree", "add", "--quiet", "-b", "nested", ".claude/worktrees/nested"]);
    write(".claude/worktrees/nested/inside.txt", b"nested worktree file");
    let common: PathBuf = std::fs::canonicalize(main.join(".git")).expect("common");
    return (std::fs::canonicalize(main).expect("canonical main"), common);
}

/// Runs `git worktree add` for a new `topic` worktree from the main checkout through the lifecycle.
fn add_from_main(root: &Path, main: &Path, common: &Path, flags: (bool, bool)) -> (Settlement, String, PathBuf) {
    let topic: PathBuf = root.join("topic");
    let identity: WorktreeIdentity = main_identity(main, common);
    let args: Vec<String> = vec![
        String::from("worktree"),
        String::from("add"),
        String::from("--quiet"),
        String::from("-b"),
        String::from("topic"),
        topic.to_string_lossy().into_owned(),
    ];
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &Invocation {
            identity: identity.clone(),
            effective_cwd: main.to_path_buf(),
            creates: flags.0,
            destination: Some(topic.clone()),
            opted_out: flags.1,
            short_circuit: false,
            context: git_context(),
            add_plan: None,
            repair: crate::package_repair::RepairSwitch::default(),
        },
        &mut |_step| return Ok(run_real(main, args.as_slice())),
        &mut stderr,
    )
    .expect("settled");
    return (settlement, String::from_utf8(stderr).expect("utf8"), topic);
}

/// Nothing of the copy stays behind: no lock file, no pending journal and no private stage beside the worktree.
fn assert_nothing_left(root: &Path, common: &Path) {
    assert!(!journal_root(common).join("settlement.lock").exists(), "no lock is ever taken");
    assert!(read_pending_journals(common).expect("pending").is_empty());
    let leftovers: Vec<String> = std::fs::read_dir(root)
        .expect("listing")
        .map(|entry| return entry.expect("entry").file_name().to_string_lossy().into_owned())
        .filter(|name| return name.starts_with(crate::worktree_copy_selection::STAGE_PREFIX))
        .collect();
    assert!(leftovers.is_empty(), "{leftovers:?}");
}

/// A worktree added from the main checkout receives the dependencies and builds it needs, links and all.
#[cfg(unix)]
#[test]
fn main_checkout_source_carries_dependencies_and_builds() {
    let root: PathBuf = fixture("lifecycle-main-carries");
    let (main, common) = main_with_state(root.as_path());
    let (settlement, stderr, topic) = add_from_main(root.as_path(), main.as_path(), common.as_path(), (true, false));
    assert_eq!(exit_code(&settlement), 0, "{stderr} {:?}", settlement.copy_failure);
    assert!(settlement.copy_failure.is_none());
    let dependency: &str = "node_modules/.pnpm/dep@1.0.0/node_modules/dep/index.js";
    assert_eq!(std::fs::read(topic.join(dependency)).expect("root dependency"), b"module");
    assert_eq!(
        std::fs::read_link(topic.join("node_modules/dep")).expect("root link"),
        PathBuf::from(".pnpm/dep@1.0.0/node_modules/dep"),
        "a link into the virtual store stays a link with the same target"
    );
    assert_eq!(
        std::fs::read_link(topic.join("package/a/node_modules/dep")).expect("package link"),
        PathBuf::from("../../../node_modules/.pnpm/dep@1.0.0/node_modules/dep"),
    );
    assert_eq!(std::fs::read(topic.join("package/a/node_modules/dep/index.js")).expect("through the link"), b"module");
    assert!(topic.join("package/a/node_modules/.bin/tool").is_file());
    assert_eq!(std::fs::read(topic.join("target/debug/deps/libx.rlib")).expect("build"), b"rlib");
    assert!(topic.join("target/debug/.fingerprint/x/fp").is_file());
    assert_eq!(std::fs::read(topic.join("target/debug/deps/data.bin")).expect("hard linked file"), b"data");
    assert_eq!(std::fs::read(topic.join("node_modules/.pnpm/dep@1.0.0/node_modules/dep/data.bin")).expect("its twin"), b"data");
    assert_eq!(std::fs::read(topic.join(RULES_PATH)).expect("the root cache"), b"rules");
    assert!(topic.join(".cache/test-scratch/keep.txt").is_file());
    assert!(topic.join("mise.local.toml").is_file(), "local files are carried");
    assert!(
        stderr.starts_with("cli-git: copied ")
            && stderr.contains(quote(main.to_string_lossy().as_ref()).as_str())
            && stderr.ends_with(" into 1 new worktree.\n"),
        "{stderr}"
    );
    assert_nothing_left(root.as_path(), common.as_path());
    remove(root.as_path());
}

/// A worktree added from the main checkout never receives its nested worktrees, another checkout, caches or run records.
#[cfg(unix)]
#[test]
fn main_checkout_source_leaves_out_checkouts_caches_and_run_records() {
    let root: PathBuf = fixture("lifecycle-main-leaves-out");
    let (main, common) = main_with_state(root.as_path());
    let (settlement, stderr, topic) = add_from_main(root.as_path(), main.as_path(), common.as_path(), (true, false));
    assert_eq!(exit_code(&settlement), 0, "{stderr} {:?}", settlement.copy_failure);
    for absent in [
        ".claude/worktrees/nested/inside.txt",
        "clone",
        "node_modules/.pnpm-store",
        "node_modules/.cache",
        "node_modules/.monochromatic",
        "package/a/node_modules/.monochromatic",
        "package/a/.cache",
        "target/debug/incremental",
        "target/verification",
        "mutants.out",
        ".pnpm-store",
    ] {
        assert!(std::fs::symlink_metadata(topic.join(absent)).is_err(), "{absent} must not be carried");
    }
    assert_nothing_left(root.as_path(), common.as_path());
    remove(root.as_path());
}

/// A file the user cannot read is left out with one notice line, and the add still succeeds.
#[cfg(unix)]
#[test]
fn main_checkout_source_skips_unreadable_entries_with_a_notice() {
    use std::os::unix::fs::PermissionsExt;
    let root: PathBuf = fixture("lifecycle-main-unreadable");
    let (main, common) = main_with_state(root.as_path());
    let hidden: PathBuf = main.join("node_modules/.pnpm/dep@1.0.0/node_modules/dep/secret.bin");
    std::fs::write(hidden.as_path(), b"secret").expect("secret");
    std::fs::set_permissions(hidden.as_path(), std::fs::Permissions::from_mode(0o000)).expect("mode");
    // A process with the privilege to read anything cannot show the case, so the control proves the file is unreadable.
    if std::fs::File::open(hidden.as_path()).is_ok() {
        remove(root.as_path());
        return;
    }
    let (settlement, stderr, topic) = add_from_main(root.as_path(), main.as_path(), common.as_path(), (true, false));
    assert_eq!(exit_code(&settlement), 0, "{stderr} {:?}", settlement.copy_failure);
    assert!(settlement.copy_failure.is_none());
    assert!(!topic.join("node_modules/.pnpm/dep@1.0.0/node_modules/dep/secret.bin").exists());
    assert!(topic.join("node_modules/.pnpm/dep@1.0.0/node_modules/dep/index.js").is_file());
    assert!(stderr.contains("cli-git: left out 1 unreadable ignored entry from"), "{stderr}");
    assert!(stderr.contains("secret.bin"), "{stderr}");
    assert_eq!(stderr.matches("left out").count(), 1, "one notice line: {stderr}");
    assert_nothing_left(root.as_path(), common.as_path());
    std::fs::set_permissions(hidden.as_path(), std::fs::Permissions::from_mode(0o600)).expect("restore");
    remove(root.as_path());
}

/// The opt-out flag leaves the main checkout's ignored state behind and prints nothing.
#[cfg(unix)]
#[test]
fn main_checkout_source_honors_the_opt_out() {
    let root: PathBuf = fixture("lifecycle-main-opt-out");
    let (main, common) = main_with_state(root.as_path());
    let (settlement, stderr, topic) = add_from_main(root.as_path(), main.as_path(), common.as_path(), (true, true));
    assert_eq!(exit_code(&settlement), 0);
    assert!(stderr.is_empty(), "{stderr}");
    assert!(topic.exists(), "Git itself still created the worktree");
    assert!(!topic.join("node_modules").exists());
    assert!(!topic.join(RULES_PATH).exists());
    remove(root.as_path());
}

/// A command the caller reports as creating no worktree is never settled, even from a main checkout with ignored state.
#[cfg(unix)]
#[test]
fn main_checkout_source_ignores_commands_that_create_no_worktree() {
    let root: PathBuf = fixture("lifecycle-main-not-creating");
    let (main, common) = main_with_state(root.as_path());
    let (settlement, stderr, topic) = add_from_main(root.as_path(), main.as_path(), common.as_path(), (false, false));
    assert_eq!(exit_code(&settlement), 0);
    assert!(stderr.is_empty(), "{stderr}");
    assert!(topic.exists(), "the runner still created the worktree");
    assert!(!topic.join("node_modules").exists());
    remove(root.as_path());
}

/// A destination Git made whose registration does not name it back gets no copy, and the command says so instead of
/// succeeding in silence.
#[cfg(unix)]
#[test]
fn a_made_destination_whose_registration_cannot_be_confirmed_is_told() {
    let root: PathBuf = fixture("lifecycle-unconfirmed");
    let (main, common) = source(root.as_path());
    let topic: PathBuf = std::fs::canonicalize(root.as_path()).expect("canonical root").join("topic");
    let mut stderr: Vec<u8> = Vec::new();
    let settlement: Settlement = run_with_worktree_copy(
        &creation_from(main.as_path(), common.as_path(), Some(topic.as_path())),
        &mut |_step| {
            let status: GitStatus = run_real(main.as_path(), add_arguments("topic", topic.as_path()).as_slice());
            std::fs::write(common.join("worktrees/topic/gitdir"), b"/somewhere/else/.git\n").expect("registration edited");
            return Ok(status);
        },
        &mut stderr,
    )
    .expect("settled");
    assert_eq!(exit_code(&settlement), 0);
    let text: String = String::from_utf8(stderr).expect("utf8");
    assert!(text.contains("registration could not be confirmed"), "{text}");
    assert!(text.contains(topic.to_str().expect("utf8")), "{text}");
    assert!(!topic.join("cache").exists());
    remove(root.as_path());
}

/// The worktrees handed on for the package-manager repair are exactly the destination whose registration the binding
/// confirmed: never another add's worktree registered meanwhile, and none when the destination is unknown or unconfirmed.
#[cfg(unix)]
#[test]
fn the_repair_is_handed_exactly_the_confirmed_destination() {
    let root: PathBuf = fixture("lifecycle-repair-destination");
    let (main, common) = source(root.as_path());
    let canonical_root: PathBuf = std::fs::canonicalize(root.as_path()).expect("canonical root");
    let topic: PathBuf = canonical_root.join("topic");
    let other: PathBuf = canonical_root.join("other");
    let observation: WorktreeCopyObservation = observe_copy_source(&main_identity(main.as_path(), common.as_path()), main.as_path())
        .expect("observed")
        .expect("applicable");
    let (_first, after) = settle_creation(
        &creation_from(main.as_path(), common.as_path(), Some(topic.as_path())),
        &observation,
        &mut |_step| {
            run_real(main.as_path(), add_arguments("other", other.as_path()).as_slice());
            return Ok(run_real(main.as_path(), add_arguments("topic", topic.as_path()).as_slice()));
        },
        &mut Vec::new(),
    )
    .expect("settled");
    assert_eq!(after.created, vec![topic.clone()]);
    assert_eq!(after.source_root, Some(main.clone()));
    // An unknown destination hands nothing on, even though Git made a worktree.
    let unknown: PathBuf = canonical_root.join("unknown");
    let (_second, none) = settle_creation(
        &creation_from(main.as_path(), common.as_path(), None),
        &observation,
        &mut |_step| return Ok(run_real(main.as_path(), add_arguments("unknown", unknown.as_path()).as_slice())),
        &mut Vec::new(),
    )
    .expect("settled");
    assert!(none.created.is_empty());
    remove(root.as_path());
}