Created
July 7, 2012 15:03
-
-
Save jonatasnona/3066806 to your computer and use it in GitHub Desktop.
Git: pre-commit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# A git hook script to find and fix trailing whitespace | |
# in your commits. Bypass it with the --no-verify option | |
# to git-commit | |
# | |
# Find files with trailing whitespace | |
for file in `git diff --check --cached | grep '^[^+-]' | grep -o '^.*[0-9]\+:'` ; do | |
file_name=`echo ${file} | grep -o '^[^:]\+'` | |
line_number=`echo ${file} | grep -oP '(?<=:)[0-9]+(?=:)'` | |
(sed -i "${line_number}s/\s*$//" ${file_name} > /dev/null 2>&1 \ | |
|| sed -i '' -E "${line_number}s/\s*$//" ${file_name}) | |
git add ${file_name} | |
done | |
# Now we can commit | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
name this script as pre-commit and place it in the .git/hooks folder of your git repository, then apply chmod +x in .git/hooks/pre-commit.