1
0
mirror of https://github.com/o2sh/onefetch synced 2024-06-28 13:44:36 +00:00

exclude bot commits from churn when --no-bots option is used (#1335)

* init

* fix

* review

* early exit in is_bot_commit
This commit is contained in:
Ossama Hjaji 2024-05-07 19:07:14 +02:00 committed by GitHub
parent 2bc5aed55b
commit 1775cc1c32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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,
@ -169,6 +171,8 @@ 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>,
@ -176,6 +180,8 @@ fn get_churn_channel(
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<_> {
@ -183,6 +189,9 @@ fn get_churn_channel(
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(
@ -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())