git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hook: provide GIT_HOOK for all hooks
@ 2022-05-27 20:52 John Cai via GitGitGadget
  2022-05-27 21:20 ` Junio C Hamano
  2022-05-28 15:53 ` Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 7+ messages in thread
From: John Cai via GitGitGadget @ 2022-05-27 20:52 UTC (permalink / raw)
  To: git; +Cc: Emily Shaffer, John Cai, John Cai

From: John Cai <johncai86@gmail.com>

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>
---
    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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-05-31  1:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-27 20:52 [PATCH] hook: provide GIT_HOOK for all hooks John Cai via GitGitGadget
2022-05-27 21:20 ` Junio C Hamano
2022-05-28  4:26   ` John Cai
2022-05-28 15:53 ` Ævar Arnfjörð Bjarmason
2022-05-28 16:42   ` John Cai
2022-05-28 17:24   ` Junio C Hamano
2022-05-31  1:31     ` John Cai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).