Skip to content

Commit 76e665f

Browse files
committed
ignore bots in commits
1 parent 0ede2b6 commit 76e665f

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Diff for: src/onefetch/cli.rs

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ 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,
3031
pub no_color_palette: bool,
3132
pub number_of_authors: usize,
3233
pub excluded: Vec<String>,
@@ -208,6 +209,11 @@ impl Cli {
208209
.long("no-merges")
209210
.help("Ignores merge commits."),
210211
)
212+
.arg(
213+
Arg::with_name("no-bots")
214+
.long("no-bots")
215+
.help("Ignore bots."),
216+
)
211217
.arg(
212218
Arg::with_name("isotime")
213219
.short("z")
@@ -263,6 +269,7 @@ impl Cli {
263269
};
264270
let no_bold = matches.is_present("no-bold");
265271
let no_merges = matches.is_present("no-merges");
272+
let no_bots = matches.is_present("no-bots");
266273
let no_color_palette = matches.is_present("no-palette");
267274
let print_languages = matches.is_present("languages");
268275
let print_package_managers = matches.is_present("package-managers");
@@ -360,6 +367,7 @@ impl Cli {
360367
image_backend,
361368
image_color_resolution,
362369
no_merges,
370+
no_bots,
363371
no_color_palette,
364372
number_of_authors,
365373
excluded,

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)?;
214+
let internal_repo = Repo::new(&repo, config.no_merges, config.no_bots)?;
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

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::onefetch::{commit_info::CommitInfo, error::*, utils};
22
use git2::{
3-
BranchType, Commit, Repository, RepositoryOpenFlags, Status, StatusOptions, StatusShow,
3+
BranchType, Commit, Repository, RepositoryOpenFlags, Signature, Status, StatusOptions,
4+
StatusShow,
45
};
56
use regex::Regex;
67
use std::path::Path;
@@ -11,12 +12,12 @@ pub struct Repo<'a> {
1112
}
1213

1314
impl<'a> Repo<'a> {
14-
pub fn new(repo: &'a Repository, no_merges: bool) -> Result<Self> {
15-
let logs = Repo::get_logs(repo, no_merges)?;
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)?;
1617
Ok(Self { repo, logs })
1718
}
1819

19-
fn get_logs(repo: &'a Repository, no_merges: bool) -> Result<Vec<Commit<'a>>> {
20+
fn get_logs(repo: &'a Repository, no_merges: bool, no_bots: bool) -> Result<Vec<Commit<'a>>> {
2021
let mut revwalk = repo.revwalk()?;
2122
revwalk.push_head()?;
2223
let logs: Vec<Commit<'a>> = revwalk
@@ -25,7 +26,8 @@ impl<'a> Repo<'a> {
2526
Ok(r) => repo
2627
.find_commit(r)
2728
.ok()
28-
.filter(|commit| !(no_merges && commit.parents().len() > 1)),
29+
.filter(|commit| !(no_merges && commit.parents().len() > 1))
30+
.filter(|commit| !(no_bots && is_bot(commit.author()))),
2931
})
3032
.collect();
3133

@@ -269,3 +271,8 @@ pub fn is_valid(repo_path: &str) -> Result<bool> {
269271
let repo = Repository::open_ext(repo_path, RepositoryOpenFlags::empty(), Vec::<&Path>::new());
270272
Ok(repo.is_ok() && !repo?.is_bare())
271273
}
274+
275+
pub fn is_bot(author: Signature) -> bool {
276+
let author_name = String::from_utf8_lossy(author.name_bytes()).into_owned();
277+
author_name.contains("[bot]")
278+
}

0 commit comments

Comments
 (0)