Skip to content

Instantly share code, notes, and snippets.

@mattes
Last active December 21, 2015 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattes/6256810 to your computer and use it in GitHub Desktop.
Save mattes/6256810 to your computer and use it in GitHub Desktop.
Git pre-commit hook. Verify this repository has a clean work tree.
#!/bin/sh
# see also ...
# https://github.com/git/git/blob/master/git-sh-setup.sh#L168
# Verify this repository has a clean work tree.
# Return false (= 0), if there are any unstaged/ uncommitted changes.
# Return true (= 1), if there are no changes in the repository.
has_clean_work_tree () {
git rev-parse --quiet --verify HEAD &> /dev/null || return 0 # false
git update-index --quiet --ignore-submodules --refresh
if ! git diff-files --quiet --ignore-submodules
then
return 0 # false
fi
if ! git diff-index --cached --quiet --ignore-submodules HEAD --
then
return 0 # false
fi
return 1 # true
}
has_clean_work_tree
if [ "$?" == 0 ]; then
# no clean work tree ...
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment