-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
exclude bot commits from churn when --no-bots
option is used
#1335
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ pub mod sig; | |
|
||
pub fn traverse_commit_graph( | ||
repo: &gix::Repository, | ||
no_bots: &Option<Option<MyRegex>>, | ||
no_bots: Option<&Option<MyRegex>>, | ||
max_churn_pool_size: Option<usize>, | ||
no_merges: bool, | ||
) -> Result<GitMetrics> { | ||
|
@@ -51,13 +51,15 @@ pub fn traverse_commit_graph( | |
|
||
let (churn_thread, churn_tx) = get_churn_channel( | ||
repo, | ||
&mailmap, | ||
bot_regex_pattern.clone(), | ||
&has_commit_graph_traversal_ended, | ||
&total_number_of_commits, | ||
max_churn_pool_size, | ||
)?; | ||
|
||
let author_threads = can_use_author_threads | ||
.then(|| get_author_channel(repo, num_threads, &bot_regex_pattern, &mailmap)); | ||
.then(|| get_author_channel(repo, num_threads, bot_regex_pattern.clone(), &mailmap)); | ||
|
||
let mut count = 0; | ||
for commit in commit_iter { | ||
|
@@ -73,7 +75,7 @@ pub fn traverse_commit_graph( | |
update_signature_counts( | ||
&commit.object()?, | ||
&mailmap, | ||
&bot_regex_pattern, | ||
bot_regex_pattern.as_ref(), | ||
&mut number_of_commits_by_signature, | ||
)?; | ||
} | ||
|
@@ -127,7 +129,7 @@ type NumberOfCommitsBySignature = HashMap<Sig, usize>; | |
fn get_author_channel( | ||
repo: &gix::Repository, | ||
num_threads: usize, | ||
bot_regex_pattern: &Option<MyRegex>, | ||
bot_regex_pattern: Option<MyRegex>, | ||
mailmap: &gix::mailmap::Snapshot, | ||
) -> ( | ||
Vec<JoinHandle<Result<NumberOfCommitsBySignature>>>, | ||
|
@@ -155,7 +157,7 @@ fn get_author_channel( | |
update_signature_counts( | ||
&commit, | ||
&mailmap, | ||
&bot_regex_pattern, | ||
bot_regex_pattern.as_ref(), | ||
&mut number_of_commits_by_signature, | ||
)?; | ||
} | ||
|
@@ -172,20 +174,29 @@ type ChurnPair = (NumberOfCommitsByFilepath, usize); | |
|
||
fn get_churn_channel( | ||
repo: &gix::Repository, | ||
mailmap: &gix::mailmap::Snapshot, | ||
bot_regex_pattern: Option<MyRegex>, | ||
has_commit_graph_traversal_ended: &Arc<AtomicBool>, | ||
total_number_of_commits: &Arc<AtomicUsize>, | ||
max_churn_pool_size: Option<usize>, | ||
) -> Result<(JoinHandle<Result<ChurnPair>>, Sender<ObjectId>)> { | ||
let (tx, rx) = channel::<gix::hash::ObjectId>(); | ||
let thread = std::thread::spawn({ | ||
let repo = repo.clone(); | ||
let mailmap = mailmap.clone(); | ||
let bot_regex_pattern = bot_regex_pattern.clone(); | ||
let has_commit_graph_traversal_ended = has_commit_graph_traversal_ended.clone(); | ||
let total_number_of_commits = total_number_of_commits.clone(); | ||
move || -> Result<_> { | ||
let mut number_of_commits_by_file_path = NumberOfCommitsByFilepath::new(); | ||
let mut number_of_diffs_computed = 0; | ||
while let Ok(commit_id) = rx.recv() { | ||
let commit = repo.find_object(commit_id)?.into_commit(); | ||
if bot_regex_pattern.is_some() | ||
&& is_bot_commit(&commit, &mailmap, bot_regex_pattern.as_ref())? | ||
{ | ||
continue; | ||
} | ||
compute_diff_with_parent(&mut number_of_commits_by_file_path, &commit, &repo)?; | ||
number_of_diffs_computed += 1; | ||
if should_break( | ||
|
@@ -223,7 +234,7 @@ fn should_break( | |
fn update_signature_counts( | ||
commit: &gix::Commit, | ||
mailmap: &gix::mailmap::Snapshot, | ||
bot_regex_pattern: &Option<MyRegex>, | ||
bot_regex_pattern: Option<&MyRegex>, | ||
number_of_commits_by_signature: &mut HashMap<Sig, usize>, | ||
) -> Result<()> { | ||
let sig = mailmap.resolve(commit.author()?); | ||
|
@@ -275,10 +286,10 @@ fn compute_diff_with_parent( | |
Ok(()) | ||
} | ||
|
||
fn get_no_bots_regex(no_bots: &Option<Option<MyRegex>>) -> Result<Option<MyRegex>> { | ||
let reg = if let Some(r) = no_bots.clone() { | ||
fn get_no_bots_regex(no_bots: Option<&Option<MyRegex>>) -> Result<Option<MyRegex>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that this is the same concern as #1335 (comment), and you'd want to make this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no more Option<Option... cf. #1340 |
||
let reg = if let Some(r) = no_bots { | ||
match r { | ||
Some(p) => Some(p), | ||
Some(p) => Some(p.clone()), | ||
None => Some(MyRegex(Regex::from_str(r"(?:-|\s)[Bb]ot$|\[[Bb]ot\]")?)), | ||
} | ||
} else { | ||
|
@@ -288,7 +299,16 @@ fn get_no_bots_regex(no_bots: &Option<Option<MyRegex>>) -> Result<Option<MyRegex | |
Ok(reg) | ||
} | ||
|
||
fn is_bot(author_name: &BString, bot_regex_pattern: &Option<MyRegex>) -> bool { | ||
fn is_bot_commit( | ||
commit: &Commit, | ||
mailmap: &gix::mailmap::Snapshot, | ||
bot_regex_pattern: Option<&MyRegex>, | ||
) -> Result<bool> { | ||
let sig = mailmap.resolve(commit.author()?); | ||
Ok(is_bot(&sig.name, bot_regex_pattern)) | ||
} | ||
|
||
fn is_bot(author_name: &BString, bot_regex_pattern: Option<&MyRegex>) -> bool { | ||
bot_regex_pattern.as_ref().map_or(false, |regex| { | ||
regex.0.is_match(author_name.to_str_lossy().as_ref()) | ||
}) | ||
|
@@ -303,18 +323,18 @@ mod tests { | |
fn test_get_no_bots_regex() -> Result<()> { | ||
// Test case 1: no_bots is None | ||
let no_bots: Option<Option<MyRegex>> = None; | ||
let result = get_no_bots_regex(&no_bots)?; | ||
let result = get_no_bots_regex(no_bots.as_ref())?; | ||
assert_eq!(result, None); | ||
|
||
// Test case 2: no_bots is Some(None) | ||
let no_bots: Option<Option<MyRegex>> = Some(None); | ||
let result = get_no_bots_regex(&no_bots)?; | ||
let result = get_no_bots_regex(no_bots.as_ref())?; | ||
assert_eq!(result.unwrap().0.as_str(), r"(?:-|\s)[Bb]ot$|\[[Bb]ot\]"); | ||
|
||
// Test case 3: no_bots is Some(Some(regex)) | ||
let regex = MyRegex(Regex::new(r"foo")?); | ||
let no_bots: Option<Option<MyRegex>> = Some(Some(regex)); | ||
let result = get_no_bots_regex(&no_bots)?; | ||
let result = get_no_bots_regex(no_bots.as_ref())?; | ||
assert_eq!(result.unwrap().0.as_str(), "foo"); | ||
|
||
Ok(()) | ||
|
@@ -328,8 +348,8 @@ mod tests { | |
#[case("bot", false)] | ||
fn test_is_bot(#[case] author_name: &str, #[case] expected: bool) -> Result<()> { | ||
let no_bots: Option<Option<MyRegex>> = Some(None); | ||
let regex = get_no_bots_regex(&no_bots)?; | ||
assert_eq!(is_bot(&author_name.into(), ®ex), expected); | ||
let regex = get_no_bots_regex(no_bots.as_ref())?; | ||
assert_eq!(is_bot(&author_name.into(), regex.as_ref()), expected); | ||
Ok(()) | ||
} | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extra check shouldn't be needed if
is_bot_commit()
would exit early. I agree that all the hassle might go away ifis_bot_commit()
would take the regex without anOption
right away.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, moved the check inside
is_bot_commit