threads / discuss / 22249

Stamp Git commit id into file during build process

Subject: Stamp Git commit id into file during build process

## tl;dr

10 messages between Jan 16, 2010 and Jan 16, 2010.

replies: 9people: 5as markdown or json

Paul Richards· Jan 16, 2010, 18:50 UTC · lore

Hi, I am in the process of migrating from Subversion to Git. One thing I am unsure of is how to stamp the 'version' or 'commit id' into a file as part of a build process.

With subversion I used the SubWCRev tool from TortoiseSVN (http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html).

With Git I imagine that I'd like to put a copy of the current commit id (either the full hash or a truncated version of that) into a file which then gets included into the program source code in some way.

Is there a recommended way of doing this with git? Perhaps with something similar to SubWCRev?

Currently I am thinking about using "git log", and grepping the output in some way so that I just get the hash.

Thanks in advance,
-- 
Paul Richards
Erik Faye-Lund· Jan 16, 2010, 19:14 UTC · re: Paul Richards · lore

Re: Stamp Git commit id into file during build process

On Sat, Jan 16, 2010 at 7:50 PM, Paul Richards <paul.richards@gmail.com> wrote:
Show 18 quoted lines
> Hi,
> I am in the process of migrating from Subversion to Git.  One thing I
> am unsure of is how to stamp the 'version' or 'commit id' into a file
> as part of a build process.
>
> With subversion I used the SubWCRev tool from TortoiseSVN
> (http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html).
>
> With Git I imagine that I'd like to put a copy of the current commit
> id (either the full hash or a truncated version of that) into a file
> which then gets included into the program source code in some way.
>
> Is there a recommended way of doing this with git?  Perhaps with
> something similar to SubWCRev?
>
> Currently I am thinking about using "git log", and grepping the output
> in some way so that I just get the hash.
>

I think you are looking for "git rev-parse HEAD". This outputs the hash of HEAD as a single line on stdout. Or even better, you can use the "git describe"-tool, which gives a nice and short description of the commit relative to the most recent commit.

-- 
Erik "kusma" Faye-Lund
Erik Faye-Lund· Jan 16, 2010, 19:15 UTC · re: Erik Faye-Lund · lore

Re: Stamp Git commit id into file during build process

On Sat, Jan 16, 2010 at 8:14 PM, Erik Faye-Lund <kusmabite@googlemail.com> wrote:

> the "git describe"-tool, which gives a nice and short description of
> the commit relative to the most recent commit.
>
...by "most recent commit", I mean "most recent tag". Sorry about that.
-- 
Erik "kusma" Faye-Lund
Sverre Rabbelier· Jan 16, 2010, 19:17 UTC · re: Erik Faye-Lund · lore

Re: Stamp Git commit id into file during build process

Heya,
On Sat, Jan 16, 2010 at 20:14, Erik Faye-Lund <kusmabite@googlemail.com> wrote:
> I think you are looking for "git rev-parse HEAD". This outputs the
> hash of HEAD as a single line on stdout. Or even better, you can use
> the "git describe"-tool, which gives a nice and short description of
> the commit relative to the most recent commit.
It would be better to have a look at gitattributes [0] instead.
[0] http://www.kernel.org/pub/software/scm/git/docs/gitattributes.html#_tt_ident_tt
-- 
Cheers,

Sverre Rabbelier
Erik Faye-Lund· Jan 16, 2010, 19:22 UTC · re: Sverre Rabbelier · lore

Re: Stamp Git commit id into file during build process

On Sat, Jan 16, 2010 at 8:17 PM, Sverre Rabbelier <srabbelier@gmail.com> wrote:
Show 10 quoted lines
> Heya,
>
> On Sat, Jan 16, 2010 at 20:14, Erik Faye-Lund <kusmabite@googlemail.com> wrote:
>> I think you are looking for "git rev-parse HEAD". This outputs the
>> hash of HEAD as a single line on stdout. Or even better, you can use
>> the "git describe"-tool, which gives a nice and short description of
>> the commit relative to the most recent commit.
>
> It would be better to have a look at gitattributes [0] instead.
>

Since he's looking for a replacement for SubWCRev (as opposed to svn:keywords); not really.

-- 
Erik "kusma" Faye-Lund
Sverre Rabbelier· Jan 16, 2010, 19:29 UTC · re: Erik Faye-Lund · lore

Re: Stamp Git commit id into file during build process

Heya,
On Sat, Jan 16, 2010 at 20:22, Erik Faye-Lund <kusmabite@googlemail.com> wrote:
> Since he's looking for a replacement for SubWCRev (as opposed to
> svn:keywords); not really.

I misunderstood then, since he mentioned 'git log' and you mentioned 'git rev-parse HEAD', and I do not know what SubWCRev does.

-- 
Cheers,

Sverre Rabbelier
Jakub Narebski· Jan 16, 2010, 19:35 UTC · re: Paul Richards · lore

Re: Stamp Git commit id into file during build process

Paul Richards <paul.richards@gmail.com> writes:
Show 17 quoted lines
> Hi,
> I am in the process of migrating from Subversion to Git.  One thing I
> am unsure of is how to stamp the 'version' or 'commit id' into a file
> as part of a build process.
> 
> With subversion I used the SubWCRev tool from TortoiseSVN
> (http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html).
> 
> With Git I imagine that I'd like to put a copy of the current commit
> id (either the full hash or a truncated version of that) into a file
> which then gets included into the program source code in some way.
> 
> Is there a recommended way of doing this with git?  Perhaps with
> something similar to SubWCRev?
> 
> Currently I am thinking about using "git log", and grepping the output
> in some way so that I just get the hash.
Not "git log".

Take a look how for example git project and Linux kernel use "git describe" in GIT-VERSION-GEN script, and how they use GIT-VERSION-GEN script in the Makefile.

-- 
Jakub Narebski
Poland
ShadeHawk on #git
Paul Richards· Jan 16, 2010, 21:12 UTC · re: Jakub Narebski · lore

Re: Stamp Git commit id into file during build process

2010/1/16 Jakub Narebski <jnareb@gmail.com>:
Show 26 quoted lines
> Paul Richards <paul.richards@gmail.com> writes:
>
>> Hi,
>> I am in the process of migrating from Subversion to Git.  One thing I
>> am unsure of is how to stamp the 'version' or 'commit id' into a file
>> as part of a build process.
>>
>> With subversion I used the SubWCRev tool from TortoiseSVN
>> (http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html).
>>
>> With Git I imagine that I'd like to put a copy of the current commit
>> id (either the full hash or a truncated version of that) into a file
>> which then gets included into the program source code in some way.
>>
>> Is there a recommended way of doing this with git?  Perhaps with
>> something similar to SubWCRev?
>>
>> Currently I am thinking about using "git log", and grepping the output
>> in some way so that I just get the hash.
>
> Not "git log".
>
> Take a look how for example git project and Linux kernel use "git describe"
> in GIT-VERSION-GEN script, and how they use GIT-VERSION-GEN script in the
> Makefile.
>
Thanks, it appears though that "git describe" does not work in Cygwin git. :(
-- 
Paul Richards
Shawn O. Pearce· Jan 16, 2010, 21:26 UTC · re: Paul Richards · lore

Re: Stamp Git commit id into file during build process

Paul Richards <paul.richards@gmail.com> wrote:
Show 5 quoted lines
> > Take a look how for example git project and Linux kernel use "git describe"
> > in GIT-VERSION-GEN script, and how they use GIT-VERSION-GEN script in the
> > Makefile.
> 
> Thanks, it appears though that "git describe" does not work in Cygwin git. :(

That's a serious bug. git describe should work fine on any system Git itself runs on; its written in pure C and with the exception of the --contains flag, doesn't rely on any external programs.

-- 
Shawn.
Jakub Narebski· Jan 16, 2010, 21:27 UTC · re: Paul Richards · lore

Re: Stamp Git commit id into file during build process

Dnia sobota 16. stycznia 2010 22:12, Paul Richards napisał:
> 2010/1/16 Jakub Narebski <jnareb@gmail.com>:
>> Paul Richards <paul.richards@gmail.com> writes:
>>> I am in the process of migrating from Subversion to Git.  One thing I
>>> am unsure of is how to stamp the 'version' or 'commit id' into a file
>>> as part of a build process.
Show 5 quoted lines
>> Take a look how for example git project and Linux kernel use "git describe"
>> in GIT-VERSION-GEN script, and how they use GIT-VERSION-GEN script in the
>> Makefile. 
> 
> Thanks, it appears though that "git describe" does not work in Cygwin git. :(
If you got something like
  $ git describe
  fatal: cannot describe 'a27cb622b30f18cb8510b7b3856d4029e617d95b'

it means that you don't have any tags in your history. You should tag your releases (released versions) using annotated tags.

Anyway you have
  $ git describe --always
  a27cb62
-- 
Jakub Narebski
Poland

← back to recent threads