require-clean-working-tree.sh 852 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. # http://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommited-changes
  3. # optional argument
  4. working_subdir="$1"
  5. # git complains if empty string path in statements below
  6. if [[ -z "$working_subdir" ]]
  7. then
  8. working_subdir="."
  9. fi
  10. # Update the index
  11. git update-index -q --refresh
  12. err=0
  13. # Disallow unstaged changes in the working tree
  14. if ! git diff-files --quiet -- "$working_subdir"
  15. then
  16. echo >&2 "You have unstaged changes."
  17. git diff-files --name-status -r -- "$working_subdir" >&2
  18. err=1
  19. fi
  20. # Disallow uncommitted changes in the index
  21. if ! git diff-index --cached --quiet HEAD --
  22. then
  23. echo >&2 "Your index contains uncommitted changes."
  24. git diff-index --cached --name-status -r HEAD -- >&2
  25. err=1
  26. fi
  27. if [ $err = 1 ]
  28. then
  29. echo >&2 "Please commit or stash them."
  30. exit 1
  31. fi