Skip to content

Commit 1ecce72

Browse files
10ne1gitster
authored andcommitted
hook: allow disabling config hooks
Hooks specified via configs are always enabled, however users might want to disable them without removing from the config, like locally disabling a global hook. Add a hook.<name>.enabled config which defaults to true and can be optionally set for each configured hook. Suggested-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 03b4043 commit 1ecce72

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

Documentation/config/hook.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ hook.<name>.event::
1313
specified event, the associated `hook.<name>.command` is executed.
1414
This is a multi-valued key. To run `hook.<name>` on multiple
1515
events, specify the key more than once. See linkgit:git-hook[1].
16+
17+
hook.<name>.enabled::
18+
Whether the hook `hook.<name>` is enabled. Defaults to `true`.
19+
Set to `false` to disable the hook without removing its
20+
configuration. This is particularly useful when a hook is defined
21+
in a system or global config file and needs to be disabled for a
22+
specific repository. See linkgit:git-hook[1].

hook.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ static int hook_config_lookup_all(const char *key, const char *value,
164164
char *old = strmap_put(&data->commands, hook_name,
165165
xstrdup(value));
166166
free(old);
167+
} else if (!strcmp(subkey, "enabled")) {
168+
switch (git_parse_maybe_bool(value)) {
169+
case 0: /* disabled */
170+
if (!unsorted_string_list_lookup(&data->disabled_hooks,
171+
hook_name))
172+
string_list_append(&data->disabled_hooks,
173+
hook_name);
174+
break;
175+
case 1: /* enabled: undo a prior disabled entry */
176+
unsorted_string_list_remove(&data->disabled_hooks,
177+
hook_name);
178+
break;
179+
default:
180+
break; /* ignore unrecognised values */
181+
}
167182
}
168183

169184
free(hook_name);
@@ -216,6 +231,11 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
216231
const char *hname = hook_names->items[i].string;
217232
char *command;
218233

234+
/* filter out disabled hooks */
235+
if (unsorted_string_list_lookup(&cb_data.disabled_hooks,
236+
hname))
237+
continue;
238+
219239
command = strmap_get(&cb_data.commands, hname);
220240
if (!command)
221241
die(_("'hook.%s.command' must be configured or "

t/t1800-hook.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,38 @@ test_expect_success 'rejects hooks with no commands configured' '
318318
test_grep "hook.broken.command" actual
319319
'
320320

321+
test_expect_success 'disabled hook is not run' '
322+
test_config hook.skipped.event "test-hook" &&
323+
test_config hook.skipped.command "echo \"Should not run\"" &&
324+
test_config hook.skipped.enabled false &&
325+
326+
git hook run --ignore-missing test-hook 2>actual &&
327+
test_must_be_empty actual
328+
'
329+
330+
test_expect_success 'disabled hook does not appear in git hook list' '
331+
test_config hook.active.event "pre-commit" &&
332+
test_config hook.active.command "echo active" &&
333+
test_config hook.inactive.event "pre-commit" &&
334+
test_config hook.inactive.command "echo inactive" &&
335+
test_config hook.inactive.enabled false &&
336+
337+
git hook list pre-commit >actual &&
338+
test_grep "active" actual &&
339+
test_grep ! "inactive" actual
340+
'
341+
342+
test_expect_success 'globally disabled hook can be re-enabled locally' '
343+
test_config_global hook.global-hook.event "test-hook" &&
344+
test_config_global hook.global-hook.command "echo \"global-hook ran\"" &&
345+
test_config_global hook.global-hook.enabled false &&
346+
test_config hook.global-hook.enabled true &&
347+
348+
echo "global-hook ran" >expected &&
349+
git hook run test-hook 2>actual &&
350+
test_cmp expected actual
351+
'
352+
321353
test_expect_success 'git hook run a hook with a bad shebang' '
322354
test_when_finished "rm -rf bad-hooks" &&
323355
mkdir bad-hooks &&

0 commit comments

Comments
 (0)