#!/bin/sh
set -x
TEMPFILE=`mktemp /tmp/extract.$$.XXXXXX` || exit 1
TEMPFILE2=`mktemp /tmp/extract.$$.XXXXXX` || exit 1
TEMPFILE3=`mktemp /tmp/extract.$$.XXXXXX` || exit 1

export GIT_DIR=/pub/scm/linux/kernel/git/dwmw2/kernel-headers.git
cd $GIT_DIR || exit 1
export EQUIV_DIR=/home/dwmw2/.khdr-st2
cd $EQUIV_DIR || exit 1

function incparent()
{
    git-rev-list --max-count=1 --topo-order $1 -- .
}

function convertcommit()
{
    local COMMIT PARENTS CONVERTEDPARENTS TREE

    COMMIT=$1
    PARENTS=
    CONVERTEDPARENTS=

    if [ ! -r $EQUIV_DIR/st2-$COMMIT ] ; then
	git-cat-file commit $1 | sed /^\$/q | grep ^parent > $TEMPFILE
	while read foo PARENT; do
	    PARENTS="$PARENTS `incparent $PARENT`"
	done < $TEMPFILE

	for a in $PARENTS; do
	    CONVERTEDPARENTS="$CONVERTEDPARENTS `convertcommit $a`"
	done
	TREE=`git-cat-file commit $1 | sed /^\$/q | grep ^tree | cut -f2 -d\ `
	echo tree $TREE > $TEMPFILE
	for a in $CONVERTEDPARENTS; do
	    echo parent $a >> $TEMPFILE
	done
	git-cat-file commit $COMMIT | while read key value; do
	    case $key in
		tree|parent)
		    ;;
		*)
		    echo "$key $value"
		    cat
		    ;;
	    esac
	done >> $TEMPFILE
	git-hash-object -t commit -w $TEMPFILE > $EQUIV_DIR/st2-$COMMIT
    fi
    cat $EQUIV_DIR/st2-$COMMIT
}

NEWUPSTREAM=`cat $GIT_DIR/refs/heads/stage1`
TOP=`incparent $NEWUPSTREAM`
NEWMASTER=`convertcommit $TOP`

echo $NEWMASTER > $GIT_DIR/refs/heads/master.new
mv $GIT_DIR/refs/heads/master.new $GIT_DIR/refs/heads/master
rm -f $TEMPFILE $TEMPFILE2 $TEMPFILE3
exit 0
DAY=`date +%w`

if [ "$DAY" = "0" ]; then
    REPACKOPT="-a -d"
fi

git-repack -l $REPACKOPT
git-prune-packed



