Re: How to embed a hash, tag or branch name?
- From
- Dirk Süsserott <newsletter@dirk.my1.cc>
- Date
- Aug 1, 2009, 12:38 UTC
- Message-ID
- <4A743735.9020806@dirk.my1.cc>
- In-Reply-To
- <20090731095238.GG29909@nalle>
Am 31.07.2009 11:52 schrieb Mikko Rapeli:
Show 10 quoted lines
> On Fri, Jul 31, 2009 at 02:27:50AM -0700, Jakub Narebski wrote: >> You can embed SHA-1 of a _file contents_ (blob) using '$Id: $' keyword >> with `ident` attribute - see gitattributes manpage. > > Great, thanks! > >> The correct solution of embedding version number is to do it at >> _build time_, using e.g. script similar to GIT-VERSION-GEN used by >> Git itself and by Linux kernel. It helps if you tag your releases. >
Hi Mikko,
I don't know whether you want to use the "ident" command on your final binary. With Git, that's pointless, as all source files will have the same $Id$. So it's perfectly sufficient to have only ONE file enriched with Git's SHA1.
Suggestion: During build time, compile (and link in) a file (e.g. version.c) with "-DVERSION=\"$(git describe)\"". My version.c looks like this:
----------------------------------
char const * version()
{
return VERSION;
}
----------------------------------The function then returns sth. like "foo12004-26-gc6c9273", which means that you're 26 commits ahead of the annotated tag "foo12004" and your commit has SHA1 "c6c9273". Then make your application somehow return that version string when asked (or deliver a version.txt together with it). This way you know that your binary was built using commit "c6c9273". You can further refine the VERSION by adding a "-dirty" if the working tree was dirty at build time. Then you'll know that your binary was based on c6c9273 "with some changes".
Make sure that version.c is the ONLY file that compiles with that -DVERSION switch. Otherwise all files get re-compiled when $(git describe) changes. That's not what you want.
HTH, Dirk