Skip to content

Commit 0414b38

Browse files
nasamuffingitster
authored andcommitted
hook: support a --to-stdin=<path> option
Expose the "path_to_stdin" API added in the preceding commit in the "git hook run" command. For now we won't be using this command interface outside of the tests, but exposing this functionality makes it easier to test the hook API. The plan is to use this to extend the "sendemail-validate" hook[1][2]. 1. https://lore.kernel.org/git/[email protected] 2. https://lore.kernel.org/git/[email protected] Signed-off-by: Emily Shaffer <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 96af564 commit 0414b38

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Documentation/git-hook.txt

Lines changed: 6 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 [--ignore-missing] <hook-name> [-- <hook-args>]
11+
'git hook' run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]
1212

1313
DESCRIPTION
1414
-----------
@@ -31,6 +31,11 @@ linkgit:githooks[5] for arguments hooks might expect (if any).
3131
OPTIONS
3232
-------
3333

34+
--to-stdin::
35+
For "run"; Specify a file which will be streamed into the
36+
hook's stdin. The hook will receive the entire file from
37+
beginning to EOF.
38+
3439
--ignore-missing::
3540
Ignore any missing hook by quietly returning zero. Used for
3641
tools that want to do a blind one-shot run of a hook that may

builtin/hook.c

Lines changed: 3 additions & 1 deletion
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 [--ignore-missing] <hook-name> [-- <hook-args>]")
10+
N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
1111

1212
static const char * const builtin_hook_usage[] = {
1313
BUILTIN_HOOK_RUN_USAGE,
@@ -28,6 +28,8 @@ static int run(int argc, const char **argv, const char *prefix)
2828
struct option run_options[] = {
2929
OPT_BOOL(0, "ignore-missing", &ignore_missing,
3030
N_("silently ignore missing requested <hook-name>")),
31+
OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
32+
N_("file to read into hooks' stdin")),
3133
OPT_END(),
3234
};
3335
int ret;

t/t1800-hook.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,22 @@ test_expect_success 'git hook run a hook with a bad shebang' '
177177
test_cmp expect actual
178178
'
179179

180+
test_expect_success 'stdin to hooks' '
181+
write_script .git/hooks/test-hook <<-\EOF &&
182+
echo BEGIN stdin
183+
cat
184+
echo END stdin
185+
EOF
186+
187+
cat >expect <<-EOF &&
188+
BEGIN stdin
189+
hello
190+
END stdin
191+
EOF
192+
193+
echo hello >input &&
194+
git hook run --to-stdin=input test-hook 2>actual &&
195+
test_cmp expect actual
196+
'
197+
180198
test_done

0 commit comments

Comments
 (0)