#! /bin/sh

set -e

if test $# -lt 1
then
	echo "Usage: gob-collapse-commits <commit> [<commit>]"
	exit 2
fi

begin=$1
end=$2

begin_log=$(git show --pretty=raw $begin | head -3 | tr '\n' ' ')
begin_commit=$(expr "$begin_log" : '.*commit \([^ ]\+\)')
begin_tree=$(expr "$begin_log" : '.*tree \([^ ]\+\)')
begin_parent=$(expr "$begin_log" : '.*parent \([^ ]\+\)'||:)

end_log=$(git show --pretty=raw $end | head -3 | tr '\n' ' ')
end_commit=$(expr "$end_log" : '.*commit \([^ ]\+\)')
end_tree=$(expr "$end_log" : '.*tree \([^ ]\+\)')
end_parent=$(expr "$end_log" : '.*parent \([^ ]\+\)')

(echo gob-collapse-commits $begin_commit..$end_commit;
 echo;
 git log -1 $begin_commit | tail -n +5;
 echo '--';
 git log -1 $end_commit | tail -n +5;) \
	| sed -e 's/^ \{4\}//' \
	| git commit-tree $end_tree -p $begin_commit > .git/NEW-HEAD || (rm -f .git/new-HEAD && exit 1)
git reset --hard $(cat .git/NEW-HEAD)
rm .git/NEW-HEAD

# Local Variables:
# sh-basic-offset:8
# End:
