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/ad152e25-4061-9955-d3e6-a2c8b1bd24e7@amd.com 2. https://lore.kernel.org/git/20230120012459.920932-1-michael.strawbridge@amd.com Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Emily Shaffer committed
Feb 8, 2023 at 20:21 UTC
0414b3891cd3adb80b879c7be49d9b727e2b23f5
3 files changed
+27
-2
Documentation/git-hook.txt
+6
-1
@@ -8,7 +8,7 @@ git-hook - Run git hooks
8
SYNOPSIS
9
--------
10
[verse]
11
-'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>]
11
+'git hook' run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]
12
13
DESCRIPTION
14
-----------
@@ -31,6 +31,11 @@ linkgit:githooks[5] for arguments hooks might expect (if any).
31
OPTIONS
32
-------
33
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
+
39
--ignore-missing::
40
Ignore any missing hook by quietly returning zero. Used for
41
tools that want to do a blind one-shot run of a hook that may
builtin/hook.c
+3
-1
@@ -7,7 +7,7 @@
7
#include "strvec.h"
8
9
#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>]")
11
12
static const char * const builtin_hook_usage[] = {
13
BUILTIN_HOOK_RUN_USAGE,
@@ -28,6 +28,8 @@ static int run(int argc, const char **argv, const char *prefix)
28
struct option run_options[] = {
29
OPT_BOOL(0, "ignore-missing", &ignore_missing,
30
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")),
33
OPT_END(),
34
};
35
int ret;
t/t1800-hook.sh
+18
@@ -177,4 +177,22 @@ test_expect_success 'git hook run a hook with a bad shebang' '
177
test_cmp expect actual
178
'
179
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
+
198
test_done