Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hook: Provide GIT_HOOK for all hooks #1271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

john-cai
Copy link
Contributor

@john-cai john-cai commented May 27, 2022

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.

CC: Emily Shaffer emilyshaffer@google.com
cc: Ævar Arnfjörð Bjarmason avarab@gmail.com

@gitgitgadget-git
Copy link

There are issues in commit dd3f440:
hook: Provide GIT_HOOK for all hooks
Prefixed commit message must be in lower case
Commit not signed off

@gitgitgadget-git
Copy link

There are issues in commit 20b0fed:
hook: Provide GIT_HOOK for all hooks
Prefixed commit message must be in lower case
Commit not signed off

@john-cai john-cai force-pushed the jc/set-git-hooks-env-var branch 3 times, most recently from eb03517 to 875ff0f Compare May 27, 2022 20:17
@john-cai
Copy link
Contributor Author

/preview

@gitgitgadget-git
Copy link

Preview email sent as pull.1271.git.git.1653683544318.gitgitgadget@gmail.com

@john-cai john-cai force-pushed the jc/set-git-hooks-env-var branch 2 times, most recently from 5337d67 to 91a01c2 Compare May 27, 2022 20:44
@john-cai
Copy link
Contributor Author

/preview

@gitgitgadget-git
Copy link

Preview email sent as pull.1271.git.git.1653684451756.gitgitgadget@gmail.com

@john-cai
Copy link
Contributor Author

/submit

@gitgitgadget-git
Copy link

Submitted as pull.1271.git.git.1653684771998.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1271/john-cai/jc/set-git-hooks-env-var-v1

To fetch this version to local tag pr-git-1271/john-cai/jc/set-git-hooks-env-var-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1271/john-cai/jc/set-git-hooks-env-var-v1

@gitgitgadget-git
Copy link

On the Git mailing list, Junio C Hamano wrote (reply to this):

"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 john-cai force-pushed the jc/set-git-hooks-env-var branch 2 times, most recently from 5aac969 to 3d49598 Compare May 28, 2022 04:20
@gitgitgadget-git
Copy link

On the Git mailing list, John Cai wrote (reply to this):

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

@gitgitgadget-git
Copy link

On the Git mailing list, Ævar Arnfjörð Bjarmason wrote (reply to this):

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.

@gitgitgadget-git
Copy link

User Ævar Arnfjörð Bjarmason <avarab@gmail.com> has been added to the cc: list.

@john-cai john-cai force-pushed the jc/set-git-hooks-env-var branch 2 times, most recently from b2201ad to b80d92a Compare May 28, 2022 16:39
Currently, in order to use one executable for multiple hooks, the
executable can look at argv. With config based hooks, that can't be
relied upon anymore.

To address this, provide a GIT_HOOK_NAME variable that is set to the hook
event that triggered it. For non config based hooks, this is superfluous
to simply inspecting argv. However, a GIT_HOOK_NAME var provides a
common mechanism between config based hooks and non config based hooks.

Signed-off-by: John Cai <johncai86@gmail.com>
@gitgitgadget-git
Copy link

On the Git mailing list, John Cai wrote (reply to this):

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.

@gitgitgadget-git
Copy link

On the Git mailing list, Junio C Hamano wrote (reply to this):

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

@gitgitgadget-git
Copy link

On the Git mailing list, John Cai wrote (reply to this):

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant