# Stamp Git commit id into file during build process

10 messages from 2010-01-16 to 2010-01-16. Participants: Paul Richards, Erik Faye-Lund, Sverre Rabbelier, Jakub Narebski, Shawn O. Pearce.
Thread: https://gitlist.dev/t/22249

## Paul Richards, 2010-01-16 18:50

Subject: Stamp Git commit id into file during build process
Message-ID: <a1138db31001161050i73eade1bif968ca1256dcef2c@mail.gmail.com>
URL: https://gitlist.dev/e/a1138db31001161050i73eade1bif968ca1256dcef2c%40mail.gmail.com

```
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, 2010-01-16 19:14

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <40aa078e1001161114w5a65bebbhaf43317634899925@mail.gmail.com>
URL: https://gitlist.dev/e/40aa078e1001161114w5a65bebbhaf43317634899925%40mail.gmail.com
In-Reply-To: <a1138db31001161050i73eade1bif968ca1256dcef2c@mail.gmail.com>

```
On Sat, Jan 16, 2010 at 7:50 PM, Paul Richards <paul.richards@gmail.com> wrote:
> 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, 2010-01-16 19:15

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <40aa078e1001161115r312f2e7ahbe9bab6c28b3ad2e@mail.gmail.com>
URL: https://gitlist.dev/e/40aa078e1001161115r312f2e7ahbe9bab6c28b3ad2e%40mail.gmail.com
In-Reply-To: <40aa078e1001161114w5a65bebbhaf43317634899925@mail.gmail.com>

```
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, 2010-01-16 19:17

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <fabb9a1e1001161117t6d572024yc5598be1b32ffcde@mail.gmail.com>
URL: https://gitlist.dev/e/fabb9a1e1001161117t6d572024yc5598be1b32ffcde%40mail.gmail.com
In-Reply-To: <40aa078e1001161114w5a65bebbhaf43317634899925@mail.gmail.com>

```
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, 2010-01-16 19:22

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <40aa078e1001161122k48f55807y3f01fa0d38e5cc93@mail.gmail.com>
URL: https://gitlist.dev/e/40aa078e1001161122k48f55807y3f01fa0d38e5cc93%40mail.gmail.com
In-Reply-To: <fabb9a1e1001161117t6d572024yc5598be1b32ffcde@mail.gmail.com>

```
On Sat, Jan 16, 2010 at 8:17 PM, Sverre Rabbelier <srabbelier@gmail.com> wrote:
> 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, 2010-01-16 19:29

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <fabb9a1e1001161129j12c79a2dq21557938d4cf973c@mail.gmail.com>
URL: https://gitlist.dev/e/fabb9a1e1001161129j12c79a2dq21557938d4cf973c%40mail.gmail.com
In-Reply-To: <40aa078e1001161122k48f55807y3f01fa0d38e5cc93@mail.gmail.com>

```
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, 2010-01-16 19:35

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <m3d419desd.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3d419desd.fsf%40localhost.localdomain
In-Reply-To: <a1138db31001161050i73eade1bif968ca1256dcef2c@mail.gmail.com>

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

-- 
Jakub Narebski
Poland
ShadeHawk on #git

```

## Paul Richards, 2010-01-16 21:12

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <a1138db31001161312i2c032c38tcc0fb162c61fbb35@mail.gmail.com>
URL: https://gitlist.dev/e/a1138db31001161312i2c032c38tcc0fb162c61fbb35%40mail.gmail.com
In-Reply-To: <m3d419desd.fsf@localhost.localdomain>

```
2010/1/16 Jakub Narebski <jnareb@gmail.com>:
> 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, 2010-01-16 21:26

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <20100116212619.GB5299@spearce.org>
URL: https://gitlist.dev/e/20100116212619.GB5299%40spearce.org
In-Reply-To: <a1138db31001161312i2c032c38tcc0fb162c61fbb35@mail.gmail.com>

```
Paul Richards <paul.richards@gmail.com> wrote:
> > 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, 2010-01-16 21:27

Subject: Re: Stamp Git commit id into file during build process
Message-ID: <201001162227.23236.jnareb@gmail.com>
URL: https://gitlist.dev/e/201001162227.23236.jnareb%40gmail.com
In-Reply-To: <a1138db31001161312i2c032c38tcc0fb162c61fbb35@mail.gmail.com>

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

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

```
