forked from bobgilmore/githooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·66 lines (53 loc) · 1.88 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
#!/usr/bin/env ruby
# Git pre-commit hook that catches errors that I commonly make.
#
# To intentionally ignore the hook (i.e., when adding an alert call), commit
# from the command line with "--no-verify"
#
# Loosely based on Henrik Nyh's <http://henrik.nyh.se> work (2011-10-08)
# under the MIT License.
#
# Bob Gilmore (dev@bobgilmore.name)
$:.unshift File.dirname(__FILE__)
$:.unshift "#{File.dirname(__FILE__)}/pre_commit"
require "pre_commit_helper.rb"
require "checker_results.rb"
Dir[File.dirname(__FILE__) + "/pre_commit/checkers/*_checker.rb"].each { |f| load f }
include PreCommitHelper
NON_CHECKED_FILE_EXTENSIONS = [
'.md',
'.mmd',
'.txt'
]
NON_CHECKED_FILE_NAMES = [
'Gemfile',
'Gemfile.lock'
]
PROJECT_TYPE = PreCommitHelper::project_type
TOPLEVEL = `git rev-parse --show-toplevel`
puts("Comitting to #{PROJECT_TYPE ? PROJECT_TYPE.capitalize : 'unknown type of'} project")
res = CheckerResults.new
# Loop over ALL errors and warnings and return ALL problems.
# I want to report on *all* problems that exist in the commit before aborting,
# so that anyone calling --no-verify has been informed of all problems first.
full_diff = `git diff --cached --`.encode("UTF-8")
full_diff.scan(%r{^\+\+\+ b/(.+)\n@@.*\n([\s\S]*?)(?:^diff|\z)}).each do |file, diff|
unless (NON_CHECKED_FILE_NAMES.include?(file) || NON_CHECKED_FILE_EXTENSIONS.include?(File.extname(file)))
dir = File.dirname(file)
changed_code_for_file = diff.split("\n").select { |x| x.start_with?("+") }.join("\n")
diff_data = { directory: dir,
file: file,
changes: changed_code_for_file,
toplevel: TOPLEVEL,
project_type: PROJECT_TYPE }
PreCommitHelper.checker_classes.each do |checker_class|
res.record(checker_class.new(diff_data))
end
end
end
# Report errors and fail the commit if necessary
if res.errors?
puts(res)
puts "To commit anyway, use --no-verify"
exit 1
end