Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hook: Provide GIT_HOOK for all hooks #1271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Documentation/githooks.txt
Expand Up @@ -31,6 +31,12 @@ 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` and so on, allowing the hook
executable to inspect `$GIT_HOOK` to tell which event triggered it. This is
necessary for config based hooks, which can't rely on inspecting the argument
list to determine which hook event led to its execution.

`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_NAME=%s", hook_name);

run_processes_parallel_tr2(jobs,
pick_next_hook,
notify_start_failure,
Expand Down
10 changes: 10 additions & 0 deletions t/t1800-hook.sh
Expand Up @@ -38,6 +38,16 @@ test_expect_success 'git hook run: basic' '
test_cmp expect actual
'

test_expect_success 'git hook run: $GIT_HOOK_NAME' '
test_hook test-hook <<-EOF &&
echo \$GIT_HOOK_NAME
EOF

echo test-hook >expect &&
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