From 63d22345fa679a96a69a694a874ad481802e6260 Mon Sep 17 00:00:00 2001 From: o2sh Date: Fri, 25 Jun 2021 01:22:14 +0200 Subject: [PATCH] no-bots with optional pattern --- src/onefetch/cli.rs | 20 ++++++++++++++++---- src/onefetch/info.rs | 2 +- src/onefetch/repo.rs | 24 ++++++++++++++++++------ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/onefetch/cli.rs b/src/onefetch/cli.rs index a289e5535..d1f28eb56 100644 --- a/src/onefetch/cli.rs +++ b/src/onefetch/cli.rs @@ -27,10 +27,10 @@ pub struct Cli { pub image_backend: Option>, pub image_color_resolution: usize, pub no_merges: bool, - pub no_bots: bool, pub no_color_palette: bool, pub number_of_authors: usize, pub excluded: Vec, + pub bot_exclude_pattern: Option, pub print_languages: bool, pub print_package_managers: bool, pub output: Option, @@ -212,7 +212,10 @@ impl Cli { .arg( Arg::with_name("no-bots") .long("no-bots") - .help("Ignore bots."), + .min_values(0) + .max_values(1) + .value_name("REGEX") + .help("Exclude [bot] commits. Use to override the default pattern."), ) .arg( Arg::with_name("isotime") @@ -269,7 +272,6 @@ impl Cli { }; let no_bold = matches.is_present("no-bold"); let no_merges = matches.is_present("no-merges"); - let no_bots = matches.is_present("no-bots"); let no_color_palette = matches.is_present("no-palette"); let print_languages = matches.is_present("languages"); let print_package_managers = matches.is_present("package-managers"); @@ -356,6 +358,16 @@ impl Cli { Vec::new() }; + let bot_exclude_pattern = if matches.is_present("no-bots") { + if let Some(pattern) = matches.value_of("no-bots") { + Some(String::from(pattern)) + } else { + Some(String::from(r".*\[bot\].*")) + } + } else { + None + }; + Ok(Cli { repo_path, ascii_input, @@ -367,10 +379,10 @@ impl Cli { image_backend, image_color_resolution, no_merges, - no_bots, no_color_palette, number_of_authors, excluded, + bot_exclude_pattern, print_languages, print_package_managers, output, diff --git a/src/onefetch/info.rs b/src/onefetch/info.rs index 86035c117..5864f2e16 100644 --- a/src/onefetch/info.rs +++ b/src/onefetch/info.rs @@ -211,7 +211,7 @@ impl Info { pub fn new(config: Cli) -> Result { let git_version = cli_utils::get_git_version(); let repo = Repository::discover(&config.repo_path)?; - let internal_repo = Repo::new(&repo, config.no_merges, config.no_bots)?; + let internal_repo = Repo::new(&repo, config.no_merges, &config.bot_exclude_pattern)?; let (repo_name, repo_url) = internal_repo.get_name_and_url()?; let head_refs = internal_repo.get_head_refs()?; let pending_changes = internal_repo.get_pending_changes()?; diff --git a/src/onefetch/repo.rs b/src/onefetch/repo.rs index 05d6c8f28..c21c4a6a8 100644 --- a/src/onefetch/repo.rs +++ b/src/onefetch/repo.rs @@ -12,12 +12,20 @@ pub struct Repo<'a> { } impl<'a> Repo<'a> { - pub fn new(repo: &'a Repository, no_merges: bool, no_bots: bool) -> Result { - let logs = Repo::get_logs(repo, no_merges, no_bots)?; + pub fn new( + repo: &'a Repository, + no_merges: bool, + bot_exclude_pattern: &Option, + ) -> Result { + let logs = Repo::get_logs(repo, no_merges, bot_exclude_pattern)?; Ok(Self { repo, logs }) } - fn get_logs(repo: &'a Repository, no_merges: bool, no_bots: bool) -> Result>> { + fn get_logs( + repo: &'a Repository, + no_merges: bool, + bot_exclude_pattern: &Option, + ) -> Result>> { let mut revwalk = repo.revwalk()?; revwalk.push_head()?; let logs: Vec> = revwalk @@ -27,7 +35,10 @@ impl<'a> Repo<'a> { .find_commit(r) .ok() .filter(|commit| !(no_merges && commit.parents().len() > 1)) - .filter(|commit| !(no_bots && is_bot(commit.author()))), + .filter(|commit| { + !(bot_exclude_pattern.is_some() + && is_bot(commit.author(), bot_exclude_pattern)) + }), }) .collect(); @@ -272,7 +283,8 @@ pub fn is_valid(repo_path: &str) -> Result { Ok(repo.is_ok() && !repo?.is_bare()) } -pub fn is_bot(author: Signature) -> bool { +pub fn is_bot(author: Signature, bot_exclude_pattern: &Option) -> bool { let author_name = String::from_utf8_lossy(author.name_bytes()).into_owned(); - author_name.contains("[bot]") + let re = Regex::new(bot_exclude_pattern.as_ref().unwrap()).unwrap(); + re.is_match(&author_name) }