Skip to content

Instantly share code, notes, and snippets.

@clizzin
Created July 20, 2011 23:09
Show Gist options
  • Save clizzin/1096153 to your computer and use it in GitHub Desktop.
Save clizzin/1096153 to your computer and use it in GitHub Desktop.
Git pre-commit hook
#!/usr/bin/env ruby
# Put the contents of this gist into .git/hooks/pre-commit
# Make sure to chmod a+x .git/hooks/pre-commit
flag = false
modified_files = `git diff-index --name-status --cached HEAD`.
map {|line| line.split}.
select {|(status, name)| status != 'D'}. # ignore deleted files
map {|(status, name)| name}
# Check Ruby syntax
modified_files.select {|f| f.match /\.((rb)|(rake))$/}.each do |file|
result = `ruby -c #{file} 2>&1`
if $? != 0
puts result
puts '==================='
flag = true
end
end
# Check ERB syntax
modified_files.select {|f| f.match /((\.rhtml)|(\.erb))$/}.each do |file|
result = "sed 's/<%=/<%/g' #{file} | erb -xT - | ruby -c 2>&1"
if $? != 0
puts file
puts result
puts '==================='
flag = true
end
end
# Commented out because too many of our libraries have console in them.
#
# Dir["public/javascripts/**/*.js"].each do |file|
# result = `grep -n console #{file}`
#
# if $? == 0
# puts file
# puts result
# flag = true
# end
# end
if flag
puts 'Syntax errors! Wah wah.'
puts 'Did not commit.'
exit 1
end
# exec "rake spec"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment