{"thread":{"id":"20311","subject":"How to embed a hash, tag or branch name?","startedAt":"2009-07-31T08:17:23Z","lastAt":"2009-08-01T16:04:58Z","messageCount":6,"participants":["Mikko Rapeli","Jakub Narebski","Dirk Süsserott"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"119219","messageId":"20090731081723.GE29909@nalle","threadId":"20311","inReplyTo":null,"subject":"How to embed a hash, tag or branch name?","fromName":"Mikko Rapeli","fromEmail":"mikko.rapeli@iki.fi","sentAt":"2009-07-31T08:17:23Z","receivedAt":"2009-07-31T08:17:23Z","isPatch":false,"sender":{"key":"mikko.rapeli@iki.fi","avatar":"https://avatars.githubusercontent.com/u/2036278?v=4"},"body":"How do I embed a hash, tag or branch name into source code managed by\ngit?\n\nI've tried searching the net, git manuals etc but haven't found a\nreplacement for the old '$Id: $' strings from svn. What I need is a way to\nmap compiled binaries and flash images to git tree heads, tags and\nbranches.\n\nThanks,\n\n-Mikko\n"},{"id":"119227","messageId":"m3ljm5ryar.fsf@localhost.localdomain","threadId":"20311","inReplyTo":"20090731081723.GE29909@nalle","subject":"Re: How to embed a hash, tag or branch name?","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2009-07-31T09:27:50Z","receivedAt":"2009-07-31T09:27:50Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Mikko Rapeli <mikko.rapeli@iki.fi> writes:\n\n> How do I embed a hash, tag or branch name into source code managed by\n> git?\n> \n> I've tried searching the net, git manuals etc but haven't found a\n> replacement for the old '$Id: $' strings from svn. What I need is a way to\n> map compiled binaries and flash images to git tree heads, tags and\n> branches.\n\nYou can embed SHA-1 of a _file contents_ (blob) using '$Id: $' keyword\nwith `ident` attribute - see gitattributes manpage.\n\nThe correct solution of embedding version number is to do it at \n_build time_, using e.g. script similar to GIT-VERSION-GEN used by\nGit itself and by Linux kernel.  It helps if you tag your releases.\n\n\nIf you really, really, really need it, you can embed tag or branch\nname into source code using `filter` attribute and custom clean/smudge\nfilters to do keyword un-expansion and keyword expansion.  But please\nthink twice about what you want to achive with keyword expansion, and\nwhether keyword expansion is a best solution...\n\n-- \nJakub Narebski\nPoland\nShadeHawk on #git\n"},{"id":"119229","messageId":"20090731095238.GG29909@nalle","threadId":"20311","inReplyTo":"m3ljm5ryar.fsf@localhost.localdomain","subject":"Re: How to embed a hash, tag or branch name?","fromName":"Mikko Rapeli","fromEmail":"mikko.rapeli@iki.fi","sentAt":"2009-07-31T09:52:38Z","receivedAt":"2009-07-31T09:52:38Z","isPatch":false,"sender":{"key":"mikko.rapeli@iki.fi","avatar":"https://avatars.githubusercontent.com/u/2036278?v=4"},"body":"On Fri, Jul 31, 2009 at 02:27:50AM -0700, Jakub Narebski wrote:\n> You can embed SHA-1 of a _file contents_ (blob) using '$Id: $' keyword\n> with `ident` attribute - see gitattributes manpage.\n\nGreat, thanks!\n\n> The correct solution of embedding version number is to do it at \n> _build time_, using e.g. script similar to GIT-VERSION-GEN used by\n> Git itself and by Linux kernel.  It helps if you tag your releases.\n\nThis would help, but I'm on cygwin/msysgit and compiling with a\nmicrocontroller IDE MPLAB and PICC-18 compiler, so I really don't\nwant to dig deeper into build time integration at the moment.\n\nBtw, I don't know how didn't notice that this is a FAQ:\nhttp://git.or.cz/gitwiki/GitFaq#Doesgithavekeywordexpansion.3F\n\n-Mikko\n"},{"id":"119328","messageId":"4A743735.9020806@dirk.my1.cc","threadId":"20311","inReplyTo":"20090731095238.GG29909@nalle","subject":"Re: How to embed a hash, tag or branch name?","fromName":"Dirk Süsserott","fromEmail":"newsletter@dirk.my1.cc","sentAt":"2009-08-01T12:38:13Z","receivedAt":"2009-08-01T12:38:13Z","isPatch":false,"sender":{"key":"newsletter@dirk.my1.cc","avatar":null},"body":"Am 31.07.2009 11:52 schrieb Mikko Rapeli:\n> On Fri, Jul 31, 2009 at 02:27:50AM -0700, Jakub Narebski wrote:\n>> You can embed SHA-1 of a _file contents_ (blob) using '$Id: $' keyword\n>> with `ident` attribute - see gitattributes manpage.\n> \n> Great, thanks!\n> \n>> The correct solution of embedding version number is to do it at \n>> _build time_, using e.g. script similar to GIT-VERSION-GEN used by\n>> Git itself and by Linux kernel.  It helps if you tag your releases.\n> \n\nHi Mikko,\n\nI don't know whether you want to use the \"ident\" command on your final\nbinary. With Git, that's pointless, as all source files will have the\nsame $Id$. So it's perfectly sufficient to have only ONE file enriched\nwith Git's SHA1.\n\nSuggestion: During build time, compile (and link in) a file (e.g.\nversion.c) with \"-DVERSION=\\\"$(git describe)\\\"\". My version.c looks like\nthis:\n\n----------------------------------\nchar const * version()\n{\n     return VERSION;\n}\n----------------------------------\n\nThe function then returns sth. like \"foo12004-26-gc6c9273\", which means\nthat you're 26 commits ahead of the annotated tag \"foo12004\" and your\ncommit has SHA1 \"c6c9273\". Then make your application somehow return\nthat version string when asked (or deliver a version.txt together with\nit). This way you know that your binary was built using commit\n\"c6c9273\". You can further refine the VERSION by adding a \"-dirty\" if\nthe working tree was dirty at build time. Then you'll know that your\nbinary was based on c6c9273 \"with some changes\".\n\nMake sure that version.c is the ONLY file that compiles with that\n-DVERSION switch. Otherwise all files get re-compiled when $(git\ndescribe) changes. That's not what you want.\n\n     HTH, Dirk\n"},{"id":"119331","messageId":"200908011513.13016.jnareb@gmail.com","threadId":"20311","inReplyTo":"4A743735.9020806@dirk.my1.cc","subject":"Re: How to embed a hash, tag or branch name?","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2009-08-01T13:13:07Z","receivedAt":"2009-08-01T13:13:07Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"On Sat, 1 August 2009, Dirk Süsserott wrote:\n> Am 31.07.2009 11:52 schrieb Mikko Rapeli:\n>> On Fri, Jul 31, 2009 at 02:27:50AM -0700, Jakub Narebski wrote:\n\n>>> You can embed SHA-1 of a _file contents_ (blob) using '$Id: $' keyword\n>>> with `ident` attribute - see gitattributes manpage.\n>> \n>> Great, thanks!\n>> \n>>> The correct solution of embedding version number is to do it at \n>>> _build time_, using e.g. script similar to GIT-VERSION-GEN used by\n>>> Git itself and by Linux kernel.  It helps if you tag your releases.\n>> \n> \n> Hi Mikko,\n> \n> I don't know whether you want to use the \"ident\" command on your final\n> binary. With Git, that's pointless, as all source files will have the\n> same $Id$. So it's perfectly sufficient to have only ONE file enriched\n> with Git's SHA1.\n\nYou are wrong.  In Git $Id$ / $Id: $ expands to SHA-1 id of _blob_ \n(i.e. of file *contents*), not SHA-1 id of a commit.  This way when\nswitching branches or rewinding branch using git-reset we don't have\nto pay huge performance penalty because of `ident`, as we would have\nbecause every file would have to be changed if $Id$ was about commit\nid (or if there was $Revision$ or $Author$ implemented).\n\n> Suggestion: During build time, compile (and link in) a file (e.g.\n> version.c) with \"-DVERSION=\\\"$(git describe)\\\"\". My version.c looks like\n> this:\n\nGIT-VERSION-GEN and various tricks in Makefile do just that, not only\nfor compiled parts, but also for scripts.\n\n[...]\n-- \nJakub Narebski\nPoland\n"},{"id":"119343","messageId":"4A7467AA.7060400@dirk.my1.cc","threadId":"20311","inReplyTo":"200908011513.13016.jnareb@gmail.com","subject":"Re: How to embed a hash, tag or branch name?","fromName":"Dirk Süsserott","fromEmail":"newsletter@dirk.my1.cc","sentAt":"2009-08-01T16:04:58Z","receivedAt":"2009-08-01T16:04:58Z","isPatch":false,"sender":{"key":"newsletter@dirk.my1.cc","avatar":null},"body":"Am 01.08.2009 15:13 schrieb Jakub Narebski:\n> On Sat, 1 August 2009, Dirk Süsserott wrote:\n>> Am 31.07.2009 11:52 schrieb Mikko Rapeli:\n>>> On Fri, Jul 31, 2009 at 02:27:50AM -0700, Jakub Narebski wrote:\n> \n>>>> You can embed SHA-1 of a _file contents_ (blob) using '$Id: $' keyword\n>>>> with `ident` attribute - see gitattributes manpage.\n>>> Great, thanks!\n>>>\n>>>> The correct solution of embedding version number is to do it at \n>>>> _build time_, using e.g. script similar to GIT-VERSION-GEN used by\n>>>> Git itself and by Linux kernel.  It helps if you tag your releases.\n>> Hi Mikko,\n>>\n>> I don't know whether you want to use the \"ident\" command on your final\n>> binary. With Git, that's pointless, as all source files will have the\n>> same $Id$. So it's perfectly sufficient to have only ONE file enriched\n>> with Git's SHA1.\n> \n> You are wrong.  In Git $Id$ / $Id: $ expands to SHA-1 id of _blob_ \n> (i.e. of file *contents*), not SHA-1 id of a commit.  This way when\n> switching branches or rewinding branch using git-reset we don't have\n> to pay huge performance penalty because of `ident`, as we would have\n> because every file would have to be changed if $Id$ was about commit\n> id (or if there was $Revision$ or $Author$ implemented).\n\nOk, sorry about that. Actually I haven't read the concerning docs\n*that* well when I figured out that keyword expansion with Git\nis not exactly a good idea.\n\n> \n>> Suggestion: During build time, compile (and link in) a file (e.g.\n>> version.c) with \"-DVERSION=\\\"$(git describe)\\\"\". My version.c looks like\n>> this:\n> \n> GIT-VERSION-GEN and various tricks in Makefile do just that, not only\n> for compiled parts, but also for scripts.\n> \n\nRight, but my explanation shows the overall intention of that.\nSo, Mikko, have a look at GIT-VERSION-GEN and my suggestion and\nuse some kind of summary of them.\n"}]}