Skip to content

Commit

Permalink
tools: Modernize require_clean_work_tree a bit.
Browse files Browse the repository at this point in the history
Use `local`.  Also `set -u`-compatible `${2-}`, and normalize
formatting of `if .. then`.
  • Loading branch information
gnprice authored and andersk committed Apr 7, 2020
1 parent 73acca7 commit 43ca39c
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions tools/lib/git-tools.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,28 @@
# this function has only needed one edit since then, adding localization
# with gettext, which we can omit.
require_clean_work_tree () {
local action="$1" message="${2-}"

git rev-parse --verify HEAD >/dev/null || exit 1
git update-index -q --ignore-submodules --refresh
err=0
local err=0

if ! git diff-files --quiet --ignore-submodules
then
echo >&2 "Cannot $1: You have unstaged changes."
if ! git diff-files --quiet --ignore-submodules; then
echo >&2 "Cannot $action: 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."
if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
if [ $err = 0 ]; then
echo >&2 "Cannot $action: 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"
if [ $err = 1 ]; then
[ -n "$message" ] && echo >&2 "$message"
exit 1
fi
}

0 comments on commit 43ca39c

Please sign in to comment.