#!/bin/bash
#
# Copyright (C) 2005-2020 René Rebe

die () {
	echo "$*"
	exit -1
}

repo="$1" ; shift
tag1="$1" ; shift
tag2="$1" ; shift
trunk="$1" ; shift

[ "$trunk" ] || trunk="trunk"

if [ "$repo" = "" -o "$tag1" = "" -o "$tag2" = "" ]; then
	echo "Usage: repository tag1 tag2 [ trunk ]"
	exit
fi

tmp="`mktemp`"

svn log --stop-on-copy $repo/tags/$tag1 > $tmp.tag1
[ $? -ne 0 ] && die "Error connecting to svn server ..."

svn log --stop-on-copy $repo/tags/$tag2 > $tmp.tag2
[ $? -ne 0 ] && die "Error connecting to svn server ..."

firstrev1="`grep '^r[0-9]' $tmp.tag1 | sed 's/r\([^ ]*\) .*/\1/' | tail -n 1`"
firstrev2="`grep '^r[0-9]' $tmp.tag2 | sed 's/r\([^ ]*\) .*/\1/' | tail -n 1`"

[ -z "$firstrev1" ] && die "revision for $tag1 not found"
[ -z "$firstrev2" ] && die "revision for $tag2 not found"

# first get the changes in trunk between the two tags ...
svn log $repo/$trunk -r$firstrev2:$firstrev1 > $tmp.trunk
# and then append the stuff from the work on the tag ...
cat $tmp.tag2 $tmp.trunk > ChangeLog

changesets="`grep '^ *r[0-9]* | ' ChangeLog | wc -l`"
lineslog="`sed -e '/ *r[0-9]* | /d' -e '/^-----*/d' -e '/^$/d' ChangeLog | wc -l`"
updates="`grep -i update ChangeLog | wc -l`"
fixed="`grep -i fix ChangeLog | wc -l`"
improved="`grep -i improve ChangeLog | wc -l`"
added="`grep -i update ChangeLog | wc -l`"
removed="`grep -i remove ChangeLog | wc -l`"

echo "There were $changesets changesets with $lineslog lines of commit messages.
Approximately $updates packages got updates, $fixed issues fixed, $added packages or features added and $removed removed.
Around $improved improvements have been committed."

echo -e "\nCommits by contributor:"
grep '^ *r[0-9]* | ' ChangeLog | cut -d '|' -f 2 | sort | uniq -c | sort -r

echo -e "\nToolchain updates:"
grep -i 'update' ChangeLog | grep -ie binutils -e gcc -e libc$ -e linux$ -e llvm -e clang

rm $tmp.{tag1,tag2,trunk}
