Skip to content

Instantly share code, notes, and snippets.

@jamesr2323
Created December 22, 2020 08:55
Show Gist options
  • Save jamesr2323/9b8bf7cfc32e6ef53e3f4a4453a744b8 to your computer and use it in GitHub Desktop.
Save jamesr2323/9b8bf7cfc32e6ef53e3f4a4453a744b8 to your computer and use it in GitHub Desktop.
Get git stats by contributing user, in time period
# This is mostly just copied from this awesome SO answer: https://stackoverflow.com/a/20414465/4094427
# get_stats '*' - get stats for repo lifetime
# get_stats 'scss' get stats just for *.scss files
get_stats_all() {
echo "*.$1"
git log --shortstat --pretty="%cE" -- "*.$1" | sed 's/\(.*\)@.*/\1/' | grep -v "^$" | awk 'BEGIN { line=""; } !/^ / { if (line=="" || !match(line, $0)) {line = $0 "," line }} /^ / { print line " # " $0; line=""}' | sort | sed -E 's/# //;s/ files? changed,//;s/([0-9]+) ([0-9]+ deletion)/\1 0 insertions\(+\), \2/;s/\(\+\)$/\(\+\), 0 deletions\(-\)/;s/insertions?\(\+\), //;s/ deletions?\(-\)//' | awk 'BEGIN {name=""; files=0; insertions=0; deletions=0;} {if ($1 != name && name != "") { print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net"; files=0; insertions=0; deletions=0; name=$1; } name=$1; files+=$2; insertions+=$3; deletions+=$4} END {print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net";}'
}
# get_stats '*' 365 = get stats for one year
# get_stats 'rb' 365 = get stats for one year just for *.rb files.
get_stats() {
echo "*.$1"
git log $(git rev-list -n1 --before="$2 day ago" master)..HEAD --shortstat --pretty="%cE" -- "*.$1" | sed 's/\(.*\)@.*/\1/' | grep -v "^$" | awk 'BEGIN { line=""; } !/^ / { if (line=="" || !match(line, $0)) {line = $0 "," line }} /^ / { print line " # " $0; line=""}' | sort | sed -E 's/# //;s/ files? changed,//;s/([0-9]+) ([0-9]+ deletion)/\1 0 insertions\(+\), \2/;s/\(\+\)$/\(\+\), 0 deletions\(-\)/;s/insertions?\(\+\), //;s/ deletions?\(-\)//' | awk 'BEGIN {name=""; files=0; insertions=0; deletions=0;} {if ($1 != name && name != "") { print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net"; files=0; insertions=0; deletions=0; name=$1; } name=$1; files+=$2; insertions+=$3; deletions+=$4} END {print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net";}'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment