Skip to content

ignore bots in commits #448

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

Merged
merged 3 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/onefetch/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use {
},
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
image::DynamicImage,
regex::Regex,
std::{convert::From, env, str::FromStr},
strum::IntoEnumIterator,
term_size,
Expand All @@ -30,6 +31,7 @@ pub struct Cli {
pub no_color_palette: bool,
pub number_of_authors: usize,
pub excluded: Vec<String>,
pub bot_regex_pattern: Option<Regex>,
pub print_languages: bool,
pub print_package_managers: bool,
pub output: Option<SerializationFormat>,
Expand Down Expand Up @@ -208,6 +210,21 @@ impl Cli {
.long("no-merges")
.help("Ignores merge commits."),
)
.arg(
Arg::with_name("no-bots")
.long("no-bots")
.min_values(0)
.max_values(1)
.value_name("REGEX")
.help("Exclude [bot] commits. Use <REGEX> to override the default pattern.")
.validator(|p| {
if Regex::from_str(&p).is_err() {
return Err(String::from("must be a valid regex pattern"));
} else {
Ok(())
}
}),
)
.arg(
Arg::with_name("isotime")
.short("z")
Expand Down Expand Up @@ -349,6 +366,12 @@ impl Cli {
Vec::new()
};

let bot_regex_pattern = matches.is_present("no-bots").then(|| {
matches
.value_of("no-bots")
.map_or(Regex::from_str(r"\[bot\]").unwrap(), |s| Regex::from_str(s).unwrap())
});

Ok(Cli {
repo_path,
ascii_input,
Expand All @@ -363,6 +386,7 @@ impl Cli {
no_color_palette,
number_of_authors,
excluded,
bot_regex_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)?;
let internal_repo = Repo::new(&repo, config.no_merges, &config.bot_regex_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
28 changes: 23 additions & 5 deletions src/onefetch/repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::onefetch::{commit_info::CommitInfo, error::*, utils};
use git2::{
BranchType, Commit, Repository, RepositoryOpenFlags, Status, StatusOptions, StatusShow,
BranchType, Commit, Repository, RepositoryOpenFlags, Signature, Status, StatusOptions,
StatusShow,
};
use regex::Regex;
use std::path::Path;
Expand All @@ -11,12 +12,20 @@ pub struct Repo<'a> {
}

impl<'a> Repo<'a> {
pub fn new(repo: &'a Repository, no_merges: bool) -> Result<Self> {
let logs = Repo::get_logs(repo, no_merges)?;
pub fn new(
repo: &'a Repository,
no_merges: bool,
bot_regex_pattern: &Option<Regex>,
) -> Result<Self> {
let logs = Repo::get_logs(repo, no_merges, bot_regex_pattern)?;
Ok(Self { repo, logs })
}

fn get_logs(repo: &'a Repository, no_merges: bool) -> Result<Vec<Commit<'a>>> {
fn get_logs(
repo: &'a Repository,
no_merges: bool,
bot_regex_pattern: &Option<Regex>,
) -> Result<Vec<Commit<'a>>> {
let mut revwalk = repo.revwalk()?;
revwalk.push_head()?;
let logs: Vec<Commit<'a>> = revwalk
Expand All @@ -25,7 +34,11 @@ impl<'a> Repo<'a> {
Ok(r) => repo
.find_commit(r)
.ok()
.filter(|commit| !(no_merges && commit.parents().len() > 1)),
.filter(|commit| !(no_merges && commit.parents().len() > 1))
.filter(|commit| {
!(bot_regex_pattern.is_some()
&& is_bot(commit.author(), &bot_regex_pattern))
}),
})
.collect();

Expand Down Expand Up @@ -269,3 +282,8 @@ pub fn is_valid(repo_path: &str) -> Result<bool> {
let repo = Repository::open_ext(repo_path, RepositoryOpenFlags::empty(), Vec::<&Path>::new());
Ok(repo.is_ok() && !repo?.is_bare())
}

pub fn is_bot(author: Signature, bot_regex_pattern: &Option<Regex>) -> bool {
let author_name = String::from_utf8_lossy(author.name_bytes()).into_owned();
bot_regex_pattern.as_ref().unwrap().is_match(&author_name)
}