All of lore.kernel.org
 help / color / mirror / Atom feed
From: "John Cai via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Emily Shaffer <emilyshaffer@google.com>,
	John Cai <johncai86@gmail.com>, John Cai <johncai86@gmail.com>
Subject: [PATCH] hook: provide GIT_HOOK for all hooks
Date: Fri, 27 May 2022 20:52:51 +0000	[thread overview]
Message-ID: <pull.1271.git.git.1653684771998.gitgitgadget@gmail.com> (raw)

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

             reply	other threads:[~2022-05-27 20:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-27 20:52 John Cai via GitGitGadget [this message]
2022-05-27 21:20 ` [PATCH] hook: provide GIT_HOOK for all hooks 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.1271.git.git.1653684771998.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=johncai86@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.