Skip to content

Commit

Permalink
feat: ein tool hours -b ignores bots.
Browse files Browse the repository at this point in the history
For now it only considers bots with names containing `[bot]`.
  • Loading branch information
Byron committed Sep 18, 2022
1 parent f28783b commit 5d0332f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
24 changes: 21 additions & 3 deletions gitoxide-core/src/hours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ use std::{

use anyhow::{anyhow, bail};
use git_repository as git;
use git_repository::{actor, bstr::BString, interrupt, objs, prelude::*, progress, refs::file::ReferenceExt, Progress};
use git_repository::{
actor, bstr::BString, bstr::ByteSlice, interrupt, objs, prelude::*, progress, refs::file::ReferenceExt, Progress,
};
use itertools::Itertools;
use rayon::prelude::*;

/// Additional configuration for the hours estimation functionality.
pub struct Context<W> {
/// Ignore github bots which match the `[bot]` search string.
pub ignore_bots: bool,
/// Show personally identifiable information before the summary. Includes names and email addresses.
pub show_pii: bool,
/// Omit unifying identities by name and email which can lead to the same author appear multiple times
Expand All @@ -35,6 +39,7 @@ pub fn estimate<W, P>(
mut progress: P,
Context {
show_pii,
ignore_bots,
omit_unify_identities,
mut out,
}: Context<W>,
Expand Down Expand Up @@ -110,11 +115,17 @@ where
let mut current_email = &all_commits[0].email;
let mut slice_start = 0;
let mut results_by_hours = Vec::new();
let mut ignored_bot_commits = 0_u32;
for (idx, elm) in all_commits.iter().enumerate() {
if elm.email != *current_email {
results_by_hours.push(estimate_hours(&all_commits[slice_start..idx]));
let estimate = estimate_hours(&all_commits[slice_start..idx]);
slice_start = idx;
current_email = &elm.email;
if ignore_bots && estimate.name.contains_str(b"[bot]") {
ignored_bot_commits += estimate.num_commits;
continue;
}
results_by_hours.push(estimate);
}
}
if let Some(commits) = all_commits.get(slice_start..) {
Expand Down Expand Up @@ -170,7 +181,14 @@ where
(1.0 - (num_unique_authors as f32 / num_authors as f32)) * 100.0
)?;
}
assert_eq!(total_commits, all_commits.len() as u32, "need to get all commits");
if ignored_bot_commits != 0 {
writeln!(out, "commits by bots: {}", ignored_bot_commits,)?;
}
assert_eq!(
total_commits,
all_commits.len() as u32 - ignored_bot_commits,
"need to get all commits"
);
Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions src/porcelain/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn main() -> Result<()> {
crate::porcelain::options::ToolCommands::EstimateHours(crate::porcelain::options::EstimateHours {
working_dir,
refname,
no_bots,
show_pii,
omit_unify_identities,
}) => {
Expand All @@ -56,6 +57,7 @@ pub fn main() -> Result<()> {
progress,
hours::Context {
show_pii,
ignore_bots: no_bots,
omit_unify_identities,
out,
},
Expand Down
3 changes: 3 additions & 0 deletions src/porcelain/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ pub struct EstimateHours {
/// The name of the ref like 'HEAD' or 'main' at which to start iterating the commit graph.
#[clap(default_value("HEAD"))]
pub refname: OsString,
/// Ignore github bots which match the `[bot]` search string.
#[clap(short = 'b', long)]
pub no_bots: bool,
/// Show personally identifiable information before the summary. Includes names and email addresses.
#[clap(short = 'p', long)]
pub show_pii: bool,
Expand Down

0 comments on commit 5d0332f

Please sign in to comment.