#!/usr/bin/zsh -f

# nothing to do with hierearchy!  todo: move to ../../mmc-shell/

# cherry-pick between non-current branches?
# todo: stash around.

usage()
{
    echo "usage: ${0##*/} [+-h} [--] commit branch"
}

while getopts :h OPT; do
    case $OPT in
	h|+h)
	    usage
	    exit 0
	    ;;
	*)
	    usage
	    exit 2
    esac
done
shift $(( OPTIND - 1 ))
OPTIND=1

if [ $# -lt 2 ]; then
    usage
    exit 1
fi

source /usr/share/git-hierarchy/functions.sh
GIT_DIR=$(git_dir)


commit=$1
branch=$2

# resolve before checkout. if HEAD, it must be the original.
commit=$(commit_id $commit)


# current branch?
head=$(dump_ref_without_ref HEAD)
head=${head##refs/heads/}

git checkout $branch
git cherry-pick $commit

# this migth be in TRAP
# and avoid the warning on detached.
# so just echo > .git/HEAD ?
git checkout $head
