Skip to main content

fallow_core/
changed_files.rs

1//! Git-aware "changed files" filtering shared between fallow-cli and fallow-lsp.
2//!
3//! Provides:
4//! - [`validate_git_ref`] for input validation at trust boundaries.
5//! - [`ChangedFilesError`] / [`try_get_changed_files`] / [`get_changed_files`]
6//!   for resolving a git ref into the set of changed files.
7//! - [`filter_results_by_changed_files`] for narrowing an [`AnalysisResults`]
8//!   to issues in those files.
9//! - [`filter_duplication_by_changed_files`] for narrowing a
10//!   [`DuplicationReport`] to clone groups touching at least one changed file.
11//!
12//! Both filters intentionally exclude dependency-level issues (unused deps,
13//! type-only deps, test-only deps) since "unused dependency" is a function of
14//! the entire import graph and can't be attributed to individual changed files.
15
16use std::path::{Path, PathBuf};
17use std::process::Output;
18use std::sync::OnceLock;
19
20use rustc_hash::{FxHashMap, FxHashSet};
21
22use crate::duplicates::{DuplicationReport, DuplicationStats, families};
23use crate::results::AnalysisResults;
24
25/// Function pointer signature used by `set_spawn_hook` to intercept the
26/// short-running `git rev-parse` / `git diff` / `git ls-files` subprocesses
27/// this module spawns. Lets the CLI route those git children through its
28/// `ScopedChild` registry so a SIGINT delivered to the parent during
29/// watch mode (or any analysis) reaps them instead of letting them run
30/// to completion. See `crates/cli/src/signal/` and issue #477.
31pub type ChangedFilesSpawnHook = fn(&mut std::process::Command) -> std::io::Result<Output>;
32
33static SPAWN_HOOK: OnceLock<ChangedFilesSpawnHook> = OnceLock::new();
34
35/// Install a spawn-hook for this module's git subprocesses. Idempotent;
36/// subsequent calls are no-ops. Called once from the CLI's `main()` so
37/// long-running watch sessions reap pending git children on Ctrl+C.
38/// Defaults to `Command::output` when not set; the function-pointer
39/// indirection costs nothing for embedders and tests that don't install
40/// a hook.
41pub fn set_spawn_hook(hook: ChangedFilesSpawnHook) {
42    let _ = SPAWN_HOOK.set(hook);
43}
44
45fn spawn_output(command: &mut std::process::Command) -> std::io::Result<Output> {
46    if let Some(hook) = SPAWN_HOOK.get() {
47        hook(command)
48    } else {
49        command.output()
50    }
51}
52
53/// Validate a user-supplied git ref before passing it to `git diff`.
54///
55/// Rejects empty strings, refs starting with `-` (which `git` would interpret
56/// as an option flag), and characters outside the safe allowlist for branch
57/// names, tags, SHAs, and reflog expressions (`HEAD~N`, `HEAD@{...}`).
58///
59/// Inside `@{...}` braces, colons and spaces are allowed so reflog timestamps
60/// like `HEAD@{2025-01-01}` and `HEAD@{1 week ago}` round-trip.
61///
62/// Used by both the CLI (clap value parser) and the LSP (initializationOptions
63/// trust boundary) to fail fast with a readable error rather than handing a
64/// malformed ref to git.
65pub fn validate_git_ref(s: &str) -> Result<&str, String> {
66    if s.is_empty() {
67        return Err("git ref cannot be empty".to_string());
68    }
69    if s.starts_with('-') {
70        return Err("git ref cannot start with '-'".to_string());
71    }
72    let mut in_braces = false;
73    for c in s.chars() {
74        match c {
75            '{' => in_braces = true,
76            '}' => in_braces = false,
77            ':' | ' ' if in_braces => {}
78            c if c.is_ascii_alphanumeric()
79                || matches!(c, '.' | '_' | '-' | '/' | '~' | '^' | '@' | '{' | '}') => {}
80            _ => return Err(format!("git ref contains disallowed character: '{c}'")),
81        }
82    }
83    if in_braces {
84        return Err("git ref has unclosed '{'".to_string());
85    }
86    Ok(s)
87}
88
89/// Classification of a `git diff` failure, so callers can pick their own
90/// wording (soft warning vs hard error) without re-parsing stderr.
91#[derive(Debug)]
92pub enum ChangedFilesError {
93    /// Git ref failed validation before invoking `git`.
94    InvalidRef(String),
95    /// `git` binary not found / not executable.
96    GitMissing(String),
97    /// Command ran but the directory isn't a git repository.
98    NotARepository,
99    /// Command ran but the ref is invalid / another git error.
100    GitFailed(String),
101}
102
103impl ChangedFilesError {
104    /// Human-readable clause suitable for embedding in an error message.
105    /// Does not include the flag name (e.g. "--changed-since") so callers can
106    /// prepend their own context.
107    pub fn describe(&self) -> String {
108        match self {
109            Self::InvalidRef(e) => format!("invalid git ref: {e}"),
110            Self::GitMissing(e) => format!("failed to run git: {e}"),
111            Self::NotARepository => "not a git repository".to_owned(),
112            Self::GitFailed(stderr) => augment_git_failed(stderr),
113        }
114    }
115}
116
117/// Enrich a raw `git diff` stderr with actionable hints when the failure mode
118/// is recognizable. Today: shallow-clone misses (`actions/checkout@v4` defaults
119/// to `fetch-depth: 1`, GitLab CI to `GIT_DEPTH: 50`), where the baseline ref
120/// predates the fetch boundary. Bare git stderr is famously cryptic; a hint
121/// here is much more useful than a docs link the reader has to chase.
122fn augment_git_failed(stderr: &str) -> String {
123    let lower = stderr.to_ascii_lowercase();
124    if lower.contains("not a valid object name")
125        || lower.contains("unknown revision")
126        || lower.contains("ambiguous argument")
127    {
128        format!(
129            "{stderr} (shallow clone? try `git fetch --unshallow`, or set `fetch-depth: 0` on actions/checkout / `GIT_DEPTH: 0` in GitLab CI)"
130        )
131    } else {
132        stderr.to_owned()
133    }
134}
135
136/// Resolve the canonical git toplevel for `cwd`.
137///
138/// Runs `git rev-parse --show-toplevel`, which is git's own answer to "where
139/// does this repository live?". The returned path is canonicalized so it
140/// agrees with paths produced by `fs::canonicalize` elsewhere on macOS
141/// (`/tmp` -> `/private/tmp`) and Windows (8.3 short paths).
142///
143/// Used by `try_get_changed_files` to produce changed-file paths whose
144/// absolute form matches what the analysis pipeline emits, regardless of
145/// whether the caller's `cwd` is the repo root or a subdirectory of it.
146pub fn resolve_git_toplevel(cwd: &Path) -> Result<PathBuf, ChangedFilesError> {
147    let output = spawn_output(&mut git_command(cwd, &["rev-parse", "--show-toplevel"]))
148        .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
149
150    if !output.status.success() {
151        let stderr = String::from_utf8_lossy(&output.stderr);
152        return Err(if stderr.contains("not a git repository") {
153            ChangedFilesError::NotARepository
154        } else {
155            ChangedFilesError::GitFailed(stderr.trim().to_owned())
156        });
157    }
158
159    let raw = String::from_utf8_lossy(&output.stdout);
160    let trimmed = raw.trim();
161    if trimmed.is_empty() {
162        return Err(ChangedFilesError::GitFailed(
163            "git rev-parse --show-toplevel returned empty output".to_owned(),
164        ));
165    }
166
167    let path = PathBuf::from(trimmed);
168    // `dunce::canonicalize` strips Windows `\\?\` verbatim prefix; without
169    // this, every changed file emitted by `git diff --name-only` got joined
170    // onto a verbatim-prefixed toplevel, and downstream `strip_prefix`
171    // comparisons against an `opts.root` that does NOT carry the verbatim
172    // prefix silently mismatched. The focus-filter then dropped EVERY
173    // finding on Windows, breaking `fallow audit` and `--changed-since`.
174    // On POSIX `dunce::canonicalize` is identical to `std::fs::canonicalize`.
175    Ok(dunce::canonicalize(&path).unwrap_or(path))
176}
177
178fn collect_git_paths(
179    cwd: &Path,
180    toplevel: &Path,
181    args: &[&str],
182) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
183    let output = spawn_output(&mut git_command(cwd, args))
184        .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
185
186    if !output.status.success() {
187        let stderr = String::from_utf8_lossy(&output.stderr);
188        return Err(if stderr.contains("not a git repository") {
189            ChangedFilesError::NotARepository
190        } else {
191            ChangedFilesError::GitFailed(stderr.trim().to_owned())
192        });
193    }
194
195    // All callers use modes whose output is repository-root-relative
196    // (`git diff --name-only`, `git ls-files --full-name --others`). Joining
197    // against `toplevel` yields absolute paths that line up with what
198    // `analyze_project` emits when given a canonical workspace root, even if
199    // the LSP / CLI was invoked from a subdirectory.
200    //
201    // Windows-specific normalisation: `git diff --name-only` always emits
202    // forward-slashed paths (`src/legacy.ts`) regardless of OS. `PathBuf::join`
203    // on Windows appends with the native backslash separator without
204    // converting separators inside the appended segment, so the result is
205    // `C:\Users\...\Temp\test\src/legacy.ts` (mixed). File discovery via
206    // walkdir produces all-backslash paths. `FxHashSet::contains` compares
207    // bytes, not components, so the two forms mismatch and the focused
208    // duplicates / changed-since filters silently drop every finding.
209    // Convert forward slashes to backslashes inside the relative segment
210    // before joining so both sides land in native shape. On POSIX the
211    // segment is already in native form (forward slashes) so the conversion
212    // is a no-op.
213    #[cfg(windows)]
214    let normalise_segment = |line: &str| line.replace('/', "\\");
215    #[cfg(not(windows))]
216    let normalise_segment = |line: &str| line.to_owned();
217
218    let files: FxHashSet<PathBuf> = String::from_utf8_lossy(&output.stdout)
219        .lines()
220        .filter(|line| !line.is_empty())
221        .map(|line| toplevel.join(normalise_segment(line)))
222        .collect();
223
224    Ok(files)
225}
226
227fn git_command(cwd: &Path, args: &[&str]) -> std::process::Command {
228    let mut command = crate::spawn::git();
229    command.args(args).current_dir(cwd);
230    command
231}
232
233/// Get files changed since a git ref. Returns `Err` (with details) when the
234/// git invocation itself failed, so callers can choose between warn-and-ignore
235/// and hard-error behavior.
236///
237/// Includes both:
238/// - committed changes from the merge-base range `git_ref...HEAD`
239/// - tracked staged/unstaged changes from `HEAD` to the current worktree
240/// - untracked files not ignored by Git
241///
242/// This keeps `--changed-since` useful for local validation instead of only
243/// reflecting the last committed `HEAD`.
244///
245/// All paths in the returned set are absolute and rooted at the canonical
246/// git toplevel, not at `root`. This matters when the LSP / CLI is invoked
247/// from a subdirectory of the repository (e.g., a Turborepo workspace at
248/// `apps/web`): `git diff` emits root-relative paths, and we need to join
249/// them against the actual repo root rather than the caller's cwd.
250pub fn try_get_changed_files(
251    root: &Path,
252    git_ref: &str,
253) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
254    // Validate the ref BEFORE resolving the toplevel so the security-relevant
255    // boundary check (rejects refs starting with `-`, etc.) runs even when
256    // `cwd` happens to not be a git repo. Otherwise an attacker-controlled
257    // `--changed-since=--upload-pack=evil` would leak through to
258    // `git rev-parse` instead of being rejected at validation.
259    validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
260    let toplevel = resolve_git_toplevel(root)?;
261    try_get_changed_files_with_toplevel(root, &toplevel, git_ref)
262}
263
264/// Like [`try_get_changed_files`], but takes a pre-resolved canonical
265/// `toplevel` so callers (the LSP) can cache it across runs and avoid the
266/// extra `git rev-parse --show-toplevel` subprocess on every save.
267///
268/// `toplevel` MUST be the canonical git toplevel for `cwd`; passing anything
269/// else produces incorrect changed-file paths. The CLI does not call this
270/// directly: it uses [`try_get_changed_files`] which resolves on each call.
271pub fn try_get_changed_files_with_toplevel(
272    cwd: &Path,
273    toplevel: &Path,
274    git_ref: &str,
275) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
276    validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
277
278    let mut files = collect_git_paths(
279        cwd,
280        toplevel,
281        &[
282            "diff",
283            "--name-only",
284            "--end-of-options",
285            &format!("{git_ref}...HEAD"),
286        ],
287    )?;
288    files.extend(collect_git_paths(
289        cwd,
290        toplevel,
291        &["diff", "--name-only", "HEAD"],
292    )?);
293    // `--full-name` forces `ls-files` to emit repository-root-relative paths,
294    // matching `git diff`'s default. Without it, `ls-files` emits paths
295    // relative to cwd, which silently produces wrong joins when the caller
296    // invokes from a subdirectory.
297    files.extend(collect_git_paths(
298        cwd,
299        toplevel,
300        &["ls-files", "--full-name", "--others", "--exclude-standard"],
301    )?);
302    Ok(files)
303}
304
305/// Get files changed since a git ref. Returns `None` on git failure after
306/// printing a warning to stderr. Used by `--changed-since` and `--file`, where
307/// a failure falls back to full-scope analysis.
308#[expect(
309    clippy::print_stderr,
310    reason = "intentional user-facing warning for the CLI's --changed-since fallback path; LSP callers use try_get_changed_files instead"
311)]
312pub fn get_changed_files(root: &Path, git_ref: &str) -> Option<FxHashSet<PathBuf>> {
313    match try_get_changed_files(root, git_ref) {
314        Ok(files) => Some(files),
315        Err(ChangedFilesError::InvalidRef(e)) => {
316            eprintln!("Warning: --changed-since ignored: invalid git ref: {e}");
317            None
318        }
319        Err(ChangedFilesError::GitMissing(e)) => {
320            eprintln!("Warning: --changed-since ignored: failed to run git: {e}");
321            None
322        }
323        Err(ChangedFilesError::NotARepository) => {
324            eprintln!("Warning: --changed-since ignored: not a git repository");
325            None
326        }
327        Err(ChangedFilesError::GitFailed(stderr)) => {
328            eprintln!("Warning: --changed-since failed for ref '{git_ref}': {stderr}");
329            None
330        }
331    }
332}
333
334/// Filter `results` to only include issues whose source file is in
335/// `changed_files`.
336///
337/// Dependency-level issues (unused deps, dev deps, optional deps, type-only
338/// deps, test-only deps) are intentionally NOT filtered here. Unlike
339/// file-level issues, a dependency being "unused" is a function of the entire
340/// import graph and can't be attributed to individual changed source files.
341#[expect(
342    clippy::implicit_hasher,
343    reason = "fallow standardizes on FxHashSet across the workspace"
344)]
345pub fn filter_results_by_changed_files(
346    results: &mut AnalysisResults,
347    changed_files: &FxHashSet<PathBuf>,
348) {
349    let cf = normalize_changed_files_set(changed_files);
350    results
351        .unused_files
352        .retain(|f| contains_normalized(&cf, &f.file.path));
353    results
354        .unused_exports
355        .retain(|e| contains_normalized(&cf, &e.export.path));
356    results
357        .unused_types
358        .retain(|e| contains_normalized(&cf, &e.export.path));
359    results
360        .private_type_leaks
361        .retain(|e| contains_normalized(&cf, &e.leak.path));
362    results
363        .unused_enum_members
364        .retain(|m| contains_normalized(&cf, &m.member.path));
365    results
366        .unused_class_members
367        .retain(|m| contains_normalized(&cf, &m.member.path));
368    results
369        .unresolved_imports
370        .retain(|i| contains_normalized(&cf, &i.import.path));
371
372    // Unlisted deps: keep only if any importing file is changed
373    results.unlisted_dependencies.retain(|d| {
374        d.dep
375            .imported_from
376            .iter()
377            .any(|s| contains_normalized(&cf, &s.path))
378    });
379
380    // Duplicate exports: filter locations to changed files, drop groups with < 2
381    for dup in &mut results.duplicate_exports {
382        dup.export
383            .locations
384            .retain(|loc| contains_normalized(&cf, &loc.path));
385    }
386    results
387        .duplicate_exports
388        .retain(|d| d.export.locations.len() >= 2);
389
390    // Circular deps: keep cycles where at least one file is changed
391    results
392        .circular_dependencies
393        .retain(|c| c.cycle.files.iter().any(|f| contains_normalized(&cf, f)));
394
395    // Re-export cycles: same file-level treatment as circular deps; the
396    // cycle is file-scoped so any member changing counts as touching the
397    // cycle.
398    results
399        .re_export_cycles
400        .retain(|c| c.cycle.files.iter().any(|f| contains_normalized(&cf, f)));
401
402    // Boundary violations: keep if the importing file changed
403    results
404        .boundary_violations
405        .retain(|v| contains_normalized(&cf, &v.violation.from_path));
406
407    // Stale suppressions: keep if the file changed
408    results
409        .stale_suppressions
410        .retain(|s| contains_normalized(&cf, &s.path));
411
412    // Unresolved catalog references: anchored at the consumer package.json,
413    // so keep only findings whose path is in the changed set.
414    results
415        .unresolved_catalog_references
416        .retain(|r| contains_normalized(&cf, &r.reference.path));
417    results
418        .empty_catalog_groups
419        .retain(|g| normalized_set_contains_path(&cf, &g.group.path));
420
421    // Unused / misconfigured dependency overrides: anchored at the declaring
422    // source file (pnpm-workspace.yaml or root package.json). Keep only
423    // findings whose source file is in the changed set.
424    results
425        .unused_dependency_overrides
426        .retain(|o| contains_normalized(&cf, &o.entry.path));
427    results
428        .misconfigured_dependency_overrides
429        .retain(|o| contains_normalized(&cf, &o.entry.path));
430}
431
432/// Pre-normalise a `changed_files` set through `dunce::simplified` so each
433/// per-entry comparison can normalise its lookup side and avoid the Windows
434/// `\\?\` verbatim-vs-non-verbatim mismatch. On POSIX `dunce::simplified` is
435/// a no-op, so this is identical to cloning the set.
436///
437/// Background: `try_get_changed_files` joins git-emitted segments onto the
438/// `dunce::canonicalize`d toplevel, so entries land in non-verbatim shape.
439/// Analysis-pipeline paths (clone instances, finding paths) inherit the
440/// shape of `opts.root`, which `validate_root` / discovery / cache lookups
441/// pre-canonicalise with `std::fs::canonicalize` in test fixtures and tools
442/// (which yields verbatim paths on Windows). Comparing the two sides byte
443/// for byte silently dropped every finding before this normalisation.
444fn normalize_changed_files_set(changed_files: &FxHashSet<PathBuf>) -> FxHashSet<PathBuf> {
445    changed_files
446        .iter()
447        .map(|p| dunce::simplified(p).to_path_buf())
448        .collect()
449}
450
451fn contains_normalized(normalized: &FxHashSet<PathBuf>, path: &Path) -> bool {
452    normalized.contains(dunce::simplified(path))
453}
454
455fn normalized_set_contains_path(normalized: &FxHashSet<PathBuf>, path: &Path) -> bool {
456    contains_normalized(normalized, path)
457        || (path.is_relative() && normalized.iter().any(|changed| changed.ends_with(path)))
458}
459
460/// Recompute duplication statistics after filtering.
461///
462/// Uses per-file line deduplication (matching `compute_stats` in
463/// `duplicates/detect.rs`) so overlapping clone instances don't inflate the
464/// duplicated line count.
465fn recompute_duplication_stats(report: &DuplicationReport) -> DuplicationStats {
466    let mut files_with_clones: FxHashSet<&Path> = FxHashSet::default();
467    let mut file_dup_lines: FxHashMap<&Path, FxHashSet<usize>> = FxHashMap::default();
468    let mut duplicated_tokens = 0_usize;
469    let mut clone_instances = 0_usize;
470
471    for group in &report.clone_groups {
472        for instance in &group.instances {
473            files_with_clones.insert(&instance.file);
474            clone_instances += 1;
475            let lines = file_dup_lines.entry(&instance.file).or_default();
476            for line in instance.start_line..=instance.end_line {
477                lines.insert(line);
478            }
479        }
480        duplicated_tokens += group.token_count * group.instances.len();
481    }
482
483    let duplicated_lines: usize = file_dup_lines.values().map(FxHashSet::len).sum();
484
485    DuplicationStats {
486        total_files: report.stats.total_files,
487        files_with_clones: files_with_clones.len(),
488        total_lines: report.stats.total_lines,
489        duplicated_lines,
490        total_tokens: report.stats.total_tokens,
491        duplicated_tokens,
492        clone_groups: report.clone_groups.len(),
493        clone_instances,
494        #[expect(
495            clippy::cast_precision_loss,
496            reason = "stat percentages are display-only; precision loss at usize::MAX line counts is acceptable"
497        )]
498        duplication_percentage: if report.stats.total_lines > 0 {
499            (duplicated_lines as f64 / report.stats.total_lines as f64) * 100.0
500        } else {
501            0.0
502        },
503        clone_groups_below_min_occurrences: report.stats.clone_groups_below_min_occurrences,
504    }
505}
506
507/// Filter a duplication report to only retain clone groups where at least one
508/// instance belongs to a changed file. Families, mirrored directories, and
509/// stats are rebuilt from the surviving groups so consumers see consistent,
510/// correctly-scoped numbers.
511#[expect(
512    clippy::implicit_hasher,
513    reason = "fallow standardizes on FxHashSet across the workspace"
514)]
515pub fn filter_duplication_by_changed_files(
516    report: &mut DuplicationReport,
517    changed_files: &FxHashSet<PathBuf>,
518    root: &Path,
519) {
520    let cf = normalize_changed_files_set(changed_files);
521    report.clone_groups.retain(|g| {
522        g.instances
523            .iter()
524            .any(|i| contains_normalized(&cf, &i.file))
525    });
526    report.clone_families = families::group_into_families(&report.clone_groups, root);
527    report.mirrored_directories =
528        families::detect_mirrored_directories(&report.clone_families, root);
529    report.stats = recompute_duplication_stats(report);
530}
531
532#[cfg(test)]
533mod tests {
534    use super::*;
535    use crate::duplicates::{CloneGroup, CloneInstance};
536    use crate::results::{
537        BoundaryViolation, CircularDependency, EmptyCatalogGroup, UnusedExport, UnusedFile,
538    };
539    use fallow_types::output_dead_code::{
540        BoundaryViolationFinding, CircularDependencyFinding, EmptyCatalogGroupFinding,
541        UnusedExportFinding, UnusedFileFinding,
542    };
543
544    #[test]
545    fn changed_files_error_describe_variants() {
546        assert!(
547            ChangedFilesError::InvalidRef("bad".to_owned())
548                .describe()
549                .contains("invalid git ref")
550        );
551        assert!(
552            ChangedFilesError::GitMissing("oops".to_owned())
553                .describe()
554                .contains("oops")
555        );
556        assert_eq!(
557            ChangedFilesError::NotARepository.describe(),
558            "not a git repository"
559        );
560        assert!(
561            ChangedFilesError::GitFailed("bad ref".to_owned())
562                .describe()
563                .contains("bad ref")
564        );
565    }
566
567    #[test]
568    fn augment_git_failed_appends_shallow_clone_hint_for_unknown_revision() {
569        let stderr = "fatal: ambiguous argument 'fallow-baseline...HEAD': unknown revision or path not in the working tree.";
570        let described = ChangedFilesError::GitFailed(stderr.to_owned()).describe();
571        assert!(described.contains(stderr), "original stderr preserved");
572        assert!(
573            described.contains("shallow clone"),
574            "hint surfaced: {described}"
575        );
576        assert!(
577            described.contains("fetch-depth: 0") || described.contains("git fetch --unshallow"),
578            "hint actionable: {described}"
579        );
580    }
581
582    #[test]
583    fn augment_git_failed_passthrough_for_other_errors() {
584        // Errors that aren't shallow-clone-related stay verbatim
585        let stderr = "fatal: refusing to merge unrelated histories";
586        let described = ChangedFilesError::GitFailed(stderr.to_owned()).describe();
587        assert_eq!(described, stderr);
588    }
589
590    #[test]
591    fn validate_git_ref_rejects_leading_dash() {
592        assert!(validate_git_ref("--upload-pack=evil").is_err());
593        assert!(validate_git_ref("-flag").is_err());
594    }
595
596    #[test]
597    fn validate_git_ref_accepts_baseline_tag() {
598        assert_eq!(
599            validate_git_ref("fallow-baseline").unwrap(),
600            "fallow-baseline"
601        );
602    }
603
604    #[test]
605    fn try_get_changed_files_rejects_invalid_ref() {
606        // Validation runs before git invocation, so any path will do
607        let err = try_get_changed_files(Path::new("/"), "--evil")
608            .expect_err("leading-dash ref must be rejected");
609        assert!(matches!(err, ChangedFilesError::InvalidRef(_)));
610        assert!(err.describe().contains("cannot start with"));
611    }
612
613    #[test]
614    fn validate_git_ref_rejects_option_like_ref() {
615        assert!(validate_git_ref("--output=/tmp/fallow-proof").is_err());
616    }
617
618    #[test]
619    fn validate_git_ref_allows_reflog_relative_date() {
620        assert!(validate_git_ref("HEAD@{1 week ago}").is_ok());
621    }
622
623    #[test]
624    fn try_get_changed_files_rejects_option_like_ref_before_git() {
625        let root = tempfile::tempdir().expect("create temp dir");
626        let proof_path = root.path().join("proof");
627
628        let result = try_get_changed_files(
629            root.path(),
630            &format!("--output={}", proof_path.to_string_lossy()),
631        );
632
633        assert!(matches!(result, Err(ChangedFilesError::InvalidRef(_))));
634        assert!(
635            !proof_path.exists(),
636            "invalid changedSince ref must not be passed through to git as an option"
637        );
638    }
639
640    #[test]
641    fn git_command_clears_parent_git_environment() {
642        let command = git_command(Path::new("."), &["status", "--short"]);
643        let overrides: Vec<_> = command.get_envs().collect();
644
645        for var in crate::git_env::AMBIENT_GIT_ENV_VARS {
646            assert!(
647                overrides
648                    .iter()
649                    .any(|(key, value)| key.to_str() == Some(*var) && value.is_none()),
650                "git helper must clear inherited {var}",
651            );
652        }
653    }
654
655    #[test]
656    fn filter_results_keeps_only_changed_files() {
657        let mut results = AnalysisResults::default();
658        results
659            .unused_files
660            .push(UnusedFileFinding::with_actions(UnusedFile {
661                path: "/a.ts".into(),
662            }));
663        results
664            .unused_files
665            .push(UnusedFileFinding::with_actions(UnusedFile {
666                path: "/b.ts".into(),
667            }));
668        results
669            .unused_exports
670            .push(UnusedExportFinding::with_actions(UnusedExport {
671                path: "/a.ts".into(),
672                export_name: "foo".into(),
673                is_type_only: false,
674                line: 1,
675                col: 0,
676                span_start: 0,
677                is_re_export: false,
678            }));
679
680        let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
681        changed.insert("/a.ts".into());
682
683        filter_results_by_changed_files(&mut results, &changed);
684
685        assert_eq!(results.unused_files.len(), 1);
686        assert_eq!(results.unused_files[0].file.path, PathBuf::from("/a.ts"));
687        assert_eq!(results.unused_exports.len(), 1);
688    }
689
690    #[test]
691    fn filter_results_preserves_dependency_level_issues() {
692        let mut results = AnalysisResults::default();
693        results.unused_dependencies.push(
694            fallow_types::output_dead_code::UnusedDependencyFinding::with_actions(
695                crate::results::UnusedDependency {
696                    package_name: "lodash".into(),
697                    location: crate::results::DependencyLocation::Dependencies,
698                    path: "/pkg.json".into(),
699                    line: 3,
700                    used_in_workspaces: Vec::new(),
701                },
702            ),
703        );
704
705        let changed: FxHashSet<PathBuf> = FxHashSet::default();
706        filter_results_by_changed_files(&mut results, &changed);
707
708        // Dependency-level issues survive even when no source files changed
709        assert_eq!(results.unused_dependencies.len(), 1);
710    }
711
712    #[test]
713    fn filter_results_keeps_circular_dep_when_any_file_changed() {
714        let mut results = AnalysisResults::default();
715        results
716            .circular_dependencies
717            .push(CircularDependencyFinding::with_actions(
718                CircularDependency {
719                    files: vec!["/a.ts".into(), "/b.ts".into()],
720                    length: 2,
721                    line: 1,
722                    col: 0,
723                    is_cross_package: false,
724                },
725            ));
726
727        let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
728        changed.insert("/b.ts".into());
729
730        filter_results_by_changed_files(&mut results, &changed);
731        assert_eq!(results.circular_dependencies.len(), 1);
732    }
733
734    #[test]
735    fn filter_results_drops_circular_dep_when_no_file_changed() {
736        let mut results = AnalysisResults::default();
737        results
738            .circular_dependencies
739            .push(CircularDependencyFinding::with_actions(
740                CircularDependency {
741                    files: vec!["/a.ts".into(), "/b.ts".into()],
742                    length: 2,
743                    line: 1,
744                    col: 0,
745                    is_cross_package: false,
746                },
747            ));
748
749        let changed: FxHashSet<PathBuf> = FxHashSet::default();
750        filter_results_by_changed_files(&mut results, &changed);
751        assert!(results.circular_dependencies.is_empty());
752    }
753
754    #[test]
755    fn filter_results_drops_boundary_violation_when_importer_unchanged() {
756        let mut results = AnalysisResults::default();
757        results
758            .boundary_violations
759            .push(BoundaryViolationFinding::with_actions(BoundaryViolation {
760                from_path: "/a.ts".into(),
761                to_path: "/b.ts".into(),
762                from_zone: "ui".into(),
763                to_zone: "data".into(),
764                import_specifier: "../data/db".into(),
765                line: 1,
766                col: 0,
767            }));
768
769        let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
770        // only the imported file changed, not the importer
771        changed.insert("/b.ts".into());
772
773        filter_results_by_changed_files(&mut results, &changed);
774        assert!(results.boundary_violations.is_empty());
775    }
776
777    #[test]
778    fn filter_results_keeps_relative_empty_catalog_group_when_manifest_changed() {
779        let mut results = AnalysisResults::default();
780        results
781            .empty_catalog_groups
782            .push(EmptyCatalogGroupFinding::with_actions(EmptyCatalogGroup {
783                catalog_name: "legacy".into(),
784                path: PathBuf::from("pnpm-workspace.yaml"),
785                line: 4,
786            }));
787
788        let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
789        changed.insert(PathBuf::from("/repo/pnpm-workspace.yaml"));
790
791        filter_results_by_changed_files(&mut results, &changed);
792
793        assert_eq!(results.empty_catalog_groups.len(), 1);
794        assert_eq!(results.empty_catalog_groups[0].group.catalog_name, "legacy");
795    }
796
797    #[test]
798    fn filter_duplication_keeps_groups_with_at_least_one_changed_instance() {
799        let mut report = DuplicationReport {
800            clone_groups: vec![CloneGroup {
801                instances: vec![
802                    CloneInstance {
803                        file: "/a.ts".into(),
804                        start_line: 1,
805                        end_line: 5,
806                        start_col: 0,
807                        end_col: 10,
808                        fragment: "code".into(),
809                    },
810                    CloneInstance {
811                        file: "/b.ts".into(),
812                        start_line: 1,
813                        end_line: 5,
814                        start_col: 0,
815                        end_col: 10,
816                        fragment: "code".into(),
817                    },
818                ],
819                token_count: 20,
820                line_count: 5,
821            }],
822            clone_families: vec![],
823            mirrored_directories: vec![],
824            stats: DuplicationStats {
825                total_files: 2,
826                files_with_clones: 2,
827                total_lines: 100,
828                duplicated_lines: 10,
829                total_tokens: 200,
830                duplicated_tokens: 40,
831                clone_groups: 1,
832                clone_instances: 2,
833                duplication_percentage: 10.0,
834                clone_groups_below_min_occurrences: 0,
835            },
836        };
837
838        let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
839        changed.insert("/a.ts".into());
840
841        filter_duplication_by_changed_files(&mut report, &changed, Path::new(""));
842        assert_eq!(report.clone_groups.len(), 1);
843        // stats recomputed from surviving groups
844        assert_eq!(report.stats.clone_groups, 1);
845        assert_eq!(report.stats.clone_instances, 2);
846    }
847
848    /// Regression for issue #561: on Windows, `try_get_changed_files` joins
849    /// segments onto the `dunce::canonicalize`d toplevel (non-verbatim),
850    /// while analysis-pipeline paths inherit the shape of `opts.root` which
851    /// tools / test fixtures often pre-canonicalise with `std::fs::canonicalize`
852    /// (verbatim). The byte-level lookup against `FxHashSet<PathBuf>` then
853    /// silently dropped every clone group. Pin both sides through a synthetic
854    /// verbatim path on one side and a plain path on the other.
855    #[cfg(windows)]
856    #[test]
857    fn filter_duplication_normalises_verbatim_prefix_mismatch() {
858        let mut report = DuplicationReport {
859            clone_groups: vec![CloneGroup {
860                instances: vec![
861                    CloneInstance {
862                        file: PathBuf::from(r"\\?\C:\repo\src\changed.ts"),
863                        start_line: 1,
864                        end_line: 5,
865                        start_col: 0,
866                        end_col: 10,
867                        fragment: "code".into(),
868                    },
869                    CloneInstance {
870                        file: PathBuf::from(r"\\?\C:\repo\src\focused-copy.ts"),
871                        start_line: 1,
872                        end_line: 5,
873                        start_col: 0,
874                        end_col: 10,
875                        fragment: "code".into(),
876                    },
877                ],
878                token_count: 20,
879                line_count: 5,
880            }],
881            clone_families: vec![],
882            mirrored_directories: vec![],
883            stats: DuplicationStats {
884                total_files: 2,
885                files_with_clones: 2,
886                total_lines: 100,
887                duplicated_lines: 10,
888                total_tokens: 200,
889                duplicated_tokens: 40,
890                clone_groups: 1,
891                clone_instances: 2,
892                duplication_percentage: 10.0,
893                clone_groups_below_min_occurrences: 0,
894            },
895        };
896
897        let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
898        changed.insert(PathBuf::from(r"C:\repo\src\changed.ts"));
899
900        filter_duplication_by_changed_files(&mut report, &changed, Path::new(""));
901        assert_eq!(
902            report.clone_groups.len(),
903            1,
904            "verbatim instance path must match non-verbatim changed-file entry"
905        );
906    }
907
908    #[cfg(windows)]
909    #[test]
910    fn filter_results_normalises_verbatim_prefix_mismatch() {
911        let mut results = AnalysisResults::default();
912        results
913            .unused_exports
914            .push(UnusedExportFinding::with_actions(UnusedExport {
915                path: PathBuf::from(r"\\?\C:\repo\src\a.ts"),
916                export_name: "foo".into(),
917                is_type_only: false,
918                line: 1,
919                col: 0,
920                span_start: 0,
921                is_re_export: false,
922            }));
923
924        let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
925        changed.insert(PathBuf::from(r"C:\repo\src\a.ts"));
926
927        filter_results_by_changed_files(&mut results, &changed);
928        assert_eq!(
929            results.unused_exports.len(),
930            1,
931            "verbatim finding path must match non-verbatim changed-file entry"
932        );
933    }
934
935    // -----------------------------------------------------------------------
936    // Real git interactions (tempdir + git init). These exercise the
937    // path-resolution boundary between `git rev-parse --show-toplevel`,
938    // `git diff --name-only`, and `git ls-files --full-name --others` to
939    // catch regressions like issue #190 where the LSP workspace was a
940    // subdirectory of the git repo and changed-file paths were joined
941    // against the wrong base.
942    // -----------------------------------------------------------------------
943
944    /// Initialize a temp git repo with a single committed file plus a tag
945    /// at HEAD. Returns the canonical repo root.
946    ///
947    /// Uses `dunce::canonicalize` rather than `std::fs::canonicalize` so the
948    /// returned path agrees with what `resolve_git_toplevel` produces in
949    /// production (PR #566 swapped that helper to `dunce::canonicalize` to
950    /// strip the Windows `\\?\` verbatim prefix). `std::fs::canonicalize`
951    /// still produces verbatim on Windows, so the prior shape diverged from
952    /// the production helper and downstream `changed.contains(&expected)`
953    /// assertions silently failed because one side was verbatim and the
954    /// other was not. POSIX behaviour is identical to `std::fs::canonicalize`.
955    fn init_repo(repo: &Path) -> PathBuf {
956        run_git(repo, &["init", "--quiet", "--initial-branch=main"]);
957        run_git(repo, &["config", "user.email", "test@example.com"]);
958        run_git(repo, &["config", "user.name", "test"]);
959        run_git(repo, &["config", "commit.gpgsign", "false"]);
960        std::fs::write(repo.join("seed.txt"), "seed\n").unwrap();
961        run_git(repo, &["add", "seed.txt"]);
962        run_git(repo, &["commit", "--quiet", "-m", "initial"]);
963        run_git(repo, &["tag", "fallow-baseline"]);
964        dunce::canonicalize(repo).unwrap()
965    }
966
967    fn run_git(cwd: &Path, args: &[&str]) {
968        let output = std::process::Command::new("git")
969            .args(args)
970            .current_dir(cwd)
971            .output()
972            .expect("git available");
973        assert!(
974            output.status.success(),
975            "git {args:?} failed: {}",
976            String::from_utf8_lossy(&output.stderr)
977        );
978    }
979
980    /// Workspace at git root, an untracked file is included in the
981    /// changed-files set with an absolute path joined from the repo root.
982    #[test]
983    fn try_get_changed_files_workspace_at_repo_root() {
984        let tmp = tempfile::tempdir().unwrap();
985        let repo = init_repo(tmp.path());
986        std::fs::create_dir_all(repo.join("src")).unwrap();
987        std::fs::write(repo.join("src/new.ts"), "export const x = 1;\n").unwrap();
988
989        let changed = try_get_changed_files(&repo, "fallow-baseline").unwrap();
990
991        let expected = repo.join("src/new.ts");
992        assert!(
993            changed.contains(&expected),
994            "changed set should contain {expected:?}; actual: {changed:?}"
995        );
996    }
997
998    /// Regression test for #190. When the workspace is a subdirectory of
999    /// the git repository, `git diff --name-only` emits paths relative to
1000    /// the repo root (e.g., `frontend/src/new.ts`). Without the
1001    /// rev-parse-based toplevel resolution the function joined those
1002    /// against the workspace root, producing bogus paths like
1003    /// `<repo>/frontend/frontend/src/new.ts` that never matched
1004    /// `analyze_project` output and silently dropped the filter.
1005    #[test]
1006    fn try_get_changed_files_workspace_in_subdirectory() {
1007        let tmp = tempfile::tempdir().unwrap();
1008        let repo = init_repo(tmp.path());
1009        let frontend = repo.join("frontend");
1010        std::fs::create_dir_all(frontend.join("src")).unwrap();
1011        std::fs::write(frontend.join("src/new.ts"), "export const x = 1;\n").unwrap();
1012
1013        let changed = try_get_changed_files(&frontend, "fallow-baseline").unwrap();
1014
1015        let expected = repo.join("frontend/src/new.ts");
1016        assert!(
1017            changed.contains(&expected),
1018            "changed set should contain canonical {expected:?}; actual: {changed:?}"
1019        );
1020        // Verify the bogus double-frontend path is NOT in the set
1021        let bogus = frontend.join("frontend/src/new.ts");
1022        assert!(
1023            !changed.contains(&bogus),
1024            "changed set must not contain double-frontend path {bogus:?}"
1025        );
1026    }
1027
1028    /// A *committed* change in a sibling subdirectory (outside the
1029    /// workspace) appears in the changed-files set because `git diff`
1030    /// is repo-wide regardless of cwd. The downstream
1031    /// `filter_results_by_changed_files` retains it only if
1032    /// `analyze_project` saw it; for a workspace scoped to one subdir,
1033    /// the sibling file is not in the analysis paths and falls away at
1034    /// the result-merge boundary, not here. This test pins the contract:
1035    /// for committed changes, the set is repo-wide.
1036    ///
1037    /// Note: `git ls-files --others --exclude-standard` only lists
1038    /// untracked files in cwd's subtree, so untracked siblings are NOT
1039    /// in the set when invoked from a subdirectory. That's harmless for
1040    /// the LSP because `analyze_project` only walks files under the
1041    /// workspace root either way.
1042    #[test]
1043    fn try_get_changed_files_includes_committed_sibling_changes() {
1044        let tmp = tempfile::tempdir().unwrap();
1045        let repo = init_repo(tmp.path());
1046        let backend = repo.join("backend");
1047        std::fs::create_dir_all(&backend).unwrap();
1048        std::fs::write(backend.join("server.py"), "print('hi')\n").unwrap();
1049        run_git(&repo, &["add", "."]);
1050        run_git(&repo, &["commit", "--quiet", "-m", "add backend"]);
1051
1052        let frontend = repo.join("frontend");
1053        std::fs::create_dir_all(&frontend).unwrap();
1054
1055        let changed = try_get_changed_files(&frontend, "fallow-baseline").unwrap();
1056
1057        let expected = repo.join("backend/server.py");
1058        assert!(
1059            changed.contains(&expected),
1060            "committed sibling backend/server.py should be in the set: {changed:?}"
1061        );
1062    }
1063
1064    /// Modifying a tracked file shows up via `git diff --name-only HEAD`,
1065    /// not just via `ls-files --others`. Confirm the path-join fix
1066    /// applies to that codepath too.
1067    #[test]
1068    fn try_get_changed_files_includes_modified_tracked_file() {
1069        let tmp = tempfile::tempdir().unwrap();
1070        let repo = init_repo(tmp.path());
1071        let frontend = repo.join("frontend");
1072        std::fs::create_dir_all(frontend.join("src")).unwrap();
1073        std::fs::write(frontend.join("src/old.ts"), "export const x = 1;\n").unwrap();
1074        run_git(&repo, &["add", "."]);
1075        run_git(&repo, &["commit", "--quiet", "-m", "add old"]);
1076        run_git(&repo, &["tag", "fallow-baseline-v2"]);
1077        // Modify the tracked file (no commit, so diff-HEAD picks it up)
1078        std::fs::write(frontend.join("src/old.ts"), "export const x = 2;\n").unwrap();
1079
1080        let changed = try_get_changed_files(&frontend, "fallow-baseline-v2").unwrap();
1081
1082        let expected = repo.join("frontend/src/old.ts");
1083        assert!(
1084            changed.contains(&expected),
1085            "modified tracked file {expected:?} missing from set: {changed:?}"
1086        );
1087    }
1088
1089    /// `resolve_git_toplevel` returns the canonical repo path even when
1090    /// invoked from inside a subdirectory and via a symlinked input path.
1091    /// On macOS this guards against the `/tmp` -> `/private/tmp`
1092    /// canonicalization gap that would otherwise make the LSP filter set
1093    /// disagree with `analyze_project` paths.
1094    #[test]
1095    fn resolve_git_toplevel_returns_canonical_path() {
1096        let tmp = tempfile::tempdir().unwrap();
1097        let repo = init_repo(tmp.path());
1098        let frontend = repo.join("frontend");
1099        std::fs::create_dir_all(&frontend).unwrap();
1100
1101        let toplevel = resolve_git_toplevel(&frontend).unwrap();
1102        assert_eq!(toplevel, repo, "toplevel should equal canonical repo root");
1103        // Use `dunce::canonicalize` rather than `std::fs::canonicalize` on
1104        // the RHS so the assertion stays self-consistent on Windows.
1105        // Production `resolve_git_toplevel` runs `dunce::canonicalize` (PR
1106        // #566); `std::fs::canonicalize` on Windows would re-add the `\\?\`
1107        // verbatim prefix and diverge from `toplevel`. POSIX is identical.
1108        assert_eq!(
1109            toplevel,
1110            dunce::canonicalize(&toplevel).unwrap(),
1111            "resolved toplevel should already be canonical"
1112        );
1113    }
1114
1115    /// Outside any git repo, `resolve_git_toplevel` returns
1116    /// `NotARepository` rather than panicking or returning a wrong path.
1117    /// The LSP relies on this to fall back to the workspace root cleanly.
1118    #[test]
1119    fn resolve_git_toplevel_not_a_repository() {
1120        let tmp = tempfile::tempdir().unwrap();
1121        let result = resolve_git_toplevel(tmp.path());
1122        assert!(
1123            matches!(result, Err(ChangedFilesError::NotARepository)),
1124            "expected NotARepository, got {result:?}"
1125        );
1126    }
1127
1128    /// `try_get_changed_files` propagates the not-a-repo error so the
1129    /// LSP can warn and fall back to full-scope results.
1130    #[test]
1131    fn try_get_changed_files_not_a_repository() {
1132        let tmp = tempfile::tempdir().unwrap();
1133        let result = try_get_changed_files(tmp.path(), "main");
1134        assert!(matches!(result, Err(ChangedFilesError::NotARepository)));
1135    }
1136
1137    #[test]
1138    fn filter_duplication_drops_groups_with_no_changed_instance() {
1139        let mut report = DuplicationReport {
1140            clone_groups: vec![CloneGroup {
1141                instances: vec![CloneInstance {
1142                    file: "/a.ts".into(),
1143                    start_line: 1,
1144                    end_line: 5,
1145                    start_col: 0,
1146                    end_col: 10,
1147                    fragment: "code".into(),
1148                }],
1149                token_count: 20,
1150                line_count: 5,
1151            }],
1152            clone_families: vec![],
1153            mirrored_directories: vec![],
1154            stats: DuplicationStats {
1155                total_files: 1,
1156                files_with_clones: 1,
1157                total_lines: 100,
1158                duplicated_lines: 5,
1159                total_tokens: 100,
1160                duplicated_tokens: 20,
1161                clone_groups: 1,
1162                clone_instances: 1,
1163                duplication_percentage: 5.0,
1164                clone_groups_below_min_occurrences: 0,
1165            },
1166        };
1167
1168        let changed: FxHashSet<PathBuf> = FxHashSet::default();
1169        filter_duplication_by_changed_files(&mut report, &changed, Path::new(""));
1170        assert!(report.clone_groups.is_empty());
1171        assert_eq!(report.stats.clone_groups, 0);
1172        assert_eq!(report.stats.clone_instances, 0);
1173        assert!((report.stats.duplication_percentage - 0.0).abs() < f64::EPSILON);
1174    }
1175}