Skip to content

Instantly share code, notes, and snippets.

@shinzui
Forked from cypher/gist:150248
Created July 22, 2009 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinzui/151849 to your computer and use it in GitHub Desktop.
Save shinzui/151849 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# A hook script to verify that only syntactically valid ruby code is commited.
# Called by git-commit with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Put this code into a file called "pre-commit" inside your .git/hooks
# directory, and make sure it is executable ("chmod +x .git/hooks/pre-commit")
#
# Tested only with Git 1.6.4-rc1, but should work with any Git 1.6.*
# Requires Ruby 1.8.6 or better
#
# UPDATE: This hook now has its own repo: http://github.com/cypher/git-ruby-syntax-check
require 'open3'
include Open3
changed_ruby_files = `git diff-index --name-only --cached HEAD`.inject([]) do |files, line|
files << line.chomp if line =~ /(.+\.(e?rb|task)|Rakefile)/
files
end
problematic_files = changed_ruby_files.inject([]) do |problematic_files, file|
cmd = if file =~ /\.erb\z/
# Set trim mode to "-", just as Rails does
"erb -xT - #{file} | ruby -c"
else
"ruby -c #{file}"
end
errors = nil
popen3(cmd) do |stdin, stdout, stderr|
errors = stderr.read.split("\n")
end
unless errors.empty?
errors.map!{ |line| line.sub(/#{file}:/, '') }
problematic_files << "#{file}:\n#{errors.join("\n")}"
end
problematic_files
end
if problematic_files.size > 0
$stderr.puts problematic_files.join("\n\n")
exit 1
else
# All is well
exit 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment