From 203f5b48ad6198b5b93e48568c6916dfcd4d8485 Mon Sep 17 00:00:00 2001 From: spacewander Date: Fri, 25 Oct 2019 10:27:28 +0800 Subject: [PATCH] git-summary: add --dedup-by-email to remove duplicate users --- bin/git-summary | 80 +++++++++++++++++++++++++++++++++-- etc/git-extras-completion.zsh | 1 + man/git-summary.1 | 28 +++++++++++- man/git-summary.html | 21 ++++++++- man/git-summary.md | 18 +++++++- 5 files changed, 140 insertions(+), 8 deletions(-) diff --git a/bin/git-summary b/bin/git-summary index fca942f9c..cc77beb86 100755 --- a/bin/git-summary +++ b/bin/git-summary @@ -3,8 +3,32 @@ cd "$(git root)" || { echo "Can't cd to top level directory";exit 1; } +SUMMARY_BY_LINE= +DEDUP_BY_EMAIL= +for arg in "$@"; do + case "$arg" in + --line) + SUMMARY_BY_LINE=1 + ;; + --dedup-by-email) + DEDUP_BY_EMAIL=1 + ;; + *) + # set the argument back + set -- "$@" "$arg" + ;; + esac + + shift +done + +if [ -n "$DEDUP_BY_EMAIL" ] && [ -n "$SUMMARY_BY_LINE" ]; then + >&2 echo "--dedup-by-email used with --line is not supported" + exit 1 +fi + commit="" -test $# -ne 0 && commit=$@ +test $# -ne 0 && commit=$* project=${PWD##*/} # @@ -31,6 +55,7 @@ active_days() { # commit_count() { + # shellcheck disable=SC2086 git log --oneline $commit | wc -l | tr -d ' ' } @@ -42,6 +67,46 @@ file_count() { git ls-files | wc -l | tr -d ' ' } +# +# remove duplicate authors who belong to the same email address +# + +dedup_by_email() { + # in: + # 27 luo zexuan + # 7 罗泽轩 + # out: + # 34 luo zexuan + LC_ALL=C awk ' + { + sum += $1 + if ($NF in emails) { + emails[$NF] += $1 + } else { + email = $NF + emails[email] = $1 + # set commits/email to empty + $1=$NF="" + sub(/^[[:space:]]+/, "", $0) + sub(/[[:space:]]+$/, "", $0) + name = $0 + if (name in names) { + # when the same name is associated with existed email, + # merge the previous email into the later one. + emails[email] += emails[names[name]] + emails[names[name]] = 0 + } + names[name] = email + } + } + END { + for (name in names) { + email = names[name] + printf "%6d\t%s\n", emails[email], name + } + }' | sort -rn -k 1 +} + # # list authors # @@ -110,7 +175,7 @@ result() { echo echo " project : $project" -if test "$1" = "--line"; then +if [ -n "$SUMMARY_BY_LINE" ]; then echo " lines : $(line_count)" echo " authors :" lines | sort | uniq -c | sort -rn | format_authors @@ -120,8 +185,15 @@ else echo " active :" $(active_days) days echo " commits :" $(commit_count) if test "$commit" = ""; then - echo " files :" $(file_count) + echo " files :" "$(file_count)" fi echo " authors : " - git shortlog -n -s $commit | format_authors + if [ -n "$DEDUP_BY_EMAIL" ]; then + # the $commit can be empty + # shellcheck disable=SC2086 + git shortlog -n -s -e $commit | dedup_by_email | format_authors + else + # shellcheck disable=SC2086 + git shortlog -n -s $commit | format_authors + fi fi diff --git a/etc/git-extras-completion.zsh b/etc/git-extras-completion.zsh index dd3c18043..6a078f2fe 100644 --- a/etc/git-extras-completion.zsh +++ b/etc/git-extras-completion.zsh @@ -423,6 +423,7 @@ _git-standup() { _git-summary() { _arguments '--line[summarize with lines rather than commits]' + _arguments '--dedup-by-email[remove duplicate users by the email address]' __gitex_commits } diff --git a/man/git-summary.1 b/man/git-summary.1 index 3c1ba2d41..87870957f 100644 --- a/man/git-summary.1 +++ b/man/git-summary.1 @@ -7,7 +7,7 @@ \fBgit\-summary\fR \- Show repository summary . .SH "SYNOPSIS" -\fBgit\-summary\fR [\-\-line] [] +\fBgit\-summary\fR [\-\-line] [\-\-dedup\-by\-email] [] . .SH "DESCRIPTION" Shows a summary of the repository\. @@ -19,6 +19,32 @@ Shows a summary of the repository\. Summarize only the range of commits included in the \. . .P +\-\-dedup\-by\-email +. +.P +Remove duplicate authors who belong to the same email address\. For example, +. +.IP "" 4 +. +.nf + +$ git summary +\.\.\. +133 TJ Holowaychuk 9\.9% +115 Tj Holowaychuk 8\.5% + +$ git summary \-\-dedup\-by\-email +\.\.\. +248 TJ Holowaychuk 18\.4% +. +.fi +. +.IP "" 0 +. +.P +This option can not be used together with \fB\-\-line\fR\. +. +.P \-\-line . .P diff --git a/man/git-summary.html b/man/git-summary.html index 450115d84..c57271ada 100644 --- a/man/git-summary.html +++ b/man/git-summary.html @@ -76,7 +76,7 @@

NAME

SYNOPSIS

-

git-summary [--line] [<commitish>]

+

git-summary [--line] [--dedup-by-email] [<commitish>]

DESCRIPTION

@@ -88,6 +88,23 @@

OPTIONS

Summarize only the range of commits included in the <commitish>.

+

--dedup-by-email

+ +

Remove duplicate authors who belong to the same email address. + For example,

+ +
$ git summary
+...
+133  TJ Holowaychuk            9.9%
+115  Tj Holowaychuk            8.5%
+
+$ git summary --dedup-by-email
+...
+248  TJ Holowaychuk            18.4%
+
+ +

This option can not be used together with --line.

+

--line

Summarize with lines other than commits. @@ -145,7 +162,7 @@

EXAMPLES

AUTHOR

-

Written by Tj Holowaychuk <tj@vision-media.ca>

+

Written by Tj Holowaychuk <tj@vision-media.ca>

REPORTING BUGS

diff --git a/man/git-summary.md b/man/git-summary.md index 8946ef44c..d6a6336b4 100644 --- a/man/git-summary.md +++ b/man/git-summary.md @@ -3,7 +3,7 @@ git-summary(1) -- Show repository summary ## SYNOPSIS -`git-summary` [--line] [<commitish>] +`git-summary` [--line] [--dedup-by-email] [<commitish>] ## DESCRIPTION @@ -15,6 +15,22 @@ Shows a summary of the repository. Summarize only the range of commits included in the <commitish>. + --dedup-by-email + + Remove duplicate authors who belong to the same email address. + For example, + + $ git summary + ... + 133 TJ Holowaychuk 9.9% + 115 Tj Holowaychuk 8.5% + + $ git summary --dedup-by-email + ... + 248 TJ Holowaychuk 18.4% + + This option can not be used together with `--line`. + --line Summarize with lines other than commits.