Skip to content

Commit 0d3979c

Browse files
avargitster
authored andcommitted
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 <[email protected]> Reviewed-by: Emily Shaffer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a3017d commit 0d3979c

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Documentation/git-hook.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-hook - Run git hooks
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git hook' run <hook-name> [-- <hook-args>]
11+
'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>]
1212

1313
DESCRIPTION
1414
-----------
@@ -28,6 +28,14 @@ Any positional arguments to the hook should be passed after a
2828
mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
2929
linkgit:githooks[5] for arguments hooks might expect (if any).
3030

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+
3139
SEE ALSO
3240
--------
3341
linkgit:githooks[5]

builtin/hook.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "strvec.h"
88

99
#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>]")
1111

1212
static const char * const builtin_hook_usage[] = {
1313
BUILTIN_HOOK_RUN_USAGE,
@@ -23,8 +23,11 @@ static int run(int argc, const char **argv, const char *prefix)
2323
{
2424
int i;
2525
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
26+
int ignore_missing = 0;
2627
const char *hook_name;
2728
struct option run_options[] = {
29+
OPT_BOOL(0, "ignore-missing", &ignore_missing,
30+
N_("silently ignore missing requested <hook-name>")),
2831
OPT_END(),
2932
};
3033
int ret;
@@ -52,7 +55,8 @@ static int run(int argc, const char **argv, const char *prefix)
5255
git_config(git_default_config, NULL);
5356

5457
hook_name = argv[0];
55-
opt.error_if_missing = 1;
58+
if (!ignore_missing)
59+
opt.error_if_missing = 1;
5660
ret = run_hooks_opt(hook_name, &opt);
5761
if (ret < 0) /* error() return */
5862
ret = 1;

t/t1800-hook.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ test_expect_success 'git hook run: nonexistent hook' '
2121
test_cmp stderr.expect stderr.actual
2222
'
2323

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+
2429
test_expect_success 'git hook run: basic' '
2530
write_script .git/hooks/test-hook <<-EOF &&
2631
echo Test hook

0 commit comments

Comments
 (0)