git-cow 0.6.0

Git worktrees that share files with a template through copy-on-write clones (APFS, XFS, btrfs)
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
//! `git cow cleanup` and `git cow tree`: find worktrees and branches whose work already landed

use std::collections::{HashMap, HashSet};
use std::io::{self, BufRead, IsTerminal, Write};
use std::os::unix::fs::DirBuilderExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

use git_cow::REPO_ENV;

use crate::{is_template, other};

// Ahead of every git call here: no fsmonitor or gpg program from a repository's own config, and
// no index refresh written back. `git status` can still run a clean filter, so only cleanup does
const QUIET: &[&str] = &[
    "-c",
    "core.fsmonitor=false",
    "-c",
    "log.showSignature=false",
    "--no-optional-locks",
];

// Files a rebase, merge, bisect, cherry-pick or revert in progress leaves in a worktree's gitdir
const BUSY: &[&str] = &[
    "rebase-merge",
    "rebase-apply",
    "MERGE_HEAD",
    "BISECT_LOG",
    "CHERRY_PICK_HEAD",
    "REVERT_HEAD",
    "sequencer",
];

// Reads attributes from the empty tree, so no merge driver or filter a repository names runs
const NO_ATTRIBUTES: &str = "--attr-source=4b825dc642cb6eb9a060e54bf8d69288fbee4904";

// Diffs compared byte for byte, without a diff program or textconv filter a repository may set
const DIFF: &[&str] = &[
    "--no-ext-diff",
    "--no-textconv",
    "--binary",
    "--full-index",
    "--no-color",
];

fn git_cmd(dir: &Path) -> Command {
    let mut cmd = Command::new("git");
    for var in REPO_ENV {
        cmd.env_remove(var);
    }
    // a partial clone would fetch a missing blob, through ssh or a credential helper
    cmd.env("GIT_NO_LAZY_FETCH", "1");
    cmd.arg("-C").arg(dir).args(QUIET);
    cmd
}

fn run(dir: &Path, args: &[&str]) -> io::Result<String> {
    let out = git_cmd(dir).args(args).stderr(Stdio::piped()).output()?;
    if !out.status.success() {
        return Err(other(format!(
            "git {args:?}: {}",
            String::from_utf8_lossy(&out.stderr).trim()
        )));
    }
    Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
}

// `git patch-id --verbatim` of what `args` prints, fed through a pipe so no byte is changed.
// Whitespace counts, so a later fix to indentation is never taken for the squash that landed
fn patch_ids(dir: &Path, args: &[&str], input: Option<String>) -> io::Result<String> {
    let mut producer = git_cmd(dir)
        .args(args)
        .stdin(if input.is_some() {
            Stdio::piped()
        } else {
            Stdio::null()
        })
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()?;
    let writer = match (input, producer.stdin.take()) {
        (Some(text), Some(mut stdin)) => {
            Some(std::thread::spawn(move || stdin.write_all(text.as_bytes())))
        }
        _ => None,
    };
    let diff = producer
        .stdout
        .take()
        .ok_or_else(|| other("git gave no output pipe".into()))?;
    let out = git_cmd(dir)
        .args(["patch-id", "--verbatim"])
        .stdin(diff)
        .stderr(Stdio::null())
        .output()?;
    if let Some(w) = writer {
        let _ = w.join();
    }
    if !producer.wait()?.success() || !out.status.success() {
        return Err(other(format!("git {args:?} | git patch-id failed")));
    }
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum State {
    Merged,
    Squashed,
    Gone,
    Active,
}

impl State {
    fn label(self) -> &'static str {
        match self {
            State::Merged => "merged",
            State::Squashed => "squashed",
            State::Gone => "gone",
            State::Active => "active",
        }
    }

    fn landed(self) -> bool {
        matches!(self, State::Merged | State::Squashed)
    }
}

struct Worktree {
    path: PathBuf,
    branch: Option<String>,
    head: String,
    locked: bool,
}

// The main worktree, or the bare repository, and every linked worktree after it. Read with -z, so
// a path holding a newline stays one path
fn worktrees(dir: &Path) -> io::Result<(Worktree, Vec<Worktree>)> {
    let out = git_cmd(dir)
        .args(["worktree", "list", "--porcelain", "-z"])
        .stderr(Stdio::piped())
        .output()?;
    if !out.status.success() {
        return Err(other(format!(
            "git worktree list: {}",
            String::from_utf8_lossy(&out.stderr).trim()
        )));
    }
    let text = String::from_utf8_lossy(&out.stdout).into_owned();
    let mut all = text.split("\0\0").filter(|b| !b.is_empty()).map(|block| {
        let mut wt = Worktree {
            path: PathBuf::new(),
            branch: None,
            head: String::new(),
            locked: false,
        };
        for line in block.split('\0') {
            if let Some(p) = line.strip_prefix("worktree ") {
                wt.path = PathBuf::from(p);
            } else if let Some(h) = line.strip_prefix("HEAD ") {
                wt.head = h.to_string();
            } else if let Some(b) = line.strip_prefix("branch refs/heads/") {
                wt.branch = Some(b.to_string());
            } else if line == "locked" || line.starts_with("locked ") {
                wt.locked = true;
            }
        }
        wt
    });
    let main = all
        .next()
        .ok_or_else(|| other("git lists no worktree".into()))?;
    Ok((main, all.collect()))
}

struct Base {
    name: String,
    oid: String,
    // the local branch the base stands for, which cleanup never deletes
    local: Option<String>,
}

// The branch work lands on: the one named, or the remote's default, or a local main. A remote's
// HEAD is followed to the branch it names, and the plan prints that name
fn base(repo: &Path, named: Option<&str>) -> io::Result<Base> {
    let candidates: Vec<String> = match named {
        Some(b) => vec![
            run(repo, &["rev-parse", "--symbolic-full-name", b])
                .ok()
                .filter(|f| !f.is_empty())
                .unwrap_or_else(|| b.to_string()),
        ],
        None => [
            "refs/remotes/origin/HEAD",
            "refs/remotes/origin/main",
            "refs/remotes/origin/master",
            "refs/heads/main",
            "refs/heads/master",
        ]
        .map(String::from)
        .to_vec(),
    };
    for c in candidates {
        let full = run(repo, &["symbolic-ref", "-q", &c]).unwrap_or(c);
        let Ok(oid) = run(
            repo,
            &["rev-parse", "--verify", "-q", &format!("{full}^{{commit}}")],
        ) else {
            continue;
        };
        let local = if let Some(rest) = full.strip_prefix("refs/remotes/") {
            remote_branch(repo, rest)
        } else {
            full.strip_prefix("refs/heads/").map(String::from)
        };
        let name = full
            .strip_prefix("refs/remotes/")
            .or_else(|| full.strip_prefix("refs/heads/"))
            .unwrap_or(&full)
            .to_string();
        return Ok(Base { name, oid, local });
    }
    Err(other(
        "no branch to compare with, name one: git cow cleanup <branch>".into(),
    ))
}

// The branch in `<remote>/<branch>`, taking the longest remote name that fits, since a remote name
// may hold a slash
fn remote_branch(repo: &Path, rest: &str) -> Option<String> {
    let remotes = run(repo, &["remote"]).unwrap_or_default();
    remotes
        .lines()
        .filter_map(|r| {
            rest.strip_prefix(r)
                .and_then(|b| b.strip_prefix('/'))
                .map(|b| (r.len(), b))
        })
        .max_by_key(|(n, _)| *n)
        .map(|(_, b)| b.to_string())
        .or_else(|| rest.split_once('/').map(|(_, b)| b.to_string()))
}

// In a partial clone reading a branch's blobs would fetch them, so only ancestry counts there
fn is_partial(repo: &Path) -> bool {
    run(repo, &["config", "--get", "extensions.partialClone"]).is_ok()
        || run(repo, &["config", "--get-regexp", r"^remote\..*\.promisor$"])
            .is_ok_and(|o| o.lines().any(|l| l.ends_with(" true")))
}

struct Probe {
    // patch ids of what the rev changed since the merge base, and of undoing it
    own: String,
    undo: String,
    // base commits after the merge base, children before parents
    commits: String,
    mb: String,
}

// None for an ancestor of the base, or a rev that changes nothing in total
fn squash_probe(repo: &Path, base: &str, rev: &str) -> Option<Probe> {
    if run(repo, &["merge-base", "--is-ancestor", rev, base]).is_ok() {
        return None;
    }
    let mb = run(repo, &["merge-base", base, rev]).ok()?;
    let pid = |from: &str, to: &str| {
        let mut args = vec!["diff"];
        args.extend(DIFF);
        args.extend([from, to]);
        let out = patch_ids(repo, &args, None).ok()?;
        out.split_whitespace().next().map(String::from)
    };
    let own = pid(&mb, rev)?;
    let undo = pid(rev, &mb)?;
    let commits = run(
        repo,
        &[
            "rev-list",
            "--no-merges",
            "--topo-order",
            base,
            &format!("^{mb}"),
        ],
    )
    .ok()?;
    Some(Probe {
        own,
        undo,
        commits,
        mb,
    })
}

// A new empty folder in the repository's git dir, which only its owner can write. A folder that
// already exists is never reused, since git would read objects planted in it before the real ones
fn scratch_dir(common: &Path) -> Option<PathBuf> {
    static COUNT: AtomicUsize = AtomicUsize::new(0);
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.subsec_nanos());
    for _ in 0..8 {
        let n = COUNT.fetch_add(1, Ordering::Relaxed);
        let dir = common.join(format!("git-cow-merge-{}-{nanos}-{n}", std::process::id()));
        match std::fs::DirBuilder::new().mode(0o700).create(&dir) {
            Ok(()) => return Some(dir),
            Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
            Err(_) => return None,
        }
    }
    None
}

// The tree of a three way merge, written to a throwaway object folder over the repository's so the
// repository gets no new objects. None on a conflict
fn merge_tree(repo: &Path, merge_base: &str, ours: &str, theirs: &str) -> Option<String> {
    let common = run(
        repo,
        &["rev-parse", "--path-format=absolute", "--git-common-dir"],
    )
    .ok()?;
    // --attr-source covers only the tree, info/attributes would still name a merge driver
    let info =
        std::fs::read_to_string(Path::new(&common).join("info/attributes")).unwrap_or_default();
    if info.contains("merge") {
        return None;
    }
    let scratch = scratch_dir(Path::new(&common))?;
    let merge_base = format!("--merge-base={merge_base}");
    let out = git_cmd(repo)
        .env("GIT_OBJECT_DIRECTORY", &scratch)
        .env(
            "GIT_ALTERNATE_OBJECT_DIRECTORIES",
            Path::new(&common).join("objects"),
        )
        .args([
            NO_ATTRIBUTES,
            "-c",
            "core.attributesFile=/dev/null",
            "-c",
            "merge.default=text",
            "merge-tree",
            "--write-tree",
            "--no-messages",
            &merge_base,
            ours,
            theirs,
        ])
        .stderr(Stdio::null())
        .output();
    let _ = std::fs::remove_dir_all(&scratch);
    let out = out.ok().filter(|o| o.status.success())?;
    String::from_utf8_lossy(&out.stdout)
        .lines()
        .next()
        .map(String::from)
}

// Replaying the rev's change onto `c`'s parent gives exactly `c`, so `c` is its squash and not the
// same patch made somewhere else in a file
fn is_its_squash(repo: &Path, mb: &str, c: &str, rev: &str) -> bool {
    let tree = run(repo, &["rev-parse", &format!("{c}^{{tree}}")]).ok();
    tree.is_some() && merge_tree(repo, mb, &format!("{c}^"), rev) == tree
}

// A base commit after the squash `c` reverts it: git revert names it, or the patch undoes the rev.
// An undo hidden in other work goes unseen, but the work stays in `c` in the base's history
fn reverted(
    repo: &Path,
    base: &str,
    c: &str,
    later: &[&str],
    undo: &str,
    by_commit: &HashMap<String, String>,
) -> bool {
    later
        .iter()
        .any(|l| by_commit.get(*l).map(String::as_str) == Some(undo))
        || run(
            repo,
            &[
                "log",
                "--format=%H",
                "--fixed-strings",
                &format!("--grep=This reverts commit {c}"),
                base,
                &format!("^{c}"),
            ],
        )
        .is_ok_and(|out| !out.is_empty())
}

// One base commit after the merge base is the rev's squash, replayed exactly, and no later base
// commit reverts it
fn squashed(
    repo: &Path,
    base: &str,
    rev: &str,
    probe: &Probe,
    by_commit: &HashMap<String, String>,
) -> bool {
    let commits: Vec<&str> = probe.commits.lines().collect();
    commits.iter().enumerate().any(|(i, c)| {
        by_commit.get(*c) == Some(&probe.own)
            && is_its_squash(repo, &probe.mb, c, rev)
            && !reverted(repo, base, c, &commits[..i], &probe.undo, by_commit)
    })
}

// Patch ids of `commits`, all in one `git log`. An empty commit has none, so it matches nothing
fn commit_patch_ids(repo: &Path, commits: &HashSet<&str>) -> HashMap<String, String> {
    let mut by_commit = HashMap::new();
    if commits.is_empty() {
        return by_commit;
    }
    let mut args = vec![
        "log",
        "-p",
        "--no-walk=unsorted",
        "--stdin",
        "--format=commit %H",
    ];
    args.extend(DIFF);
    let input = commits.iter().fold(String::new(), |mut list, c| {
        list.push_str(c);
        list.push('\n');
        list
    });
    if let Ok(out) = patch_ids(repo, &args, Some(input)) {
        for line in out.lines() {
            if let Some((pid, commit)) = line.split_once(' ') {
                by_commit.insert(commit.to_string(), pid.to_string());
            }
        }
    }
    by_commit
}

// Merged is an ancestor of the base. Squashed is git-extras' check with whitespace counted, made
// sure by replaying the squash, and not reverted since
fn states(repo: &Path, base: &Base, revs: &[(String, Option<String>)]) -> Vec<State> {
    let partial = is_partial(repo);
    let probes: Vec<Option<Probe>> = revs
        .iter()
        .map(|(rev, _)| {
            if partial {
                None
            } else {
                squash_probe(repo, &base.oid, rev)
            }
        })
        .collect();
    let wanted: HashSet<&str> = probes
        .iter()
        .flatten()
        .flat_map(|p| p.commits.lines())
        .collect();
    let by_commit = commit_patch_ids(repo, &wanted);
    revs.iter()
        .zip(&probes)
        .map(|((rev, branch), probe)| {
            if run(repo, &["merge-base", "--is-ancestor", rev, &base.oid]).is_ok() {
                return State::Merged;
            }
            if probe
                .as_ref()
                .is_some_and(|p| squashed(repo, &base.oid, rev, p, &by_commit))
            {
                return State::Squashed;
            }
            let gone = branch.as_deref().is_some_and(|b| {
                run(
                    repo,
                    &[
                        "for-each-ref",
                        "--format=%(upstream:track)",
                        &format!("refs/heads/{b}"),
                    ],
                )
                .is_ok_and(|t| t == "[gone]")
            });
            if gone { State::Gone } else { State::Active }
        })
        .collect()
}

// Edits, staged changes and every untracked file, whatever status.showUntrackedFiles says, and
// files marked assume-unchanged or skip-worktree, whose edits git status hides. Empty when clean
fn changes(path: &Path) -> String {
    if !path.exists() {
        return String::new();
    }
    let mut seen = run(
        path,
        &[
            "status",
            "--porcelain",
            "--untracked-files=all",
            "--ignore-submodules=none",
        ],
    )
    .unwrap_or_else(|e| format!("unreadable: {e}"));
    if let Ok(files) = run(path, &["ls-files", "-v"]) {
        for l in files
            .lines()
            .filter(|l| l.starts_with(|c: char| c.is_ascii_lowercase() || c == 'S'))
        {
            seen.push('\n');
            seen.push_str(l);
        }
    }
    seen.trim().to_string()
}

// A folder, or any folder below it, that is a repository of its own. Symlinks are not followed,
// since removing the worktree removes only the link
fn repository_below(dir: &Path) -> bool {
    let mut stack = vec![dir.to_path_buf()];
    while let Some(d) = stack.pop() {
        if d.join(".git").exists() || (d.join("HEAD").is_file() && d.join("objects").is_dir()) {
            return true;
        }
        let Ok(entries) = std::fs::read_dir(&d) else {
            continue;
        };
        stack.extend(
            entries
                .flatten()
                .filter(|e| e.file_type().is_ok_and(|t| t.is_dir()))
                .map(|e| e.path()),
        );
    }
    false
}

// An untracked or ignored folder holding a repository, whose history a remove takes with it
fn holds_repository(path: &Path) -> bool {
    let Ok(out) = git_cmd(path)
        .args([
            "status",
            "--porcelain",
            "-z",
            "--ignored=traditional",
            "--untracked-files=normal",
        ])
        .stderr(Stdio::null())
        .output()
    else {
        return true;
    };
    // a status that fails hides what is inside, so it counts as holding one
    !out.status.success()
        || String::from_utf8_lossy(&out.stdout)
            .split('\0')
            .filter_map(|e| e.strip_prefix("?? ").or_else(|| e.strip_prefix("!! ")))
            .filter(|p| p.ends_with('/'))
            .any(|p| repository_below(&path.join(p)))
}

// Each linked worktree's admin folder in the common git dir, keyed by the worktree path its
// `gitdir` file names
fn admin_dirs(repo: &Path) -> HashMap<PathBuf, PathBuf> {
    let mut map = HashMap::new();
    let Ok(common) = run(
        repo,
        &["rev-parse", "--path-format=absolute", "--git-common-dir"],
    ) else {
        return map;
    };
    if let Ok(entries) = std::fs::read_dir(Path::new(&common).join("worktrees")) {
        for e in entries.flatten() {
            if let Ok(gitfile) = std::fs::read_to_string(e.path().join("gitdir"))
                && let Some(wt) = Path::new(gitfile.trim()).parent()
            {
                map.insert(wt.to_path_buf(), e.path());
            }
        }
    }
    map
}

// What a worktree holds that git keeps nowhere else: submodules and other repositories inside it,
// refs of its own, a rebase, merge or bisect. Read from its admin folder, so a missing folder too
fn own_state(repo: &Path, path: &Path, admin: Option<&PathBuf>) -> Option<&'static str> {
    let Some(admin) = admin else {
        return Some("unreadable");
    };
    let submodules = admin.join("modules").exists()
        || path.join(".gitmodules").exists()
        || run(path, &["ls-files", "-s"]).is_ok_and(|s| s.lines().any(|l| l.starts_with("160000")));
    if submodules {
        return Some("has submodules");
    }
    if path.exists() && holds_repository(path) {
        return Some("holds a repository");
    }
    if BUSY.iter().any(|f| admin.join(f).exists()) {
        return Some("in a rebase, merge or bisect");
    }
    let admin = admin.to_string_lossy();
    let refs = run(
        repo,
        &[
            "--git-dir",
            &admin,
            "for-each-ref",
            "refs/worktree/",
            "refs/bisect/",
        ],
    );
    if refs.is_ok_and(|s| !s.is_empty()) {
        return Some("has its own refs");
    }
    None
}

// Claude Code removes its own worktrees, and a session may still run in one
fn claude_worktree(path: &Path) -> bool {
    let parts: Vec<_> = path
        .components()
        .map(std::path::Component::as_os_str)
        .collect();
    parts
        .windows(2)
        .any(|w| w[0] == ".claude" && w[1] == "worktrees")
}

// Branches a rebase or bisect in any worktree works on, which git lists as detached
fn busy_branches(repo: &Path) -> HashSet<String> {
    let mut busy = HashSet::new();
    let Ok(common) = run(
        repo,
        &["rev-parse", "--path-format=absolute", "--git-common-dir"],
    ) else {
        return busy;
    };
    let common = PathBuf::from(common);
    let mut gitdirs = vec![common.clone()];
    if let Ok(entries) = std::fs::read_dir(common.join("worktrees")) {
        gitdirs.extend(entries.flatten().map(|e| e.path()));
    }
    for g in gitdirs {
        for f in [
            "rebase-merge/head-name",
            "rebase-apply/head-name",
            "BISECT_START",
        ] {
            if let Ok(text) = std::fs::read_to_string(g.join(f)) {
                let name = text.trim();
                busy.insert(name.strip_prefix("refs/heads/").unwrap_or(name).to_string());
            }
        }
    }
    busy
}

fn rev_of(w: &Worktree) -> (String, Option<String>) {
    let rev = w
        .branch
        .as_ref()
        .map_or_else(|| w.head.clone(), |b| format!("refs/heads/{b}"));
    (rev, w.branch.clone())
}

enum Action {
    Remove,
    Keep(&'static str),
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Force {
    No,
    Yes,
}

struct Item {
    path: Option<PathBuf>,
    branch: Option<String>,
    // the commit and the changes the plan judged, so the run skips anything that moved since
    oid: String,
    changes: String,
    state: State,
    action: Action,
    // whether removing this worktree also deletes its branch
    branch_too: bool,
    forced: Force,
    // the worktree's admin folder, whose HEAD reflog may hold commits no ref has
    admin: Option<PathBuf>,
}

struct Plan {
    repo: PathBuf,
    base: Base,
    items: Vec<Item>,
}

fn worktree_items(
    repo: &Path,
    base: &Base,
    linked: &[Worktree],
    protected: &HashSet<String>,
    here: &Path,
    force: Force,
) -> Vec<Item> {
    let revs: Vec<(String, Option<String>)> = linked.iter().map(rev_of).collect();
    let admins = admin_dirs(repo);
    let mut items = Vec::new();
    for (wt, state) in linked.iter().zip(states(repo, base, &revs)) {
        let changes = changes(&wt.path);
        let admin = admins.get(&wt.path).or_else(|| {
            let real = std::fs::canonicalize(&wt.path).ok()?;
            admins
                .iter()
                .find(|(p, _)| std::fs::canonicalize(p).is_ok_and(|q| q == real))
                .map(|(_, a)| a)
        });
        let inside = here.starts_with(&wt.path)
            || std::fs::canonicalize(&wt.path).is_ok_and(|p| here.starts_with(p));
        let action = if !state.landed() {
            Action::Keep("")
        } else if wt.locked {
            Action::Keep("locked")
        } else if inside {
            Action::Keep("you are in it")
        } else if claude_worktree(&wt.path) {
            Action::Keep("Claude Code worktree")
        } else if wt.branch.as_ref().is_some_and(|b| protected.contains(b)) {
            Action::Keep("base branch")
        } else if !wt.path.exists() && force == Force::No {
            Action::Keep("folder missing")
        } else if let Some(why) = own_state(repo, &wt.path, admin) {
            Action::Keep(why)
        } else if !changes.is_empty() && force == Force::No {
            Action::Keep("has changes")
        } else {
            Action::Remove
        };
        let forced = if !changes.is_empty() || !wt.path.exists() {
            Force::Yes
        } else {
            Force::No
        };
        items.push(Item {
            path: Some(wt.path.clone()),
            branch: wt.branch.clone(),
            changes,
            oid: wt.head.clone(),
            state,
            action,
            branch_too: false,
            forced,
            admin: admin.cloned(),
        });
    }
    // A branch goes with the last of its worktrees, and only when every one of them goes
    let mut last: HashMap<String, usize> = HashMap::new();
    for (i, item) in items.iter().enumerate() {
        if let Some(b) = &item.branch {
            last.insert(b.clone(), i);
        }
    }
    for (b, i) in &last {
        let all_go = items
            .iter()
            .filter(|it| it.branch.as_ref() == Some(b))
            .all(|it| matches!(it.action, Action::Remove));
        if all_go && !protected.contains(b) {
            items[*i].branch_too = true;
        }
    }
    items
}

fn plan(dir: &Path, named: Option<&str>, force: Force) -> io::Result<Plan> {
    let (main, linked) = worktrees(dir)?;
    // every call runs from the main worktree, so removing the one you stand in breaks nothing
    let repo = main.path.clone();
    let base = base(&repo, named)?;
    let here = std::fs::canonicalize(dir).unwrap_or_else(|_| dir.to_path_buf());
    let mut protected: HashSet<String> = base.local.iter().cloned().collect();
    protected.extend(main.branch);
    protected.extend(head_branch(&repo));
    if named.is_some() {
        protected.extend(base_of(&repo));
    }
    protected.extend(busy_branches(&repo));
    // Local branches with no worktree, never one checked out anywhere, an extra checkout too
    let checked_out: HashSet<String> = linked.iter().filter_map(|w| w.branch.clone()).collect();
    let linked: Vec<Worktree> = linked
        .into_iter()
        .filter(|w| !is_template(&repo, &w.path))
        .collect();
    let mut items = worktree_items(&repo, &base, &linked, &protected, &here, force);

    // a symbolic ref such as master pointing at main is an alias, deleting it would delete main
    let heads = run(
        &repo,
        &[
            "for-each-ref",
            "--format=%(refname)%00%(symref)",
            "refs/heads/",
        ],
    )?;
    let mut only = Vec::new();
    for entry in heads.lines() {
        let (full, symref) = entry.split_once('\0').unwrap_or((entry, ""));
        let Some(b) = full.strip_prefix("refs/heads/") else {
            continue;
        };
        if !symref.is_empty() {
            continue;
        }
        if checked_out.contains(b) || protected.contains(b) {
            continue;
        }
        let oid = run(&repo, &["rev-parse", "--verify", "-q", full])?;
        only.push((b.to_string(), oid));
    }
    let revs: Vec<(String, Option<String>)> = only
        .iter()
        .map(|(b, _)| (format!("refs/heads/{b}"), Some(b.clone())))
        .collect();
    for ((b, oid), state) in only.into_iter().zip(states(&repo, &base, &revs)) {
        items.push(Item {
            path: None,
            branch: Some(b),
            changes: String::new(),
            oid,
            state,
            action: if state.landed() {
                Action::Remove
            } else {
                Action::Keep("")
            },
            branch_too: true,
            forced: Force::No,
            admin: None,
        });
    }
    Ok(Plan { repo, base, items })
}

// The branch HEAD names in `dir`, from the full ref so a tag with the same name cannot shorten it
fn head_branch(dir: &Path) -> Option<String> {
    run(dir, &["symbolic-ref", "-q", "HEAD"])
        .ok()
        .and_then(|h| h.strip_prefix("refs/heads/").map(String::from))
}

// The local default branch, kept even when cleanup compares with another one
fn base_of(repo: &Path) -> Option<String> {
    base(repo, None).ok().and_then(|b| b.local)
}

// One tab separated line: what happens, how far the work landed, the branch, the worktree path
fn line(item: &Item) -> String {
    let verb = match (&item.action, &item.path) {
        (Action::Remove, Some(_)) if !item.changes.is_empty() => {
            "remove (discards changes)".to_string()
        }
        (Action::Remove, Some(_)) if item.branch_too || item.branch.is_none() => {
            "remove".to_string()
        }
        (Action::Remove, Some(_)) => "remove (keeps branch)".to_string(),
        (Action::Remove, None) => "delete".to_string(),
        (Action::Keep(""), _) => "keep".to_string(),
        (Action::Keep(why), _) => format!("keep ({why})"),
    };
    let branch = item.branch.as_deref().unwrap_or("(detached)");
    let path = item
        .path
        .as_ref()
        .map_or_else(String::new, |p| p.display().to_string());
    format!("{verb}\t{}\t{branch}\t{path}", item.state.label())
        .trim_end()
        .to_string()
}

fn short(oid: &str) -> &str {
    oid.get(..12).unwrap_or(oid)
}

// Whether a worktree is still as the plan saw it: same commit, same branch, same changes
fn unchanged(item: &Item, path: &Path) -> bool {
    if !path.exists() {
        return true;
    }
    let head = run(path, &["rev-parse", "HEAD"]).unwrap_or_default();
    head == item.oid && head_branch(path) == item.branch && changes(path) == item.changes
}

// Where cleanup keeps every commit it lets go of, one folder per run, so gc never prunes them
const BACKUP: &str = "refs/git-cow/deleted";

// A worktree folder name as a ref name part
fn ref_part(name: &str) -> String {
    let clean: String = name
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || "-_".contains(c) {
                c
            } else {
                '-'
            }
        })
        .collect();
    if clean.is_empty() {
        "worktree".into()
    } else {
        clean
    }
}

// Commits only the worktree's HEAD reflog holds, saved before git deletes that reflog with it
fn back_up_reflog(repo: &Path, admin: &Path, name: &str, stamp: &str) -> io::Result<()> {
    let admin = admin.to_string_lossy();
    let Ok(log) = run(
        repo,
        &["--git-dir", &admin, "reflog", "show", "--format=%H", "HEAD"],
    ) else {
        return Ok(());
    };
    let oids: HashSet<&str> = log.lines().filter(|l| !l.is_empty()).collect();
    if oids.is_empty() {
        return Ok(());
    }
    let mut args = vec!["rev-list"];
    args.extend(oids.iter());
    args.extend(["--not", "--branches", "--tags", "--remotes"]);
    let unreachable = run(repo, &args)?;
    let lost = unreachable.lines().filter(|o| oids.contains(o));
    for (i, oid) in lost.enumerate() {
        let backup = format!("{BACKUP}/{stamp}/worktree/{}/{i}", ref_part(name));
        run(repo, &["update-ref", &backup, oid])?;
    }
    Ok(())
}

// Delete the local `b` only if it still points at `oid` and no worktree has it checked out now,
// after saving it under the backup refs. Only refs/heads changes, a remote is never touched
fn delete_branch(repo: &Path, b: &str, oid: &str, stamp: &str) -> io::Result<String> {
    let (main, linked) = worktrees(repo)?;
    if std::iter::once(&main)
        .chain(&linked)
        .any(|w| w.branch.as_deref() == Some(b))
    {
        return Err(other(format!("{b} is checked out in a worktree")));
    }
    if busy_branches(repo).contains(b) {
        return Err(other(format!("a rebase or bisect works on {b}")));
    }
    let backup = format!("{BACKUP}/{stamp}/branch/{b}");
    run(repo, &["update-ref", &backup, oid])?;
    run(
        repo,
        &[
            "update-ref",
            "--no-deref",
            "-d",
            &format!("refs/heads/{b}"),
            oid,
        ],
    )?;
    // git branch -D drops these too
    let _ = run(
        repo,
        &["config", "--remove-section", &format!("branch.{b}")],
    );
    Ok(backup)
}

struct Mode {
    dry_run: bool,
    yes: bool,
}

fn confirm(mode: &Mode) -> io::Result<bool> {
    if mode.dry_run {
        return Ok(false);
    }
    if mode.yes {
        return Ok(true);
    }
    if !io::stdin().is_terminal() {
        println!("pass --yes to remove them");
        return Ok(false);
    }
    print!("remove them? [y/N] ");
    io::stdout().flush()?;
    let mut answer = String::new();
    io::stdin().lock().read_line(&mut answer)?;
    Ok(matches!(answer.trim(), "y" | "Y" | "yes"))
}

fn execute(repo: &Path, removals: &[&Item]) -> usize {
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs());
    let stamp = format!("{secs}-{}", std::process::id());
    let mut failed = 0;
    for item in removals {
        if let Some(path) = &item.path {
            let p = path.to_string_lossy();
            if !unchanged(item, path) {
                eprintln!("git-cow: kept {p}, it changed since the plan");
                failed += 1;
                continue;
            }
            let name = path.file_name().unwrap_or_default().to_string_lossy();
            if let Some(admin) = &item.admin
                && let Err(e) = back_up_reflog(repo, admin, &name, &stamp)
            {
                eprintln!("git-cow: kept {p}, its reflog could not be backed up: {e}");
                failed += 1;
                continue;
            }
            let mut cmd = vec!["worktree", "remove"];
            if item.forced == Force::Yes {
                cmd.push("--force");
            }
            cmd.push(&p);
            if let Err(e) = run(repo, &cmd) {
                eprintln!("git-cow: kept {p}: {e}");
                failed += 1;
                continue;
            }
            println!("removed {p}");
        }
        if let (Some(b), true) = (&item.branch, item.branch_too) {
            match delete_branch(repo, b, &item.oid, &stamp) {
                Ok(backup) => println!(
                    "deleted {b} (was {}, git branch {b} {backup} restores it)",
                    short(&item.oid)
                ),
                Err(e) => {
                    eprintln!("git-cow: kept branch {b}: {e}");
                    failed += 1;
                }
            }
        }
    }
    failed
}

/// Remove worktrees and delete local branches whose work is in the base, after showing the plan
pub(crate) fn cleanup(args: &[String]) -> io::Result<()> {
    let mut mode = Mode {
        dry_run: false,
        yes: false,
    };
    let mut force = Force::No;
    let mut named = None;
    for a in args {
        match a.as_str() {
            "-n" | "--dry-run" => mode.dry_run = true,
            "-y" | "--yes" => mode.yes = true,
            "-f" | "--force" => force = Force::Yes,
            s if s.starts_with('-') => return Err(other(format!("unknown option {s}"))),
            s if named.is_none() => named = Some(s),
            _ => return Err(other("give at most one branch to compare with".into())),
        }
    }
    warn_without_squash_check();
    let Plan { repo, base, items } = plan(&std::env::current_dir()?, named, force)?;
    for item in &items {
        println!("{}", line(item));
    }
    let removals: Vec<&Item> = items
        .iter()
        .filter(|i| matches!(i.action, Action::Remove))
        .collect();
    let against = format!("against {} ({})", base.name, short(&base.oid));
    if removals.is_empty() {
        println!("nothing to clean up {against}");
        return Ok(());
    }
    let worktrees = removals.iter().filter(|i| i.path.is_some()).count();
    let branches = removals.iter().filter(|i| i.branch_too).count();
    let wt_noun = if worktrees == 1 {
        "worktree"
    } else {
        "worktrees"
    };
    let br_noun = if branches == 1 { "branch" } else { "branches" };
    println!("{worktrees} {wt_noun} and {branches} local {br_noun} to remove, {against}");
    if !confirm(&mode)? {
        return Ok(());
    }
    let failed = execute(&repo, &removals);
    if failed > 0 {
        return Err(other(format!("{failed} could not be removed")));
    }
    Ok(())
}

// Repositories at or below `dir`, not looking inside one or inside hidden and build folders
fn repositories(dir: &Path) -> Vec<PathBuf> {
    let mut found = Vec::new();
    let mut stack = vec![(dir.to_path_buf(), 0)];
    while let Some((d, depth)) = stack.pop() {
        let bare =
            d.join("HEAD").is_file() && d.join("objects").is_dir() && d.join("refs").is_dir();
        if d.join(".git").is_dir() || bare {
            found.push(d);
            continue;
        }
        if depth == 4 {
            continue;
        }
        let Ok(entries) = std::fs::read_dir(&d) else {
            continue;
        };
        for entry in entries.flatten() {
            let name = entry.file_name();
            let name = name.to_string_lossy();
            let is_dir = entry.file_type().is_ok_and(|t| t.is_dir());
            if is_dir && !name.starts_with('.') && name != "node_modules" && name != "target" {
                stack.push((entry.path(), depth + 1));
            }
        }
    }
    found.sort();
    found
}

// The squash check needs patch-id --verbatim and --attr-source, both in git 2.40. Older git cannot
// tell a squash merge, so those branches show as active and stay, which is safe but worth saying
fn warn_without_squash_check() {
    let works = git_cmd(Path::new("."))
        .args([NO_ATTRIBUTES, "patch-id", "--verbatim"])
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .is_ok_and(|s| s.success());
    if !works {
        eprintln!(
            "git-cow: this git is older than 2.40, so squash-merged branches show as active and stay"
        );
    }
}

/// List the worktrees of every repository at or below a folder, with how far their work landed.
/// It reads only commits and refs, never a worktree's files, so no filter or hook a repository
/// sets can run
pub(crate) fn tree(args: &[String]) -> io::Result<()> {
    let dir = match args {
        [] => std::env::current_dir()?,
        [d] if !d.starts_with('-') => PathBuf::from(d),
        _ => return Err(other("give one folder".into())),
    };
    warn_without_squash_check();
    for repo in repositories(&std::fs::canonicalize(&dir)?) {
        let Ok((_, linked)) = worktrees(&repo) else {
            continue;
        };
        let linked: Vec<Worktree> = linked
            .into_iter()
            .filter(|w| !is_template(&repo, &w.path))
            .collect();
        if linked.is_empty() {
            continue;
        }
        let revs: Vec<(String, Option<String>)> = linked.iter().map(rev_of).collect();
        let states: Vec<Option<State>> = base(&repo, None).map_or_else(
            |_| vec![None; linked.len()],
            |b| states(&repo, &b, &revs).into_iter().map(Some).collect(),
        );
        let landed = states.iter().flatten().filter(|s| s.landed()).count();
        let n = linked.len();
        let noun = if n == 1 { "worktree" } else { "worktrees" };
        println!(
            "{}  ({n} {noun}, {landed} merged or squashed)",
            repo.display()
        );
        for (wt, state) in linked.iter().zip(states) {
            let label = state.map_or("?", State::label);
            let branch = wt.branch.as_deref().unwrap_or("(detached)");
            println!("  {label}\t{branch}\t{}", wt.path.display());
        }
    }
    Ok(())
}