diff --git a/tools/lib/git-tools.bash b/tools/lib/git-tools.bash new file mode 100644 index 0000000000..0aa0f6ad8d --- /dev/null +++ b/tools/lib/git-tools.bash @@ -0,0 +1,35 @@ +# 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 () { + git rev-parse --verify HEAD >/dev/null || exit 1 + git update-index -q --ignore-submodules --refresh + err=0 + + if ! git diff-files --quiet --ignore-submodules + then + echo >&2 "Cannot $1: You have unstaged changes." + err=1 + fi + + if ! git diff-index --cached --quiet --ignore-submodules HEAD -- + then + if [ $err = 0 ] + then + echo >&2 "Cannot $1: Your index contains uncommitted changes." + else + echo >&2 "Additionally, your index contains uncommitted changes." + fi + err=1 + fi + + if [ $err = 1 ] + then + test -n "$2" && echo >&2 "$2" + exit 1 + fi +}