Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git-summary: add --dedup-by-email to remove duplicate users #790

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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