upstream_branch=$1
if [ "$#" -eq 2 ]; then
branch_being_rebased=$2
else
branch_being_rebased=$(git symbolic-ref --quiet --short HEAD) || exit 0 fi
if git log --oneline ${upstream_branch}..${branch_being_rebased} > /dev/null; then
echo "Warning: The branch being rebased (${branch_being_rebased}) is behind the upstream branch (${upstream_branch})." 1>&2
echo "Please update your branch before rebasing." 1>&2
exit 1
fi
if ! git diff-index --quiet HEAD --; then
echo "Warning: There are uncommitted changes in your branch ${branch_being_rebased}." 1>&2
echo "Please commit or stash your changes before rebasing." 1>&2
exit 2
fi
exit 0