Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Created March 20, 2012 21:08
Show Gist options
  • Save callumacrae/2141321 to your computer and use it in GitHub Desktop.
Save callumacrae/2141321 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment