#!/bin/sh

FILES=$1.files
PATCHFILE=$1
LOG=$1.log
. $1.author

if [ -e .git/HEAD ]; then
	git-check-files $(cat $FILES) || exit 1
	git-checkout-cache -q $(cat $FILES) || exit 1
fi

patch -u --no-backup-if-mismatch -f -p0 --fuzz=0 --input=$PATCHFILE || exit 1
git-update-cache --add --remove $(cat $FILES) || exit 1
tree=$(git-write-tree) || exit 1
echo Wrote tree $tree

if [ -e .git/HEAD ]; then
	commit=$(git-commit-tree $tree -p HEAD < $LOG) || exit 1
else
	commit=$(git-commit-tree $tree < $LOG) || exit 1
fi

echo Committed: $commit
echo $commit > .git/HEAD

