Skip to content

Instantly share code, notes, and snippets.

@jamoes
Created October 11, 2011 19:16
Show Gist options
  • Save jamoes/1279096 to your computer and use it in GitHub Desktop.
Save jamoes/1279096 to your computer and use it in GitHub Desktop.
Pre-commit hook to add newlines and remove trailing whitespace
#!/usr/bin/env ruby
files = `git diff-index --name-status --cached HEAD | grep -v ^D | cut -c3-`
unless files == ''
files.split("\n").each do |f|
# Only examine known text files
if f =~ /.(conf|css|erb|html|haml|scss|css|js|json|log|properties|rb|ru|txt|xml|yml)$/ && f =~ /^tapjoyads\/(app|test|lib|public|db\/migrate|config)\//
# Add a linebreak to the file if it doesn't have one
if `tail -c1 #{f}` != "\n"
`echo >> #{f}`
`git add #{f}`
end
# Remove trailing whitespace if it exists
if system %(grep -q "[[:blank:]]$" #{f})
`sed -i "" -e $'s/[ \t]*$//g' #{f}`
`git add #{f}`
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment