le-change 0.2.0

Ultra-fast Git change detection library with zero-cost abstractions
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
//! Vanished-file detection: first-parent history walk over base..head.
//!
//! The endpoint diff (`diff_tree_to_tree(base, head)`) cannot see a file that
//! was added in some commit of a PR and removed again before head — it exists
//! in neither endpoint tree. This walker finds those files and records the
//! last commit where each existed, so consumers can reconstruct content
//! (e.g. to destroy infrastructure the file defined).
//!
//! Scoping guarantee: the revwalk pushes head, hides base, and simplifies to
//! first parents — only commits introduced by this base..head range are ever
//! visited, never another branch's work. Side-branch-internal churn (a file
//! added and deleted entirely within a merged side branch) is invisible by
//! design: it never existed on the mainline.
//!
//! Concurrency: the walk is single-threaded by necessity — `git2::Repository`,
//! `Tree`, and `Diff` are `!Send`, and after pathspec filtering the per-commit
//! delta sets are tiny (usually empty), so shipping paths across a rayon
//! boundary would cost more than the glob checks it saves. The feature is
//! zero-cost when disabled: one bool branch in the pipeline.

use std::collections::{HashMap, HashSet};
use std::path::Path;

use crate::error::{Error, Result};
use crate::git::sha::ShaResolver;
use crate::interner::StringInterner;
use crate::types::{InternedString, VanishedFile};

/// Result of a vanished-file scan
#[derive(Debug, Default)]
pub struct VanishedScan {
    /// Vanished files, ordered newest-deletion-first (the first entry's
    /// `last_seen_sha` is the natural group-level reconstruction commit)
    pub vanished: Vec<VanishedFile>,
    /// Commits visited on the first-parent chain
    pub commits_walked: u32,
    /// True when the vanished_max_commits cap was hit (results may be partial)
    pub truncated: bool,
    /// Soft anomalies (e.g. rename cycle) — caller maps these to diagnostics
    pub anomalies: Vec<String>,
}

/// Terminal event observed for a path while walking newest -> oldest
enum Terminal {
    /// Deleted; payload = first parent of the deleting commit (last commit
    /// where the file existed)
    DeletedAt(git2::Oid),
    /// Renamed away to another path
    RenamedTo(InternedString),
}

/// Per-path event record (newest event wins — the walk is newest-first)
struct PathEvents {
    added_in_range: bool,
    terminal: Option<Terminal>,
}

/// First-parent history walker detecting files added then removed within
/// base..head. Holds the repo path (git2 objects are `!Send`) and opens the
/// repository once per `detect_sync` call, mirroring `ShaResolver`.
pub struct VanishedDetector<'a> {
    repo_path: &'a Path,
    interner: &'a StringInterner,
}

impl<'a> VanishedDetector<'a> {
    /// Create a new detector rooted at the repository path
    pub fn new(repo_path: &'a Path, interner: &'a StringInterner) -> Self {
        Self {
            repo_path,
            interner,
        }
    }

    /// Walk base..head (first-parent) and report files that were added in the
    /// range, match `matches`, and no longer exist in the head tree.
    ///
    /// `matches` is a statically-dispatched predicate over repo-relative
    /// paths. `pathspecs` are coarse literal prefixes (see
    /// [`pathspec_prefixes`]) used to keep the common per-commit diff cheap;
    /// correctness never depends on them. `max_commits` of 0 means unlimited.
    pub fn detect_sync<F: Fn(&str) -> bool>(
        &self,
        base_sha: &str,
        head_sha: &str,
        matches: F,
        pathspecs: &[String],
        max_commits: u32,
    ) -> Result<VanishedScan> {
        let mut scan = VanishedScan::default();
        if base_sha == head_sha {
            return Ok(scan);
        }

        let repo = git2::Repository::open(self.repo_path)?;
        let head_oid = git2::Oid::from_str(head_sha)?;
        let head_tree = repo.find_commit(head_oid)?.tree()?;

        // Initial-push base (the empty tree SHA) has no commit to hide; the
        // walk then covers head's whole first-parent history, bounded by the
        // cap. A missing/unreachable base (shallow clone, force push) is a
        // hard error here — the caller soft-fails it into a diagnostic.
        let base_tree = if base_sha == ShaResolver::empty_tree_sha() {
            None
        } else {
            let base_oid = git2::Oid::from_str(base_sha)?;
            let base_commit = repo.find_commit(base_oid).map_err(|e| {
                Error::Git(format!(
                    "base SHA {} not found (shallow clone or force push?): {}",
                    base_sha, e
                ))
            })?;
            Some(base_commit.tree()?)
        };

        let mut walk = repo.revwalk()?;
        walk.push(head_oid)?;
        if base_tree.is_some() {
            walk.hide(git2::Oid::from_str(base_sha)?)?;
        }
        walk.simplify_first_parent()?;
        walk.set_sorting(git2::Sort::TOPOLOGICAL)?;

        let mut events: HashMap<InternedString, PathEvents> = HashMap::new();
        let mut deletion_order: HashMap<InternedString, u32> = HashMap::new();

        for oid in walk {
            if max_commits > 0 && scan.commits_walked >= max_commits {
                scan.truncated = true;
                break;
            }
            let oid = oid?;
            scan.commits_walked += 1;

            let commit = repo.find_commit(oid)?;
            let commit_tree = commit.tree()?;
            // Root commit diffs against the empty tree
            let parent = commit.parent(0).ok();
            let parent_tree = match &parent {
                Some(p) => Some(p.tree()?),
                None => None,
            };

            // Cheap pass: pathspec-filtered. Commits touching nothing under
            // the prefixes produce an empty delta set for near-zero cost.
            let mut opts = git2::DiffOptions::new();
            opts.ignore_submodules(true);
            for p in pathspecs {
                opts.pathspec(p);
            }
            let cheap =
                repo.diff_tree_to_tree(parent_tree.as_ref(), Some(&commit_tree), Some(&mut opts))?;

            let needs_precise = cheap.deltas().any(|d| {
                d.status() == git2::Delta::Deleted
                    && d.old_file()
                        .path()
                        .and_then(|p| p.to_str())
                        .is_some_and(&matches)
            });

            // Precise pass only when a matching deletion appeared: re-diff
            // WITHOUT pathspec and with rename detection, so a delete that is
            // actually a rename (possibly out of the prefixes) is classified
            // Renamed — pathspec filtering would have hidden the Add side.
            // Static dispatch: one closure applied to whichever diff is live.
            let parent_oid = parent.as_ref().map(|p| p.id());
            let mut record = |delta: git2::DiffDelta<'_>| {
                match delta.status() {
                    git2::Delta::Deleted => {
                        let Some(path) = delta.old_file().path().and_then(|p| p.to_str()) else {
                            return;
                        };
                        if !matches(path) {
                            return;
                        }
                        let key = self.interner.intern(path);
                        let entry = events.entry(key).or_insert(PathEvents {
                            added_in_range: false,
                            terminal: None,
                        });
                        if entry.terminal.is_none() {
                            if let Some(parent_oid) = parent_oid {
                                entry.terminal = Some(Terminal::DeletedAt(parent_oid));
                                deletion_order.insert(key, scan.commits_walked);
                            }
                            // A deletion in a root commit is impossible
                            // (nothing existed before it) — ignore.
                        }
                    }
                    git2::Delta::Renamed => {
                        let (Some(old), Some(new)) = (
                            delta.old_file().path().and_then(|p| p.to_str()),
                            delta.new_file().path().and_then(|p| p.to_str()),
                        ) else {
                            return;
                        };
                        if !matches(old) {
                            return;
                        }
                        let key = self.interner.intern(old);
                        let entry = events.entry(key).or_insert(PathEvents {
                            added_in_range: false,
                            terminal: None,
                        });
                        if entry.terminal.is_none() {
                            entry.terminal = Some(Terminal::RenamedTo(self.interner.intern(new)));
                        }
                    }
                    git2::Delta::Added => {
                        let Some(path) = delta.new_file().path().and_then(|p| p.to_str()) else {
                            return;
                        };
                        if !matches(path) {
                            return;
                        }
                        let key = self.interner.intern(path);
                        events
                            .entry(key)
                            .or_insert(PathEvents {
                                added_in_range: false,
                                terminal: None,
                            })
                            .added_in_range = true;
                    }
                    _ => {}
                }
            };

            if needs_precise {
                let mut full_opts = git2::DiffOptions::new();
                full_opts.ignore_submodules(true);
                let mut diff = repo.diff_tree_to_tree(
                    parent_tree.as_ref(),
                    Some(&commit_tree),
                    Some(&mut full_opts),
                )?;
                let mut find = git2::DiffFindOptions::new();
                find.renames(true);
                diff.find_similar(Some(&mut find))?;
                for delta in diff.deltas() {
                    record(delta);
                }
            } else {
                for delta in cheap.deltas() {
                    record(delta);
                }
            }
        }

        // Resolution: a path is vanished when it was added in-range, is absent
        // from the head tree, did not exist at base (the endpoint diff already
        // reports those as Deleted), and its newest terminal event — following
        // rename chains — is a deletion.
        for (&path, ev) in &events {
            if !ev.added_in_range {
                continue;
            }
            let Some(path_str) = self.interner.resolve(path) else {
                continue;
            };
            if head_tree.get_path(Path::new(path_str)).is_ok() {
                continue; // alive at head (re-added or never really gone)
            }
            if let Some(base_tree) = &base_tree {
                if base_tree.get_path(Path::new(path_str)).is_ok() {
                    continue; // existed at base -> endpoint Deleted covers it
                }
            }

            let mut cur = path;
            let mut seen: HashSet<InternedString> = HashSet::from([path]);
            loop {
                match events.get(&cur).and_then(|e| e.terminal.as_ref()) {
                    Some(Terminal::DeletedAt(parent_oid)) => {
                        scan.vanished.push(VanishedFile {
                            path: cur,
                            last_seen_sha: self.interner.intern(&parent_oid.to_string()),
                        });
                        break;
                    }
                    Some(Terminal::RenamedTo(new_path)) => {
                        let Some(new_str) = self.interner.resolve(*new_path) else {
                            break;
                        };
                        if head_tree.get_path(Path::new(new_str)).is_ok() {
                            break; // renamed and alive at head -> not vanished
                        }
                        if !seen.insert(*new_path) {
                            scan.anomalies.push(format!(
                                "rename cycle detected while resolving '{}'",
                                path_str
                            ));
                            break;
                        }
                        cur = *new_path;
                    }
                    None => {
                        scan.anomalies.push(format!(
                            "'{}' was added in range and is absent at head, but no \
                             deletion was observed on the first-parent chain within \
                             the walked window",
                            path_str
                        ));
                        break;
                    }
                }
            }
        }

        // Deterministic order: newest deletion first, path as tie-break.
        scan.vanished.sort_by(|a, b| {
            let oa = deletion_order.get(&a.path).copied().unwrap_or(u32::MAX);
            let ob = deletion_order.get(&b.path).copied().unwrap_or(u32::MAX);
            oa.cmp(&ob).then_with(|| {
                self.interner
                    .resolve(a.path)
                    .cmp(&self.interner.resolve(b.path))
            })
        });

        Ok(scan)
    }
}

/// Derive coarse literal pathspec prefixes from glob patterns: the substring
/// up to the first glob metacharacter (`*?[{`), truncated at the last `/`.
/// A pattern with no derivable prefix yields no entry; if ANY pattern lacks a
/// prefix the result is empty (meaning: diff everything — pathspec filtering
/// is an optimization, never a correctness gate).
pub fn pathspec_prefixes(patterns: &[&str]) -> Vec<String> {
    let mut prefixes: Vec<String> = Vec::new();
    for pat in patterns {
        let meta = pat.find(['*', '?', '[', '{']).unwrap_or(pat.len());
        let literal = &pat[..meta];
        let Some(slash) = literal.rfind('/') else {
            return Vec::new(); // pattern can match at repo root: no filter
        };
        let prefix = &literal[..slash];
        if prefix.is_empty() {
            return Vec::new();
        }
        if !prefixes.iter().any(|p| p == prefix) {
            prefixes.push(prefix.to_string());
        }
    }
    prefixes
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    fn git(dir: &Path, args: &[&str]) -> String {
        let out = Command::new("git")
            .args(args)
            .current_dir(dir)
            .env("GIT_AUTHOR_NAME", "t")
            .env("GIT_AUTHOR_EMAIL", "t@t")
            .env("GIT_COMMITTER_NAME", "t")
            .env("GIT_COMMITTER_EMAIL", "t@t")
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "git {:?} failed: {}",
            args,
            String::from_utf8_lossy(&out.stderr)
        );
        String::from_utf8(out.stdout).unwrap().trim().to_string()
    }

    fn new_repo() -> TempDir {
        let dir = TempDir::new().unwrap();
        git(dir.path(), &["init", "-q", "-b", "main"]);
        dir
    }

    fn write_and_commit(dir: &Path, files: &[(&str, &str)], msg: &str) -> String {
        for (path, content) in files {
            let full = dir.join(path);
            fs::create_dir_all(full.parent().unwrap()).unwrap();
            fs::write(full, content).unwrap();
        }
        git(dir, &["add", "-A"]);
        git(dir, &["commit", "-qm", msg]);
        git(dir, &["rev-parse", "HEAD"])
    }

    fn rm_and_commit(dir: &Path, paths: &[&str], msg: &str) -> String {
        for p in paths {
            git(dir, &["rm", "-rq", p]);
        }
        git(dir, &["commit", "-qm", msg]);
        git(dir, &["rev-parse", "HEAD"])
    }

    fn matches_stacks(p: &str) -> bool {
        p.starts_with("stacks/") && p.ends_with("Pulumi.yaml")
    }

    fn detect(
        dir: &Path,
        interner: &StringInterner,
        base: &str,
        head: &str,
        max: u32,
    ) -> VanishedScan {
        VanishedDetector::new(dir, interner)
            .detect_sync(base, head, matches_stacks, &["stacks".to_string()], max)
            .unwrap()
    }

    fn resolve<'i>(interner: &'i StringInterner, scan: &VanishedScan) -> Vec<(&'i str, String)> {
        scan.vanished
            .iter()
            .map(|v| {
                (
                    interner.resolve(v.path).unwrap(),
                    interner.resolve(v.last_seen_sha).unwrap().to_string(),
                )
            })
            .collect()
    }

    #[test]
    fn test_basic_vanished() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        let add = write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "name: a")], "add");
        let head = rm_and_commit(dir.path(), &["stacks/a"], "remove");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        let got = resolve(&interner, &scan);
        assert_eq!(got, vec![("stacks/a/Pulumi.yaml", add)]);
        assert!(!scan.truncated);
        assert!(scan.anomalies.is_empty());
    }

    #[test]
    fn test_not_vanished_when_alive_at_head() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "v1")], "add");
        let head = write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "v2")], "modify");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        assert!(scan.vanished.is_empty());
    }

    #[test]
    fn test_readded_at_head_not_vanished() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "v1")], "add");
        rm_and_commit(dir.path(), &["stacks/a"], "remove");
        let head = write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "v2")], "re-add");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        assert!(scan.vanished.is_empty());
    }

    #[test]
    fn test_add_remove_readd_remove_uses_newest_deletion() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "v1")], "add1");
        rm_and_commit(dir.path(), &["stacks/a"], "rm1");
        let add2 = write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "v2")], "add2");
        let head = rm_and_commit(dir.path(), &["stacks/a"], "rm2");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        let got = resolve(&interner, &scan);
        // last_seen must be add2 (parent of the NEWEST deletion), content v2
        assert_eq!(got, vec![("stacks/a/Pulumi.yaml", add2)]);
    }

    #[test]
    fn test_rename_within_scope_not_vanished() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(
            dir.path(),
            &[("stacks/a/Pulumi.yaml", "same-content")],
            "add",
        );
        git(dir.path(), &["mv", "stacks/a", "stacks/b"]);
        git(dir.path(), &["commit", "-qm", "rename"]);
        let head = git(dir.path(), &["rev-parse", "HEAD"]);

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        assert!(
            scan.vanished.is_empty(),
            "renamed-and-alive must not vanish"
        );
    }

    #[test]
    fn test_rename_chain_then_delete() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(
            dir.path(),
            &[("stacks/a/Pulumi.yaml", "same-content")],
            "add",
        );
        git(dir.path(), &["mv", "stacks/a", "stacks/b"]);
        git(dir.path(), &["commit", "-qm", "rename"]);
        let renamed = git(dir.path(), &["rev-parse", "HEAD"]);
        let head = rm_and_commit(dir.path(), &["stacks/b"], "remove");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        let got = resolve(&interner, &scan);
        // vanishes under its FINAL name, reconstructable at the rename commit
        assert_eq!(got, vec![("stacks/b/Pulumi.yaml", renamed)]);
    }

    #[test]
    fn test_rename_out_of_scope_not_vanished() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(
            dir.path(),
            &[("stacks/a/Pulumi.yaml", "same-content")],
            "add",
        );
        fs::create_dir_all(dir.path().join("archive")).unwrap();
        git(
            dir.path(),
            &["mv", "stacks/a/Pulumi.yaml", "archive/Pulumi.yaml"],
        );
        git(dir.path(), &["commit", "-qm", "archive"]);
        let head = git(dir.path(), &["rev-parse", "HEAD"]);

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        assert!(
            scan.vanished.is_empty(),
            "rename out of the glob scope is a rename, not a deletion"
        );
    }

    #[test]
    fn test_existed_at_base_excluded() {
        let dir = new_repo();
        let base = write_and_commit(
            dir.path(),
            &[("stacks/a/Pulumi.yaml", "v0"), ("README.md", "r")],
            "base-with-stack",
        );
        rm_and_commit(dir.path(), &["stacks/a"], "rm");
        write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "v1")], "re-add");
        let head = rm_and_commit(dir.path(), &["stacks/a"], "rm-again");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        assert!(
            scan.vanished.is_empty(),
            "existed at base -> endpoint diff reports Deleted; no double report"
        );
    }

    #[test]
    fn test_first_parent_scoping_ignores_side_branch() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        // side branch adds AND deletes a matching file, then merges
        git(dir.path(), &["checkout", "-qb", "side"]);
        write_and_commit(dir.path(), &[("stacks/side/Pulumi.yaml", "s")], "side-add");
        rm_and_commit(dir.path(), &["stacks/side"], "side-rm");
        git(dir.path(), &["checkout", "-q", "main"]);
        write_and_commit(dir.path(), &[("mainline.txt", "m")], "mainline");
        git(
            dir.path(),
            &["merge", "-q", "--no-ff", "-m", "merge side", "side"],
        );
        let head = git(dir.path(), &["rev-parse", "HEAD"]);

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        assert!(
            scan.vanished.is_empty(),
            "side-branch churn never existed on the first-parent mainline"
        );
        // mainline: merge + mainline + base-excluded => walked visits merge and mainline commits only
        assert!(scan.commits_walked <= 3);
    }

    #[test]
    fn test_commit_cap_truncation() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "a")], "add");
        for i in 0..4 {
            write_and_commit(dir.path(), &[("pad.txt", &format!("{i}"))], "pad");
        }
        let head = rm_and_commit(dir.path(), &["stacks/a"], "rm");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 2);
        assert!(scan.truncated);
        // The deletion IS seen (newest commits first) but the Add is beyond
        // the cap -> no vanished entry, plus no anomaly (added_in_range false)
        assert!(scan.vanished.is_empty());
    }

    #[test]
    fn test_base_equals_head_empty() {
        let dir = new_repo();
        let sha = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &sha, &sha, 0);
        assert!(scan.vanished.is_empty());
        assert_eq!(scan.commits_walked, 0);
    }

    #[test]
    fn test_empty_tree_base_initial_push() {
        let dir = new_repo();
        write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "a")], "add");
        let head = rm_and_commit(dir.path(), &["stacks/a"], "rm");

        let interner = StringInterner::new();
        let scan = detect(
            dir.path(),
            &interner,
            ShaResolver::empty_tree_sha(),
            &head,
            0,
        );
        assert_eq!(scan.vanished.len(), 1);
    }

    #[test]
    fn test_missing_base_is_error() {
        let dir = new_repo();
        let head = write_and_commit(dir.path(), &[("README.md", "r")], "only");
        let interner = StringInterner::new();
        let err = VanishedDetector::new(dir.path(), &interner)
            .detect_sync(
                "1111111111111111111111111111111111111111",
                &head,
                matches_stacks,
                &[],
                0,
            )
            .unwrap_err();
        assert!(err.to_string().contains("not found"));
    }

    #[test]
    fn test_glob_filtering_ignores_non_matching() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(dir.path(), &[("docs/temp.md", "d")], "add-doc");
        git(dir.path(), &["rm", "-q", "docs/temp.md"]);
        git(dir.path(), &["commit", "-qm", "rm-doc"]);
        let head = git(dir.path(), &["rev-parse", "HEAD"]);

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        assert!(scan.vanished.is_empty());
    }

    #[test]
    fn test_pathspec_prefixes() {
        assert_eq!(
            pathspec_prefixes(&["stacks/**/Pulumi.yaml"]),
            vec!["stacks".to_string()]
        );
        assert_eq!(
            pathspec_prefixes(&["stacks/**/Pulumi.yaml", "apps/prod/*.yaml"]),
            vec!["stacks".to_string(), "apps/prod".to_string()]
        );
        // A rootless pattern disables filtering entirely (correctness first)
        assert!(pathspec_prefixes(&["*.md"]).is_empty());
        assert!(pathspec_prefixes(&["stacks/**", "*.md"]).is_empty());
        // Dedup
        assert_eq!(
            pathspec_prefixes(&["stacks/a/**", "stacks/b/**"]),
            vec!["stacks/a".to_string(), "stacks/b".to_string()]
        );
    }

    #[test]
    fn test_multiple_vanished_ordered_newest_first() {
        let dir = new_repo();
        let base = write_and_commit(dir.path(), &[("README.md", "r")], "base");
        write_and_commit(dir.path(), &[("stacks/a/Pulumi.yaml", "a")], "add-a");
        let add_b = write_and_commit(dir.path(), &[("stacks/b/Pulumi.yaml", "b")], "add-b");
        let rm_a_parent = add_b.clone(); // a deleted next -> parent is add-b commit
        rm_and_commit(dir.path(), &["stacks/a"], "rm-a");
        let rm_b_parent = git(dir.path(), &["rev-parse", "HEAD"]);
        let head = rm_and_commit(dir.path(), &["stacks/b"], "rm-b");

        let interner = StringInterner::new();
        let scan = detect(dir.path(), &interner, &base, &head, 0);
        let got = resolve(&interner, &scan);
        // b deleted last (newest) -> first in the list
        assert_eq!(
            got,
            vec![
                ("stacks/b/Pulumi.yaml", rm_b_parent),
                ("stacks/a/Pulumi.yaml", rm_a_parent),
            ]
        );
    }
}