Git pre-commit hook to fix trailing whitespace


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/bin/sh
  2. #
  3. # A git hook script to find and fix trailing whitespace
  4. # in your commits. Bypass it with the --no-verify option
  5. # to git-commit
  6. #
  7.  
  8. if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
  9. against=HEAD
  10. else
  11. # Initial commit: diff against an empty tree object
  12. against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  13. fi
  14. # Find files with trailing whitespace
  15. for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -r 's/:[0-9]+:.*//' | uniq` ; do
  16. # Fix them!
  17. sed -i 's/[[:space:]]*$//' "$FILE"
  18. done
  19.  
  20. # Now we can commit
  21. exit

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.