threads / discuss / 50399

hook to get info on commit, and include that in a file that is part of the commit

Subject: hook to get info on commit, and include that in a file that is part of the commit

## tl;dr

7 messages between Feb 5, 2019 and Feb 5, 2019.

replies: 6people: 2as markdown or json

Larry Martell· Feb 5, 2019, 01:36 UTC · lore

Is there any way using a git hook to get info on the commit (id, message), put that in a file, and include that file in the commit?

If I try that in a pre-commit hook I do not get the info on the current commit. I tried it in pre-receive but it does not seem to get executed at all.

If I do it in post-receive it's too late to check the file in with the commit.
Os there no way to do this?
brian m. carlson· Feb 5, 2019, 01:55 UTC · re: Larry Martell · lore

Re: hook to get info on commit, and include that in a file that is part of the commit

On Mon, Feb 04, 2019 at 08:36:10PM -0500, Larry Martell wrote:
Show 6 quoted lines
> Is there any way using a git hook to get info on the commit (id,
> message), put that in a file, and include that file in the commit?
> 
> If I try that in a pre-commit hook I do not get the info on the
> current commit. I tried it in pre-receive but it does not seem to get
> executed at all.
You cannot include a file in the commit with that commit's ID.

The object ID of a commit is based on the data it contains, which is in turn based on the data in its top-level tree, which is in turn based on the data in all of its trees and blobs. If you add or change a file in a commit (or the message of that commit), you necessarily result in that commit having a different object ID. This is by design and is valuable for checking the integrity of the history.

Furthermore, the hooks for a commit are designed for checking and notification, not for editing the commit, with the exception of the commit message hooks, which are designed only for editing the commit message, not the contents.

The pre-receive hook runs on the server side, so if you aren't running it there, then it won't get called at all.

-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
Larry Martell· Feb 5, 2019, 02:00 UTC · re: brian m. carlson · lore

Re: hook to get info on commit, and include that in a file that is part of the commit

On Mon, Feb 4, 2019 at 8:55 PM brian m. carlson <sandals@crustytoothpaste.net> wrote:

Show 25 quoted lines
>
> On Mon, Feb 04, 2019 at 08:36:10PM -0500, Larry Martell wrote:
> > Is there any way using a git hook to get info on the commit (id,
> > message), put that in a file, and include that file in the commit?
> >
> > If I try that in a pre-commit hook I do not get the info on the
> > current commit. I tried it in pre-receive but it does not seem to get
> > executed at all.
>
> You cannot include a file in the commit with that commit's ID.
>
> The object ID of a commit is based on the data it contains, which is in
> turn based on the data in its top-level tree, which is in turn based on
> the data in all of its trees and blobs. If you add or change a file in a
> commit (or the message of that commit), you necessarily result in that
> commit having a different object ID. This is by design and is valuable
> for checking the integrity of the history.
>
> Furthermore, the hooks for a commit are designed for checking and
> notification, not for editing the commit, with the exception of the
> commit message hooks, which are designed only for editing the commit
> message, not the contents.
>
> The pre-receive hook runs on the server side, so if you aren't running
> it there, then it won't get called at all.
Thanks for the reply. Any suggestions on how to achieve what I want to do?

The use case is that we want to have a file that is part of the install that has certain info (commit id, date of commit, commit message, etc.). and we'd like that to be generated automatically.

brian m. carlson· Feb 5, 2019, 02:15 UTC · re: Larry Martell · lore

Re: hook to get info on commit, and include that in a file that is part of the commit

On Mon, Feb 04, 2019 at 09:00:42PM -0500, Larry Martell wrote:
Show 5 quoted lines
> Thanks for the reply. Any suggestions on how to achieve what I want to do?
> 
> The use case is that we want to have a file that is part of the
> install that has certain info (commit id, date of commit, commit
> message, etc.). and we'd like that to be generated automatically.

If you want to generate a file, you can certainly do that in the post-commit hook or using a Makefile target. You just can't check it into the repo. Lots of projects do this as part of their build process.

An example of what you could do is "git log --pretty='tformat:%H%n%B' HEAD". That will print the commit hash and commit message to standard output for each commit. If you want just one commit, you can use "-1".

-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
Larry Martell· Feb 5, 2019, 02:18 UTC · re: brian m. carlson · lore

Re: hook to get info on commit, and include that in a file that is part of the commit

On Mon, Feb 4, 2019 at 9:15 PM brian m. carlson <sandals@crustytoothpaste.net> wrote:

Show 15 quoted lines
>
> On Mon, Feb 04, 2019 at 09:00:42PM -0500, Larry Martell wrote:
> > Thanks for the reply. Any suggestions on how to achieve what I want to do?
> >
> > The use case is that we want to have a file that is part of the
> > install that has certain info (commit id, date of commit, commit
> > message, etc.). and we'd like that to be generated automatically.
>
> If you want to generate a file, you can certainly do that in the
> post-commit hook or using a Makefile target. You just can't check it
> into the repo. Lots of projects do this as part of their build process.
>
> An example of what you could do is "git log --pretty='tformat:%H%n%B' HEAD".
> That will print the commit hash and commit message to standard output
> for each commit. If you want just one commit, you can use "-1".
Yeah, but I want it part of the repo so it makes it to the target system.
brian m. carlson· Feb 5, 2019, 02:40 UTC · re: Larry Martell · lore

Re: hook to get info on commit, and include that in a file that is part of the commit

On Mon, Feb 04, 2019 at 09:18:14PM -0500, Larry Martell wrote:
Show 11 quoted lines
> On Mon, Feb 4, 2019 at 9:15 PM brian m. carlson
> <sandals@crustytoothpaste.net> wrote:
> > If you want to generate a file, you can certainly do that in the
> > post-commit hook or using a Makefile target. You just can't check it
> > into the repo. Lots of projects do this as part of their build process.
> >
> > An example of what you could do is "git log --pretty='tformat:%H%n%B' HEAD".
> > That will print the commit hash and commit message to standard output
> > for each commit. If you want just one commit, you can use "-1".
> 
> Yeah, but I want it part of the repo so it makes it to the target system.

You can create an additional commit for each commit that you put in with the additional file, or you can serialize it as a note for the commit (using git notes), which can then be pushed to the remote server by pushing refs/notes/commits.

I'm not aware of any other way to do what you want.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
Larry Martell· Feb 5, 2019, 02:42 UTC · re: brian m. carlson · lore

Re: hook to get info on commit, and include that in a file that is part of the commit

On Mon, Feb 4, 2019 at 9:41 PM brian m. carlson <sandals@crustytoothpaste.net> wrote:

Show 20 quoted lines
>
> On Mon, Feb 04, 2019 at 09:18:14PM -0500, Larry Martell wrote:
> > On Mon, Feb 4, 2019 at 9:15 PM brian m. carlson
> > <sandals@crustytoothpaste.net> wrote:
> > > If you want to generate a file, you can certainly do that in the
> > > post-commit hook or using a Makefile target. You just can't check it
> > > into the repo. Lots of projects do this as part of their build process.
> > >
> > > An example of what you could do is "git log --pretty='tformat:%H%n%B' HEAD".
> > > That will print the commit hash and commit message to standard output
> > > for each commit. If you want just one commit, you can use "-1".
> >
> > Yeah, but I want it part of the repo so it makes it to the target system.
>
> You can create an additional commit for each commit that you put in with
> the additional file, or you can serialize it as a note for the commit
> (using git notes), which can then be pushed to the remote server by
> pushing refs/notes/commits.
>
> I'm not aware of any other way to do what you want.
Thanks. That is what I will end up doing.

← back to recent threads