#!/bin/bash
#
# needs to be run from inside a linux-next git tree
# option gives the name of the txt file with the sizes.  Will be used as a cache
#    next-size [file.txt]
#
# (c) GPLv3 and later. Michael Neuling 2009

RELEASES=`git tag -l 'next-*'`

CSVFILE="next-size.txt"
if [ -n "$1" ] ; then
    CSVFILE="$1"
fi

test -e $CSVFILE || echo "Date,linux-next,added,removed,Linus,added,removed,Conflicts,linux-next,Linus" > $CSVFILE

#for i in $(seq 1203296400 86400 $(date +%s)) ; do date +%Y%m%d -d @$i; done
for release in $RELEASES ; do
    releasedate=${release/next-/}
    if grep $releasedate $CSVFILE > /dev/null ; then
	continue
    fi
    if [ $release == "next-20080428a" ] ; then
	continue
    fi
    originsha1=`git grep origin $release -- Next/SHA1s |gawk '{print $2}'`
    if [ -z "$originsha1" ] ; then
	originsha1ambiguous=`git grep HEAD $release -- Next/merge.log |head -1 | gawk '{print $5}' | sed -e 's/\.//g'`
	if [ -z "$originsha1ambiguous" ] ; then
	    continue
	fi
	originsha1=`git rev-list $release | sed -n "/^$originsha1ambiguous/{p;q}"`
    fi

    if [ -z "$originsha1" ] ; then
	continue
    fi
    size=`git diff --shortstat -M -C -l 10000 $originsha1..$release^ | gawk '{print $4 + $6 "," $4 "," $6}'`
    # iterate back until you find the last release
    lastrelease=`git describe --match "v*" --abbrev=0 $originsha1`
    while [ $lastrelease != ${lastrelease%-rc*} ] ; do
	    lastrelease=`git describe --match "v*" --abbrev=0 $lastrelease^`
    done

    linus=`git diff --shortstat -M -C -l 10000 $lastrelease..$originsha1 | gawk '{print $4 + $6 "," $4 "," $6}'`
    conflicts=`git cat-file -p $release:Next/merge.log | grep -c '^CONFLICT'`
    commitsnext=`git rev-list --no-merges $originsha1..$release^ | wc -l`
    commitslinus=`git rev-list --no-merges $lastrelease..$originsha1 | wc -l`
    if [ -z "$linus" ] ; then
	linus="0,0,0"
    fi
    if [ -z "$size" ] ; then
	size=$sizelast
    fi

    echo $releasedate,$size,$linus,$conflicts,$commitsnext,$commitslinus >> "$CSVFILE"

    sizelast=$size
done

(head -1 "$CSVFILE" ; sort "$CSVFILE" |head -n -1) > "$CSVFILE-$$"
mv -f "$CSVFILE-$$" "$CSVFILE"
