Skip to content

Instantly share code, notes, and snippets.

@bencrouse
Created March 8, 2011 19:51
Show Gist options
  • Save bencrouse/860894 to your computer and use it in GitHub Desktop.
Save bencrouse/860894 to your computer and use it in GitHub Desktop.
#Files (not deleted) in the index
files=$(git diff-index --name-status --cached HEAD | grep -v ^D | cut -c3-)
if [ "$files" != "" ]
then
for f in $files
do
# Only examine known text files
if [[ "$f" =~ [.](conf|css|erb|html|js|json|log|properties|rb|ru|txt|xml|yml)$ ]]
then
# Add a linebreak to the file if it doesn't have one
if [ "$(tail -c1 $f)" != '' ]
then
echo >> $f
git add $f
fi
# Remove trailing whitespace if it exists
if grep -q "[[:blank:]]$" $f
then
sed -i "" -e $'s/[ \t]*$//g' $f
git add $f
fi
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment