public
Created

  • Download Gist
pre-commit
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/bin/sh
 
# Compile a list of changed files
FILES=`git diff --name-only HEAD^`
 
# Attempt to fix whitespace issues
for FILE in `egrep -l '(\s$| {1,3}\t)' $FILES`
do
# Remove trailing whitespace
(sed -i 's/[ ]*$//' $FILE > /dev/null 2>&1 || sed -i '' -E 's/[ ]*$//' $FILE)
 
# Remove spaces before tabs
(sed -i 's/ {1,3} / /' $FILE > /dev/null 2>&1 || sed -i '' -E 's/ {1,3} / /' $FILE)
 
# Add to git
git add $FILE
echo "Automatically fixed whitespace in $FILE"
done
 
# Check for new lines at end of file, add if it is not there
for FILE in $FILES
do
if ! tail -c1 $FILE | read _
then
echo >> $FILE
git add $FILE
echo "Added new line to end of $FILE"
fi
done

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.