# To enable this hook, use:
# git config gitpkg.deb-export-hook /usr/share/gitpkg/hooks/debcherry-deb-export-hook

if [ -z "$REPO_DIR" -o -z "$GITPKG_TREEISH" ]; then
   echo "internal error: please define REPO_DIR and GITPKG_TREEISH" >&2
   exit 1
fi

orig_treeish="$GITPKG_ORIG_TREEISH"
commit_file="../$DEB_ORIG".commit
if [ -z "$orig_treeish" ]; then
   if [ ! -r "$commit_file" ]; then
       echo "No origbranch specified and no $commit_file" >&2
       echo "Cannot export patches" >&2
       exit 1
   else
       orig_treeish=$(cat $commit_file)
   fi
else
   echo "$orig_treeish" > "$commit_file"
fi

git --git-dir="$REPO_DIR/.git" debcherry -o debian/patches $orig_treeish $GITPKG_TREEISH
