threads / discuss / 13978

current repository hash

Subject: current repository hash

## tl;dr

9 messages between Jun 16, 2008 and Jun 17, 2008.

replies: 8people: 6as markdown or json

Alf Clement· Jun 16, 2008, 11:51 UTC · lore
Hi all,

I want to compile the current repository hash id into my program, to be able to checkout later this id an see the exact files for the compile. It should flag if there are currently modified files in the directory tree.

What would be the best command to get a unique identifier? Do I need the 40 digit hash id? How can I make sure that a shorter hash id will be unique?

Thanks, Alf

David Tweed· Jun 16, 2008, 12:11 UTC · re: Alf Clement · lore

Re: current repository hash

On Mon, Jun 16, 2008 at 12:51 PM, Alf Clement <alf.clement@gmail.com> wrote:
> What would be the best command to get a unique identifier?

Note that git itself does this via the GIT-VERSION-GEN script so you could look at that for ideas.

> How can I make sure that a shorter hash id will be unique?

Theoretically isn't that going to be tricky, as you don't just want something that's unique at the time you compile the program but which won't potentially be rendered non-unique by later commits (after you've released your binary)? If you just want to be reasonably sure you can truncate the hash to the length you'd like, eg "addcc13a" for 8 characters and run

git rev-parse addcc13a
and see if it says

addcc13a fatal: ambiguous argument 'addcc13a': unknown revision or path not in the working tree. Use '--' to separate paths from revisions

-- 
cheers, dave tweed__________________________
david.tweed@gmail.com
Rm 124, School of Systems Engineering, University of Reading.
"while having code so boring anyone can maintain it, use Python." --
attempted insult seen on slashdot
Santi Béjar· Jun 16, 2008, 12:15 UTC · re: Alf Clement · lore

Re: current repository hash

On Mon, Jun 16, 2008 at 13:51, Alf Clement <alf.clement@gmail.com> wrote:
Show 7 quoted lines
> Hi all,
>
> I want to compile the current repository hash id into my program, to
> be able to checkout later this
> id an see the exact files for the compile. It should flag if there are
> currently modified files in the
> directory tree.
You can look at how git does it:
http://git.kernel.org/?p=git/git.git;a=blob;f=GIT-VERSION-GEN;hb=HEAD
>
> What would be the best command to get a unique identifier?

"git rev-parse HEAD" for a unique identifier, but "git describe" is more convenient/useful.

> Do I need the 40 digit hash id?
Normally 7 or 8 are sufficient.
> How can I make sure that a shorter hash id will be unique?

You can't. Even the 40 digit hash is "only" cryptographically unique. But within a project 7 or 8 digits are unique enough for practical use.

Santi
Alf Clement· Jun 16, 2008, 18:30 UTC · re: Santi Béjar · lore

Re: current repository hash

Hi all,
thanks for the tips.

When I run describe I get an error: $ git describe --debug HEAD fatal: cannot describe '792815de6e3c2403f1e2ed5f2264ca88a0ae7000'

Any hints why? I have a tag v1.3 attached to HEAD and all commited.

CU, Alf

On Mon, Jun 16, 2008 at 2:15 PM, Santi Béjar <sbejar@gmail.com> wrote:
Show 31 quoted lines
> On Mon, Jun 16, 2008 at 13:51, Alf Clement <alf.clement@gmail.com> wrote:
>> Hi all,
>>
>> I want to compile the current repository hash id into my program, to
>> be able to checkout later this
>> id an see the exact files for the compile. It should flag if there are
>> currently modified files in the
>> directory tree.
>
> You can look at how git does it:
>
> http://git.kernel.org/?p=git/git.git;a=blob;f=GIT-VERSION-GEN;hb=HEAD
>
>>
>> What would be the best command to get a unique identifier?
>
> "git rev-parse HEAD" for a unique identifier,
> but "git describe" is more convenient/useful.
>
>> Do I need the 40 digit hash id?
>
> Normally 7 or 8 are sufficient.
>
>> How can I make sure that a shorter hash id will be unique?
>
> You can't. Even the 40 digit hash is "only" cryptographically unique.
> But within a project 7 or 8 digits are unique enough for practical
> use.
>
> Santi
>
Jeff King· Jun 16, 2008, 18:45 UTC · re: Alf Clement · lore

Re: current repository hash

On Mon, Jun 16, 2008 at 08:30:16PM +0200, Alf Clement wrote:
Show 5 quoted lines
> When I run describe I get an error:
> $ git describe --debug HEAD
> fatal: cannot describe '792815de6e3c2403f1e2ed5f2264ca88a0ae7000'
> 
> Any hints why? I have a tag v1.3 attached to HEAD and all commited.

By default, git-describe uses only annotated tags. Try "git describe --tags HEAD".

-Peff
Alf Clement· Jun 17, 2008, 06:48 UTC · re: Jeff King · lore

Re: current repository hash

Hi all,
when I run
   $ git describe --tags HEAD
on my branch "fire1", I get:
    v1.5-13-g27f64b3

I wonder why it's -13. The last action I did was to merge the master branch, which has the v1.5 tag. So it should be much less than 13? A snapshot from gitk is attached.

CU, Alf

Johan Herland· Jun 17, 2008, 07:07 UTC · re: Alf Clement · lore

Re: current repository hash

On Tuesday 17 June 2008, Alf Clement wrote:
Show 13 quoted lines
> Hi all,
>
> when I run
>    $ git describe --tags HEAD
> on my branch "fire1", I get:
>     v1.5-13-g27f64b3
>
> I wonder why it's -13. The last action I did was to merge the master
> branch, which has the v1.5 tag. So it should be much less than 13?
> A snapshot from gitk is attached.
>
> CU,
> Alf
AFAICS from your graph there are exactly 13 commits that are part of fire1, 
but not part of v.1.5 (from the bottom of the graph):
- "fire 1       modified: main.c"
- The 8 commits on a parallel branch leading towards origin/fire1
- The origin/fire1 merge
- The 4th last commit: "Merge branch 'master' into fire1"
- The 3rd last commit: "modified: main.c"
- The very last commit: fire1
...Johan
-- 
Johan Herland, <johan@herland.net>
www.herland.net
Jeff King· Jun 16, 2008, 15:04 UTC · re: Alf Clement · lore

Re: current repository hash

On Mon, Jun 16, 2008 at 01:51:52PM +0200, Alf Clement wrote:
> What would be the best command to get a unique identifier?

If you just want the commit sha1, then "git rev-list -1 HEAD" will give it to you. But take a look at git-describe, which is designed to give a nice human-readable name based on your tags.

> Do I need the 40 digit hash id?

No, but you increase your chances of a collision in the future. In practice, 8 or 9 characters tends to give unique commits.

> How can I make sure that a shorter hash id will be unique?

git-describe will find the shortest unique hash. But bear in mind that it may not be unique forever.

-Peff
Johannes Schindelin· Jun 17, 2008, 09:51 UTC · re: Alf Clement · lore

Re: current repository hash

Hi,
On Mon, 16 Jun 2008, Alf Clement wrote:
> I want to compile the current repository hash id into my program, to be 
> able to checkout later this id an see the exact files for the compile.

If this program is meant for source-distribution, you might be interested in the "export-subst" attribute. See Documentation/gitattributes.txt for details.

Ciao, Dscho

← back to recent threads