From: John Cai <johncai86@xxxxxxxxx> In order to allow users to use one executable for multiple hooks, provide a GIT_HOOK variable that is set to the hook event that triggered it. Signed-off-by: John Cai <johncai86@xxxxxxxxx> --- hook: Provide GIT_HOOK for all hooks In order to allow users to use one executable for multiple hooks, provide a GIT_HOOK variable that is set to the hook event that triggered it. Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1271%2Fjohn-cai%2Fjc%2Fset-git-hooks-env-var-v1 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1271/john-cai/jc/set-git-hooks-env-var-v1 Pull-Request: https://github.com/git/git/pull/1271 Documentation/githooks.txt | 4 ++++ hook.c | 2 ++ t/t1800-hook.sh | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index a16e62bc8c8..b27ed8d11b6 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -31,6 +31,10 @@ Hooks can get their arguments via the environment, command-line arguments, and stdin. See the documentation for each hook below for details. +The `$GIT_HOOK` environment variable is passed to all hooks and holds the +triggering hook event, eg: `pre-commit`, `update`, etc. This allows one +executable to be used for multiple hooks. + `git init` may copy hooks to the new repository, depending on its configuration. See the "TEMPLATE DIRECTORY" section in linkgit:git-init[1] for details. When the rest of this document refers diff --git a/hook.c b/hook.c index 1d51be3b77a..966f2114db4 100644 --- a/hook.c +++ b/hook.c @@ -144,6 +144,8 @@ int run_hooks_opt(const char *hook_name, struct run_hooks_opt *options) cb_data.hook_path = abs_path.buf; } + strvec_pushf(&cb_data.options->env,"GIT_HOOK=%s", hook_name); + run_processes_parallel_tr2(jobs, pick_next_hook, notify_start_failure, diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh index 26ed5e11bc8..a22c1a82a5e 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -38,6 +38,18 @@ test_expect_success 'git hook run: basic' ' test_cmp expect actual ' +test_expect_success 'git hook run: $GIT_HOOK' ' + test_hook test-hook <<-EOF && + printenv GIT_HOOK + EOF + + cat >expect <<-\EOF && + test-hook + EOF + git hook run test-hook 2>actual && + test_cmp expect actual +' + test_expect_success 'git hook run: stdout and stderr both write to our stderr' ' test_hook test-hook <<-EOF && echo >&1 Will end up on stderr base-commit: 8ddf593a250e07d388059f7e3f471078e1d2ed5c -- gitgitgadget