Skip to content

Commit 9a4028e

Browse files
author
Kevin Deisz
authored
Add skip_if hook option (sds#715)
Skip hook with `skip_if` configuration if defined.
1 parent a15edd2 commit 9a4028e

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Option | Description
241241
`processors` | The number of processing units to reserve for this hook. This does not reserve CPUs, but indicates that out of the total number of possible concurrent hooks allowed by the global `concurrency` option, this hook requires the specified number. Thus in the typical case where `concurrency` is set to the number of available cores (default), and you have a hook that executes an application which itself creates 2 threads (or is otherwise scheduled on 2 cores), you can indicate that Overcommit should allocate 2 `processors` to the hook. Ideally this means your hooks won't put undue load on your available cores.
242242
`install_command` | Command the user can run to install the `required_executable` (or alternately the specified `required_libraries`). This is intended for documentation purposes, as Overcommit does not install software on your behalf since there are too many edge cases where such behavior would result in incorrectly configured installations (e.g. installing a Python package in the global package space instead of in a virtual environment).
243243
`skip_file_checkout` | Whether to skip this hook for file checkouts (e.g. `git checkout some-ref -- file`). Only applicable to `PostCheckout` hooks.
244+
`skip_if` | Array of arguments to be executed to determine whether or not the hook should run. For example, setting this to a value of `['bash', '-c', '! which my-executable']` would allow you to skip running this hook if `my-executable` was not in the bin path.
244245

245246
In addition to the built-in configuration options, each hook can expose its
246247
own unique configuration options. The `AuthorEmail` hook, for example, allows

lib/overcommit/hook/base.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def excluded?
8484
end
8585

8686
def skip?
87-
@config['skip']
87+
@config['skip'] ||
88+
(@config['skip_if'] ? execute(@config['skip_if']).success? : false)
8889
end
8990

9091
def run?

spec/overcommit/hook/base_spec.rb

+65
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,69 @@
122122
end
123123
end
124124
end
125+
126+
context '#skip?' do
127+
before do
128+
config.stub(:for_hook).and_return(hook_config)
129+
end
130+
131+
subject { hook.skip? }
132+
133+
context 'with skip_if not specified' do
134+
let(:hook_config) do
135+
{ 'skip' => skip }
136+
end
137+
138+
context 'with skip true' do
139+
let(:skip) { true }
140+
141+
it { subject.should == true }
142+
end
143+
144+
context 'with skip false' do
145+
let(:skip) { false }
146+
147+
it { subject.should == false }
148+
end
149+
end
150+
151+
context 'with skip_if specified' do
152+
before do
153+
result = Overcommit::Subprocess::Result.new(success ? 0 : 1, '', '')
154+
allow(Overcommit::Utils).to receive(:execute).and_return(result)
155+
end
156+
157+
let(:hook_config) do
158+
{ 'skip' => skip, 'skip_if' => ['bash', '-c', '! which my-executable'] }
159+
end
160+
161+
context 'with skip true and skip_if returning true' do
162+
let(:skip) { true }
163+
let(:success) { true }
164+
165+
it { subject.should == true }
166+
end
167+
168+
context 'with skip true and skip_if returning false' do
169+
let(:skip) { true }
170+
let(:success) { false }
171+
172+
it { subject.should == true }
173+
end
174+
175+
context 'with skip false and skip_if returning true' do
176+
let(:skip) { false }
177+
let(:success) { true }
178+
179+
it { subject.should == true }
180+
end
181+
182+
context 'with skip false and skip_if returning false' do
183+
let(:skip) { false }
184+
let(:success) { false }
185+
186+
it { subject.should == false }
187+
end
188+
end
189+
end
125190
end

0 commit comments

Comments
 (0)