Skip to content

Commit 1fdcc53

Browse files
committed
only validate tracked files using tracked files map
1 parent 1b87d0f commit 1fdcc53

File tree

9 files changed

+106
-213
lines changed

9 files changed

+106
-213
lines changed

src/config.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ pub struct Config {
2424

2525
#[serde(default = "default_ignore_dirs")]
2626
pub ignore_dirs: Vec<String>,
27-
28-
#[serde(default = "default_codeowners_override_config_file_path")]
29-
pub codeowners_override_config_file_path: String,
3027
}
3128

3229
#[allow(dead_code)]
@@ -63,10 +60,6 @@ fn vendored_gems_path() -> String {
6360
"vendored/".to_string()
6461
}
6562

66-
fn default_codeowners_override_config_file_path() -> String {
67-
".codeowners.yml".to_string()
68-
}
69-
7063
fn default_ignore_dirs() -> Vec<String> {
7164
vec![
7265
".cursor".to_owned(),

src/files.rs

Lines changed: 0 additions & 157 deletions
This file was deleted.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ pub mod cache;
22
pub(crate) mod common_test;
33
pub mod config;
44
pub mod crosscheck;
5-
pub(crate) mod files;
65
pub mod ownership;
76
pub(crate) mod project;
87
pub mod project_builder;
98
pub mod project_file_builder;
109
pub mod runner;
10+
pub(crate) mod tracked_files;

src/ownership/for_file_fast.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ mod tests {
303303
vendored_gems_path: vendored_path.to_string(),
304304
cache_directory: "tmp/cache/codeowners".to_string(),
305305
ignore_dirs: vec![],
306-
codeowners_override_config_file_path: ".codeowners.yml".to_string(),
307306
}
308307
}
309308

src/project_builder.rs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ use error_stack::{Report, Result, ResultExt};
88
use fast_glob::glob_match;
99
use ignore::{DirEntry, WalkBuilder, WalkParallel, WalkState};
1010
use rayon::iter::{IntoParallelIterator, ParallelIterator};
11-
use serde::Deserialize;
1211
use tracing::{instrument, warn};
1312

1413
use crate::{
1514
cache::Cache,
1615
config::Config,
17-
files,
1816
project::{DirectoryCodeownersFile, Error, Package, PackageType, Project, ProjectFile, Team, VendoredGem, deserializers},
1917
project_file_builder::ProjectFileBuilder,
18+
tracked_files,
2019
};
2120

2221
type AbsolutePath = PathBuf;
@@ -62,17 +61,17 @@ impl<'a> ProjectBuilder<'a> {
6261
// Prune traversal early: skip heavy and irrelevant directories
6362
let ignore_dirs = self.config.ignore_dirs.clone();
6463
let base_path = self.base_path.clone();
65-
let untracked_files = if should_skip_untracked_files(&self.base_path, &self.config.codeowners_override_config_file_path) {
66-
files::untracked_files(&base_path).unwrap_or_default()
67-
} else {
68-
vec![]
69-
};
64+
let tracked_files = tracked_files::find_tracked_files(&self.base_path);
7065

7166
builder.filter_entry(move |entry: &DirEntry| {
7267
let path = entry.path();
7368
let file_name = entry.file_name().to_str().unwrap_or("");
74-
if !untracked_files.is_empty() && untracked_files.contains(&path.to_path_buf()) {
75-
return false;
69+
if let Some(tracked_files) = &tracked_files {
70+
if let Some(ft) = entry.file_type() {
71+
if ft.is_file() && !tracked_files.contains_key(path) {
72+
return false;
73+
}
74+
}
7675
}
7776
if let Some(ft) = entry.file_type()
7877
&& ft.is_dir()
@@ -298,29 +297,6 @@ fn matches_globs(path: &Path, globs: &[String]) -> bool {
298297
}
299298
}
300299

301-
fn should_skip_untracked_files(base_path: &Path, codeowners_override_config_file_path: &str) -> bool {
302-
let path = base_path.join(codeowners_override_config_file_path);
303-
if !path.exists() {
304-
return false;
305-
}
306-
if let Ok(file) = File::open(path) {
307-
#[derive(Deserialize)]
308-
struct OverrideConfig {
309-
#[serde(default)]
310-
skip_untracked_files: bool,
311-
}
312-
313-
let config: OverrideConfig = match serde_yaml::from_reader(file) {
314-
Ok(cfg) => cfg,
315-
Err(_) => return false,
316-
};
317-
318-
return config.skip_untracked_files;
319-
}
320-
321-
false
322-
}
323-
324300
fn ruby_package_owner(path: &Path) -> Result<Option<String>, Error> {
325301
let file = File::open(path).change_context(Error::Io)?;
326302
let deserializer: deserializers::RubyPackage = serde_yaml::from_reader(file).change_context(Error::SerdeYaml)?;

src/tracked_files.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::{
2+
collections::HashMap,
3+
path::{Path, PathBuf},
4+
process::Command,
5+
};
6+
7+
pub(crate) fn find_tracked_files(base_path: &Path) -> Option<HashMap<PathBuf, bool>> {
8+
let output = Command::new("git")
9+
.args(["ls-files", "--full-name", "-z", "--", "."])
10+
.current_dir(base_path)
11+
.output()
12+
.ok()?;
13+
14+
if !output.status.success() {
15+
return None;
16+
}
17+
18+
let results: HashMap<PathBuf, bool> = output
19+
.stdout
20+
.split(|&b| b == b'\0')
21+
.filter(|chunk| !chunk.is_empty())
22+
.map(|rel| std::str::from_utf8(rel).ok().map(|s| (base_path.join(s), true)))
23+
.collect::<Option<HashMap<PathBuf, bool>>>()?;
24+
25+
Some(results)
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn test_untracked_files() {
34+
let tmp_dir = tempfile::tempdir().unwrap();
35+
assert!(find_tracked_files(tmp_dir.path()).is_none());
36+
37+
std::process::Command::new("git")
38+
.arg("init")
39+
.current_dir(tmp_dir.path())
40+
.output()
41+
.expect("failed to run git init");
42+
43+
std::fs::write(tmp_dir.path().join("test.txt"), "test").unwrap();
44+
let tracked = find_tracked_files(tmp_dir.path()).unwrap();
45+
assert!(tracked.len() == 0);
46+
47+
std::process::Command::new("git")
48+
.arg("add")
49+
.arg("test.txt")
50+
.current_dir(tmp_dir.path())
51+
.output()
52+
.expect("failed to add test.txt");
53+
54+
let tracked = find_tracked_files(tmp_dir.path()).unwrap();
55+
assert!(tracked.len() == 1);
56+
assert!(tracked.get(&tmp_dir.path().join("test.txt")).unwrap());
57+
}
58+
}

tests/common/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@ pub fn copy_dir_recursive(from: &Path, to: &Path) {
3636
}
3737
}
3838

39+
#[allow(dead_code)]
40+
pub fn git_reset_all(path: &Path) {
41+
let status = Command::new("git")
42+
.arg("reset")
43+
.current_dir(path)
44+
.output()
45+
.expect("failed to run git reset --all");
46+
assert!(
47+
status.status.success(),
48+
"git reset --all failed: {}",
49+
String::from_utf8_lossy(&status.stderr)
50+
);
51+
}
52+
53+
#[allow(dead_code)]
54+
pub fn git_add_all_files(path: &Path) {
55+
let status = Command::new("git")
56+
.arg("add")
57+
.arg("--all")
58+
.current_dir(path)
59+
.output()
60+
.expect("failed to run git add --all");
61+
assert!(
62+
status.status.success(),
63+
"git add --all failed: {}",
64+
String::from_utf8_lossy(&status.stderr)
65+
);
66+
}
67+
3968
#[allow(dead_code)]
4069
pub fn init_git_repo(path: &Path) {
4170
let status = Command::new("git")

tests/crosscheck_owners_test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::{error::Error, fs, path::Path, process::Command};
66
mod common;
77
use common::setup_fixture_repo;
88

9+
use crate::common::git_add_all_files;
10+
911
const FIXTURE: &str = "tests/fixtures/valid_project";
1012

1113
#[test]
@@ -22,6 +24,7 @@ fn test_crosscheck_owners_reports_team_mismatch() -> Result<(), Box<dyn Error>>
2224
"/ruby/app/models/payroll.rb @PaymentsTeam",
2325
);
2426
fs::write(&codeowners_path, modified)?;
27+
git_add_all_files(project_root);
2528

2629
// Act + Assert
2730
Command::cargo_bin("codeowners")?
@@ -53,6 +56,7 @@ fn test_crosscheck_owners_reports_unowned_mismatch() -> Result<(), Box<dyn Error
5356
.map(|l| format!("{}\n", l))
5457
.collect();
5558
fs::write(&codeowners_path, modified)?;
59+
git_add_all_files(project_root);
5660

5761
// Act + Assert
5862
Command::cargo_bin("codeowners")?

0 commit comments

Comments
 (0)