git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / 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

* Re: [PATCH] hook: provide GIT_HOOK for all hooks
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2022-05-27 21:20 UTC (permalink / raw)
  To: John Cai via GitGitGadget; +Cc: git, Emily Shaffer, John Cai

"John Cai via GitGitGadget" <gitgitgadget@gmail.com> writes:

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

I agree it would be handy to give hooks to play multiple roles by
dispatching on its name, just like our "git" potty can dispatch
built-ins when called "git-foo".

I do not think GIT_HOOK is a good name for the environment variable
that is used for that purpose, though.  It is easily mistaken as if
end users can set GIT_HOOK environment themselves to point at a
program and cause "git" to run it whenever it may want to run any
hook, for example.  IOW, the name is overly broad.

How about calling it with a name with "HOOK" and "NAME" in it?

> 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

This will introduce the first hit from "git grep printenv".

It is not even in POSIX.  Do we absolutely need to?

Perhaps

    echo "$GIT_HOOK"

is sufficient, or if you want to distinguish an unset and set to
empty string:

    if test "${GIT_HOOK+set}" = "set"
    then
        echo "GIT_HOOK is set to '$GIT_HOOK'"
    else
        echo "GIT_HOOK is unset"
	exit 1
    fi

may be another way.

> +	cat >expect <<-\EOF &&
> +	test-hook
> +	EOF

For one-liner, 

	echo test-hook >expect &&

should be a more compact and equally understandable way to write this.

> +	git hook run test-hook 2>actual &&
> +	test_cmp expect actual
> +'

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

* Re: [PATCH] hook: provide GIT_HOOK for all hooks
  2022-05-27 21:20 ` Junio C Hamano
@ 2022-05-28  4:26   ` John Cai
  0 siblings, 0 replies; 7+ messages in thread
From: John Cai @ 2022-05-28  4:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: John Cai via GitGitGadget, git, Emily Shaffer

Hi Junio

On 27 May 2022, at 17:20, Junio C Hamano wrote:

> "John Cai via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> 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.
>
> I agree it would be handy to give hooks to play multiple roles by
> dispatching on its name, just like our "git" potty can dispatch
> built-ins when called "git-foo".
>
> I do not think GIT_HOOK is a good name for the environment variable
> that is used for that purpose, though.  It is easily mistaken as if
> end users can set GIT_HOOK environment themselves to point at a
> program and cause "git" to run it whenever it may want to run any
> hook, for example.  IOW, the name is overly broad.

Yes, I see what you mean. It would be good to pick a more specific variable.

>
> How about calling it with a name with "HOOK" and "NAME" in it?

For lack of imagination, would GIT_HOOK_NAME still be too broad?

>
>> 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
>
> This will introduce the first hit from "git grep printenv".
>
> It is not even in POSIX.  Do we absolutely need to?

certainly not, I'll change this.

>
> Perhaps
>
>     echo "$GIT_HOOK"
>
> is sufficient, or if you want to distinguish an unset and set to
> empty string:
>
>     if test "${GIT_HOOK+set}" = "set"
>     then
>         echo "GIT_HOOK is set to '$GIT_HOOK'"
>     else
>         echo "GIT_HOOK is unset"
> 	exit 1
>     fi
>
> may be another way.
>
>> +	cat >expect <<-\EOF &&
>> +	test-hook
>> +	EOF
>
> For one-liner,
>
> 	echo test-hook >expect &&
>
> should be a more compact and equally understandable way to write this.

good point!

>
>> +	git hook run test-hook 2>actual &&
>> +	test_cmp expect actual
>> +'

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

* Re: [PATCH] hook: provide GIT_HOOK for all hooks
  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 15:53 ` Ævar Arnfjörð Bjarmason
  2022-05-28 16:42   ` John Cai
  2022-05-28 17:24   ` Junio C Hamano
  1 sibling, 2 replies; 7+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-05-28 15:53 UTC (permalink / raw)
  To: John Cai via GitGitGadget; +Cc: git, Emily Shaffer, John Cai


On Fri, May 27 2022, John Cai via GitGitGadget wrote:

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

You can use one executable for multiple hooks already, I've written such
dispatchers that just look at the argv of the process.

What we will need something like this for is for the config-based hooks,
and I think it makes sense to have a facility that's portable across
both methods of hook invocations.

I really don't mind this change, and I think it's a good one to
make.

But the commit message & documentation here really should be updated to
reflect that this is currently superfluous to inspecting argv in the
hook process, and that we're providing this anyway for XYZ reason.

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

* Re: [PATCH] hook: provide GIT_HOOK for all hooks
  2022-05-28 15:53 ` Ævar Arnfjörð Bjarmason
@ 2022-05-28 16:42   ` John Cai
  2022-05-28 17:24   ` Junio C Hamano
  1 sibling, 0 replies; 7+ messages in thread
From: John Cai @ 2022-05-28 16:42 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: John Cai via GitGitGadget, git, Emily Shaffer

Hi Ævar

On 28 May 2022, at 11:53, Ævar Arnfjörð Bjarmason wrote:

> On Fri, May 27 2022, John Cai via GitGitGadget wrote:
>
>> 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.
>
> You can use one executable for multiple hooks already, I've written such
> dispatchers that just look at the argv of the process.
>
> What we will need something like this for is for the config-based hooks,
> and I think it makes sense to have a facility that's portable across
> both methods of hook invocations.

Ah yes, thanks for pointing this out. I will re-roll the commit message as we
as clarity the documentation.

>
> I really don't mind this change, and I think it's a good one to
> make.
>
> But the commit message & documentation here really should be updated to
> reflect that this is currently superfluous to inspecting argv in the
> hook process, and that we're providing this anyway for XYZ reason.

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

* Re: [PATCH] hook: provide GIT_HOOK for all hooks
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2022-05-28 17:24 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: John Cai via GitGitGadget, git, Emily Shaffer, John Cai

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> But the commit message & documentation here really should be updated to
> reflect that this is currently superfluous to inspecting argv in the
> hook process, and that we're providing this anyway for XYZ reason.

Or this probably is better added as part of the series that actually
adds the mechanism to trigger hooks defined in the configuration
file.  

Then "we do not need it now, but we will in the future because we
will do XYZ" does not have to be said, which is a huge plus.

Thanks.

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

* Re: [PATCH] hook: provide GIT_HOOK for all hooks
  2022-05-28 17:24   ` Junio C Hamano
@ 2022-05-31  1:31     ` John Cai
  0 siblings, 0 replies; 7+ messages in thread
From: John Cai @ 2022-05-31  1:31 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Ævar Arnfjörð Bjarmason, John Cai via GitGitGadget,
	git, Emily Shaffer



On 28 May 2022, at 13:24, Junio C Hamano wrote:

> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
>
>> But the commit message & documentation here really should be updated to
>> reflect that this is currently superfluous to inspecting argv in the
>> hook process, and that we're providing this anyway for XYZ reason.
>
> Or this probably is better added as part of the series that actually
> adds the mechanism to trigger hooks defined in the configuration
> file.

I don't mind including this as part of Ævar's config hook series. On the other
hand this patch could allow the config hooks series to be smaller and more
easily reviewed.

I'm okay either way--maybe Ævar can speak to what his preference is.

>
> Then "we do not need it now, but we will in the future because we
> will do XYZ" does not have to be said, which is a huge plus.
>
> Thanks.

thanks
John

^ permalink raw reply	[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

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).