Skip to content

Commit

Permalink
hook: provide GIT_HOOK for all hooks
Browse files Browse the repository at this point in the history
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@gmail.com>
  • Loading branch information
john-cai authored and John Cai committed May 27, 2022
1 parent 8ddf593 commit 91a01c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Documentation/githooks.txt
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions hook.c
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions t/t1800-hook.sh
Expand Up @@ -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
Expand Down

0 comments on commit 91a01c2

Please sign in to comment.