Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 6 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Showing with 57 additions and 11 deletions.
  1. +1 −0 .gitignore
  2. +33 −11 pre-commit
  3. +23 −0 test.sh
View
@@ -0,0 +1 @@
+test-files
View
@@ -14,34 +14,56 @@ include Open3
# Set this to true if you want warnings to stop your commit
stop_on_warnings = (`git config --bool hooks.stop-on-warnings` != "false\n")
-# Set this to true if you
-skip_erb_files = (`git config --bool hooks.skip-erb-files` == "true\n")
+def cmd_path(cmd)
+ p = `which #{cmd}`.strip
+ p if p.length > 0
+end
+
+def compiler_ruby
+ @compiler_ruby ||= cmd_path('rbx') or cmd_path('ruby') or raise 'Ruby compiler not found!'
+end
-compiler_ruby = `which rbx`.strip
-compiler_ruby = `which ruby`.strip if compiler_ruby.length == 0
+def check_rb_cmd(file)
+ "#{compiler_ruby} -wc #{file}"
+end
+
+def check_erb_cmd(file)
+ # Use erubis if it is available; so that .erb templates in Rails 3 projects will be
+ # syntax-checked correctly. If using `bundle` we also need to test-run it first to
+ # make sure that a Gemfile exists.
+ @erb_cmd ||= if bundle_path = cmd_path('bundle') and `#{bundle_path}` and $? == 0
+ "#{bundle_path} exec erubis -z %s"
+ elsif erubis_path = cmd_path('erubis')
+ "#{erubis_path} -z %s"
+ elsif erb_path = cmd_path('erb')
+ # Set trim mode to "-", just as Rails does
+ "#{erb_path} -xT - %s | #{compiler_ruby} -wc"
+ else
+ raise "eRuby parser not found!"
+ end
+ @erb_cmd % file
+end
changed_ruby_files = `git diff-index --name-only --cached HEAD`.split("\n").inject([]) do |files, line|
files << line.chomp if line =~ /(.+\.(e?rb|task|rake|thor)|[Rr]akefile|[Tt]horfile)/
- files
end
problematic_files = changed_ruby_files.inject([]) do |problematic_files, file|
if File.readable? file
cmd = if file =~ /\.erb\z/
- # Set trim mode to "-", just as Rails does
- "erb -xT - #{file} | #{compiler_ruby} -wc" unless skip_erb_files
+ check_erb_cmd(file)
else
- "#{compiler_ruby} -wc #{file}"
+ check_rb_cmd(file)
end
unless cmd.nil? then
errors = nil
popen3(cmd) do |stdin, stdout, stderr|
- errors = stderr.read.split("\n")
+ errors = stdout.read.split("\n") + stderr.read.split("\n")
end
-
+ # erubis prints its warnings on stdout, so we need to scan everything.
+ errors.reject!{ |line| line =~ /Syntax OK/ }
errors.reject!{ |line| line =~ /[0-9]+:\s+warning:/ } unless stop_on_warnings
-
unless errors.empty?
errors.map!{ |line| line.sub(/#{file}:/, '') }
problematic_files << "#{file}:\n#{errors.join("\n")}"
View
23 test.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+# Simple test script. If TEST_DIR does not exist, populates it with some test files.
+# Temporarily adds them to the git index, runs the pre-commit script, then removes them.
+# You can add your own files to TEST_DIR, then remove the whole directory when you're
+# done testing.
+
+TEST_DIR="test-files"
+
+if [ ! -d $TEST_DIR ]; then
+ echo "Creating directory $TEST_DIR"
+ mkdir $TEST_DIR
+ cd $TEST_DIR
+ echo "puts \"Hello, world.\"" > syntax-ok.rb
+ echo "puts (\"Hello, world.\")" > syntax-warning.rb
+ echo "puts 'Hello, world.\"" > syntax-error.rb
+ echo "Hello, <%= 'world' %>." > syntax-ok.erb
+ echo "Hello, <%= world' %>." > syntax-error.erb
+ cd ..
+fi
+git add -f $TEST_DIR
+./pre-commit
+git reset $TEST_DIR

No commit comments for this range