Skip to content

Commit d3d42b5

Browse files
committed
re-org
1 parent f42100d commit d3d42b5

4 files changed

Lines changed: 71 additions & 73 deletions

File tree

src/bin/compare_for_file.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
// This is a tool to compare the output of the original codeowners CLI with the optimized version.
2+
// It's useful for verifying that the optimized version is correct.
3+
//
4+
// It's not used in CI, but it's useful for debugging.
5+
//
6+
// To run it, use `cargo run --bin compare_for_file <absolute_project_root>`
7+
//
8+
// It will compare the output of the original codeowners CLI with the optimized version for all files in the project.
9+
110
use std::{
211
fs::File,
312
io::{self, Write},
@@ -6,15 +15,12 @@ use std::{
615
};
716

817
use codeowners::config::Config as OwnershipConfig;
18+
use codeowners::ownership::{FileOwner, for_file_fast};
919
use codeowners::runner::{RunConfig, Runner};
10-
use codeowners::ownership::{fast, FileOwner};
1120
use ignore::WalkBuilder;
12-
use serde_yaml;
1321

1422
fn main() {
15-
let project_root = std::env::args()
16-
.nth(1)
17-
.expect("usage: compare_for_file <absolute_project_root>");
23+
let project_root = std::env::args().nth(1).expect("usage: compare_for_file <absolute_project_root>");
1824
let project_root = PathBuf::from(project_root);
1925
if !project_root.is_absolute() {
2026
eprintln!("Project root must be absolute");
@@ -60,22 +66,24 @@ fn main() {
6066
let mut diff_count: usize = 0;
6167

6268
// Prefer tracked files from git; fall back to walking the FS if git is unavailable
63-
let tracked_files_output = Command::new("git")
64-
.arg("-C")
65-
.arg(&project_root)
66-
.arg("ls-files")
67-
.arg("-z")
68-
.output();
69+
let tracked_files_output = Command::new("git").arg("-C").arg(&project_root).arg("ls-files").arg("-z").output();
6970

7071
match tracked_files_output {
7172
Ok(output) if output.status.success() => {
7273
let bytes = output.stdout;
7374
for rel in bytes.split(|b| *b == 0u8) {
74-
if rel.is_empty() { continue; }
75-
let rel_str = match std::str::from_utf8(rel) { Ok(s) => s, Err(_) => continue };
75+
if rel.is_empty() {
76+
continue;
77+
}
78+
let rel_str = match std::str::from_utf8(rel) {
79+
Ok(s) => s,
80+
Err(_) => continue,
81+
};
7682
let abs_path = project_root.join(rel_str);
7783
// Only process regular files that currently exist
78-
if !abs_path.is_file() { continue; }
84+
if !abs_path.is_file() {
85+
continue;
86+
}
7987

8088
total_files += 1;
8189
let original = run_original(&runner, &abs_path);
@@ -90,10 +98,7 @@ fn main() {
9098
}
9199

92100
if total_files % 1000 == 0 {
93-
eprintln!(
94-
"Processed {} files... diffs so far: {}",
95-
total_files, diff_count
96-
);
101+
eprintln!("Processed {} files... diffs so far: {}", total_files, diff_count);
97102
}
98103
}
99104
}
@@ -132,10 +137,7 @@ fn main() {
132137
}
133138

134139
if total_files % 1000 == 0 {
135-
eprintln!(
136-
"Processed {} files... diffs so far: {}",
137-
total_files, diff_count
138-
);
140+
eprintln!("Processed {} files... diffs so far: {}", total_files, diff_count);
139141
}
140142
}
141143
}
@@ -159,7 +161,7 @@ fn run_original(runner: &Runner, file_path: &Path) -> String {
159161
}
160162

161163
fn run_optimized(project_root: &Path, config: &OwnershipConfig, file_path: &Path) -> String {
162-
let owners: Vec<FileOwner> = match fast::find_file_owners(project_root, config, file_path) {
164+
let owners: Vec<FileOwner> = match for_file_fast::find_file_owners(project_root, config, file_path) {
163165
Ok(v) => v,
164166
Err(e) => return format!("IO_ERROR: {}", e),
165167
};
@@ -175,5 +177,3 @@ fn run_optimized(project_root: &Path, config: &OwnershipConfig, file_path: &Path
175177
}
176178
}
177179
}
178-
179-

src/ownership.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use tracing::{info, instrument};
1111

1212
mod file_generator;
1313
mod file_owner_finder;
14+
pub mod for_file_fast;
1415
pub(crate) mod mapper;
1516
pub(crate) mod parser;
1617
mod validator;
17-
pub mod fast;
1818

1919
use crate::{
2020
ownership::mapper::DirectoryMapper,
Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{collections::{HashMap, HashSet}, fs, path::Path};
1+
use std::{
2+
collections::{HashMap, HashSet},
3+
fs,
4+
path::Path,
5+
};
26

37
use fast_glob::glob_match;
48
use glob::glob;
@@ -20,10 +24,7 @@ pub fn find_file_owners(project_root: &Path, config: &Config, file_path: &Path)
2024
.unwrap_or(&absolute_file_path)
2125
.to_path_buf();
2226

23-
let teams = match load_teams(project_root, &config.team_file_glob) {
24-
Ok(t) => t,
25-
Err(e) => return Err(e),
26-
};
27+
let teams = load_teams(project_root, &config.team_file_glob)?;
2728
let teams_by_name = build_teams_by_name_map(&teams);
2829

2930
let mut sources_by_team: HashMap<String, Vec<Source>> = HashMap::new();
@@ -68,11 +69,7 @@ pub fn find_file_owners(project_root: &Path, config: &Config, file_path: &Path)
6869
}
6970

7071
for team in &teams {
71-
let team_rel = team
72-
.path
73-
.strip_prefix(project_root)
74-
.unwrap_or(&team.path)
75-
.to_path_buf();
72+
let team_rel = team.path.strip_prefix(project_root).unwrap_or(&team.path).to_path_buf();
7673
if team_rel == relative_file_path {
7774
sources_by_team.entry(team.name.clone()).or_default().push(Source::TeamYml);
7875
}
@@ -99,18 +96,8 @@ pub fn find_file_owners(project_root: &Path, config: &Config, file_path: &Path)
9996
// This is simply matching the order of behavior of the original codeowners CLI
10097
if file_owners.len() > 1 {
10198
file_owners.sort_by(|a, b| {
102-
let priority_a = a
103-
.sources
104-
.iter()
105-
.map(|s| source_priority(s))
106-
.min()
107-
.unwrap_or(u8::MAX);
108-
let priority_b = b
109-
.sources
110-
.iter()
111-
.map(|s| source_priority(s))
112-
.min()
113-
.unwrap_or(u8::MAX);
99+
let priority_a = a.sources.iter().map(source_priority).min().unwrap_or(u8::MAX);
100+
let priority_b = b.sources.iter().map(source_priority).min().unwrap_or(u8::MAX);
114101
priority_a.cmp(&priority_b).then_with(|| a.team.name.cmp(&b.team.name))
115102
});
116103
}
@@ -204,7 +191,9 @@ fn most_specific_directory_owner(
204191
}
205192
}
206193
}
207-
if parent == project_root { break; }
194+
if parent == project_root {
195+
break;
196+
}
208197
current = parent.clone();
209198
}
210199
best
@@ -229,10 +218,10 @@ fn nearest_package_owner(
229218
if let Some(team) = teams_by_name.get(&owner) {
230219
let package_path = parent_rel.join("package.yml");
231220
let package_glob = format!("{}/**/**", rel_str);
232-
return Some((team.name.clone(), Source::Package(
233-
package_path.to_string_lossy().to_string(),
234-
package_glob,
235-
)));
221+
return Some((
222+
team.name.clone(),
223+
Source::Package(package_path.to_string_lossy().to_string(), package_glob),
224+
));
236225
}
237226
}
238227
}
@@ -244,16 +233,18 @@ fn nearest_package_owner(
244233
if let Some(team) = teams_by_name.get(&owner) {
245234
let package_path = parent_rel.join("package.json");
246235
let package_glob = format!("{}/**/**", rel_str);
247-
return Some((team.name.clone(), Source::Package(
248-
package_path.to_string_lossy().to_string(),
249-
package_glob,
250-
)));
236+
return Some((
237+
team.name.clone(),
238+
Source::Package(package_path.to_string_lossy().to_string(), package_glob),
239+
));
251240
}
252241
}
253242
}
254243
}
255244
}
256-
if parent == project_root { break; }
245+
if parent == project_root {
246+
break;
247+
}
257248
current = parent;
258249
}
259250
None
@@ -285,18 +276,22 @@ fn read_js_package_owner(path: &Path) -> std::result::Result<String, String> {
285276
.ok_or_else(|| "Missing owner".to_string())
286277
}
287278

288-
fn vendored_gem_owner(
289-
relative_file_path: &Path,
290-
config: &Config,
291-
teams: &[Team],
292-
) -> Option<(String, Source)> {
279+
fn vendored_gem_owner(relative_file_path: &Path, config: &Config, teams: &[Team]) -> Option<(String, Source)> {
293280
use std::path::Component;
294281
let mut comps = relative_file_path.components();
295282
let first = comps.next()?;
296283
let second = comps.next()?;
297-
let first_str = match first { Component::Normal(s) => s.to_string_lossy(), _ => return None };
298-
if first_str != config.vendored_gems_path { return None; }
299-
let gem_name = match second { Component::Normal(s) => s.to_string_lossy().to_string(), _ => return None };
284+
let first_str = match first {
285+
Component::Normal(s) => s.to_string_lossy(),
286+
_ => return None,
287+
};
288+
if first_str != config.vendored_gems_path {
289+
return None;
290+
}
291+
let gem_name = match second {
292+
Component::Normal(s) => s.to_string_lossy().to_string(),
293+
_ => return None,
294+
};
300295
for team in teams {
301296
if team.owned_gems.iter().any(|g| g == &gem_name) {
302297
return Some((team.name.clone(), Source::TeamGem));
@@ -316,5 +311,3 @@ fn source_priority(source: &Source) -> u8 {
316311
Source::TeamYml => 5,
317312
}
318313
}
319-
320-

src/runner.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,18 @@ fn for_file_optimized(run_config: &RunConfig, file_path: &str) -> RunResult {
9393
return RunResult {
9494
io_errors: vec![err.to_string()],
9595
..Default::default()
96-
}
96+
};
9797
}
9898
};
9999

100-
use crate::ownership::fast::find_file_owners;
100+
use crate::ownership::for_file_fast::find_file_owners;
101101
let file_owners = match find_file_owners(&run_config.project_root, &config, std::path::Path::new(file_path)) {
102102
Ok(v) => v,
103103
Err(err) => {
104-
return RunResult { io_errors: vec![err], ..Default::default() };
104+
return RunResult {
105+
io_errors: vec![err],
106+
..Default::default()
107+
};
105108
}
106109
};
107110

@@ -119,10 +122,12 @@ fn for_file_optimized(run_config: &RunConfig, file_path: &str) -> RunResult {
119122
};
120123
}
121124
};
122-
RunResult { info_messages, ..Default::default() }
125+
RunResult {
126+
info_messages,
127+
..Default::default()
128+
}
123129
}
124130

125-
126131
pub fn for_team(run_config: &RunConfig, team_name: &str) -> RunResult {
127132
run_with_runner(run_config, |runner| runner.for_team(team_name))
128133
}

0 commit comments

Comments
 (0)