Skip to content

Commit eb526b2

Browse files
committed
CR, ignore bot commits
- validator for regex pattern provided to no-bots flag - switch type of bot_exclude_pattern to prevent multiple regex compilation - improve syntax for no-bots cli flag
1 parent 63d2234 commit eb526b2

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

Diff for: src/onefetch/cli.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use {
99
},
1010
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
1111
image::DynamicImage,
12+
regex::Regex,
1213
std::{convert::From, env, str::FromStr},
1314
strum::IntoEnumIterator,
1415
term_size,
@@ -30,7 +31,7 @@ pub struct Cli {
3031
pub no_color_palette: bool,
3132
pub number_of_authors: usize,
3233
pub excluded: Vec<String>,
33-
pub bot_exclude_pattern: Option<String>,
34+
pub bot_regex_pattern: Option<Regex>,
3435
pub print_languages: bool,
3536
pub print_package_managers: bool,
3637
pub output: Option<SerializationFormat>,
@@ -215,7 +216,14 @@ impl Cli {
215216
.min_values(0)
216217
.max_values(1)
217218
.value_name("REGEX")
218-
.help("Exclude [bot] commits. Use <REGEX> to override the default pattern."),
219+
.help("Exclude [bot] commits. Use <REGEX> to override the default pattern.")
220+
.validator(|p| {
221+
if Regex::from_str(&p).is_err() {
222+
return Err(String::from("must be a valid regex pattern"));
223+
} else {
224+
Ok(())
225+
}
226+
}),
219227
)
220228
.arg(
221229
Arg::with_name("isotime")
@@ -358,15 +366,11 @@ impl Cli {
358366
Vec::new()
359367
};
360368

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-
};
369+
let bot_regex_pattern = matches.is_present("no-bots").then(|| {
370+
matches
371+
.value_of("no-bots")
372+
.map_or(Regex::from_str(r"\[bot\]").unwrap(), |s| Regex::from_str(s).unwrap())
373+
});
370374

371375
Ok(Cli {
372376
repo_path,
@@ -382,7 +386,7 @@ impl Cli {
382386
no_color_palette,
383387
number_of_authors,
384388
excluded,
385-
bot_exclude_pattern,
389+
bot_regex_pattern,
386390
print_languages,
387391
print_package_managers,
388392
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.bot_exclude_pattern)?;
214+
let internal_repo = Repo::new(&repo, config.no_merges, &config.bot_regex_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

+7-8
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ impl<'a> Repo<'a> {
1515
pub fn new(
1616
repo: &'a Repository,
1717
no_merges: bool,
18-
bot_exclude_pattern: &Option<String>,
18+
bot_regex_pattern: &Option<Regex>,
1919
) -> Result<Self> {
20-
let logs = Repo::get_logs(repo, no_merges, bot_exclude_pattern)?;
20+
let logs = Repo::get_logs(repo, no_merges, bot_regex_pattern)?;
2121
Ok(Self { repo, logs })
2222
}
2323

2424
fn get_logs(
2525
repo: &'a Repository,
2626
no_merges: bool,
27-
bot_exclude_pattern: &Option<String>,
27+
bot_regex_pattern: &Option<Regex>,
2828
) -> Result<Vec<Commit<'a>>> {
2929
let mut revwalk = repo.revwalk()?;
3030
revwalk.push_head()?;
@@ -36,8 +36,8 @@ impl<'a> Repo<'a> {
3636
.ok()
3737
.filter(|commit| !(no_merges && commit.parents().len() > 1))
3838
.filter(|commit| {
39-
!(bot_exclude_pattern.is_some()
40-
&& is_bot(commit.author(), bot_exclude_pattern))
39+
!(bot_regex_pattern.is_some()
40+
&& is_bot(commit.author(), &bot_regex_pattern))
4141
}),
4242
})
4343
.collect();
@@ -283,8 +283,7 @@ pub fn is_valid(repo_path: &str) -> Result<bool> {
283283
Ok(repo.is_ok() && !repo?.is_bare())
284284
}
285285

286-
pub fn is_bot(author: Signature, bot_exclude_pattern: &Option<String>) -> bool {
286+
pub fn is_bot(author: Signature, bot_regex_pattern: &Option<Regex>) -> bool {
287287
let author_name = String::from_utf8_lossy(author.name_bytes()).into_owned();
288-
let re = Regex::new(bot_exclude_pattern.as_ref().unwrap()).unwrap();
289-
re.is_match(&author_name)
288+
bot_regex_pattern.as_ref().unwrap().is_match(&author_name)
290289
}

0 commit comments

Comments
 (0)