Merge pull request #790 from spacewander/git-summary-merge-email

git-summary: add --dedup-by-email to remove duplicate users
This commit is contained in:
罗泽轩 2019-10-31 10:01:35 +08:00 committed by GitHub
commit a14547e92f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 140 additions and 8 deletions

View file

@ -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 <luozexuan@xxx.com>
# 7 罗泽轩 <luozexuan@xxx.com>
# 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

View file

@ -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
}

View file

@ -7,7 +7,7 @@
\fBgit\-summary\fR \- Show repository summary
.
.SH "SYNOPSIS"
\fBgit\-summary\fR [\-\-line] [<commitish>]
\fBgit\-summary\fR [\-\-line] [\-\-dedup\-by\-email] [<commitish>]
.
.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 <commitish>\.
.
.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

View file

@ -76,7 +76,7 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>git-summary</code> [--line] [&lt;commitish&gt;]</p>
<p><code>git-summary</code> [--line] [--dedup-by-email] [&lt;commitish&gt;]</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
@ -88,6 +88,23 @@
<p> Summarize only the range of commits included in the &lt;commitish&gt;.</p>
<p> --dedup-by-email</p>
<p> Remove duplicate authors who belong to the same email address.
For example,</p>
<pre><code>$ git summary
...
133 TJ Holowaychuk 9.9%
115 Tj Holowaychuk 8.5%
$ git summary --dedup-by-email
...
248 TJ Holowaychuk 18.4%
</code></pre>
<p> This option can not be used together with <code>--line</code>.</p>
<p> --line</p>
<p> Summarize with lines other than commits.
@ -145,7 +162,7 @@ authors :
<h2 id="AUTHOR">AUTHOR</h2>
<p>Written by Tj Holowaychuk &lt;<a href="&#x6d;&#97;&#105;&#108;&#116;&#x6f;&#58;&#x74;&#x6a;&#64;&#118;&#x69;&#115;&#105;&#x6f;&#110;&#45;&#x6d;&#x65;&#100;&#x69;&#x61;&#46;&#99;&#x61;" data-bare-link="true">&#x74;&#x6a;&#64;&#118;&#105;&#x73;&#x69;&#x6f;&#110;&#x2d;&#x6d;&#x65;&#x64;&#105;&#x61;&#x2e;&#99;&#97;</a>&gt;</p>
<p>Written by Tj Holowaychuk &lt;<a href="&#109;&#97;&#105;&#108;&#116;&#x6f;&#x3a;&#116;&#106;&#x40;&#x76;&#105;&#x73;&#x69;&#x6f;&#110;&#45;&#x6d;&#x65;&#x64;&#x69;&#97;&#x2e;&#x63;&#97;" data-bare-link="true">&#116;&#106;&#64;&#x76;&#105;&#115;&#x69;&#111;&#110;&#45;&#x6d;&#x65;&#x64;&#x69;&#97;&#46;&#99;&#97;</a>&gt;</p>
<h2 id="REPORTING-BUGS">REPORTING BUGS</h2>

View file

@ -3,7 +3,7 @@ git-summary(1) -- Show repository summary
## SYNOPSIS
`git-summary` [--line] [&lt;commitish&gt;]
`git-summary` [--line] [--dedup-by-email] [&lt;commitish&gt;]
## DESCRIPTION
@ -15,6 +15,22 @@ Shows a summary of the repository.
Summarize only the range of commits included in the &lt;commitish&gt;.
--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.