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

Conversation

o2sh
Copy link
Owner

@o2sh o2sh commented May 4, 2024

Fix for #1334

@o2sh o2sh added the fix label May 4, 2024
@o2sh o2sh requested a review from Byron May 4, 2024 22:29
Copy link
Collaborator

@Byron Byron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -172,20 +174,29 @@ type ChurnPair = (NumberOfCommitsByFilepath, usize);

fn get_churn_channel(
repo: &gix::Repository,
mailmap: &gix::mailmap::Snapshot,
bot_regex_pattern: &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.

Passing &Option<T> isn't idiomatic, but Option<&T> is. That requires .as_ref() on the call-site. This is likely the work of an IDE and refactoring tools.

It's a minor detail though and one doesn't have to care, it also seems to be done in other places that aren't touched here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there perhaps a blanket AsRef implementation that can be used? Something like this?

bot_regex_pattern: impl AsRef<Option<MyRegex>>

(also perhaps MyRegex should implement AsRef<Regex>)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that adding generics to method signatures would do more than allowing to avoid manual .as_ref(), which I'd be more than happy to do for clear compiler errors.
But in the end, it's probably Ok the way it is as it's most natural for IDEs. It's surprising clippy is happy with it though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH looking at this more it is kind of odd IMO to have functions that don't do anything significant unless that option is Some.
Basically instead of this

fn foo(ref: &Option<T1>) -> T2 {
    if bar(ref) {
        do_thing()
    } else {
        do_other_thing()
    }
}

fn bar(ref: &Option<T1>) -> bool {
    let thing = do_calculation();
    baz(thing, ref)
}

fn baz(thing: Thing, ref: &Option<T1>) -> bool {
    ref.as_ref().map_or(false, |t| t.process(thing))
}

I think it should be more like this:

fn foo(ref: &Option<T1>) -> T2 {
    if ref.as_ref().unwrap_or(false, bar) {
        do_thing()
    } else {
        do_other_thing()
    }
}

fn bar(ref: &T1) -> bool {
    let thing = do_calculation();
    ref.process(thing)
}

So, like you said, move the .as_ref call higher up in the "layers" of functions, but while we're at it these function calls could probably use some other cleanup. I think this non-idiomatic usage is partially caused by an overly complex flow of functions getting called.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Byron @spenserblack 1b682e9 Did I do it right?
There seems to be a little more cloning involved.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, as a follow up #1338

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a second follow up #1340

@@ -286,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 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

@o2sh o2sh merged commit 1775cc1 into main May 7, 2024
9 checks passed
@o2sh o2sh deleted the fix/churn-with-no-bots branch May 7, 2024 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants