1use std::path::{Path, PathBuf};
17
18use rustc_hash::{FxHashMap, FxHashSet};
19
20use crate::duplicates::{DuplicationReport, DuplicationStats, families};
21use crate::results::AnalysisResults;
22
23pub fn validate_git_ref(s: &str) -> Result<&str, String> {
36 if s.is_empty() {
37 return Err("git ref cannot be empty".to_string());
38 }
39 if s.starts_with('-') {
40 return Err("git ref cannot start with '-'".to_string());
41 }
42 let mut in_braces = false;
43 for c in s.chars() {
44 match c {
45 '{' => in_braces = true,
46 '}' => in_braces = false,
47 ':' | ' ' if in_braces => {}
48 c if c.is_ascii_alphanumeric()
49 || matches!(c, '.' | '_' | '-' | '/' | '~' | '^' | '@' | '{' | '}') => {}
50 _ => return Err(format!("git ref contains disallowed character: '{c}'")),
51 }
52 }
53 if in_braces {
54 return Err("git ref has unclosed '{'".to_string());
55 }
56 Ok(s)
57}
58
59#[derive(Debug)]
62pub enum ChangedFilesError {
63 InvalidRef(String),
65 GitMissing(String),
67 NotARepository,
69 GitFailed(String),
71}
72
73impl ChangedFilesError {
74 pub fn describe(&self) -> String {
78 match self {
79 Self::InvalidRef(e) => format!("invalid git ref: {e}"),
80 Self::GitMissing(e) => format!("failed to run git: {e}"),
81 Self::NotARepository => "not a git repository".to_owned(),
82 Self::GitFailed(stderr) => augment_git_failed(stderr),
83 }
84 }
85}
86
87fn augment_git_failed(stderr: &str) -> String {
93 let lower = stderr.to_ascii_lowercase();
94 if lower.contains("not a valid object name")
95 || lower.contains("unknown revision")
96 || lower.contains("ambiguous argument")
97 {
98 format!(
99 "{stderr} (shallow clone? try `git fetch --unshallow`, or set `fetch-depth: 0` on actions/checkout / `GIT_DEPTH: 0` in GitLab CI)"
100 )
101 } else {
102 stderr.to_owned()
103 }
104}
105
106pub fn resolve_git_toplevel(cwd: &Path) -> Result<PathBuf, ChangedFilesError> {
117 let output = git_command(cwd, &["rev-parse", "--show-toplevel"])
118 .output()
119 .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
120
121 if !output.status.success() {
122 let stderr = String::from_utf8_lossy(&output.stderr);
123 return Err(if stderr.contains("not a git repository") {
124 ChangedFilesError::NotARepository
125 } else {
126 ChangedFilesError::GitFailed(stderr.trim().to_owned())
127 });
128 }
129
130 let raw = String::from_utf8_lossy(&output.stdout);
131 let trimmed = raw.trim();
132 if trimmed.is_empty() {
133 return Err(ChangedFilesError::GitFailed(
134 "git rev-parse --show-toplevel returned empty output".to_owned(),
135 ));
136 }
137
138 let path = PathBuf::from(trimmed);
139 Ok(path.canonicalize().unwrap_or(path))
140}
141
142fn collect_git_paths(
143 cwd: &Path,
144 toplevel: &Path,
145 args: &[&str],
146) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
147 let output = git_command(cwd, args)
148 .output()
149 .map_err(|e| ChangedFilesError::GitMissing(e.to_string()))?;
150
151 if !output.status.success() {
152 let stderr = String::from_utf8_lossy(&output.stderr);
153 return Err(if stderr.contains("not a git repository") {
154 ChangedFilesError::NotARepository
155 } else {
156 ChangedFilesError::GitFailed(stderr.trim().to_owned())
157 });
158 }
159
160 let files: FxHashSet<PathBuf> = String::from_utf8_lossy(&output.stdout)
166 .lines()
167 .filter(|line| !line.is_empty())
168 .map(|line| toplevel.join(line))
169 .collect();
170
171 Ok(files)
172}
173
174fn git_command(cwd: &Path, args: &[&str]) -> std::process::Command {
175 let mut command = std::process::Command::new("git");
176 command.args(args).current_dir(cwd);
177 crate::git_env::clear_ambient_git_env(&mut command);
178 command
179}
180
181pub fn try_get_changed_files(
199 root: &Path,
200 git_ref: &str,
201) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
202 validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
208 let toplevel = resolve_git_toplevel(root)?;
209 try_get_changed_files_with_toplevel(root, &toplevel, git_ref)
210}
211
212pub fn try_get_changed_files_with_toplevel(
220 cwd: &Path,
221 toplevel: &Path,
222 git_ref: &str,
223) -> Result<FxHashSet<PathBuf>, ChangedFilesError> {
224 validate_git_ref(git_ref).map_err(ChangedFilesError::InvalidRef)?;
225
226 let mut files = collect_git_paths(
227 cwd,
228 toplevel,
229 &[
230 "diff",
231 "--name-only",
232 "--end-of-options",
233 &format!("{git_ref}...HEAD"),
234 ],
235 )?;
236 files.extend(collect_git_paths(
237 cwd,
238 toplevel,
239 &["diff", "--name-only", "HEAD"],
240 )?);
241 files.extend(collect_git_paths(
246 cwd,
247 toplevel,
248 &["ls-files", "--full-name", "--others", "--exclude-standard"],
249 )?);
250 Ok(files)
251}
252
253#[expect(
257 clippy::print_stderr,
258 reason = "intentional user-facing warning for the CLI's --changed-since fallback path; LSP callers use try_get_changed_files instead"
259)]
260pub fn get_changed_files(root: &Path, git_ref: &str) -> Option<FxHashSet<PathBuf>> {
261 match try_get_changed_files(root, git_ref) {
262 Ok(files) => Some(files),
263 Err(ChangedFilesError::InvalidRef(e)) => {
264 eprintln!("Warning: --changed-since ignored: invalid git ref: {e}");
265 None
266 }
267 Err(ChangedFilesError::GitMissing(e)) => {
268 eprintln!("Warning: --changed-since ignored: failed to run git: {e}");
269 None
270 }
271 Err(ChangedFilesError::NotARepository) => {
272 eprintln!("Warning: --changed-since ignored: not a git repository");
273 None
274 }
275 Err(ChangedFilesError::GitFailed(stderr)) => {
276 eprintln!("Warning: --changed-since failed for ref '{git_ref}': {stderr}");
277 None
278 }
279 }
280}
281
282#[expect(
290 clippy::implicit_hasher,
291 reason = "fallow standardizes on FxHashSet across the workspace"
292)]
293pub fn filter_results_by_changed_files(
294 results: &mut AnalysisResults,
295 changed_files: &FxHashSet<PathBuf>,
296) {
297 results
298 .unused_files
299 .retain(|f| changed_files.contains(&f.path));
300 results
301 .unused_exports
302 .retain(|e| changed_files.contains(&e.path));
303 results
304 .unused_types
305 .retain(|e| changed_files.contains(&e.path));
306 results
307 .private_type_leaks
308 .retain(|e| changed_files.contains(&e.path));
309 results
310 .unused_enum_members
311 .retain(|m| changed_files.contains(&m.path));
312 results
313 .unused_class_members
314 .retain(|m| changed_files.contains(&m.path));
315 results
316 .unresolved_imports
317 .retain(|i| changed_files.contains(&i.path));
318
319 results.unlisted_dependencies.retain(|d| {
321 d.imported_from
322 .iter()
323 .any(|s| changed_files.contains(&s.path))
324 });
325
326 for dup in &mut results.duplicate_exports {
328 dup.locations
329 .retain(|loc| changed_files.contains(&loc.path));
330 }
331 results.duplicate_exports.retain(|d| d.locations.len() >= 2);
332
333 results
335 .circular_dependencies
336 .retain(|c| c.files.iter().any(|f| changed_files.contains(f)));
337
338 results
340 .boundary_violations
341 .retain(|v| changed_files.contains(&v.from_path));
342
343 results
345 .stale_suppressions
346 .retain(|s| changed_files.contains(&s.path));
347
348 results
351 .unresolved_catalog_references
352 .retain(|r| changed_files.contains(&r.path));
353 results
354 .empty_catalog_groups
355 .retain(|g| changed_files_contains_path(changed_files, &g.path));
356
357 results
361 .unused_dependency_overrides
362 .retain(|o| changed_files.contains(&o.path));
363 results
364 .misconfigured_dependency_overrides
365 .retain(|o| changed_files.contains(&o.path));
366}
367
368fn changed_files_contains_path(changed_files: &FxHashSet<PathBuf>, path: &Path) -> bool {
369 changed_files.contains(path)
370 || (path.is_relative() && changed_files.iter().any(|changed| changed.ends_with(path)))
371}
372
373fn recompute_duplication_stats(report: &DuplicationReport) -> DuplicationStats {
379 let mut files_with_clones: FxHashSet<&Path> = FxHashSet::default();
380 let mut file_dup_lines: FxHashMap<&Path, FxHashSet<usize>> = FxHashMap::default();
381 let mut duplicated_tokens = 0_usize;
382 let mut clone_instances = 0_usize;
383
384 for group in &report.clone_groups {
385 for instance in &group.instances {
386 files_with_clones.insert(&instance.file);
387 clone_instances += 1;
388 let lines = file_dup_lines.entry(&instance.file).or_default();
389 for line in instance.start_line..=instance.end_line {
390 lines.insert(line);
391 }
392 }
393 duplicated_tokens += group.token_count * group.instances.len();
394 }
395
396 let duplicated_lines: usize = file_dup_lines.values().map(FxHashSet::len).sum();
397
398 DuplicationStats {
399 total_files: report.stats.total_files,
400 files_with_clones: files_with_clones.len(),
401 total_lines: report.stats.total_lines,
402 duplicated_lines,
403 total_tokens: report.stats.total_tokens,
404 duplicated_tokens,
405 clone_groups: report.clone_groups.len(),
406 clone_instances,
407 #[expect(
408 clippy::cast_precision_loss,
409 reason = "stat percentages are display-only; precision loss at usize::MAX line counts is acceptable"
410 )]
411 duplication_percentage: if report.stats.total_lines > 0 {
412 (duplicated_lines as f64 / report.stats.total_lines as f64) * 100.0
413 } else {
414 0.0
415 },
416 clone_groups_below_min_occurrences: report.stats.clone_groups_below_min_occurrences,
417 }
418}
419
420#[expect(
425 clippy::implicit_hasher,
426 reason = "fallow standardizes on FxHashSet across the workspace"
427)]
428pub fn filter_duplication_by_changed_files(
429 report: &mut DuplicationReport,
430 changed_files: &FxHashSet<PathBuf>,
431 root: &Path,
432) {
433 report
434 .clone_groups
435 .retain(|g| g.instances.iter().any(|i| changed_files.contains(&i.file)));
436 report.clone_families = families::group_into_families(&report.clone_groups, root);
437 report.mirrored_directories =
438 families::detect_mirrored_directories(&report.clone_families, root);
439 report.stats = recompute_duplication_stats(report);
440}
441
442#[cfg(test)]
443mod tests {
444 use super::*;
445 use crate::duplicates::{CloneGroup, CloneInstance};
446 use crate::results::{
447 BoundaryViolation, CircularDependency, EmptyCatalogGroup, UnusedExport, UnusedFile,
448 };
449
450 #[test]
451 fn changed_files_error_describe_variants() {
452 assert!(
453 ChangedFilesError::InvalidRef("bad".to_owned())
454 .describe()
455 .contains("invalid git ref")
456 );
457 assert!(
458 ChangedFilesError::GitMissing("oops".to_owned())
459 .describe()
460 .contains("oops")
461 );
462 assert_eq!(
463 ChangedFilesError::NotARepository.describe(),
464 "not a git repository"
465 );
466 assert!(
467 ChangedFilesError::GitFailed("bad ref".to_owned())
468 .describe()
469 .contains("bad ref")
470 );
471 }
472
473 #[test]
474 fn augment_git_failed_appends_shallow_clone_hint_for_unknown_revision() {
475 let stderr = "fatal: ambiguous argument 'fallow-baseline...HEAD': unknown revision or path not in the working tree.";
476 let described = ChangedFilesError::GitFailed(stderr.to_owned()).describe();
477 assert!(described.contains(stderr), "original stderr preserved");
478 assert!(
479 described.contains("shallow clone"),
480 "hint surfaced: {described}"
481 );
482 assert!(
483 described.contains("fetch-depth: 0") || described.contains("git fetch --unshallow"),
484 "hint actionable: {described}"
485 );
486 }
487
488 #[test]
489 fn augment_git_failed_passthrough_for_other_errors() {
490 let stderr = "fatal: refusing to merge unrelated histories";
492 let described = ChangedFilesError::GitFailed(stderr.to_owned()).describe();
493 assert_eq!(described, stderr);
494 }
495
496 #[test]
497 fn validate_git_ref_rejects_leading_dash() {
498 assert!(validate_git_ref("--upload-pack=evil").is_err());
499 assert!(validate_git_ref("-flag").is_err());
500 }
501
502 #[test]
503 fn validate_git_ref_accepts_baseline_tag() {
504 assert_eq!(
505 validate_git_ref("fallow-baseline").unwrap(),
506 "fallow-baseline"
507 );
508 }
509
510 #[test]
511 fn try_get_changed_files_rejects_invalid_ref() {
512 let err = try_get_changed_files(Path::new("/"), "--evil")
514 .expect_err("leading-dash ref must be rejected");
515 assert!(matches!(err, ChangedFilesError::InvalidRef(_)));
516 assert!(err.describe().contains("cannot start with"));
517 }
518
519 #[test]
520 fn validate_git_ref_rejects_option_like_ref() {
521 assert!(validate_git_ref("--output=/tmp/fallow-proof").is_err());
522 }
523
524 #[test]
525 fn validate_git_ref_allows_reflog_relative_date() {
526 assert!(validate_git_ref("HEAD@{1 week ago}").is_ok());
527 }
528
529 #[test]
530 fn try_get_changed_files_rejects_option_like_ref_before_git() {
531 let root = tempfile::tempdir().expect("create temp dir");
532 let proof_path = root.path().join("proof");
533
534 let result = try_get_changed_files(
535 root.path(),
536 &format!("--output={}", proof_path.to_string_lossy()),
537 );
538
539 assert!(matches!(result, Err(ChangedFilesError::InvalidRef(_))));
540 assert!(
541 !proof_path.exists(),
542 "invalid changedSince ref must not be passed through to git as an option"
543 );
544 }
545
546 #[test]
547 fn git_command_clears_parent_git_environment() {
548 let command = git_command(Path::new("."), &["status", "--short"]);
549 let overrides: Vec<_> = command.get_envs().collect();
550
551 for var in crate::git_env::AMBIENT_GIT_ENV_VARS {
552 assert!(
553 overrides
554 .iter()
555 .any(|(key, value)| key.to_str() == Some(*var) && value.is_none()),
556 "git helper must clear inherited {var}",
557 );
558 }
559 }
560
561 #[test]
562 fn filter_results_keeps_only_changed_files() {
563 let mut results = AnalysisResults::default();
564 results.unused_files.push(UnusedFile {
565 path: "/a.ts".into(),
566 });
567 results.unused_files.push(UnusedFile {
568 path: "/b.ts".into(),
569 });
570 results.unused_exports.push(UnusedExport {
571 path: "/a.ts".into(),
572 export_name: "foo".into(),
573 is_type_only: false,
574 line: 1,
575 col: 0,
576 span_start: 0,
577 is_re_export: false,
578 });
579
580 let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
581 changed.insert("/a.ts".into());
582
583 filter_results_by_changed_files(&mut results, &changed);
584
585 assert_eq!(results.unused_files.len(), 1);
586 assert_eq!(results.unused_files[0].path, PathBuf::from("/a.ts"));
587 assert_eq!(results.unused_exports.len(), 1);
588 }
589
590 #[test]
591 fn filter_results_preserves_dependency_level_issues() {
592 let mut results = AnalysisResults::default();
593 results
594 .unused_dependencies
595 .push(crate::results::UnusedDependency {
596 package_name: "lodash".into(),
597 location: crate::results::DependencyLocation::Dependencies,
598 path: "/pkg.json".into(),
599 line: 3,
600 used_in_workspaces: Vec::new(),
601 });
602
603 let changed: FxHashSet<PathBuf> = FxHashSet::default();
604 filter_results_by_changed_files(&mut results, &changed);
605
606 assert_eq!(results.unused_dependencies.len(), 1);
608 }
609
610 #[test]
611 fn filter_results_keeps_circular_dep_when_any_file_changed() {
612 let mut results = AnalysisResults::default();
613 results.circular_dependencies.push(CircularDependency {
614 files: vec!["/a.ts".into(), "/b.ts".into()],
615 length: 2,
616 line: 1,
617 col: 0,
618 is_cross_package: false,
619 });
620
621 let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
622 changed.insert("/b.ts".into());
623
624 filter_results_by_changed_files(&mut results, &changed);
625 assert_eq!(results.circular_dependencies.len(), 1);
626 }
627
628 #[test]
629 fn filter_results_drops_circular_dep_when_no_file_changed() {
630 let mut results = AnalysisResults::default();
631 results.circular_dependencies.push(CircularDependency {
632 files: vec!["/a.ts".into(), "/b.ts".into()],
633 length: 2,
634 line: 1,
635 col: 0,
636 is_cross_package: false,
637 });
638
639 let changed: FxHashSet<PathBuf> = FxHashSet::default();
640 filter_results_by_changed_files(&mut results, &changed);
641 assert!(results.circular_dependencies.is_empty());
642 }
643
644 #[test]
645 fn filter_results_drops_boundary_violation_when_importer_unchanged() {
646 let mut results = AnalysisResults::default();
647 results.boundary_violations.push(BoundaryViolation {
648 from_path: "/a.ts".into(),
649 to_path: "/b.ts".into(),
650 from_zone: "ui".into(),
651 to_zone: "data".into(),
652 import_specifier: "../data/db".into(),
653 line: 1,
654 col: 0,
655 });
656
657 let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
658 changed.insert("/b.ts".into());
660
661 filter_results_by_changed_files(&mut results, &changed);
662 assert!(results.boundary_violations.is_empty());
663 }
664
665 #[test]
666 fn filter_results_keeps_relative_empty_catalog_group_when_manifest_changed() {
667 let mut results = AnalysisResults::default();
668 results.empty_catalog_groups.push(EmptyCatalogGroup {
669 catalog_name: "legacy".into(),
670 path: PathBuf::from("pnpm-workspace.yaml"),
671 line: 4,
672 });
673
674 let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
675 changed.insert(PathBuf::from("/repo/pnpm-workspace.yaml"));
676
677 filter_results_by_changed_files(&mut results, &changed);
678
679 assert_eq!(results.empty_catalog_groups.len(), 1);
680 assert_eq!(results.empty_catalog_groups[0].catalog_name, "legacy");
681 }
682
683 #[test]
684 fn filter_duplication_keeps_groups_with_at_least_one_changed_instance() {
685 let mut report = DuplicationReport {
686 clone_groups: vec![CloneGroup {
687 instances: vec![
688 CloneInstance {
689 file: "/a.ts".into(),
690 start_line: 1,
691 end_line: 5,
692 start_col: 0,
693 end_col: 10,
694 fragment: "code".into(),
695 },
696 CloneInstance {
697 file: "/b.ts".into(),
698 start_line: 1,
699 end_line: 5,
700 start_col: 0,
701 end_col: 10,
702 fragment: "code".into(),
703 },
704 ],
705 token_count: 20,
706 line_count: 5,
707 }],
708 clone_families: vec![],
709 mirrored_directories: vec![],
710 stats: DuplicationStats {
711 total_files: 2,
712 files_with_clones: 2,
713 total_lines: 100,
714 duplicated_lines: 10,
715 total_tokens: 200,
716 duplicated_tokens: 40,
717 clone_groups: 1,
718 clone_instances: 2,
719 duplication_percentage: 10.0,
720 clone_groups_below_min_occurrences: 0,
721 },
722 };
723
724 let mut changed: FxHashSet<PathBuf> = FxHashSet::default();
725 changed.insert("/a.ts".into());
726
727 filter_duplication_by_changed_files(&mut report, &changed, Path::new(""));
728 assert_eq!(report.clone_groups.len(), 1);
729 assert_eq!(report.stats.clone_groups, 1);
731 assert_eq!(report.stats.clone_instances, 2);
732 }
733
734 fn init_repo(repo: &Path) -> PathBuf {
746 run_git(repo, &["init", "--quiet", "--initial-branch=main"]);
747 run_git(repo, &["config", "user.email", "test@example.com"]);
748 run_git(repo, &["config", "user.name", "test"]);
749 run_git(repo, &["config", "commit.gpgsign", "false"]);
750 std::fs::write(repo.join("seed.txt"), "seed\n").unwrap();
751 run_git(repo, &["add", "seed.txt"]);
752 run_git(repo, &["commit", "--quiet", "-m", "initial"]);
753 run_git(repo, &["tag", "fallow-baseline"]);
754 repo.canonicalize().unwrap()
755 }
756
757 fn run_git(cwd: &Path, args: &[&str]) {
758 let output = std::process::Command::new("git")
759 .args(args)
760 .current_dir(cwd)
761 .output()
762 .expect("git available");
763 assert!(
764 output.status.success(),
765 "git {args:?} failed: {}",
766 String::from_utf8_lossy(&output.stderr)
767 );
768 }
769
770 #[test]
773 fn try_get_changed_files_workspace_at_repo_root() {
774 let tmp = tempfile::tempdir().unwrap();
775 let repo = init_repo(tmp.path());
776 std::fs::create_dir_all(repo.join("src")).unwrap();
777 std::fs::write(repo.join("src/new.ts"), "export const x = 1;\n").unwrap();
778
779 let changed = try_get_changed_files(&repo, "fallow-baseline").unwrap();
780
781 let expected = repo.join("src/new.ts");
782 assert!(
783 changed.contains(&expected),
784 "changed set should contain {expected:?}; actual: {changed:?}"
785 );
786 }
787
788 #[test]
796 fn try_get_changed_files_workspace_in_subdirectory() {
797 let tmp = tempfile::tempdir().unwrap();
798 let repo = init_repo(tmp.path());
799 let frontend = repo.join("frontend");
800 std::fs::create_dir_all(frontend.join("src")).unwrap();
801 std::fs::write(frontend.join("src/new.ts"), "export const x = 1;\n").unwrap();
802
803 let changed = try_get_changed_files(&frontend, "fallow-baseline").unwrap();
804
805 let expected = repo.join("frontend/src/new.ts");
806 assert!(
807 changed.contains(&expected),
808 "changed set should contain canonical {expected:?}; actual: {changed:?}"
809 );
810 let bogus = frontend.join("frontend/src/new.ts");
812 assert!(
813 !changed.contains(&bogus),
814 "changed set must not contain double-frontend path {bogus:?}"
815 );
816 }
817
818 #[test]
833 fn try_get_changed_files_includes_committed_sibling_changes() {
834 let tmp = tempfile::tempdir().unwrap();
835 let repo = init_repo(tmp.path());
836 let backend = repo.join("backend");
837 std::fs::create_dir_all(&backend).unwrap();
838 std::fs::write(backend.join("server.py"), "print('hi')\n").unwrap();
839 run_git(&repo, &["add", "."]);
840 run_git(&repo, &["commit", "--quiet", "-m", "add backend"]);
841
842 let frontend = repo.join("frontend");
843 std::fs::create_dir_all(&frontend).unwrap();
844
845 let changed = try_get_changed_files(&frontend, "fallow-baseline").unwrap();
846
847 let expected = repo.join("backend/server.py");
848 assert!(
849 changed.contains(&expected),
850 "committed sibling backend/server.py should be in the set: {changed:?}"
851 );
852 }
853
854 #[test]
858 fn try_get_changed_files_includes_modified_tracked_file() {
859 let tmp = tempfile::tempdir().unwrap();
860 let repo = init_repo(tmp.path());
861 let frontend = repo.join("frontend");
862 std::fs::create_dir_all(frontend.join("src")).unwrap();
863 std::fs::write(frontend.join("src/old.ts"), "export const x = 1;\n").unwrap();
864 run_git(&repo, &["add", "."]);
865 run_git(&repo, &["commit", "--quiet", "-m", "add old"]);
866 run_git(&repo, &["tag", "fallow-baseline-v2"]);
867 std::fs::write(frontend.join("src/old.ts"), "export const x = 2;\n").unwrap();
869
870 let changed = try_get_changed_files(&frontend, "fallow-baseline-v2").unwrap();
871
872 let expected = repo.join("frontend/src/old.ts");
873 assert!(
874 changed.contains(&expected),
875 "modified tracked file {expected:?} missing from set: {changed:?}"
876 );
877 }
878
879 #[test]
885 fn resolve_git_toplevel_returns_canonical_path() {
886 let tmp = tempfile::tempdir().unwrap();
887 let repo = init_repo(tmp.path());
888 let frontend = repo.join("frontend");
889 std::fs::create_dir_all(&frontend).unwrap();
890
891 let toplevel = resolve_git_toplevel(&frontend).unwrap();
892 assert_eq!(toplevel, repo, "toplevel should equal canonical repo root");
893 assert_eq!(
894 toplevel,
895 toplevel.canonicalize().unwrap(),
896 "resolved toplevel should already be canonical"
897 );
898 }
899
900 #[test]
904 fn resolve_git_toplevel_not_a_repository() {
905 let tmp = tempfile::tempdir().unwrap();
906 let result = resolve_git_toplevel(tmp.path());
907 assert!(
908 matches!(result, Err(ChangedFilesError::NotARepository)),
909 "expected NotARepository, got {result:?}"
910 );
911 }
912
913 #[test]
916 fn try_get_changed_files_not_a_repository() {
917 let tmp = tempfile::tempdir().unwrap();
918 let result = try_get_changed_files(tmp.path(), "main");
919 assert!(matches!(result, Err(ChangedFilesError::NotARepository)));
920 }
921
922 #[test]
923 fn filter_duplication_drops_groups_with_no_changed_instance() {
924 let mut report = DuplicationReport {
925 clone_groups: vec![CloneGroup {
926 instances: vec![CloneInstance {
927 file: "/a.ts".into(),
928 start_line: 1,
929 end_line: 5,
930 start_col: 0,
931 end_col: 10,
932 fragment: "code".into(),
933 }],
934 token_count: 20,
935 line_count: 5,
936 }],
937 clone_families: vec![],
938 mirrored_directories: vec![],
939 stats: DuplicationStats {
940 total_files: 1,
941 files_with_clones: 1,
942 total_lines: 100,
943 duplicated_lines: 5,
944 total_tokens: 100,
945 duplicated_tokens: 20,
946 clone_groups: 1,
947 clone_instances: 1,
948 duplication_percentage: 5.0,
949 clone_groups_below_min_occurrences: 0,
950 },
951 };
952
953 let changed: FxHashSet<PathBuf> = FxHashSet::default();
954 filter_duplication_by_changed_files(&mut report, &changed, Path::new(""));
955 assert!(report.clone_groups.is_empty());
956 assert_eq!(report.stats.clone_groups, 0);
957 assert_eq!(report.stats.clone_instances, 0);
958 assert!((report.stats.duplication_percentage - 0.0).abs() < f64::EPSILON);
959 }
960}