Skip to content

Commit

Permalink
feat: support --author flag for git-shortlog
Browse files Browse the repository at this point in the history
This patch adds support for `--author`, which passes the values straight
to `git-shortlog` [[1]].

Sometimes, it is useful to filter the shortlog by authors. In the past,
people might do this by piping the output of `git-stats` to `grep` or
similar. However, with the introduction of the Totals line, that line
becomes inaccurate.

This way, they can still get an accurate Total by filtering with `git`
directly. This can be useful, for example, for filtering out automated
commits (e.g., dependabot) or non-company contributors.

[1]: https://git-scm.com/docs/git-shortlog
  • Loading branch information
lukehsiao committed May 3, 2024
1 parent d806f0f commit ed80049
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ Arguments:
[revision-range] Show only commits in the specified revision range [default: HEAD]
Options:
-e, --email Show the email address of each author
-r, --reviews Show who reviewed/tested commits based on `Acked-by`, `Tested-by`, and `Reviewed-by` git trailers
-s, --sort <SORT> What column to sort by [default: commits] [possible values: author, commits, files, insertions, deletions, net]
--reverse Whether to reverse the sorting from descending to ascending
-h, --help Print help (see more with '--help')
-V, --version Print version
```
-e, --email Show the email address of each author
-r, --reviews Show who reviewed/tested commits based on `Acked-by`, `Tested-by`, and `Reviewed-by` git trailers
-s, --sort <SORT> What column to sort by [default: commits] [possible values: author, commits, files, insertions, deletions, net]
--reverse Whether to reverse the sorting from descending to ascending
-a, --author <AUTHOR> Limit the commits output to ones with author header lines that match the specified pattern (regular expression)
-h, --help Print help (see more with '--help')
-V, --version Print version
```
28 changes: 23 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ struct Cli {
/// Whether to reverse the sorting from descending to ascending
#[arg(long)]
reverse: bool,
/// Limit the commits output to ones with author header lines that match the specified pattern (regular expression).
///
/// With more than one --author=<pattern>, commits whose author matches any of the given patterns are chosen.
/// This is pased through as `--author` to `git shortlog`.
#[arg(short, long)]
author: Option<Vec<String>>,
}

#[derive(Clone, ValueEnum)]
Expand Down Expand Up @@ -113,12 +119,24 @@ fn main() -> Result<()> {
});
yansi::whenever(Condition::cached((HAVE_COLOR)()));

// Build up the command based on flags
let rev_range = cli.rev_range;
let raw_shortlog = if cli.email {
cmd!(sh, "git shortlog -sen {rev_range}").read()?
} else {
cmd!(sh, "git shortlog -sn {rev_range}").read()?
};
let author = cli.author.map(|authors| {
authors
.iter()
.map(|a| format!("--author={}", a))
.collect::<Vec<String>>()
});
let mut cmd = cmd!(sh, "git shortlog -sn");
if cli.email {
cmd = cmd.arg("-e")
}
if let Some(a) = author {
cmd = cmd.args(a);
}
cmd = cmd.arg(&rev_range);

let raw_shortlog = cmd.read()?;

let shortlog: Vec<(usize, &str)> = raw_shortlog
.lines()
Expand Down

0 comments on commit ed80049

Please sign in to comment.