Files
gitistics/gitistics.sh

141 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# Specify a directory with all Git repositories
REPODIR=$(pwd)
# Regex or fixed name (brackets need to be escaped with a backslash)
REPOUSER=$(whoami)
#REPOUSER="\(structix\|Janek\)"
# Specify the start date, where the statistics should be gathered
STARTDATE="1 year ago"
# Verbose flag
VERBOSE=false
# --------------------
# --- functions
# returns the amount of commits for one repository
function getCommits() {
# show amount of commits of all users
committemp=$(git rev-list --count --all --author=$REPOUSER --since="$STARTDATE" 2> /dev/null)
echo $committemp
}
# get the amount of files, insertions, deletions by specifying parameters
function getDiffChanges() {
local __files=$1
local __insert=$2
local __delete=$3
# calculate total changes between given date and HEAD
local extractdiff=$(git diff @{"$STARTDATE"} HEAD --shortstat 2>/dev/null)
# set the separator to ,
local IFS=','
local array=( $extractdiff )
# Parse the numbers
local extf=${array[0]//[!0-9]}
local exti=${array[1]//[!0-9]}
local extd=${array[2]//[!0-9]}
if [ "$VERBOSE" = true ]; then
echo "Files: $extf, Insertions: $exti, Deletions: $extd"
fi
local f=$(( extf + __files))
local i=$(( exti + __insert))
local d=$(( extd + __delete))
eval $__files="'$f'"
eval $__insert="'$i'"
eval $__delete="'$d'"
}
# repository size (only show in verbose mode)
function getRepoSize() {
if [ "$VERBOSE" = true ]; then
git count-objects -H
fi
}
function main() {
# Switch into the REPODIR (silently)
pushd $REPODIR 1> /dev/null
# Iterate over all subdirectories
for dir in $REPODIR/*/
do
dir=${dir%*/}
if [ "$VERBOSE" = true ]; then
echo ${dir##*/}
fi
# enter directory
cd ${dir##*/}
# --- RUN COMMANDS
commtemp=$(getCommits)
# Add to the total commit counter
commitcounter=$((commitcounter + commtemp))
# Output the commit counter of the current iteration
if [ "$VERBOSE" = true ]; then
echo $commtemp
fi
getDiffChanges totalfiles totalinsertions totaldeletions
getRepoSize
# go back
cd ..
done
# Switch back to the original directory state (silently)
popd 1> /dev/null
echo "-------------------------"
echo "Total commits starting from $STARTDATE: $commitcounter"
echo "Total files changed: $totalfiles"
echo "Total insertions: $totalinsertions"
echo "Total deletions: $totaldeletions"
}
# display the help on the screen
print_usage() {
echo "$0 - git statistics over multiple directories"
echo ""
echo "options:"
echo "-h show this help page"
echo "-u <user/regex> set a repository username or a regex pattern (brackets etc. must be escaped with \ )"
echo "-r <directory> set a directory with your repositories"
echo "-s <date> set the startdate (e.g. 01 JAN 2018 or 1 month ago) where the stats should start"
echo "-v show verbose output"
}
# process the arguments
while getopts 'u:r:s:vh' flag; do
case "${flag}" in
u) REPOUSER="${OPTARG}" ;;
r) REPODIR="${OPTARG}" ;;
s) STARTDATE="${OPTARG}" ;;
v) VERBOSE=true ;;
h) print_usage; exit 0 ;;
*) print_usage
exit 1 ;;
esac
done
# Start the main function
main