#!/bin/sh
set -eu

usage () {
    cat <<'EOF'
usage: git commit-to <branch> ...
EOF
}

if [ $# -eq 0 ]; then
    usage >&2
    exit 2
fi

branch="$1"
shift 1

if git is-dirty -a; then
    echo 'Cannot use this command safely when you have local files marked "skipped".' >&2
    exit 2
fi

# TODO: FIXME: There is a lot that can go wrong here.  Basically, that's not
# a problem at all in itself, but we need a mechanism to reliably recover back
# to the starting position.
git commit "$@"
git cherry-pick-to "$branch"

# The commit now lives on <branch>, so drop it from the current branch again.
# `--keep` only touches the files the commit changed, leaving unstaged changes
# to other files alone, and it refuses to run rather than overwrite unstaged
# changes to the files it does touch.
if ! git reset --keep HEAD~1; then
    echo >&2
    echo "The changes are committed to '$branch', but could not be removed from the" >&2
    echo "current branch, because some of the files they touch also have unstaged" >&2
    echo "changes.  Stash or commit those, then run:" >&2
    echo >&2
    echo "    git reset --keep HEAD~1" >&2
    exit 1
fi
