Advertisement
Guest User

Untitled

a guest
May 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Reject commits on some stop words:
  4. # nocommit: include this word (in a comment) when you're doing a local change that should not be committed
  5. # fdescribe: used in jasmine to run a single test
  6.  
  7. declare -a stopwords=("nocommit" "fdescribe")
  8.  
  9. if git rev-parse --verify HEAD >/dev/null 2>&1
  10. then
  11. against=HEAD
  12. else
  13. # Initial commit: diff against an empty tree object
  14. against=$(git hash-object -t tree /dev/null)
  15. fi
  16.  
  17. patch_filename=$(mktemp -t commit_hook_changes.XXXXXX)
  18. git diff --exit-code --binary --ignore-submodules --no-color > $patch_filename
  19. has_unstaged_changes=$?
  20.  
  21. if [ $has_unstaged_changes -ne 0 ]; then
  22. echo "Stashing unstaged changes in $patch_filename."
  23. git checkout -- .
  24. fi
  25.  
  26. quit() {
  27. if [ $has_unstaged_changes -ne 0 ]; then
  28. git apply $patch_filename
  29. if [ $? -ne 0 ]; then
  30. git checkout -- .
  31. git apply $patch_filename
  32. fi
  33. fi
  34.  
  35. exit $1
  36. }
  37.  
  38. # Redirect output to stderr.
  39. exec 1>&2
  40.  
  41. for word in "${stopwords[@]}"
  42. do
  43. files_with_word=$(git diff --cached --name-only --diff-filter=ACM $against | xargs grep -i "$word" -l | tr '\n' ' ')
  44.  
  45. if [ "x${files_with_word}x" != "xx" ]; then
  46. tput setaf 1
  47. echo "File being committed with '$word' in it:"
  48. echo $files_with_word | tr ' ' '\n'
  49. tput sgr0
  50. quit 1
  51. fi
  52. done
  53.  
  54. quit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement