Skip to content

Commit

Permalink
exclude bot commits from churn when --no-bots option is used (#1335)
Browse files Browse the repository at this point in the history
* init

* fix

* review

* early exit in is_bot_commit
  • Loading branch information
o2sh committed May 7, 2024
1 parent 2bc5aed commit 1775cc1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/info/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub fn traverse_commit_graph(

let (churn_thread, churn_tx) = get_churn_channel(
repo,
&mailmap,
no_bots.clone(),
&has_commit_graph_traversal_ended,
&total_number_of_commits,
max_churn_pool_size,
Expand Down Expand Up @@ -169,20 +171,27 @@ 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 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(
Expand Down Expand Up @@ -272,6 +281,19 @@ fn compute_diff_with_parent(
Ok(())
}

fn is_bot_commit(
commit: &Commit,
mailmap: &gix::mailmap::Snapshot,
bot_regex_pattern: Option<&MyRegex>,
) -> Result<bool> {
if bot_regex_pattern.is_some() {
let sig = mailmap.resolve(commit.author()?);
Ok(is_bot(&sig.name, bot_regex_pattern))
} else {
Ok(false)
}
}

fn is_bot(author_name: &BString, bot_regex_pattern: Option<&MyRegex>) -> bool {
bot_regex_pattern.map_or(false, |regex| {
regex.0.is_match(author_name.to_str_lossy().as_ref())
Expand Down

0 comments on commit 1775cc1

Please sign in to comment.