This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
pre-commit
executable file
·77 lines (62 loc) · 2.21 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env ruby
# encoding: utf-8
#
# 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")
# Ruby 2 or higher required
if RUBY_VERSION.to_f < 2.0
$stderr.puts "Ruby version 2.0 or higher required!"
exit 1
end
require 'shellwords'
require 'open3'
include Open3
# Set this to true if you want warnings to stop your commit
stop_on_warnings = (%x(git config --bool hooks.stop-on-warnings).chomp != "false")
# Set this to true if you'd like to skip checking erb files
skip_erb_files = (%x(git config --bool hooks.skip-erb-files).chomp == "true")
# Set this if you'd like to use a specific ruby executable to for syntax checking
ruby_compiler = %x(git config hooks.ruby-compiler).chomp
if ruby_compiler.empty?
compiler_ruby = %x(which ruby).chomp
end
if compiler_ruby.nil? or compiler_ruby == ""
$stderr.puts "Couldn't find `rbx` or `ruby` executable!"
exit 1
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
warnings_and_errors = []
changed_ruby_files.each do |file|
next if not File.readable?(file)
if file =~ /\.erb\z/
next if skip_erb_files
# use actual rails ERB renderer
cmd = "#{compiler_ruby} -e 'require \"action_view\"; puts ActionView::Template::Handlers::Erubis.new(File.read(\"#{file}\")).src' | #{compiler_ruby} -wc"
else
cmd = [compiler_ruby, "-wc", file].shelljoin
end
errors = []
popen3(cmd) do |stdin, stdout, stderr|
errors = stderr.read.split("\n")
end
errors.reject!{ |line| line =~ /[0-9]+:\s+warning:/ } unless stop_on_warnings
if errors.any?
errors.map!{ |line| line.sub(/#{file}:/, '') }
warnings_and_errors << "#{file}:\n#{errors.join("\n")}"
end
end
if warnings_and_errors.any?
$stderr.puts warnings_and_errors.join("\n\n")
exit 1
else
# All is well
exit 0
end