threads / discuss / 44778

HowTo distribute a hook with the reposity.

Subject: HowTo distribute a hook with the reposity.

## tl;dr

9 messages between Dec 28, 2016 and Dec 29, 2016.

replies: 8people: 4as markdown or json

John P. Hartmann· Dec 28, 2016, 01:34 UTC · lore

I would like a hook in .got/hooks to be made available to all who clone or pull a particular project. I'd also like the hook to be under git control (changes committed &c). I added a hook, but git status does not show it. Presumably git excludes its files in .git/ from version control lest there be a chiken-and-egg situation.

Is there a way to achieve this?  Or a better way to do it?
Thanks,  j.
Jacob Keller· Dec 28, 2016, 05:32 UTC · re: John P. Hartmann · lore

Re: HowTo distribute a hook with the reposity.

On Tue, Dec 27, 2016 at 5:34 PM, John P. Hartmann <jphartmann@gmail.com> wrote:
Show 9 quoted lines
> I would like a hook in .got/hooks to be made available to all who clone or
> pull a particular project.  I'd also like the hook to be under git control
> (changes committed &c).  I added a hook, but git status does not show it.
> Presumably git excludes its files in .git/ from version control lest there
> be a chiken-and-egg situation.
>
> Is there a way to achieve this?  Or a better way to do it?
>
> Thanks,  j.

Best way I found, was add a script with an "installme" shell script or similar that you tell all users of the repository that they are expected to run this to install the scripts. You can' make it happen automatically.

Thanks, Jake

Jeff King· Dec 28, 2016, 06:08 UTC · re: Jacob Keller · lore

Re: HowTo distribute a hook with the reposity.

On Tue, Dec 27, 2016 at 09:32:22PM -0800, Jacob Keller wrote:
Show 15 quoted lines
> On Tue, Dec 27, 2016 at 5:34 PM, John P. Hartmann <jphartmann@gmail.com> wrote:
> > I would like a hook in .got/hooks to be made available to all who clone or
> > pull a particular project.  I'd also like the hook to be under git control
> > (changes committed &c).  I added a hook, but git status does not show it.
> > Presumably git excludes its files in .git/ from version control lest there
> > be a chiken-and-egg situation.
> >
> > Is there a way to achieve this?  Or a better way to do it?
> >
> > Thanks,  j.
> 
> Best way I found, was add a script with an "installme" shell script or
> similar that you tell all users of the repository that they are
> expected to run this to install the scripts. You can' make it happen
> automatically.
I agree that is the best way to do it.
I didn't dig up previous discussions, but the gist is usually:
  1. Cloning a repository should not run arbitrary code from the remote
     without the user on the cloning side taking some further affirmative
     action.
     This is for security reasons. Obviously the next step is quite often
     to run "make" which does run arbitrary code, but that counts as an
     action.
  2. We could write a feature in git that manages hooks (or config, etc).
     But ultimately you would still be running "git clone
     --trust-remote-hooks" or something to satisfy point (1).
  3. There's not much point in doing point (2), because you can just
     spell it as "git clone && cd clone && ./install-hooks" and then git
     does not have to care at all about your scripts.
  4. A hook (or config) management system could do fancy things like
     merging your custom local config, picking up changes from the
     remote, etc. But all of that can happen outside of Git totally (and
     quite often you want to manage things in contributors setups
     _besides_ Git data anyway).
     An example system is:
       https://github.com/Autodesk/enterprise-config-for-git
     (with the disclaimer that I've never used it myself, so I have no
     idea how good it is).

I think you probably know all that, Jake, but I am laying it out for the benefit of the OP and the list. :)

-Peff
John P. Hartmann· Dec 28, 2016, 08:42 UTC · re: Jeff King · lore

Re: HowTo distribute a hook with the reposity.

Thanks; your point is taken.  One final wrinkle:

This project is hosted on github. If I put the hook into the repository manually (if I can; I don't know that), is it true that the hook would be distributed on a clone action, but not on a pull?

	j.
On 28/12/16 06:08, Jeff King wrote:
Show 56 quoted lines
> On Tue, Dec 27, 2016 at 09:32:22PM -0800, Jacob Keller wrote:
>
>> On Tue, Dec 27, 2016 at 5:34 PM, John P. Hartmann <jphartmann@gmail.com> wrote:
>>> I would like a hook in .got/hooks to be made available to all who clone or
>>> pull a particular project.  I'd also like the hook to be under git control
>>> (changes committed &c).  I added a hook, but git status does not show it.
>>> Presumably git excludes its files in .git/ from version control lest there
>>> be a chiken-and-egg situation.
>>>
>>> Is there a way to achieve this?  Or a better way to do it?
>>>
>>> Thanks,  j.
>>
>> Best way I found, was add a script with an "installme" shell script or
>> similar that you tell all users of the repository that they are
>> expected to run this to install the scripts. You can' make it happen
>> automatically.
>
> I agree that is the best way to do it.
>
> I didn't dig up previous discussions, but the gist is usually:
>
>   1. Cloning a repository should not run arbitrary code from the remote
>      without the user on the cloning side taking some further affirmative
>      action.
>
>      This is for security reasons. Obviously the next step is quite often
>      to run "make" which does run arbitrary code, but that counts as an
>      action.
>
>   2. We could write a feature in git that manages hooks (or config, etc).
>      But ultimately you would still be running "git clone
>      --trust-remote-hooks" or something to satisfy point (1).
>
>   3. There's not much point in doing point (2), because you can just
>      spell it as "git clone && cd clone && ./install-hooks" and then git
>      does not have to care at all about your scripts.
>
>   4. A hook (or config) management system could do fancy things like
>      merging your custom local config, picking up changes from the
>      remote, etc. But all of that can happen outside of Git totally (and
>      quite often you want to manage things in contributors setups
>      _besides_ Git data anyway).
>
>      An example system is:
>
>        https://github.com/Autodesk/enterprise-config-for-git
>
>      (with the disclaimer that I've never used it myself, so I have no
>      idea how good it is).
>
> I think you probably know all that, Jake, but I am laying it out for the
> benefit of the OP and the list. :)
>
> -Peff
>
Jeff King· Dec 28, 2016, 08:52 UTC · re: John P. Hartmann · lore

Re: HowTo distribute a hook with the reposity.

On Wed, Dec 28, 2016 at 08:42:25AM +0000, John P. Hartmann wrote:
> This project is hosted on github.  If I put the hook into the repository
> manually (if I can; I don't know that), is it true that the hook would be
> distributed on a clone action, but not on a pull?

I'm not sure what you mean by "into the repository". If you mean "into the .git directory", then no, you can't do that. Git will not add ".git" directory contents to a repository, you cannot manipulate the contents of ".git" directories on GitHub, and a client wouldn't ever look at them on clone or fetch anyway.

Did you mean something else?
-Peff
John P. Hartmann· Dec 28, 2016, 09:09 UTC · re: Jeff King · lore

Re: HowTo distribute a hook with the reposity.

Thanks, Peff, for your lucid answer to my question and much more. All is now clear to me.

The problem I am grappling with is how to obtain the latest git commit hash and enter it into the generated code. Configure/make would appear to the the time, but by then the user may not have git installed (e.g., extracted the project as .zip from github), so it needs to be done "back at the farm".

I hear that the best I can do is create a normal script in the repro and add to the developer handbook that "btw, here is a git hook that will run it automatically if you so choose".

Thank you both for your prompt and exhaustive answers.
	j.
On 28/12/16 08:52, Jeff King wrote:
Show 16 quoted lines
> On Wed, Dec 28, 2016 at 08:42:25AM +0000, John P. Hartmann wrote:
>
>> This project is hosted on github.  If I put the hook into the repository
>> manually (if I can; I don't know that), is it true that the hook would be
>> distributed on a clone action, but not on a pull?
>
> I'm not sure what you mean by "into the repository". If you mean "into
> the .git directory", then no, you can't do that. Git will not add ".git"
> directory contents to a repository, you cannot manipulate the contents
> of ".git" directories on GitHub, and a client wouldn't ever look at them
> on clone or fetch anyway.
>
> Did you mean something else?
>
> -Peff
>
Jeff King· Dec 28, 2016, 09:20 UTC · re: John P. Hartmann · lore

Re: HowTo distribute a hook with the reposity.

On Wed, Dec 28, 2016 at 09:09:04AM +0000, John P. Hartmann wrote:
Show 8 quoted lines
> The problem I am grappling with is how to obtain the latest git commit hash
> and enter it into the generated code.  Configure/make would appear to the
> the time, but by then the user may not have git installed (e.g., extracted
> the project as .zip from github), so it needs to be done "back at the farm".
> 
> I hear that the best I can do is create a normal script in the repro and add
> to the developer handbook that "btw, here is a git hook that will run it
> automatically if you so choose".

Yes, if you want the information to be conveyed in a .zip from GitHub, then you'll have to commit the value to the repository. For example, the script in Git itself looks like this:

  https://github.com/git/git/blob/master/GIT-VERSION-GEN

We pull the value from the Git repository if we have one, then fallback to a .version file which is generated when release tarballs are generated, and then finally fall back to a baked-in DEF_VER variable that is updated manually after each release (and that last is what you'd get if you had, say, a .zip file from GitHub).

I'm not sure you would want to update that DEF_VER for _every_ commit, though, even if you could do so with a hook. It would mean every commit would update the version field. And of course it could not have the git commit id, because that is generated from a hash of the contents that includes the contents of that version field.

If you're mostly interested in building straight from GitHub .zip files, those do include the commit id (or tag name) in the directory name, so your Makefile could deduce it at runtime from that information.

-Peff
Jacob Keller· Dec 28, 2016, 18:53 UTC · re: Jeff King · lore

Re: HowTo distribute a hook with the reposity.

On Tue, Dec 27, 2016 at 10:08 PM, Jeff King <peff@peff.net> wrote:
Show 9 quoted lines
>
>        https://github.com/Autodesk/enterprise-config-for-git
>
>      (with the disclaimer that I've never used it myself, so I have no
>      idea how good it is).
>
> I think you probably know all that, Jake, but I am laying it out for the
> benefit of the OP and the list. :)
>

Heh, well I didn't know about this last part so it's still useful to me! I knew of the larger description for why not but wasn't exactly clear how to word it so I am glad you filled that in. Thanks!

Regards, Jake

> -Peff
Lars Schneider· Dec 29, 2016, 10:29 UTC · re: Jacob Keller · lore

Re: HowTo distribute a hook with the reposity.

Show 16 quoted lines
> On 28 Dec 2016, at 19:53, Jacob Keller <jacob.keller@gmail.com> wrote:
> 
> On Tue, Dec 27, 2016 at 10:08 PM, Jeff King <peff@peff.net> wrote:
>> 
>>       https://github.com/Autodesk/enterprise-config-for-git
>> 
>>     (with the disclaimer that I've never used it myself, so I have no
>>     idea how good it is).
>> 
>> I think you probably know all that, Jake, but I am laying it out for the
>> benefit of the OP and the list. :)
>> 
> 
> Heh, well I didn't know about this last part so it's still useful to
> me! I knew of the larger description for why not but wasn't exactly
> clear how to word it so I am glad you filled that in. Thanks!

Author of the "enterprise-config-for-git" here. The version in the public Git repo is a stripped down version of the one we use at my day job for our engineering workforce. I tried to extract the "generally useful bits". Unfortunately it cannot be used "as-is". Don't hesitate to ping me if you want to learn more about it and/or if you have an idea how to make it better suited for open source collaboration.

Cheers, Lars

← back to recent threads