-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experimental support for .gitignore and CODEOWNERS (#14)
- Loading branch information
1 parent
e5d6bfb
commit 3bc40cc
Showing
11 changed files
with
288 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::io; | ||
|
||
pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> { | ||
let mut output_lines = Vec::new(); | ||
let mut block = Vec::new(); | ||
let mut is_sorting_block = false; | ||
|
||
for line in lines { | ||
if !line.trim().is_empty() { | ||
if is_single_line_comment(&line) { | ||
// Skip opening comment. | ||
output_lines.push(line); | ||
} else { | ||
is_sorting_block = true; | ||
block.push(line); | ||
} | ||
} else if is_sorting_block { | ||
is_sorting_block = false; | ||
block = sort(block); | ||
output_lines.append(&mut block); | ||
output_lines.push(line); | ||
} else { | ||
output_lines.push(line); | ||
} | ||
} | ||
|
||
if is_sorting_block { | ||
block = sort(block); | ||
output_lines.append(&mut block); | ||
} | ||
|
||
Ok(output_lines) | ||
} | ||
|
||
#[derive(Default)] | ||
struct Item { | ||
comment: Vec<String>, | ||
code: String, | ||
} | ||
|
||
/// Sorts a block of lines, keeping associated comments with their items. | ||
fn sort(block: Vec<String>) -> Vec<String> { | ||
let n = block.len(); | ||
let mut items = Vec::with_capacity(n); | ||
let mut current_item = Item::default(); | ||
for line in block { | ||
if is_single_line_comment(&line) { | ||
current_item.comment.push(line); | ||
} else { | ||
items.push(Item { | ||
comment: std::mem::take(&mut current_item.comment), | ||
code: line, | ||
}); | ||
} | ||
} | ||
let trailing_comments = std::mem::take(&mut current_item.comment); | ||
|
||
items.sort_by(|a, b| a.code.cmp(&b.code)); | ||
|
||
let mut result = Vec::with_capacity(n); | ||
for item in items { | ||
result.extend(item.comment); | ||
result.push(item.code); | ||
} | ||
result.extend(trailing_comments); | ||
|
||
result | ||
} | ||
|
||
fn is_single_line_comment(line: &str) -> bool { | ||
line.trim().starts_with('#') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod bazel; | ||
pub mod cargo_toml; | ||
pub mod generic; | ||
pub mod gitignore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#[macro_use] | ||
mod common; | ||
|
||
use keepsorted::Strategy::Gitignore; | ||
|
||
#[test] | ||
fn codeowners_simple_block() { | ||
test_inner!( | ||
Gitignore, | ||
r#" | ||
/.d/ @company/teams/a | ||
/.c/ @company/teams/b | ||
/.b/workflows @company/teams/c @company/teams/d | ||
/.a/CODEOWNERS @company/teams/e | ||
"#, | ||
r#" | ||
/.a/CODEOWNERS @company/teams/e | ||
/.b/workflows @company/teams/c @company/teams/d | ||
/.c/ @company/teams/b | ||
/.d/ @company/teams/a | ||
"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn codeowners_two_blocks() { | ||
test_inner!( | ||
Gitignore, | ||
r#" | ||
/.d/ @company/teams/a | ||
/.c/ @company/teams/b | ||
/.b/workflows @company/teams/c @company/teams/d | ||
/.a/CODEOWNERS @company/teams/e | ||
"#, | ||
r#" | ||
/.c/ @company/teams/b | ||
/.d/ @company/teams/a | ||
/.a/CODEOWNERS @company/teams/e | ||
/.b/workflows @company/teams/c @company/teams/d | ||
"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn codeowners_1() { | ||
test_inner!( | ||
Gitignore, | ||
r#" | ||
# [Misc] | ||
/b | ||
/a | ||
# [Bazel] | ||
/b | ||
/a | ||
# [Rust Lang] | ||
/b | ||
/a | ||
"#, | ||
r#" | ||
# [Misc] | ||
/a | ||
/b | ||
# [Bazel] | ||
/a | ||
/b | ||
# [Rust Lang] | ||
/a | ||
/b | ||
"# | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
b | ||
a | ||
|
||
# [Bazel] | ||
b | ||
a | ||
|
||
# [Rust] | ||
b | ||
a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
a | ||
b | ||
|
||
# [Bazel] | ||
a | ||
b | ||
|
||
# [Rust] | ||
a | ||
b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/b | ||
/a | ||
|
||
# [Bazel] | ||
/b | ||
/a | ||
|
||
# [Rust] | ||
/b | ||
/a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/a | ||
/b | ||
|
||
# [Bazel] | ||
/a | ||
/b | ||
|
||
# [Rust] | ||
/a | ||
/b |
Oops, something went wrong.