git hook run: add an --ignore-missing flag
For certain one-shot hooks we'd like to optimistically run them, and not complain if they don't exist. This was already supported by the underlying hook.c library, but had not been exposed via "git hook run". The command version of this will be used by send-email in a subsequent commit. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Reviewed-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Dec 22, 2021 at 04:59 UTC
0d3979c175054187a4bd94dbbdc4f99300f7fc04
3 files changed
+20
-3
Documentation/git-hook.txt
+9
-1
@@ -8,7 +8,7 @@ git-hook - Run git hooks
8
SYNOPSIS
9
--------
10
[verse]
11
-'git hook' run <hook-name> [-- <hook-args>]
11
+'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>]
12
13
DESCRIPTION
14
-----------
@@ -28,6 +28,14 @@ Any positional arguments to the hook should be passed after a
28
mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
29
linkgit:githooks[5] for arguments hooks might expect (if any).
30
31
+OPTIONS
32
+-------
33
+
34
+--ignore-missing::
35
+ Ignore any missing hook by quietly returning zero. Used for
36
+ tools that want to do a blind one-shot run of a hook that may
37
+ or may not be present.
38
+
39
SEE ALSO
40
--------
41
linkgit:githooks[5]
builtin/hook.c
+6
-2
@@ -7,7 +7,7 @@
7
#include "strvec.h"
8
9
#define BUILTIN_HOOK_RUN_USAGE \
10
- N_("git hook run <hook-name> [-- <hook-args>]")
10
+ N_("git hook run [--ignore-missing] <hook-name> [-- <hook-args>]")
11
12
static const char * const builtin_hook_usage[] = {
13
BUILTIN_HOOK_RUN_USAGE,
@@ -23,8 +23,11 @@ static int run(int argc, const char **argv, const char *prefix)
23
{
24
int i;
25
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
26
+ int ignore_missing = 0;
27
const char *hook_name;
28
struct option run_options[] = {
29
+ OPT_BOOL(0, "ignore-missing", &ignore_missing,
30
+ N_("silently ignore missing requested <hook-name>")),
31
OPT_END(),
32
};
33
int ret;
@@ -52,7 +55,8 @@ static int run(int argc, const char **argv, const char *prefix)
55
git_config(git_default_config, NULL);
56
57
hook_name = argv[0];
55
- opt.error_if_missing = 1;
58
+ if (!ignore_missing)
59
+ opt.error_if_missing = 1;
60
ret = run_hooks_opt(hook_name, &opt);
61
if (ret < 0) /* error() return */
62
ret = 1;
t/t1800-hook.sh
+5
@@ -21,6 +21,11 @@ test_expect_success 'git hook run: nonexistent hook' '
21
test_cmp stderr.expect stderr.actual
22
'
23
24
+test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
25
+ git hook run --ignore-missing does-not-exist 2>stderr.actual &&
26
+ test_must_be_empty stderr.actual
27
+'
28
+
29
test_expect_success 'git hook run: basic' '
30
write_script .git/hooks/test-hook <<-EOF &&
31
echo Test hook