Skip to content
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 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions src/info/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
)?;
}
Expand Down Expand Up @@ -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>>>,
Expand Down Expand Up @@ -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,
)?;
}
Expand All @@ -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()
Copy link
Collaborator

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 if is_bot_commit() would take the regex without an Option right away.

Copy link
Owner Author

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

&& 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 @@ -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()?);
Expand Down Expand Up @@ -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>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 Option<Option<&T>>.

Copy link
Owner Author

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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())
})
Expand All @@ -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(())
Expand All @@ -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(), &regex), expected);
let regex = get_no_bots_regex(no_bots.as_ref())?;
assert_eq!(is_bot(&author_name.into(), regex.as_ref()), expected);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {

let git_metrics = traverse_commit_graph(
&repo,
&cli_options.info.no_bots,
cli_options.info.no_bots.as_ref(),
cli_options.info.churn_pool_size,
cli_options.info.no_merges,
)?;
Expand Down
Loading