Skip to content

Commit 2bc5aed

Browse files
authored
init (#1340)
1 parent 2c76c06 commit 2bc5aed

File tree

3 files changed

+23
-51
lines changed

3 files changed

+23
-51
lines changed

src/cli.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::str::FromStr;
1818
use strum::IntoEnumIterator;
1919

2020
const COLOR_RESOLUTIONS: [&str; 5] = ["16", "32", "64", "128", "256"];
21+
pub const NO_BOTS_DEFAULT_REGEX_PATTERN: &str = r"(?:-|\s)[Bb]ot$|\[[Bb]ot\]";
2122

2223
#[derive(Clone, Debug, Parser, PartialEq, Eq)]
2324
#[command(version, about)]
@@ -76,8 +77,14 @@ pub struct InfoCliOptions {
7677
#[arg(long, short, num_args = 1..)]
7778
pub exclude: Vec<String>,
7879
/// Exclude [bot] commits. Use <REGEX> to override the default pattern
79-
#[arg(long, value_name = "REGEX")]
80-
pub no_bots: Option<Option<MyRegex>>,
80+
#[arg(
81+
long,
82+
num_args = 0..=1,
83+
require_equals = true,
84+
default_missing_value = NO_BOTS_DEFAULT_REGEX_PATTERN,
85+
value_name = "REGEX"
86+
)]
87+
pub no_bots: Option<MyRegex>,
8188
/// Ignores merge commits
8289
#[arg(long)]
8390
pub no_merges: bool,

src/info/git/mod.rs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use gix::object::tree::diff::Action;
99
use gix::prelude::ObjectIdExt;
1010
use gix::traverse::commit::simple::Sorting;
1111
use gix::{Commit, ObjectId};
12-
use regex::Regex;
1312
use std::collections::HashMap;
14-
use std::str::FromStr;
1513
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1614
use std::sync::mpsc::{channel, Sender};
1715
use std::sync::Arc;
@@ -22,15 +20,14 @@ pub mod sig;
2220

2321
pub fn traverse_commit_graph(
2422
repo: &gix::Repository,
25-
no_bots: &Option<Option<MyRegex>>,
23+
no_bots: Option<MyRegex>,
2624
max_churn_pool_size: Option<usize>,
2725
no_merges: bool,
2826
) -> Result<GitMetrics> {
2927
let mut time_of_most_recent_commit = None;
3028
let mut time_of_first_commit = None;
3129
let mut number_of_commits_by_signature: HashMap<Sig, usize> = HashMap::new();
3230
let mailmap = repo.open_mailmap();
33-
let bot_regex_pattern = get_no_bots_regex(no_bots)?;
3431
let has_commit_graph_traversal_ended = Arc::new(AtomicBool::default());
3532
let total_number_of_commits = Arc::new(AtomicUsize::default());
3633

@@ -57,7 +54,7 @@ pub fn traverse_commit_graph(
5754
)?;
5855

5956
let author_threads = can_use_author_threads
60-
.then(|| get_author_channel(repo, num_threads, &bot_regex_pattern, &mailmap));
57+
.then(|| get_author_channel(repo, num_threads, no_bots.clone(), &mailmap));
6158

6259
let mut count = 0;
6360
for commit in commit_iter {
@@ -73,7 +70,7 @@ pub fn traverse_commit_graph(
7370
update_signature_counts(
7471
&commit.object()?,
7572
&mailmap,
76-
&bot_regex_pattern,
73+
no_bots.as_ref(),
7774
&mut number_of_commits_by_signature,
7875
)?;
7976
}
@@ -127,7 +124,7 @@ type NumberOfCommitsBySignature = HashMap<Sig, usize>;
127124
fn get_author_channel(
128125
repo: &gix::Repository,
129126
num_threads: usize,
130-
bot_regex_pattern: &Option<MyRegex>,
127+
bot_regex_pattern: Option<MyRegex>,
131128
mailmap: &gix::mailmap::Snapshot,
132129
) -> (
133130
Vec<JoinHandle<Result<NumberOfCommitsBySignature>>>,
@@ -155,7 +152,7 @@ fn get_author_channel(
155152
update_signature_counts(
156153
&commit,
157154
&mailmap,
158-
&bot_regex_pattern,
155+
bot_regex_pattern.as_ref(),
159156
&mut number_of_commits_by_signature,
160157
)?;
161158
}
@@ -223,7 +220,7 @@ fn should_break(
223220
fn update_signature_counts(
224221
commit: &gix::Commit,
225222
mailmap: &gix::mailmap::Snapshot,
226-
bot_regex_pattern: &Option<MyRegex>,
223+
bot_regex_pattern: Option<&MyRegex>,
227224
number_of_commits_by_signature: &mut HashMap<Sig, usize>,
228225
) -> Result<()> {
229226
let sig = mailmap.resolve(commit.author()?);
@@ -275,50 +272,18 @@ fn compute_diff_with_parent(
275272
Ok(())
276273
}
277274

278-
fn get_no_bots_regex(no_bots: &Option<Option<MyRegex>>) -> Result<Option<MyRegex>> {
279-
let reg = if let Some(r) = no_bots.clone() {
280-
match r {
281-
Some(p) => Some(p),
282-
None => Some(MyRegex(Regex::from_str(r"(?:-|\s)[Bb]ot$|\[[Bb]ot\]")?)),
283-
}
284-
} else {
285-
None
286-
};
287-
288-
Ok(reg)
289-
}
290-
291-
fn is_bot(author_name: &BString, bot_regex_pattern: &Option<MyRegex>) -> bool {
292-
bot_regex_pattern.as_ref().map_or(false, |regex| {
275+
fn is_bot(author_name: &BString, bot_regex_pattern: Option<&MyRegex>) -> bool {
276+
bot_regex_pattern.map_or(false, |regex| {
293277
regex.0.is_match(author_name.to_str_lossy().as_ref())
294278
})
295279
}
296280

297281
#[cfg(test)]
298282
mod tests {
299283
use super::*;
284+
use crate::cli::NO_BOTS_DEFAULT_REGEX_PATTERN;
300285
use rstest::rstest;
301-
302-
#[test]
303-
fn test_get_no_bots_regex() -> Result<()> {
304-
// Test case 1: no_bots is None
305-
let no_bots: Option<Option<MyRegex>> = None;
306-
let result = get_no_bots_regex(&no_bots)?;
307-
assert_eq!(result, None);
308-
309-
// Test case 2: no_bots is Some(None)
310-
let no_bots: Option<Option<MyRegex>> = Some(None);
311-
let result = get_no_bots_regex(&no_bots)?;
312-
assert_eq!(result.unwrap().0.as_str(), r"(?:-|\s)[Bb]ot$|\[[Bb]ot\]");
313-
314-
// Test case 3: no_bots is Some(Some(regex))
315-
let regex = MyRegex(Regex::new(r"foo")?);
316-
let no_bots: Option<Option<MyRegex>> = Some(Some(regex));
317-
let result = get_no_bots_regex(&no_bots)?;
318-
assert_eq!(result.unwrap().0.as_str(), "foo");
319-
320-
Ok(())
321-
}
286+
use std::str::FromStr;
322287

323288
#[rstest]
324289
#[case("John Doe", false)]
@@ -327,9 +292,9 @@ mod tests {
327292
#[case("foo-bot", true)]
328293
#[case("bot", false)]
329294
fn test_is_bot(#[case] author_name: &str, #[case] expected: bool) -> Result<()> {
330-
let no_bots: Option<Option<MyRegex>> = Some(None);
331-
let regex = get_no_bots_regex(&no_bots)?;
332-
assert_eq!(is_bot(&author_name.into(), &regex), expected);
295+
let from_str = MyRegex::from_str(NO_BOTS_DEFAULT_REGEX_PATTERN);
296+
let no_bots: Option<MyRegex> = Some(from_str?);
297+
assert_eq!(is_bot(&author_name.into(), no_bots.as_ref()), expected);
333298
Ok(())
334299
}
335300

src/info/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
151151

152152
let git_metrics = traverse_commit_graph(
153153
&repo,
154-
&cli_options.info.no_bots,
154+
cli_options.info.no_bots.clone(),
155155
cli_options.info.churn_pool_size,
156156
cli_options.info.no_merges,
157157
)?;

0 commit comments

Comments
 (0)