Skip to content

Commit

Permalink
no-bots with optional pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Jun 24, 2021
1 parent 76e665f commit 63d2234
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
20 changes: 16 additions & 4 deletions src/onefetch/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ pub struct Cli {
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
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<String>,
pub bot_exclude_pattern: Option<String>,
pub print_languages: bool,
pub print_package_managers: bool,
pub output: Option<SerializationFormat>,
Expand Down Expand Up @@ -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 <REGEX> to override the default pattern."),
)
.arg(
Arg::with_name("isotime")
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/onefetch/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl Info {
pub fn new(config: Cli) -> Result<Info> {
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()?;
Expand Down
24 changes: 18 additions & 6 deletions src/onefetch/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let logs = Repo::get_logs(repo, no_merges, no_bots)?;
pub fn new(
repo: &'a Repository,
no_merges: bool,
bot_exclude_pattern: &Option<String>,
) -> Result<Self> {
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<Vec<Commit<'a>>> {
fn get_logs(
repo: &'a Repository,
no_merges: bool,
bot_exclude_pattern: &Option<String>,
) -> Result<Vec<Commit<'a>>> {
let mut revwalk = repo.revwalk()?;
revwalk.push_head()?;
let logs: Vec<Commit<'a>> = revwalk
Expand All @@ -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();

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

pub fn is_bot(author: Signature) -> bool {
pub fn is_bot(author: Signature, bot_exclude_pattern: &Option<String>) -> 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)
}

0 comments on commit 63d2234

Please sign in to comment.