I thought I was done, but apparently not.  
It seems useful to have an interpolation for %DEB_UPSTREAM% containing
the upstream version. Also I added some error handling.

#!/bin/sh
# export patches according to a recipe in debian/git-patches
# To use as a hook in gitpkg, 
#   git config gitpkg.deb-export-hook debian/export-patches.sh

trap cleanup INT 
    
# make this configable?
recipe=debian/git-patches

export GIT_DIR
if [ -n "$REPO_DIR" ]; then
    GIT_DIR=$REPO_DIR/.git
else
    # support running as a free-standing script, without gitpkg
    DEB_VERSION=$(dpkg-parsechangelog | sed -n 's/^Version: \(.*:\|\)\(.*\)/\2/p')
fi;

case $DEB_VERSION in
    *-*)
	DEB_UPSTREAM=${DEB_VERSION%-*}
	;;
    *)
	DEB_UPSTREAM=$DEB_VERSION
	;;
esac

tmpdir=$(mktemp -d patches.XXXXXXX)

cleanup (){
    echo "cleaning up..."
    rm -rf $tmpdir
    exit 1
}

do_patches (){
    while read -r base tip
    do
	case $base in
	    \#*)
		;;
	    *)
		count=$(wc -l $tmpdir/series | cut -f1 -d' ')
		if PATCHES=$(git format-patch --start-number $count -N -o $tmpdir $base...$tip); then
		    echo $PATCHES | sed -e "s%$tmpdir/%%g" -e 's% %\n%g'>> $tmpdir/series
		else
		    echo "git format-patch failed"
		    cleanup
		fi
	esac
    done
}

echo "# Patches exported from git by gitpkg-export-patches" > $tmpdir/series

sed -e s/%DEB_VERSION%/${DEB_VERSION}/g  -e s/%DEB_UPSTREAM%/${DEB_UPSTREAM}/ < $recipe | do_patches || exit 1

count=$(wc -l $tmpdir/series | cut -f1 -d' ')

if [ $count -gt 1 ]; then
    rm -rf debian/patches
    mv $tmpdir debian/patches
else
    echo "No patches found: debian/patches left untouched"
    cleanup
fi

Attachment: git-patches
Description: sample config file

Reply via email to