Skip to content

Commit 63d2234

Browse files
committed
no-bots with optional pattern
1 parent 76e665f commit 63d2234

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

Diff for: src/onefetch/cli.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ pub struct Cli {
2727
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
2828
pub image_color_resolution: usize,
2929
pub no_merges: bool,
30-
pub no_bots: bool,
3130
pub no_color_palette: bool,
3231
pub number_of_authors: usize,
3332
pub excluded: Vec<String>,
33+
pub bot_exclude_pattern: Option<String>,
3434
pub print_languages: bool,
3535
pub print_package_managers: bool,
3636
pub output: Option<SerializationFormat>,
@@ -212,7 +212,10 @@ impl Cli {
212212
.arg(
213213
Arg::with_name("no-bots")
214214
.long("no-bots")
215-
.help("Ignore bots."),
215+
.min_values(0)
216+
.max_values(1)
217+
.value_name("REGEX")
218+
.help("Exclude [bot] commits. Use <REGEX> to override the default pattern."),
216219
)
217220
.arg(
218221
Arg::with_name("isotime")
@@ -269,7 +272,6 @@ impl Cli {
269272
};
270273
let no_bold = matches.is_present("no-bold");
271274
let no_merges = matches.is_present("no-merges");
272-
let no_bots = matches.is_present("no-bots");
273275
let no_color_palette = matches.is_present("no-palette");
274276
let print_languages = matches.is_present("languages");
275277
let print_package_managers = matches.is_present("package-managers");
@@ -356,6 +358,16 @@ impl Cli {
356358
Vec::new()
357359
};
358360

361+
let bot_exclude_pattern = if matches.is_present("no-bots") {
362+
if let Some(pattern) = matches.value_of("no-bots") {
363+
Some(String::from(pattern))
364+
} else {
365+
Some(String::from(r".*\[bot\].*"))
366+
}
367+
} else {
368+
None
369+
};
370+
359371
Ok(Cli {
360372
repo_path,
361373
ascii_input,
@@ -367,10 +379,10 @@ impl Cli {
367379
image_backend,
368380
image_color_resolution,
369381
no_merges,
370-
no_bots,
371382
no_color_palette,
372383
number_of_authors,
373384
excluded,
385+
bot_exclude_pattern,
374386
print_languages,
375387
print_package_managers,
376388
output,

Diff for: src/onefetch/info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl Info {
211211
pub fn new(config: Cli) -> Result<Info> {
212212
let git_version = cli_utils::get_git_version();
213213
let repo = Repository::discover(&config.repo_path)?;
214-
let internal_repo = Repo::new(&repo, config.no_merges, config.no_bots)?;
214+
let internal_repo = Repo::new(&repo, config.no_merges, &config.bot_exclude_pattern)?;
215215
let (repo_name, repo_url) = internal_repo.get_name_and_url()?;
216216
let head_refs = internal_repo.get_head_refs()?;
217217
let pending_changes = internal_repo.get_pending_changes()?;

Diff for: src/onefetch/repo.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ pub struct Repo<'a> {
1212
}
1313

1414
impl<'a> Repo<'a> {
15-
pub fn new(repo: &'a Repository, no_merges: bool, no_bots: bool) -> Result<Self> {
16-
let logs = Repo::get_logs(repo, no_merges, no_bots)?;
15+
pub fn new(
16+
repo: &'a Repository,
17+
no_merges: bool,
18+
bot_exclude_pattern: &Option<String>,
19+
) -> Result<Self> {
20+
let logs = Repo::get_logs(repo, no_merges, bot_exclude_pattern)?;
1721
Ok(Self { repo, logs })
1822
}
1923

20-
fn get_logs(repo: &'a Repository, no_merges: bool, no_bots: bool) -> Result<Vec<Commit<'a>>> {
24+
fn get_logs(
25+
repo: &'a Repository,
26+
no_merges: bool,
27+
bot_exclude_pattern: &Option<String>,
28+
) -> Result<Vec<Commit<'a>>> {
2129
let mut revwalk = repo.revwalk()?;
2230
revwalk.push_head()?;
2331
let logs: Vec<Commit<'a>> = revwalk
@@ -27,7 +35,10 @@ impl<'a> Repo<'a> {
2735
.find_commit(r)
2836
.ok()
2937
.filter(|commit| !(no_merges && commit.parents().len() > 1))
30-
.filter(|commit| !(no_bots && is_bot(commit.author()))),
38+
.filter(|commit| {
39+
!(bot_exclude_pattern.is_some()
40+
&& is_bot(commit.author(), bot_exclude_pattern))
41+
}),
3142
})
3243
.collect();
3344

@@ -272,7 +283,8 @@ pub fn is_valid(repo_path: &str) -> Result<bool> {
272283
Ok(repo.is_ok() && !repo?.is_bare())
273284
}
274285

275-
pub fn is_bot(author: Signature) -> bool {
286+
pub fn is_bot(author: Signature, bot_exclude_pattern: &Option<String>) -> bool {
276287
let author_name = String::from_utf8_lossy(author.name_bytes()).into_owned();
277-
author_name.contains("[bot]")
288+
let re = Regex::new(bot_exclude_pattern.as_ref().unwrap()).unwrap();
289+
re.is_match(&author_name)
278290
}

0 commit comments

Comments
 (0)