#!/bin/bash
#
# Apologies for all this nastiness. This script could be so much cleaner...

COMMITS="/tmp/commits.txt"
LOGMSG="/tmp/logmsg.txt"
ACKS="/tmp/acks.txt"
ACKERS="/tmp/ackers.txt"

# Required to read one line at a time, instead of one word at a time
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

echo "Generic statistics:"
echo

# Generate a list of commit IDs, unless told otherwise
if ! [ "$1" == "--no-log-parsing" ]; then

	# Clean up after previous runs
	rm -f $COMMITS $LOGMSG $ACKS $ACKERS

	git log --since="2 years ago"|grep "^commit"|cut -d " " -f 2 > $COMMITS

	# Internal helper variables
	COMMITCOUNT=0
	ACKCOUNT=0

	for COMMIT in `cat $COMMITS`; do

		# Produces commit outputs like this:
		#
		# build: autoconf: misc sockets fixups+
		# Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
		# Acked-by: David Sommerseth <davids@redhat.com>
		# Signed-off-by: David Sommerseth <davids@redhat.com>

		# The -s switch removes the diff from git-show output, although 
		# it's undocumented.
		COMMITNAME=`git show -s --pretty=format:"%s" $COMMIT`
		ACKERNUM=`git show -s --pretty=format:"%b" $COMMIT|grep "Acked-by"|wc --lines`
		ACKCOUNT=$(( $ACKCOUNT + $ACKERNUM ))

		# Isolate individual ACK lines
		git show -s --pretty=format:"%b $COMMIT"|grep "Acked-by" >> $ACKS

		COMMITCOUNT=$(( $COMMITCOUNT + 1 ))

	done

	echo "Commit count: "$COMMITCOUNT
	echo "ACK number: "$ACKCOUNT

	# Calculate average ACK count per patch.
	AVGACKCOUNT=`echo "scale=2;$ACKCOUNT/$COMMITCOUNT"|bc`
	echo "Average ACK count: $AVGACKCOUNT"
fi

echo
echo "ACKs by person:"
echo

# Check the ACK statistics by person
for ACKER in `git log --since="2 years ago" --pretty=format:"%b"|grep "Acked-by"|sort|uniq|cut -d ":" -f 2|cut -d "<" -f 1|sed s/"^ "//1`; do
	echo -n $ACKER
	echo `git log --since="2 years ago" --pretty=format:"%b"|grep "Acked-by: $ACKER"|wc --lines`
done
