Skip to content

Instantly share code, notes, and snippets.

@mikron
Last active December 17, 2020 10:35
Show Gist options
  • Select an option

  • Save mikron/6fd856465740d0060eb6f7ce62dd236b to your computer and use it in GitHub Desktop.

Select an option

Save mikron/6fd856465740d0060eb6f7ce62dd236b to your computer and use it in GitHub Desktop.
Git statistics shell script
#!/bin/bash
LOGOPTS=()
END_AND_BEGIN=()
#argument parsing
while [ -n "$1" ]; do
case "$1" in
"-s")
shift
END_AND_BEGIN+=("--after=$1")
;;
"-e")
shift
END_AND_BEGIN+=("--before=$1")
;;
"-w")
LOGOPTS+=("-w")
;;
"-C")
LOGOPTS+=("-C")
LOGOPTS+=("--find-copies-harder")
;;
"-M")
LOGOPTS+=("-M")
;;
esac
shift
done
#test if the directory is a git
git branch &> /dev/null || exit 3
echo "Number of commits per author:"
git --no-pager shortlog "${END_AND_BEGIN[@]}" -sn --all
AUTHORS=$(git shortlog "${END_AND_BEGIN[@]}" -sn --all | cut -f2 | cut -f1 -d' ')
for a in $AUTHORS
do
echo '-------------------'
echo "Statistics for: $a"
echo -n "Number of files changed: "
git log "${LOGOPTS[@]}" "${END_AND_BEGIN[@]}" --all --numstat --format="%n" --author="$a" | grep -v -e "^$" | cut -f3 | sort -iu | wc -l
echo -n "Number of lines added: "
git log "${LOGOPTS[@]}" "${END_AND_BEGIN[@]}" --all --numstat --format="%n" --author="$a" | cut -f1 | awk '{s+=$1} END {print s}'
echo -n "Number of lines deleted: "
git log "${LOGOPTS[@]}" "${END_AND_BEGIN[@]}" --all --numstat --format="%n" --author="$a" | cut -f2 | awk '{s+=$1} END {print s}'
echo -n "Number of merges: "
git log "${LOGOPTS[@]}" "${END_AND_BEGIN[@]}" --all --merges --author="$a" | grep -c '^commit'
done
@mikron
Copy link
Copy Markdown
Author

mikron commented Dec 17, 2020

Example of usage to see the breakdown by users only counting commits from 20th of October 2020.
./git-stats.sh -s --after='20 October 2020'`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment