git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] hook: provide GIT_HOOK for all hooks

From
John Cai via GitGitGadget <gitgitgadget@gmail.com>
Date
May 27, 2022, 20:52 UTC
Message-ID
<pull.1271.git.git.1653684771998.gitgitgadget@gmail.com>
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
Next: Junio C Hamano
Message 1 of 7 in “hook: provide GIT_HOOK for all hooks”
  1. hook: provide GIT_HOOK for all hooksJohn Cai via GitGitGadget, May 27, 2022
  2. Junio C HamanoMay 27, 2022
  3. John CaiMay 28, 2022
  4. Ævar Arnfjörð BjarmasonMay 28, 2022
  5. John CaiMay 28, 2022
  6. Junio C HamanoMay 28, 2022
  7. John CaiMay 31, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.