1
- require 'wopen3 '
1
+ require 'forwardable '
2
2
3
3
module Overcommit ::Hook
4
4
# Functionality common to all hooks.
5
5
class Base
6
- def initialize ( config , hook_runner )
6
+ extend Forwardable
7
+
8
+ def_delegators :@context , :staged_files
9
+
10
+ def initialize ( config , context )
7
11
@config = config . hook_config ( self )
8
- @hook_runner = hook_runner
12
+ @context = context
9
13
end
10
14
11
15
# Runs the hook.
@@ -47,6 +51,14 @@ def run?
47
51
!( requires_modified_files? && applicable_files . empty? )
48
52
end
49
53
54
+ def in_path? ( cmd )
55
+ Overcommit ::Utils . in_path? ( cmd )
56
+ end
57
+
58
+ def command ( cmd )
59
+ Overcommit ::Utils . command ( cmd )
60
+ end
61
+
50
62
# Gets a list of staged files that apply to this hook based on its
51
63
# configured `include` and `exclude` lists.
52
64
def applicable_files
@@ -55,12 +67,6 @@ def applicable_files
55
67
56
68
private
57
69
58
- def staged_files
59
- @hook_runner . staged_files
60
- end
61
-
62
- # Returns whether the specified file is applicable to this hook based on the
63
- # hook's `include` and `exclude` file glob patterns.
64
70
def applicable_file? ( file )
65
71
includes = Array ( @config [ 'include' ] ) . map { |glob | convert_glob_to_absolute ( glob ) }
66
72
included = includes . empty? ||
@@ -81,73 +87,5 @@ def convert_glob_to_absolute(glob)
81
87
File . join ( repo_root , glob )
82
88
end
83
89
end
84
-
85
- # Returns the set of line numbers corresponding to the lines that were
86
- # changed in a specified file.
87
- def modified_lines ( staged_file )
88
- @modified_lines ||= { }
89
- @modified_lines [ staged_file ] ||= extract_modified_lines ( staged_file )
90
- end
91
-
92
- DIFF_HUNK_REGEX = /
93
- ^@@\s
94
- [^\s ]+\s # Ignore old file range
95
- \+ (\d +)(?:,(\d +))? # Extract range of hunk containing start line and number of lines
96
- \s @@.*$
97
- /x
98
-
99
- def extract_modified_lines ( staged_file )
100
- lines = Set . new
101
-
102
- `git diff --no-ext-diff --cached -U0 -- #{ staged_file } ` .
103
- scan ( DIFF_HUNK_REGEX ) do |start_line , lines_added |
104
-
105
- lines_added = ( lines_added || 1 ) . to_i # When blank, one line was added
106
- cur_line = start_line . to_i
107
-
108
- lines_added . times do
109
- lines . add cur_line
110
- cur_line += 1
111
- end
112
- end
113
-
114
- lines
115
- end
116
-
117
- # Returns whether a command can be found given the current environment path.
118
- def in_path? ( cmd )
119
- exts = ENV [ 'PATHEXT' ] ? ENV [ 'PATHEXT' ] . split ( ';' ) : [ '' ]
120
- ENV [ 'PATH' ] . split ( File ::PATH_SEPARATOR ) . each do |path |
121
- exts . each do |ext |
122
- exe = File . join ( path , "#{ cmd } #{ ext } " )
123
- return true if File . executable? exe
124
- end
125
- end
126
- false
127
- end
128
-
129
- # Wrap external subshell calls. This is necessary in order to allow
130
- # Overcommit to call other Ruby executables without requiring that they be
131
- # specified in Overcommit's Gemfile--a nasty consequence of using
132
- # `bundle exec overcommit` while developing locally.
133
- def command ( command )
134
- with_environment 'RUBYOPT' => nil do
135
- Wopen3 . system ( command )
136
- end
137
- end
138
-
139
- # Calls a block of code with a modified set of environment variables,
140
- # restoring them once the code has executed.
141
- def with_environment ( env , &block )
142
- old_env = { }
143
- env . each do |var , value |
144
- old_env [ var ] = ENV [ var . to_s ]
145
- ENV [ var . to_s ] = value
146
- end
147
-
148
- yield
149
- ensure
150
- old_env . each { |var , value | ENV [ var . to_s ] = value }
151
- end
152
90
end
153
91
end
0 commit comments