2020-03-19 22:06:33 +01:00
|
|
|
# shellcheck shell=bash
|
|
|
|
|
|
|
|
# Borrowed from Git's git-sh-setup.
|
|
|
|
#
|
|
|
|
# See git.git commit 92c62a3f4 (from 2010!); as of 2020 with Git 2.26,
|
|
|
|
# this function has only needed one edit since then, adding localization
|
|
|
|
# with gettext, which we can omit.
|
|
|
|
require_clean_work_tree () {
|
2020-03-26 02:21:08 +01:00
|
|
|
local action="$1"
|
2020-03-26 02:16:17 +01:00
|
|
|
|
2020-03-19 22:06:33 +01:00
|
|
|
git rev-parse --verify HEAD >/dev/null || exit 1
|
|
|
|
git update-index -q --ignore-submodules --refresh
|
2020-03-26 02:16:17 +01:00
|
|
|
local err=0
|
2020-03-19 22:06:33 +01:00
|
|
|
|
2020-03-26 02:16:17 +01:00
|
|
|
if ! git diff-files --quiet --ignore-submodules; then
|
|
|
|
echo >&2 "Cannot $action: You have unstaged changes."
|
2020-03-19 22:06:33 +01:00
|
|
|
err=1
|
|
|
|
fi
|
|
|
|
|
2020-03-26 02:16:17 +01:00
|
|
|
if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
|
|
|
|
if [ $err = 0 ]; then
|
|
|
|
echo >&2 "Cannot $action: Your index contains uncommitted changes."
|
2020-03-19 22:06:33 +01:00
|
|
|
else
|
|
|
|
echo >&2 "Additionally, your index contains uncommitted changes."
|
|
|
|
fi
|
|
|
|
err=1
|
|
|
|
fi
|
|
|
|
|
2020-03-26 02:16:17 +01:00
|
|
|
if [ $err = 1 ]; then
|
2020-03-26 02:21:08 +01:00
|
|
|
git status --short
|
|
|
|
echo >&2 "Doing nothing to avoid losing your work."
|
2020-03-19 22:06:33 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|