Skip to content

Commit ade60c2

Browse files
committed
Initial commit
0 parents  commit ade60c2

File tree

6 files changed

+638
-0
lines changed

6 files changed

+638
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 101 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "codeowners-rs"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
walkdir = "2.0"
10+
anyhow = "1.0.66"
11+
regex = "1.6.0"
12+
13+
[profile.release]
14+
debug = true

src/main.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::{collections::HashMap, fs::File};
2+
3+
use anyhow::Result;
4+
use nfa::PatternNFA;
5+
6+
mod nfa;
7+
mod parser;
8+
9+
fn main() -> Result<()> {
10+
let rules = parser::parse_rules(File::open("./CODEOWNERS")?);
11+
12+
let mut nfa = PatternNFA::new();
13+
let rule_ids = rules
14+
.iter()
15+
.enumerate()
16+
.map(|(i, rule)| (nfa.add_pattern(&rule.pattern), i))
17+
.collect::<HashMap<_, _>>();
18+
19+
let root = ".";
20+
for entry in walk_files(root) {
21+
let path = entry
22+
.path()
23+
.strip_prefix(".") // TODO strip root?
24+
.unwrap_or_else(|_| entry.path());
25+
26+
match nfa.matching_patterns(path.to_str().unwrap()).iter().max() {
27+
Some(id) => {
28+
let rule = &rules[*rule_ids.get(id).unwrap()];
29+
println!("{:<70} {}", path.display(), rule.owners.join(" ")) // TODO join alloc?
30+
}
31+
None => println!("{:<70} (unowned)", path.display()),
32+
}
33+
}
34+
35+
Ok(())
36+
}
37+
38+
fn walk_files(root: &str) -> impl Iterator<Item = walkdir::DirEntry> {
39+
walkdir::WalkDir::new(root)
40+
.min_depth(1)
41+
.into_iter()
42+
.filter_map(|e| e.ok())
43+
.filter(|entry| entry.file_type().is_file())
44+
.filter(|entry| !entry.path().starts_with("./.git"))
45+
}

0 commit comments

Comments
 (0)