Skip to main content

fallow_engine/
changed_files.rs

1//! Changed-file helpers owned by the engine boundary.
2
3use std::path::{Path, PathBuf};
4use std::process::{Command, Output, Stdio};
5use std::sync::OnceLock;
6
7use fallow_types::{
8    output_dead_code::{
9        CircularDependencyFinding, DuplicateExportFinding, DuplicatePropShapeFinding,
10        PackageCycleFinding, PropDrillingChainFinding, ReExportCycleFinding,
11        UnlistedDependencyFinding,
12    },
13    results::{AnalysisResults, SecurityFinding},
14};
15use rustc_hash::FxHashSet;
16
17use crate::duplicates::{self, DuplicationReport};
18
19pub use crate::git_env::{AMBIENT_GIT_ENV_VARS, clear_ambient_git_env};
20
21/// Function pointer signature used to intercept short-running git
22/// subprocesses spawned by changed-file helpers.
23pub type ChangedFilesSpawnHook = fn(&mut std::process::Command) -> std::io::Result<Output>;
24
25static SPAWN_HOOK: OnceLock<ChangedFilesSpawnHook> = OnceLock::new();
26
27/// Classification of a changed-file git failure.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub enum ChangedFilesError {
30    /// Git ref failed validation before invoking `git`.
31    InvalidRef(String),
32    /// `git` binary not found or not executable.
33    GitMissing(String),
34    /// Command ran but the directory is not a git repository.
35    NotARepository,
36    /// Command ran but the ref is invalid or another git error occurred.
37    GitFailed(String),
38}
39
40impl ChangedFilesError {
41    /// Human-readable clause suitable for embedding in an error message.
42    #[must_use]
43    pub fn describe(&self) -> String {
44        match self {
45            Self::InvalidRef(err) => format!("invalid git ref: {err}"),
46            Self::GitMissing(err) => format!("failed to run git: {err}"),
47            Self::NotARepository => "not a git repository".to_owned(),
48            Self::GitFailed(stderr) => augment_git_failed(stderr),
49        }
50    }
51
52    /// Stable kebab-case token naming this failure, for machine consumers.
53    ///
54    /// One token per variant, so a consumer can branch on the cause without
55    /// matching prose. Published on the wire as
56    /// `request_outcomes["changed-since"].reason`.
57    #[must_use]
58    pub const fn reason(&self) -> &'static str {
59        match self {
60            Self::InvalidRef(_) => "invalid-ref",
61            Self::GitMissing(_) => "git-missing",
62            Self::NotARepository => "not-a-repository",
63            Self::GitFailed(_) => "git-failed",
64        }
65    }
66
67    /// The full sentence for a run whose `--changed-since` request could not be
68    /// applied: what was asked, what happened instead, and the next step.
69    ///
70    /// The one prose source for this fact. The CLI writes it to stderr and
71    /// publishes the same string on the envelope, so a log a human read and a
72    /// report a script read cannot say different things.
73    ///
74    /// Git's own stderr is folded onto one line: the sentence travels into a
75    /// jq-rendered CI annotation and into a rendered pull-request comment, and
76    /// a multi-line value there splits one fact across two log records.
77    #[must_use]
78    pub fn changed_since_message(&self, git_ref: &str) -> String {
79        let cause = self
80            .describe()
81            .split_whitespace()
82            .collect::<Vec<_>>()
83            .join(" ");
84        format!(
85            "--changed-since '{git_ref}' was ignored because {cause}, so this report covers \
86             the whole project instead of the changed files. {}",
87            self.changed_since_remedy()
88        )
89    }
90
91    /// The next step for [`Self::changed_since_message`], one per cause.
92    const fn changed_since_remedy(&self) -> &'static str {
93        match self {
94            Self::InvalidRef(_) => {
95                "Pass a ref git can resolve, such as a branch name or a commit sha."
96            }
97            Self::GitMissing(_) => {
98                "Install git and make it available on PATH, or drop --changed-since."
99            }
100            Self::NotARepository => {
101                "Run fallow from inside the repository, or drop --changed-since."
102            }
103            Self::GitFailed(_) => {
104                "Verify the ref exists in this repository, and check out with full history."
105            }
106        }
107    }
108}
109
110fn augment_git_failed(stderr: &str) -> String {
111    let lower = stderr.to_ascii_lowercase();
112    if lower.contains("not a valid object name")
113        || lower.contains("unknown revision")
114        || lower.contains("ambiguous argument")
115    {
116        format!(
117            "{stderr} (shallow clone? try `git fetch --unshallow`, or set `fetch-depth: 0` on actions/checkout / `GIT_DEPTH: 0` in GitLab CI)"
118        )
119    } else {
120        stderr.to_owned()
121    }
122}
123
124/// Install a spawn-hook for changed-file git subprocesses.
125pub fn set_spawn_hook(hook: ChangedFilesSpawnHook) {
126    let _ = SPAWN_HOOK.set(hook);
127}
128
129/// Validate a user-supplied git ref before passing it to git.
130pub(crate) fn validate_git_ref(s: &str) -> Result<&str, String> {
131    if s.is_empty() {
132        return Err("git ref cannot be empty".to_string());
133    }
134    if s.starts_with('-') {
135        return Err("git ref cannot start with '-'".to_string());
136    }
137    let mut in_braces = false;
138    for c in s.chars() {
139        match c {
140            '{' => in_braces = true,
141            '}' => in_braces = false,
142            ':' | ' ' if in_braces => {}
143            c if c.is_ascii_alphanumeric()
144                || matches!(c, '.' | '_' | '-' | '/' | '~' | '^' | '@' | '{' | '}') => {}
145            _ => return Err(format!("git ref contains disallowed character: '{c}'")),
146        }
147    }
148    if in_braces {
149        return Err("git ref has unclosed '{'".to_string());
150    }
151    Ok(s)
152}
153
154/// Resolve the canonical git toplevel for `cwd`.
155pub fn resolve_git_toplevel(cwd: &Path) -> Result<PathBuf, ChangedFilesError> {
156    let output = spawn_output(&mut git_command(cwd, &["rev-parse", "--show-toplevel"]))
157        .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
158
159    if !output.status.success() {
160        let stderr = String::from_utf8_lossy(&output.stderr);
161        return Err(if stderr.contains("not a git repository") {
162            ChangedFilesError::NotARepository
163        } else {
164            ChangedFilesError::GitFailed(stderr.trim().to_owned())
165        });
166    }
167
168    let raw = String::from_utf8_lossy(&output.stdout);
169    let trimmed = raw.trim();
170    if trimmed.is_empty() {
171        return Err(ChangedFilesError::GitFailed(
172            "git rev-parse --show-toplevel returned empty output".to_owned(),
173        ));
174    }
175
176    let path = PathBuf::from(trimmed);
177    Ok(dunce::canonicalize(&path).unwrap_or(path))
178}
179
180/// Resolve the canonical git common directory for `cwd`.
181pub fn resolve_git_common_dir(cwd: &Path) -> Result<PathBuf, ChangedFilesError> {
182    let output = spawn_output(&mut git_command(
183        cwd,
184        &["rev-parse", "--path-format=absolute", "--git-common-dir"],
185    ))
186    .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
187
188    if !output.status.success() {
189        let stderr = String::from_utf8_lossy(&output.stderr);
190        return Err(if stderr.contains("not a git repository") {
191            ChangedFilesError::NotARepository
192        } else {
193            ChangedFilesError::GitFailed(stderr.trim().to_owned())
194        });
195    }
196
197    let raw = String::from_utf8_lossy(&output.stdout);
198    let trimmed = raw.trim();
199    if trimmed.is_empty() {
200        return Err(ChangedFilesError::GitFailed(
201            "git rev-parse --git-common-dir returned empty output".to_owned(),
202        ));
203    }
204
205    let path = PathBuf::from(trimmed);
206    Ok(dunce::canonicalize(&path).unwrap_or(path))
207}
208
209/// Resolve the canonical git common directory and toplevel for `cwd` with
210/// one `git rev-parse` call.
211///
212/// The result equals [`resolve_git_common_dir`] and [`resolve_git_toplevel`],
213/// at the cost of one subprocess instead of two. It fails where either single
214/// probe fails, for example in a bare repository, which has no toplevel, and
215/// when git prints anything other than two paths. A caller that needs one of
216/// the two paths in that case calls the single probe.
217///
218/// # Errors
219///
220/// Returns the reason when git is missing, `cwd` is not in a work tree, or the
221/// output is not two paths.
222pub fn resolve_git_common_dir_and_toplevel(
223    cwd: &Path,
224) -> Result<(PathBuf, PathBuf), ChangedFilesError> {
225    let output = spawn_output(&mut git_command(
226        cwd,
227        &[
228            "rev-parse",
229            "--path-format=absolute",
230            "--git-common-dir",
231            "--show-toplevel",
232        ],
233    ))
234    .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
235
236    if !output.status.success() {
237        let stderr = String::from_utf8_lossy(&output.stderr);
238        return Err(if stderr.contains("not a git repository") {
239            ChangedFilesError::NotARepository
240        } else {
241            ChangedFilesError::GitFailed(stderr.trim().to_owned())
242        });
243    }
244
245    let raw = String::from_utf8_lossy(&output.stdout);
246    let mut lines = raw.lines();
247    let (Some(common), Some(toplevel), None) = (lines.next(), lines.next(), lines.next()) else {
248        return Err(ChangedFilesError::GitFailed(
249            "git rev-parse did not print one common dir and one toplevel".to_owned(),
250        ));
251    };
252    let canonical = |line: &str| {
253        let path = PathBuf::from(line.trim());
254        dunce::canonicalize(&path).unwrap_or(path)
255    };
256    if common.trim().is_empty() || toplevel.trim().is_empty() {
257        return Err(ChangedFilesError::GitFailed(
258            "git rev-parse returned an empty path".to_owned(),
259        ));
260    }
261    Ok((canonical(common), canonical(toplevel)))
262}
263
264/// Get files changed since a git ref.
265fn try_get_changed_files(
266    root: &Path,
267    git_ref: &str,
268) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
269    validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
270    let toplevel = resolve_git_toplevel(root)?;
271    try_get_changed_files_with_toplevel(root, &toplevel, git_ref)
272}
273
274/// Resolve changed files for a git ref relative to a project root.
275///
276/// # Errors
277///
278/// Returns an error when git cannot resolve the ref or repository state.
279pub fn changed_files(root: &Path, git_ref: &str) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
280    try_get_changed_files(root, git_ref)
281}
282
283/// Resolve several refs against one working tree. The HEAD diff and untracked
284/// inventory do not depend on the ref, so a package-baseline run reads them
285/// once while still resolving each ref's committed diff independently.
286pub(crate) struct ChangedFilesBatch<'a> {
287    cwd: &'a Path,
288    toplevel: PathBuf,
289    working_tree: Option<FxHashSet<PathBuf>>,
290}
291
292impl<'a> ChangedFilesBatch<'a> {
293    pub(crate) fn new(cwd: &'a Path, first_ref: &str) -> Result<Self, ChangedFilesError> {
294        validate_git_ref(first_ref).map_err(ChangedFilesError::InvalidRef)?;
295        Ok(Self {
296            cwd,
297            toplevel: resolve_git_toplevel(cwd)?,
298            working_tree: None,
299        })
300    }
301
302    pub(crate) fn changed_files(
303        &mut self,
304        git_ref: &str,
305    ) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
306        let mut files = committed_changed_files(self.cwd, &self.toplevel, git_ref)?;
307        if self.working_tree.is_none() {
308            self.working_tree = Some(working_tree_changed_files(self.cwd, &self.toplevel)?);
309        }
310        if let Some(working_tree) = &self.working_tree {
311            files.extend(working_tree.iter().cloned());
312        }
313        Ok(files)
314    }
315}
316
317/// Get changed files and the git toplevel used to resolve them.
318pub fn try_get_changed_files_with_toplevel(
319    cwd: &Path,
320    toplevel: &Path,
321    git_ref: &str,
322) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
323    let mut files = committed_changed_files(cwd, toplevel, git_ref)?;
324    files.extend(working_tree_changed_files(cwd, toplevel)?);
325    Ok(files)
326}
327
328fn committed_changed_files(
329    cwd: &Path,
330    toplevel: &Path,
331    git_ref: &str,
332) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
333    validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
334    collect_git_paths(
335        cwd,
336        toplevel,
337        &[
338            "diff",
339            "--name-only",
340            "-z",
341            "--end-of-options",
342            &format!("{git_ref}...HEAD"),
343        ],
344    )
345}
346
347fn working_tree_changed_files(
348    cwd: &Path,
349    toplevel: &Path,
350) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
351    let mut files = collect_git_paths(cwd, toplevel, &["diff", "--name-only", "-z", "HEAD"])?;
352    files.extend(collect_git_paths(
353        cwd,
354        toplevel,
355        &[
356            "ls-files",
357            "--full-name",
358            "--others",
359            "--exclude-standard",
360            "-z",
361        ],
362    )?);
363    Ok(files)
364}
365
366/// A file rename detected between a git ref's merge base and the working
367/// tree, with absolute paths joined onto the git toplevel.
368#[derive(Debug, Clone, PartialEq, Eq)]
369pub struct RenamedFile {
370    /// Absolute pre-rename path (exists in the base tree).
371    pub from: PathBuf,
372    /// Absolute post-rename path (exists in the current tree).
373    pub to: PathBuf,
374}
375
376/// Detect renamed files between a git ref's merge base and the current tree.
377///
378/// Covers the committed range (`<ref>...HEAD`) plus renames staged against
379/// `HEAD`, mirroring the tracked scope of [`try_get_changed_files_with_toplevel`].
380/// A rename committed as `a -> b` and then staged as `b -> c` is composed into
381/// one `a -> c` entry. Copies are intentionally not reported: a copy leaves
382/// the original in place, so findings on the copy are genuinely new.
383///
384/// # Errors
385///
386/// Returns an error when git cannot resolve the ref or repository state.
387pub fn try_get_renamed_files(
388    root: &Path,
389    git_ref: &str,
390) -> Result<Vec<RenamedFile>, ChangedFilesError> {
391    validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
392    let toplevel = resolve_git_toplevel(root)?;
393    let mut renames = collect_git_rename_pairs(
394        root,
395        &toplevel,
396        &[
397            "diff",
398            "--name-status",
399            "-z",
400            "--find-renames",
401            "--end-of-options",
402            &format!("{git_ref}...HEAD"),
403        ],
404    )?;
405    let staged = collect_git_rename_pairs(
406        root,
407        &toplevel,
408        &["diff", "--name-status", "-z", "--find-renames", "HEAD"],
409    )?;
410    for pair in staged {
411        if let Some(chained) = renames.iter_mut().find(|rename| rename.to == pair.from) {
412            chained.to = pair.to;
413        } else {
414            renames.push(pair);
415        }
416    }
417    Ok(renames)
418}
419
420/// Run a `--name-status -z` diff and collect its `R` (rename) pairs.
421///
422/// The `-z` stream alternates status and path fields separated by NUL; rename
423/// and copy statuses (`R<score>` / `C<score>`) carry two path fields (old then
424/// new), every other status carries one.
425fn collect_git_rename_pairs(
426    cwd: &Path,
427    toplevel: &Path,
428    args: &[&str],
429) -> Result<Vec<RenamedFile>, ChangedFilesError> {
430    let output = spawn_output(&mut git_command(cwd, args))
431        .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
432
433    if !output.status.success() {
434        return Err(changed_files_error_from_output(&output));
435    }
436
437    let mut fields = output
438        .stdout
439        .split(|byte| *byte == 0)
440        .filter(|field| !field.is_empty());
441    let mut renames = Vec::new();
442    while let Some(status) = fields.next() {
443        let Some(first_path) = fields.next() else {
444            break;
445        };
446        match status.first() {
447            Some(b'R') => {
448                let Some(second_path) = fields.next() else {
449                    break;
450                };
451                renames.push(RenamedFile {
452                    from: toplevel.join(git_path_from_bytes(first_path)),
453                    to: toplevel.join(git_path_from_bytes(second_path)),
454                });
455            }
456            // Copies also carry two path fields but are conservatively treated
457            // as new files, so only the extra field is consumed.
458            Some(b'C') => {
459                let _ = fields.next();
460            }
461            _ => {}
462        }
463    }
464    Ok(renames)
465}
466
467/// Return the raw git diff from a ref's merge base through the working tree.
468///
469/// The result includes committed, staged, unstaged, and untracked changes so it
470/// covers the same scope as `try_get_changed_files`.
471pub fn try_get_changed_diff(root: &Path, git_ref: &str) -> Result<String, ChangedFilesError> {
472    validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
473    let toplevel = resolve_git_toplevel(root)?;
474    let merge_base_output = spawn_output(&mut git_command(root, &["merge-base", git_ref, "HEAD"]))
475        .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
476    if !merge_base_output.status.success() {
477        return Err(changed_files_error_from_output(&merge_base_output));
478    }
479    let merge_base = String::from_utf8_lossy(&merge_base_output.stdout)
480        .trim()
481        .to_owned();
482    if merge_base.is_empty() {
483        return Err(ChangedFilesError::GitFailed(
484            "git merge-base returned empty output".to_owned(),
485        ));
486    }
487
488    let output = spawn_output(&mut git_command(
489        root,
490        &[
491            "diff",
492            "--relative",
493            "--unified=0",
494            "--end-of-options",
495            &merge_base,
496        ],
497    ))
498    .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
499
500    if !output.status.success() {
501        return Err(changed_files_error_from_output(&output));
502    }
503
504    let mut diff = String::from_utf8_lossy(&output.stdout).into_owned();
505    append_untracked_diffs(root, &toplevel, &mut diff)?;
506    Ok(diff)
507}
508
509fn append_untracked_diffs(
510    root: &Path,
511    toplevel: &Path,
512    diff: &mut String,
513) -> Result<(), ChangedFilesError> {
514    let canonical_root = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
515    let mut untracked: Vec<PathBuf> = collect_git_paths(
516        root,
517        toplevel,
518        &[
519            "ls-files",
520            "--full-name",
521            "--others",
522            "--exclude-standard",
523            "-z",
524        ],
525    )?
526    .into_iter()
527    .filter_map(|path| {
528        path.strip_prefix(&canonical_root)
529            .ok()
530            .map(Path::to_path_buf)
531    })
532    .collect();
533    untracked.sort_unstable();
534
535    #[cfg(windows)]
536    let empty_file = "NUL";
537    #[cfg(not(windows))]
538    let empty_file = "/dev/null";
539
540    for path in untracked {
541        let mut command = git_command(root, &["diff", "--no-index", "--unified=0", "--"]);
542        command.arg(empty_file).arg(untracked_path_arg(&path));
543        let output =
544            spawn_output(&mut command).map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
545        if !output.status.success() && output.status.code() != Some(1) {
546            return Err(changed_files_error_from_output(&output));
547        }
548        if !diff.is_empty() && !diff.ends_with('\n') {
549            diff.push('\n');
550        }
551        diff.push_str(&String::from_utf8_lossy(&output.stdout));
552    }
553    Ok(())
554}
555
556/// Forward-slashed relative path for the `git diff --no-index` argument.
557/// Passing the native form on Windows makes git echo backslashes into the
558/// `+++ b/` header, so every consumer keyed on forward-slashed diff paths
559/// (added-line lookups included) silently misses the file.
560fn untracked_path_arg(path: &Path) -> String {
561    path.to_string_lossy().replace('\\', "/")
562}
563
564fn changed_files_error_from_output(output: &Output) -> ChangedFilesError {
565    let stderr = String::from_utf8_lossy(&output.stderr);
566    if stderr.contains("not a git repository") {
567        ChangedFilesError::NotARepository
568    } else {
569        ChangedFilesError::GitFailed(stderr.trim().to_owned())
570    }
571}
572
573/// Get changed files if git can resolve them, otherwise return `None`.
574#[must_use]
575#[expect(
576    clippy::print_stderr,
577    reason = "intentional user-facing warning for the CLI's --changed-since fallback path; typed callers use try_get_changed_files instead"
578)]
579pub fn get_changed_files(root: &Path, git_ref: &str) -> Option<FxHashSet<PathBuf>> {
580    match try_get_changed_files(root, git_ref) {
581        Ok(files) => Some(files),
582        Err(err) => {
583            eprintln!("Warning: {}", err.changed_since_message(git_ref));
584            None
585        }
586    }
587}
588
589fn spawn_output(command: &mut Command) -> std::io::Result<Output> {
590    if let Some(hook) = SPAWN_HOOK.get() {
591        hook(command)
592    } else {
593        command.output()
594    }
595}
596
597fn collect_git_paths(
598    cwd: &Path,
599    toplevel: &Path,
600    args: &[&str],
601) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
602    let output = spawn_output(&mut git_command(cwd, args))
603        .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
604
605    if !output.status.success() {
606        let stderr = String::from_utf8_lossy(&output.stderr);
607        return Err(if stderr.contains("not a git repository") {
608            ChangedFilesError::NotARepository
609        } else {
610            ChangedFilesError::GitFailed(stderr.trim().to_owned())
611        });
612    }
613
614    let files = output
615        .stdout
616        .split(|byte| *byte == 0)
617        .filter(|path| !path.is_empty())
618        .map(git_path_from_bytes)
619        .map(|path| toplevel.join(path))
620        .collect();
621
622    Ok(files)
623}
624
625/// Decode one NUL-separated `git` output path into a `PathBuf`.
626///
627/// Unix keeps the raw bytes, so a non-UTF-8 tree path survives intact.
628/// Shared with `crate::churn`, which decodes the same `git` byte output under
629/// the same semantics.
630#[cfg(unix)]
631pub(crate) fn git_path_from_bytes(path: &[u8]) -> PathBuf {
632    use std::ffi::OsString;
633    use std::os::unix::ffi::OsStringExt;
634
635    PathBuf::from(OsString::from_vec(path.to_vec()))
636}
637
638/// Windows counterpart: there is no byte-oriented `OsString`, so decode
639/// lossily and rewrite to backslash separators.
640#[cfg(windows)]
641pub(crate) fn git_path_from_bytes(path: &[u8]) -> PathBuf {
642    PathBuf::from(String::from_utf8_lossy(path).replace('/', "\\"))
643}
644
645#[expect(
646    clippy::disallowed_methods,
647    reason = "canonical engine-owned git spawn wrapper for changed-file orchestration"
648)]
649fn git_command(cwd: &Path, args: &[&str]) -> Command {
650    let mut command = Command::new("git");
651    clear_ambient_git_env(&mut command);
652    // Changed-file probes are non-interactive and must not inherit protocol stdin.
653    command.stdin(Stdio::null()).args(args).current_dir(cwd);
654    command
655}
656
657/// Scope dead-code results to findings affected by changed files.
658///
659/// Dependency-level issues stay unfiltered because whether a dependency is
660/// unused is a graph-global fact, not a changed-file-local fact.
661#[expect(
662    clippy::implicit_hasher,
663    reason = "fallow standardizes on FxHashSet across the workspace"
664)]
665pub fn filter_results_by_changed_files(
666    results: &mut AnalysisResults,
667    changed_files: &FxHashSet<PathBuf>,
668) {
669    filter_results_by_path_scope(results, &NormalizedChangedFiles::new(changed_files));
670}
671
672/// Path membership for one resolved result scope. Implementations own path
673/// normalization so source and manifest findings use the same policy.
674pub(crate) trait ChangedPathScope {
675    fn contains(&self, path: &Path) -> bool;
676    fn contains_workspace(&self, path: &Path) -> bool {
677        self.contains(path)
678    }
679}
680
681/// A changed-file set normalized the way every changed-file filter reads it.
682#[derive(Debug, Clone, Default)]
683pub(crate) struct NormalizedChangedFiles(FxHashSet<PathBuf>);
684
685impl NormalizedChangedFiles {
686    pub(crate) fn new(changed_files: &FxHashSet<PathBuf>) -> Self {
687        Self(normalize_changed_files_set(changed_files))
688    }
689}
690
691impl ChangedPathScope for NormalizedChangedFiles {
692    fn contains(&self, path: &Path) -> bool {
693        contains_normalized(&self.0, path)
694    }
695
696    fn contains_workspace(&self, path: &Path) -> bool {
697        normalized_set_contains_path(&self.0, path)
698    }
699}
700
701pub(crate) fn filter_results_by_path_scope(
702    results: &mut AnalysisResults,
703    scope: &impl ChangedPathScope,
704) {
705    classify_changed_file_filter_fields(results);
706    retain_basic_issue_findings_by_changed_path(results, scope);
707    retain_graph_findings_by_changed_files(results, scope);
708    retain_boundary_policy_and_suppression_findings(results, scope);
709    retain_security_and_workspace_findings(results, scope);
710    retain_framework_findings_by_changed_files(results, scope);
711}
712
713fn classify_changed_file_filter_fields(results: &AnalysisResults) {
714    let AnalysisResults {
715        unused_files: _unused_files,
716        unused_exports: _unused_exports,
717        unused_types: _unused_types,
718        private_type_leaks: _private_type_leaks,
719        deprecated_exports_in_use: _deprecated_exports_in_use,
720        unused_dependencies: _unused_dependencies,
721        unused_dev_dependencies: _unused_dev_dependencies,
722        unused_optional_dependencies: _unused_optional_dependencies,
723        unused_enum_members: _unused_enum_members,
724        unused_class_members: _unused_class_members,
725        unused_store_members: _unused_store_members,
726        unresolved_imports: _unresolved_imports,
727        unlisted_dependencies: _unlisted_dependencies,
728        duplicate_exports: _duplicate_exports,
729        type_only_dependencies: _type_only_dependencies,
730        test_only_dependencies: _test_only_dependencies,
731        dev_dependencies_in_production: _dev_dependencies_in_production,
732        circular_dependencies: _circular_dependencies,
733        re_export_cycles: _re_export_cycles,
734        package_cycles: _package_cycles,
735        boundary_violations: _boundary_violations,
736        boundary_coverage_violations: _boundary_coverage_violations,
737        boundary_call_violations: _boundary_call_violations,
738        policy_violations: _policy_violations,
739        stale_suppressions: _stale_suppressions,
740        unused_catalog_entries: _unused_catalog_entries,
741        empty_catalog_groups: _empty_catalog_groups,
742        unresolved_catalog_references: _unresolved_catalog_references,
743        unused_dependency_overrides: _unused_dependency_overrides,
744        misconfigured_dependency_overrides: _misconfigured_dependency_overrides,
745        invalid_client_exports: _invalid_client_exports,
746        mixed_client_server_barrels: _mixed_client_server_barrels,
747        misplaced_directives: _misplaced_directives,
748        unprovided_injects: _unprovided_injects,
749        unrendered_components: _unrendered_components,
750        route_collisions: _route_collisions,
751        dynamic_segment_name_conflicts: _dynamic_segment_name_conflicts,
752        unused_component_props: _unused_component_props,
753        absent_component_props: _absent_component_props,
754        unused_component_emits: _unused_component_emits,
755        unused_component_inputs: _unused_component_inputs,
756        unused_component_outputs: _unused_component_outputs,
757        unused_svelte_events: _unused_svelte_events,
758        unused_server_actions: _unused_server_actions,
759        unused_load_data_keys: _unused_load_data_keys,
760        unused_load_data_keys_global_abstain: _unused_load_data_keys_global_abstain,
761        prop_drilling_chains: _prop_drilling_chains,
762        thin_wrappers: _thin_wrappers,
763        duplicate_prop_shapes: _duplicate_prop_shapes,
764        cascade_hidden: _cascade_hidden,
765        cascade: _cascade,
766        suppression_count: _suppression_count,
767        unused_component_props_exempted: _unused_component_props_exempted,
768        active_suppressions: _active_suppressions,
769        feature_flags: _feature_flags,
770        security_findings: _security_findings,
771        security_unresolved_edge_files: _security_unresolved_edge_files,
772        security_unresolved_callee_sites: _security_unresolved_callee_sites,
773        security_unresolved_callee_diagnostics: _security_unresolved_callee_diagnostics,
774        export_usages: _export_usages,
775        entry_point_summary: _entry_point_summary,
776        render_fan_in: _render_fan_in,
777        react_component_intel: _react_component_intel,
778        semantic_framework_contracts: _semantic_framework_contracts,
779    } = results;
780}
781
782fn retain_basic_issue_findings_by_changed_path(
783    results: &mut AnalysisResults,
784    scope: &impl ChangedPathScope,
785) {
786    retain_by_changed_path(&mut results.unused_files, scope, |f| &f.file.path);
787    retain_by_changed_path(&mut results.unused_exports, scope, |e| &e.export.path);
788    retain_by_changed_path(&mut results.unused_types, scope, |e| &e.export.path);
789    retain_by_changed_path(&mut results.private_type_leaks, scope, |e| &e.leak.path);
790    retain_by_changed_path(&mut results.deprecated_exports_in_use, scope, |e| {
791        &e.export.path
792    });
793    retain_by_changed_path(&mut results.unused_enum_members, scope, |m| &m.member.path);
794    retain_by_changed_path(&mut results.unused_class_members, scope, |m| &m.member.path);
795    retain_by_changed_path(&mut results.unused_store_members, scope, |m| &m.member.path);
796    retain_by_changed_path(&mut results.unresolved_imports, scope, |i| &i.import.path);
797}
798
799fn retain_graph_findings_by_changed_files(
800    results: &mut AnalysisResults,
801    scope: &impl ChangedPathScope,
802) {
803    retain_unlisted_dependencies_by_import_site(&mut results.unlisted_dependencies, scope);
804    retain_duplicate_exports_by_changed_locations(&mut results.duplicate_exports, scope);
805    retain_circular_dependencies_by_changed_file(&mut results.circular_dependencies, scope);
806    retain_re_export_cycles_by_changed_file(&mut results.re_export_cycles, scope);
807    retain_package_cycles_by_changed_file(&mut results.package_cycles, scope);
808}
809
810fn retain_boundary_policy_and_suppression_findings(
811    results: &mut AnalysisResults,
812    scope: &impl ChangedPathScope,
813) {
814    retain_by_changed_path(&mut results.boundary_violations, scope, |v| {
815        &v.violation.from_path
816    });
817    retain_by_changed_path(&mut results.boundary_coverage_violations, scope, |v| {
818        &v.violation.path
819    });
820    retain_by_changed_path(&mut results.boundary_call_violations, scope, |v| {
821        &v.violation.path
822    });
823    retain_by_changed_path(&mut results.policy_violations, scope, |v| &v.violation.path);
824    retain_by_changed_path(&mut results.stale_suppressions, scope, |s| &s.path);
825}
826
827fn retain_security_and_workspace_findings(
828    results: &mut AnalysisResults,
829    scope: &impl ChangedPathScope,
830) {
831    retain_security_findings_by_changed_path(&mut results.security_findings, scope);
832    retain_by_changed_path(
833        &mut results.security_unresolved_callee_diagnostics,
834        scope,
835        |d| &d.path,
836    );
837    retain_by_changed_path(&mut results.unresolved_catalog_references, scope, |r| {
838        &r.reference.path
839    });
840    results
841        .empty_catalog_groups
842        .retain(|g| scope.contains_workspace(&g.group.path));
843    retain_by_changed_path(&mut results.unused_dependency_overrides, scope, |o| {
844        &o.entry.path
845    });
846    retain_by_changed_path(
847        &mut results.misconfigured_dependency_overrides,
848        scope,
849        |o| &o.entry.path,
850    );
851}
852
853fn retain_framework_findings_by_changed_files(
854    results: &mut AnalysisResults,
855    scope: &impl ChangedPathScope,
856) {
857    retain_client_boundary_findings_by_changed_files(results, scope);
858    retain_component_contract_findings_by_changed_files(results, scope);
859    retain_react_health_findings_by_changed_files(results, scope);
860    retain_nextjs_findings_by_changed_files(results, scope);
861}
862
863fn retain_client_boundary_findings_by_changed_files(
864    results: &mut AnalysisResults,
865    scope: &impl ChangedPathScope,
866) {
867    let AnalysisResults {
868        invalid_client_exports,
869        mixed_client_server_barrels,
870        misplaced_directives,
871        ..
872    } = results;
873
874    retain_by_changed_path(invalid_client_exports, scope, |e| &e.export.path);
875    retain_by_changed_path(mixed_client_server_barrels, scope, |b| &b.barrel.path);
876    retain_by_changed_path(misplaced_directives, scope, |d| &d.directive_site.path);
877}
878
879fn retain_component_contract_findings_by_changed_files(
880    results: &mut AnalysisResults,
881    scope: &impl ChangedPathScope,
882) {
883    let AnalysisResults {
884        unprovided_injects,
885        unrendered_components,
886        unused_component_props,
887        absent_component_props,
888        unused_component_emits,
889        unused_component_inputs,
890        unused_component_outputs,
891        unused_svelte_events,
892        unused_server_actions,
893        unused_load_data_keys,
894        ..
895    } = results;
896
897    retain_by_changed_path(unprovided_injects, scope, |i| &i.inject.path);
898    retain_by_changed_path(unrendered_components, scope, |c| &c.component.path);
899    retain_by_changed_path(unused_component_props, scope, |p| &p.prop.path);
900    retain_by_changed_path(absent_component_props, scope, |p| &p.prop.path);
901    retain_by_changed_path(unused_component_emits, scope, |e| &e.emit.path);
902    retain_by_changed_path(unused_component_inputs, scope, |i| &i.input.path);
903    retain_by_changed_path(unused_component_outputs, scope, |o| &o.output.path);
904    retain_by_changed_path(unused_svelte_events, scope, |e| &e.event.path);
905    retain_by_changed_path(unused_server_actions, scope, |a| &a.action.path);
906    retain_by_changed_path(unused_load_data_keys, scope, |k| &k.key.path);
907}
908
909fn retain_react_health_findings_by_changed_files(
910    results: &mut AnalysisResults,
911    scope: &impl ChangedPathScope,
912) {
913    let AnalysisResults {
914        prop_drilling_chains,
915        thin_wrappers,
916        duplicate_prop_shapes,
917        ..
918    } = results;
919
920    retain_prop_drilling_chains_by_anchor(prop_drilling_chains, scope);
921    retain_by_changed_path(thin_wrappers, scope, |w| &w.wrapper.file);
922    retain_duplicate_prop_shapes_by_anchor(duplicate_prop_shapes, scope);
923}
924
925fn retain_nextjs_findings_by_changed_files(
926    results: &mut AnalysisResults,
927    scope: &impl ChangedPathScope,
928) {
929    let AnalysisResults {
930        route_collisions,
931        dynamic_segment_name_conflicts,
932        ..
933    } = results;
934
935    retain_by_changed_path(route_collisions, scope, |c| &c.collision.path);
936    retain_by_changed_path(dynamic_segment_name_conflicts, scope, |c| &c.conflict.path);
937}
938
939fn retain_unlisted_dependencies_by_import_site(
940    dependencies: &mut Vec<UnlistedDependencyFinding>,
941    scope: &impl ChangedPathScope,
942) {
943    dependencies.retain(|dependency| {
944        dependency
945            .dep
946            .imported_from
947            .iter()
948            .any(|site| scope.contains(&site.path))
949    });
950}
951
952fn retain_duplicate_exports_by_changed_locations(
953    duplicate_exports: &mut Vec<DuplicateExportFinding>,
954    scope: &impl ChangedPathScope,
955) {
956    for duplicate in &mut *duplicate_exports {
957        duplicate
958            .export
959            .locations
960            .retain(|location| scope.contains(&location.path));
961    }
962    duplicate_exports.retain(|duplicate| duplicate.export.locations.len() >= 2);
963}
964
965fn retain_circular_dependencies_by_changed_file(
966    cycles: &mut Vec<CircularDependencyFinding>,
967    scope: &impl ChangedPathScope,
968) {
969    cycles.retain(|cycle| cycle.cycle.files.iter().any(|file| scope.contains(file)));
970}
971
972fn retain_re_export_cycles_by_changed_file(
973    cycles: &mut Vec<ReExportCycleFinding>,
974    scope: &impl ChangedPathScope,
975) {
976    cycles.retain(|cycle| cycle.cycle.files.iter().any(|file| scope.contains(file)));
977}
978
979fn retain_package_cycles_by_changed_file(
980    cycles: &mut Vec<PackageCycleFinding>,
981    scope: &impl ChangedPathScope,
982) {
983    cycles.retain(|cycle| {
984        cycle
985            .cycle
986            .edges
987            .iter()
988            .any(|edge| scope.contains(&edge.path))
989    });
990}
991
992fn retain_security_findings_by_changed_path(
993    findings: &mut Vec<SecurityFinding>,
994    scope: &impl ChangedPathScope,
995) {
996    findings.retain(|finding| security_finding_touches_changed_path(finding, scope));
997}
998
999fn retain_prop_drilling_chains_by_anchor(
1000    chains: &mut Vec<PropDrillingChainFinding>,
1001    scope: &impl ChangedPathScope,
1002) {
1003    chains.retain(|chain| {
1004        chain
1005            .chain
1006            .hops
1007            .first()
1008            .is_some_and(|hop| scope.contains(&hop.file))
1009    });
1010}
1011
1012fn retain_duplicate_prop_shapes_by_anchor(
1013    shapes: &mut Vec<DuplicatePropShapeFinding>,
1014    scope: &impl ChangedPathScope,
1015) {
1016    retain_by_changed_path(shapes, scope, |shape| &shape.shape.file);
1017}
1018
1019fn retain_by_changed_path<T>(
1020    items: &mut Vec<T>,
1021    scope: &impl ChangedPathScope,
1022    path: impl Fn(&T) -> &Path,
1023) {
1024    items.retain(|item| scope.contains(path(item)));
1025}
1026
1027fn security_finding_touches_changed_path(
1028    finding: &SecurityFinding,
1029    scope: &impl ChangedPathScope,
1030) -> bool {
1031    scope.contains(&finding.path)
1032        || finding.trace.iter().any(|hop| scope.contains(&hop.path))
1033        || finding.reachability.as_ref().is_some_and(|reachability| {
1034            reachability
1035                .untrusted_source_trace
1036                .iter()
1037                .any(|hop| scope.contains(&hop.path))
1038        })
1039}
1040
1041fn normalize_changed_files_set(changed_files: &FxHashSet<PathBuf>) -> FxHashSet<PathBuf> {
1042    changed_files
1043        .iter()
1044        .map(|p| dunce::simplified(p).to_path_buf())
1045        .collect()
1046}
1047
1048fn contains_normalized(normalized: &FxHashSet<PathBuf>, path: &Path) -> bool {
1049    normalized.contains(dunce::simplified(path))
1050}
1051
1052fn normalized_set_contains_path(normalized: &FxHashSet<PathBuf>, path: &Path) -> bool {
1053    contains_normalized(normalized, path)
1054        || (path.is_relative() && normalized.iter().any(|changed| changed.ends_with(path)))
1055}
1056
1057/// Scope duplication groups to clone groups touching at least one changed file.
1058#[expect(
1059    clippy::implicit_hasher,
1060    reason = "fallow standardizes on FxHashSet across the workspace"
1061)]
1062pub fn filter_duplication_by_changed_files(
1063    report: &mut DuplicationReport,
1064    changed_files: &FxHashSet<PathBuf>,
1065    root: &Path,
1066) {
1067    filter_duplication_by_path_scope(report, &NormalizedChangedFiles::new(changed_files), root);
1068}
1069
1070pub(crate) fn filter_duplication_by_path_scope(
1071    report: &mut DuplicationReport,
1072    scope: &impl ChangedPathScope,
1073    root: &Path,
1074) {
1075    report.clone_groups.retain(|group| {
1076        group
1077            .instances
1078            .iter()
1079            .any(|instance| scope.contains(&instance.file))
1080    });
1081    duplicates::refresh_clone_families(report, root);
1082    report.stats = duplicates::recompute_stats(report);
1083}
1084
1085#[cfg(test)]
1086mod tests {
1087    use super::*;
1088    use fallow_types::{
1089        duplicates::{CloneGroup, CloneInstance, DuplicationStats},
1090        output_dead_code::{
1091            EmptyCatalogGroupFinding, UnusedDependencyFinding, UnusedExportFinding,
1092            UnusedFileFinding,
1093        },
1094        results::{
1095            DependencyLocation, EmptyCatalogGroup, UnusedDependency, UnusedExport, UnusedFile,
1096        },
1097    };
1098
1099    /// One reason token per cause, because each carries a different remedy and
1100    /// a consumer branches on the token rather than on the prose.
1101    #[test]
1102    fn every_changed_files_cause_reports_its_own_reason() {
1103        let reasons = [
1104            ChangedFilesError::InvalidRef("unclosed brace".to_owned()).reason(),
1105            ChangedFilesError::GitMissing("no such file".to_owned()).reason(),
1106            ChangedFilesError::NotARepository.reason(),
1107            ChangedFilesError::GitFailed("unknown revision".to_owned()).reason(),
1108        ];
1109        assert_eq!(
1110            reasons,
1111            [
1112                "invalid-ref",
1113                "git-missing",
1114                "not-a-repository",
1115                "git-failed"
1116            ]
1117        );
1118        let unique: std::collections::BTreeSet<&str> = reasons.iter().copied().collect();
1119        assert_eq!(
1120            unique.len(),
1121            reasons.len(),
1122            "two causes must not share a token"
1123        );
1124    }
1125
1126    /// The published sentence and the stderr line are the same string, so the
1127    /// remedy cannot drift between a log a human read and a report a script
1128    /// read. It names the ref, says the report widened, and ends with a next
1129    /// step.
1130    #[test]
1131    fn the_changed_since_message_names_the_ref_the_widening_and_the_next_step() {
1132        let message = ChangedFilesError::NotARepository.changed_since_message("origin/main");
1133        assert!(
1134            message.contains("--changed-since 'origin/main'"),
1135            "{message}"
1136        );
1137        assert!(message.contains("covers the whole project"), "{message}");
1138        assert!(message.ends_with("or drop --changed-since."), "{message}");
1139    }
1140
1141    /// Git writes multi-line stderr, and the sentence travels into a CI
1142    /// annotation and a rendered comment where a newline splits one fact
1143    /// across two records.
1144    #[test]
1145    fn the_changed_since_message_folds_git_stderr_onto_one_line() {
1146        let message = ChangedFilesError::GitFailed(
1147            "fatal: ambiguous argument 'x'\nUse '--' to separate paths".to_owned(),
1148        )
1149        .changed_since_message("x");
1150        assert!(!message.contains('\n'), "{message}");
1151        assert!(
1152            message.contains("ambiguous argument 'x' Use '--'"),
1153            "{message}"
1154        );
1155    }
1156
1157    #[test]
1158    fn git_command_clears_parent_git_environment() {
1159        let command = git_command(Path::new("."), &["status"]);
1160        let envs: Vec<_> = command.get_envs().collect();
1161
1162        for var in AMBIENT_GIT_ENV_VARS {
1163            assert!(
1164                envs.iter()
1165                    .any(|(key, value)| key.to_str() == Some(*var) && value.is_none()),
1166                "{var} should be cleared from the command env",
1167            );
1168        }
1169    }
1170
1171    #[test]
1172    fn try_get_changed_files_not_a_repository() {
1173        let temp = tempfile::tempdir().expect("tempdir");
1174        let result = try_get_changed_files(temp.path(), "main");
1175        assert!(matches!(result, Err(ChangedFilesError::NotARepository)));
1176    }
1177
1178    #[test]
1179    fn batched_refs_match_independent_changed_file_scopes() {
1180        let repo = tempfile::tempdir().expect("tempdir");
1181        for args in [
1182            &["init", "--quiet"][..],
1183            &["config", "user.email", "test@example.com"][..],
1184            &["config", "user.name", "Test User"][..],
1185            &["config", "commit.gpgsign", "false"][..],
1186        ] {
1187            run_git(repo.path(), args);
1188        }
1189        for name in ["committed.ts", "staged.ts", "unstaged.ts"] {
1190            std::fs::write(repo.path().join(name), "old\n").expect("initial file");
1191        }
1192        run_git(repo.path(), &["add", "."]);
1193        run_git(repo.path(), &["commit", "--quiet", "-m", "base"]);
1194        run_git(repo.path(), &["branch", "base"]);
1195
1196        std::fs::write(repo.path().join("committed.ts"), "new\n").expect("committed change");
1197        run_git(repo.path(), &["add", "committed.ts"]);
1198        run_git(repo.path(), &["commit", "--quiet", "-m", "change"]);
1199        std::fs::write(repo.path().join("staged.ts"), "new\n").expect("staged change");
1200        run_git(repo.path(), &["add", "staged.ts"]);
1201        std::fs::write(repo.path().join("unstaged.ts"), "new\n").expect("unstaged change");
1202        std::fs::write(repo.path().join("untracked.ts"), "new\n").expect("untracked file");
1203
1204        let mut batch = ChangedFilesBatch::new(repo.path(), "base").expect("batch");
1205        for reference in ["base", "HEAD"] {
1206            let batched = batch.changed_files(reference).expect("batched changes");
1207            let independent = changed_files(repo.path(), reference).expect("independent changes");
1208            assert_eq!(batched, independent, "{reference} scope changed");
1209        }
1210    }
1211
1212    #[cfg(unix)]
1213    #[test]
1214    fn changed_files_preserve_special_filenames() {
1215        let repo = tempfile::tempdir().expect("tempdir");
1216        for args in [
1217            &["init", "--quiet"][..],
1218            &["config", "user.email", "test@example.com"][..],
1219            &["config", "user.name", "Test User"][..],
1220            &["config", "commit.gpgsign", "false"][..],
1221            &["config", "tag.gpgsign", "false"][..],
1222        ] {
1223            run_git(repo.path(), args);
1224        }
1225        std::fs::write(repo.path().join("initial.ts"), "initial\n").expect("initial fixture");
1226        run_git(repo.path(), &["add", "."]);
1227        run_git(repo.path(), &["commit", "--quiet", "-m", "initial"]);
1228        run_git(repo.path(), &["tag", "base"]);
1229
1230        let canonical_root = repo.path().canonicalize().expect("canonical repo");
1231        let special_files = [
1232            "src/line\nbreak.ts",
1233            "src/space name.ts",
1234            "src/quote\"name.ts",
1235            "src/back\\slash.ts",
1236            "src/unicode-λ.ts",
1237        ]
1238        .map(|path| canonical_root.join(path));
1239        std::fs::create_dir_all(canonical_root.join("src")).expect("source dir");
1240        for special in &special_files {
1241            std::fs::write(special, "changed\n").expect("special fixture");
1242        }
1243
1244        let changed = try_get_changed_files(repo.path(), "base").expect("changed files");
1245        for special in special_files {
1246            assert!(
1247                changed.contains(&special),
1248                "missing {special:?}: {changed:?}"
1249            );
1250        }
1251    }
1252
1253    #[cfg(windows)]
1254    #[test]
1255    fn git_path_bytes_use_windows_separators() {
1256        assert_eq!(
1257            git_path_from_bytes(b"src/nested/file.ts"),
1258            PathBuf::from(r"src\nested\file.ts")
1259        );
1260    }
1261
1262    #[test]
1263    fn changed_diff_covers_staged_unstaged_and_untracked_files() {
1264        let repo = tempfile::tempdir().expect("tempdir");
1265        for args in [
1266            &["init", "--quiet"][..],
1267            &["config", "user.email", "test@example.com"][..],
1268            &["config", "user.name", "Test User"][..],
1269            &["config", "commit.gpgsign", "false"][..],
1270            &["config", "tag.gpgsign", "false"][..],
1271        ] {
1272            run_git(repo.path(), args);
1273        }
1274        std::fs::write(repo.path().join("staged.ts"), "old\n").expect("staged fixture");
1275        std::fs::write(repo.path().join("unstaged.ts"), "old\n").expect("unstaged fixture");
1276        run_git(repo.path(), &["add", "."]);
1277        run_git(repo.path(), &["commit", "--quiet", "-m", "initial"]);
1278        run_git(repo.path(), &["tag", "base"]);
1279
1280        std::fs::write(repo.path().join("committed.ts"), "committed\n").expect("committed fixture");
1281        run_git(repo.path(), &["add", "committed.ts"]);
1282        run_git(
1283            repo.path(),
1284            &["commit", "--quiet", "-m", "committed change"],
1285        );
1286
1287        std::fs::write(repo.path().join("staged.ts"), "staged\n").expect("staged edit");
1288        run_git(repo.path(), &["add", "staged.ts"]);
1289        std::fs::write(repo.path().join("unstaged.ts"), "unstaged\n").expect("unstaged edit");
1290        std::fs::write(repo.path().join("untracked.ts"), "untracked\n").expect("untracked edit");
1291
1292        let diff = try_get_changed_diff(repo.path(), "base").expect("complete changeset diff");
1293        let index = fallow_output::DiffIndex::from_unified_diff(&diff);
1294
1295        assert!(diff.contains("b/committed.ts"), "{diff}");
1296        assert!(diff.contains("b/staged.ts"), "{diff}");
1297        assert!(diff.contains("b/unstaged.ts"), "{diff}");
1298        assert!(diff.contains("b/untracked.ts"), "{diff}");
1299        assert_eq!(index.hunk_count(), 4);
1300        assert_eq!(index.net_lines(), 2);
1301    }
1302
1303    fn run_git(root: &Path, args: &[&str]) {
1304        let output = spawn_output(&mut git_command(root, args)).expect("git command");
1305        assert!(
1306            output.status.success(),
1307            "git {args:?} failed: {}",
1308            String::from_utf8_lossy(&output.stderr)
1309        );
1310    }
1311
1312    #[test]
1313    fn untracked_path_arg_uses_forward_slashes() {
1314        assert_eq!(
1315            super::untracked_path_arg(Path::new("src\\nested\\b.ts")),
1316            "src/nested/b.ts"
1317        );
1318        assert_eq!(super::untracked_path_arg(Path::new("src/b.ts")), "src/b.ts");
1319    }
1320
1321    #[test]
1322    fn changed_files_error_describe_matches_core_contract() {
1323        assert_eq!(
1324            ChangedFilesError::InvalidRef("bad ref".to_string()).describe(),
1325            "invalid git ref: bad ref"
1326        );
1327        assert_eq!(
1328            ChangedFilesError::GitMissing("not found".to_string()).describe(),
1329            "failed to run git: not found"
1330        );
1331        assert_eq!(
1332            ChangedFilesError::NotARepository.describe(),
1333            "not a git repository"
1334        );
1335        assert!(
1336            ChangedFilesError::GitFailed("unknown revision main".to_string())
1337                .describe()
1338                .contains("fetch-depth: 0")
1339        );
1340    }
1341
1342    #[test]
1343    fn filter_results_keeps_only_changed_file_findings() {
1344        let mut results = AnalysisResults::default();
1345        results
1346            .unused_files
1347            .push(UnusedFileFinding::with_actions(UnusedFile {
1348                path: PathBuf::from("/repo/a.ts"),
1349            }));
1350        results
1351            .unused_files
1352            .push(UnusedFileFinding::with_actions(UnusedFile {
1353                path: PathBuf::from("/repo/b.ts"),
1354            }));
1355        results
1356            .unused_exports
1357            .push(UnusedExportFinding::with_actions(UnusedExport {
1358                path: PathBuf::from("/repo/a.ts"),
1359                export_name: "foo".to_owned(),
1360                is_type_only: false,
1361                line: 1,
1362                col: 0,
1363                span_start: 0,
1364                is_re_export: false,
1365                deprecated: false,
1366                deprecated_reason: None,
1367            }));
1368
1369        let mut changed = FxHashSet::default();
1370        changed.insert(PathBuf::from("/repo/a.ts"));
1371
1372        filter_results_by_changed_files(&mut results, &changed);
1373
1374        assert_eq!(results.unused_files.len(), 1);
1375        assert_eq!(
1376            results.unused_files[0].file.path,
1377            PathBuf::from("/repo/a.ts")
1378        );
1379        assert_eq!(results.unused_exports.len(), 1);
1380    }
1381
1382    #[test]
1383    fn filter_results_preserves_graph_global_dependency_findings() {
1384        let mut results = AnalysisResults::default();
1385        results
1386            .unused_dependencies
1387            .push(UnusedDependencyFinding::with_actions(UnusedDependency {
1388                package_name: "lodash".to_owned(),
1389                location: DependencyLocation::Dependencies,
1390                path: PathBuf::from("/repo/package.json"),
1391                line: 3,
1392                used_in_workspaces: Vec::new(),
1393                declared_and_imported_in: Vec::new(),
1394            }));
1395
1396        let changed = FxHashSet::default();
1397        filter_results_by_changed_files(&mut results, &changed);
1398
1399        assert_eq!(results.unused_dependencies.len(), 1);
1400    }
1401
1402    /// Retention for `deprecated-export-in-use` keys on the file that declares
1403    /// the export. A change that only adds a consumer does not surface an old
1404    /// deprecated export (documented v1 behavior).
1405    #[test]
1406    fn filter_results_keeps_deprecated_export_only_when_its_declaring_file_changed() {
1407        let finding = |path: &str| {
1408            fallow_types::output_dead_code::DeprecatedExportInUseFinding::with_actions(
1409                fallow_types::results::DeprecatedExportInUse {
1410                    path: PathBuf::from(path),
1411                    export_name: "old".to_owned(),
1412                    is_type_only: false,
1413                    line: 2,
1414                    col: 0,
1415                    span_start: 0,
1416                    deprecated_reason: None,
1417                    consumer_count: 1,
1418                    consumers: vec![fallow_types::results::DeprecatedExportConsumer {
1419                        path: PathBuf::from("/repo/consumer.ts"),
1420                        line: 1,
1421                        col: 0,
1422                        kind: fallow_types::results::DeprecatedConsumerKind::NamedImport,
1423                    }],
1424                    public_api: false,
1425                },
1426            )
1427        };
1428        let mut results = AnalysisResults::default();
1429        results
1430            .deprecated_exports_in_use
1431            .push(finding("/repo/a.ts"));
1432        results
1433            .deprecated_exports_in_use
1434            .push(finding("/repo/b.ts"));
1435
1436        let mut changed = FxHashSet::default();
1437        changed.insert(PathBuf::from("/repo/a.ts"));
1438        changed.insert(PathBuf::from("/repo/consumer.ts"));
1439        filter_results_by_changed_files(&mut results, &changed);
1440
1441        let kept: Vec<_> = results
1442            .deprecated_exports_in_use
1443            .iter()
1444            .map(|f| f.export.path.clone())
1445            .collect();
1446        assert_eq!(kept, vec![PathBuf::from("/repo/a.ts")]);
1447    }
1448
1449    #[test]
1450    fn filter_results_keeps_relative_manifest_finding_when_manifest_changed() {
1451        let mut results = AnalysisResults::default();
1452        results
1453            .empty_catalog_groups
1454            .push(EmptyCatalogGroupFinding::with_actions(EmptyCatalogGroup {
1455                catalog_name: "legacy".to_owned(),
1456                path: PathBuf::from("pnpm-workspace.yaml"),
1457                line: 4,
1458            }));
1459
1460        let mut changed = FxHashSet::default();
1461        changed.insert(PathBuf::from("/repo/pnpm-workspace.yaml"));
1462
1463        filter_results_by_changed_files(&mut results, &changed);
1464
1465        assert_eq!(results.empty_catalog_groups.len(), 1);
1466    }
1467
1468    #[test]
1469    fn filter_duplication_keeps_groups_with_changed_instances_and_recomputes_stats() {
1470        let mut report = DuplicationReport {
1471            clone_groups: vec![
1472                CloneGroup {
1473                    instances: vec![
1474                        CloneInstance {
1475                            is_symlink: false,
1476                            file: PathBuf::from("/repo/a.ts"),
1477                            start_line: 1,
1478                            end_line: 5,
1479                            start_col: 0,
1480                            end_col: 10,
1481                            fragment: "code".to_owned(),
1482                        },
1483                        CloneInstance {
1484                            is_symlink: false,
1485                            file: PathBuf::from("/repo/b.ts"),
1486                            start_line: 1,
1487                            end_line: 5,
1488                            start_col: 0,
1489                            end_col: 10,
1490                            fragment: "code".to_owned(),
1491                        },
1492                    ],
1493                    token_count: 20,
1494                    line_count: 5,
1495                    similarity: None,
1496                },
1497                CloneGroup {
1498                    instances: vec![
1499                        CloneInstance {
1500                            is_symlink: false,
1501                            file: PathBuf::from("/repo/c.ts"),
1502                            start_line: 1,
1503                            end_line: 5,
1504                            start_col: 0,
1505                            end_col: 10,
1506                            fragment: "other".to_owned(),
1507                        },
1508                        CloneInstance {
1509                            is_symlink: false,
1510                            file: PathBuf::from("/repo/d.ts"),
1511                            start_line: 1,
1512                            end_line: 5,
1513                            start_col: 0,
1514                            end_col: 10,
1515                            fragment: "other".to_owned(),
1516                        },
1517                    ],
1518                    token_count: 20,
1519                    line_count: 5,
1520                    similarity: None,
1521                },
1522            ],
1523            clone_families: Vec::new(),
1524            mirrored_directories: Vec::new(),
1525            stats: DuplicationStats {
1526                total_files: 4,
1527                files_with_clones: 4,
1528                total_lines: 100,
1529                duplicated_lines: 20,
1530                total_tokens: 200,
1531                duplicated_tokens: 80,
1532                clone_groups: 2,
1533                clone_families: 0,
1534                clone_instances: 4,
1535                duplication_percentage: 20.0,
1536                clone_groups_below_min_occurrences: 0,
1537                clone_groups_ignored: 0,
1538                near_candidates_skipped: 0,
1539            },
1540        };
1541
1542        let mut changed = FxHashSet::default();
1543        changed.insert(PathBuf::from("/repo/a.ts"));
1544
1545        filter_duplication_by_changed_files(&mut report, &changed, Path::new("/repo"));
1546
1547        assert_eq!(report.clone_groups.len(), 1);
1548        assert_eq!(report.stats.clone_groups, 1);
1549        assert_eq!(report.stats.clone_instances, 2);
1550    }
1551
1552    /// The combined probe gives the same two paths as the two single probes,
1553    /// from the repository root and from a subdirectory.
1554    #[test]
1555    fn one_probe_resolves_the_common_dir_and_the_toplevel() {
1556        let repo = tempfile::tempdir().expect("tempdir");
1557        run_git(repo.path(), &["init", "--quiet"]);
1558        let nested = repo.path().join("packages/app");
1559        std::fs::create_dir_all(&nested).expect("nested dir");
1560        for cwd in [repo.path(), nested.as_path()] {
1561            let (common, toplevel) =
1562                resolve_git_common_dir_and_toplevel(cwd).expect("combined probe");
1563            assert_eq!(common, resolve_git_common_dir(cwd).expect("common dir"));
1564            assert_eq!(toplevel, resolve_git_toplevel(cwd).expect("toplevel"));
1565        }
1566    }
1567
1568    /// A bare repository has a common dir but no work tree, so the combined
1569    /// probe fails and a caller falls back to the single probes.
1570    #[test]
1571    fn the_combined_probe_fails_without_a_work_tree() {
1572        let repo = tempfile::tempdir().expect("tempdir");
1573        run_git(repo.path(), &["init", "--quiet", "--bare"]);
1574        assert!(resolve_git_common_dir_and_toplevel(repo.path()).is_err());
1575        assert!(resolve_git_common_dir(repo.path()).is_ok());
1576    }
1577}