@@ -8,7 +8,7 @@ git-hook - run git hooks
SYNOPSIS
--------
[verse]
-'git hook' run [--ignore-missing] <hook-name> [-- <hook-args>]
+'git hook' run [--to-stdin=<path>] [--ignore-missing] <hook-name> [-- <hook-args>]
DESCRIPTION
-----------
@@ -30,6 +30,11 @@ run::
OPTIONS
-------
+--to-stdin::
+ For "run"; Specify a file which will be streamed into the
+ hook's stdin. The hook will receive the entire file from
+ beginning to EOF.
+
--ignore-missing::
Ignore any missing hook by quietly returning zero. Used for
tools that want to do a blind one-shot run of a hook that may
@@ -7,7 +7,7 @@
#include "strvec.h"
static const char * const builtin_hook_usage[] = {
- N_("git hook run <hook-name> [-- <hook-args>]"),
+ N_("git hook run [--to-stdin=<path>] <hook-name> [-- <hook-args>]"),
NULL
};
@@ -23,6 +23,8 @@ static int run(int argc, const char **argv, const char *prefix)
struct option run_options[] = {
OPT_BOOL(0, "ignore-missing", &ignore_missing,
N_("exit quietly with a zero exit code if the requested hook cannot be found")),
+ OPT_STRING(0, "to-stdin", &opt.path_to_stdin, N_("path"),
+ N_("file to read into hooks' stdin")),
OPT_END(),
};
@@ -58,7 +58,13 @@ static int pick_next_hook(struct child_process *cp,
if (!run_me)
BUG("did we not return 1 in notify_hook_finished?");
- cp->no_stdin = 1;
+ /* reopen the file for stdin; run_command closes it. */
+ if (hook_cb->options->path_to_stdin) {
+ cp->no_stdin = 0;
+ cp->in = xopen(hook_cb->options->path_to_stdin, O_RDONLY);
+ } else {
+ cp->no_stdin = 1;
+ }
cp->env = hook_cb->options->env.v;
cp->stdout_to_stderr = 1;
cp->trace2_hook_name = hook_cb->hook_name;
@@ -28,6 +28,8 @@ struct run_hooks_opt
/* Path to initial working directory for subprocess */
const char *dir;
+ /* Path to file which should be piped to stdin for each hook */
+ const char *path_to_stdin;
};
#define RUN_HOOKS_OPT_INIT { \
@@ -133,4 +133,22 @@ test_expect_success 'set up a pre-commit hook in core.hooksPath' '
EOF
'
+test_expect_success 'stdin to hooks' '
+ write_script .git/hooks/test-hook <<-\EOF &&
+ echo BEGIN stdin
+ cat
+ echo END stdin
+ EOF
+
+ cat >expect <<-EOF &&
+ BEGIN stdin
+ hello
+ END stdin
+ EOF
+
+ echo hello >input &&
+ git hook run --to-stdin=input test-hook 2>actual &&
+ test_cmp expect actual
+'
+
test_done