diff mbox series

hook: provide GIT_HOOK for all hooks

Message ID pull.1271.git.git.1653684771998.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series hook: provide GIT_HOOK for all hooks | expand

Commit Message

John Cai May 27, 2022, 8:52 p.m. UTC
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(+)


base-commit: 8ddf593a250e07d388059f7e3f471078e1d2ed5c

Comments

Junio C Hamano May 27, 2022, 9:20 p.m. UTC | #1
"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
> +'
John Cai May 28, 2022, 4:26 a.m. UTC | #2
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
>> +'
Ævar Arnfjörð Bjarmason May 28, 2022, 3:53 p.m. UTC | #3
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.
John Cai May 28, 2022, 4:42 p.m. UTC | #4
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.
Junio C Hamano May 28, 2022, 5:24 p.m. UTC | #5
Æ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.
John Cai May 31, 2022, 1:31 a.m. UTC | #6
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
diff mbox series

Patch

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