Skip to content

Commit

Permalink
Merge pull request #790 from spacewander/git-summary-merge-email
Browse files Browse the repository at this point in the history
git-summary: add --dedup-by-email to remove duplicate users
  • Loading branch information
spacewander authored Oct 31, 2019
2 parents 7bdfb1f + 203f5b4 commit a14547e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 8 deletions.
80 changes: 76 additions & 4 deletions bin/git-summary
Original file line number Diff line number Diff line change
Expand Up @@ -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##*/}

#
Expand All @@ -31,6 +55,7 @@ active_days() {
#

commit_count() {
# shellcheck disable=SC2086
git log --oneline $commit | wc -l | tr -d ' '
}

Expand All @@ -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 <[email protected]>
# 7 罗泽轩 <[email protected]>
# 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
#
Expand Down Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions etc/git-extras-completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
28 changes: 27 additions & 1 deletion man/git-summary.1
Original file line number Diff line number Diff line change
Expand Up @@ -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\.
Expand All @@ -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
Expand Down
21 changes: 19 additions & 2 deletions man/git-summary.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion man/git-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down

0 comments on commit a14547e

Please sign in to comment.