Page 1 of 1
Embedding Git Revision Number into Code
Posted: Fri Jun 15, 2012 12:13 pm
by sigmabeta
I would like to embed a git version / commit number into the serial output of my netburner code, something like the output of "git describe". For example:
Where 0.1 is the version tag, 6 is how many commits since I created that tag, and g4762a4e is the unique commit id number from git.
Eclipse has git support built-in; most of my google searches for how people normally do this typically center around executing the git describe command in a build script, but I really don't know how to do this with Eclipse, and especially not how to get it to work with a Netburner. Any ideas?
Re: Embedding Git Revision Number into Code
Posted: Sat Jun 16, 2012 1:12 pm
by tod
I wrote up a
blog entry on doing this with SVN for eclipse (there's a separate one on doing it for Visual Studio). I would think this would at least get you jump started. You'll have to replace the SubWcRev with the equivalent for git. I've just started looking at git and Hg so I can't help much yet on specifics.
Re: Embedding Git Revision Number into Code
Posted: Mon Jun 18, 2012 12:09 pm
by sigmabeta
Is there some additional configuration that is necessary for commands entered into that pre-build step command field to work properly? Just as a test, I placed "git describe" in there, and Eclipse complained that git is not in my PATH (which it most certainly is, as I have been committing / checking out of my repo using the Windows command prompt.)
My current plan is to have something like:
Code: Select all
echo #define VERSION \ > version.h && echo git describe > version.h
in there, but that doesn't work either:
Code: Select all
Error: Cannot run program "echo": Launching failed
Re: Embedding Git Revision Number into Code
Posted: Mon Jun 18, 2012 1:11 pm
by tod
I'm no expert on this stuff, I just tend to hack around until I get it working but...
I don't think echo exists as an executable file. I think it's built into cmd.exe. So If I wanted to write out the contents of a file from inside eclipse's build steps I would use something like this
Code: Select all
cmd /c type ${workspace_loc:/PulseGenerator_Lib/version.h}
I'm invoking cmd.exe giving it the /c flag to interpret the remaining string as a command (note I'm using "type" not "echo"). The stuff inside {} is my path to the file I want to pass to the type command.
Now Git may have a similar issue, the DOS shell may be doing some translation for you. In fact my path environment shows it leads to a git.cmd not the git.exe. Try this:
Code: Select all
"C:\Program Files (x86)\Git\bin\git" describe
If you don't have spaces in your path name to the git.exe file you won't need the quotes, otherwise you will. When I do the above in a post build step it does execute git describe. My project isn't actually in git so I get "fatal: Not a git repository (or any of the parent directories): .git" but that is a git describe generated error so it indicates to me I'm running the proper command and getting the expected error.
Re: Embedding Git Revision Number into Code
Posted: Wed Jun 20, 2012 3:20 pm
by tod
I'm in the process of switching from SVN to Mercurial (Hg) so I decided to put more effort into solving this. Since Hg and Git are very similar this might help you. I couldnt' find an equivalent of SubWcRev for Hg. I have been reading up on PowerShell so I thought I could use it with Hg and Eclipse. I'm happy with the results. I wrote up the details in a
new blog post. I'm pretty confident you can easily adapt the script shown for git with minimal effort and no knowledge of PowerShell. If you run into any problems let me know.
Re: Embedding Git Revision Number into Code
Posted: Wed Jun 20, 2012 5:35 pm
by pbreed
Realize we have added a system function:
const char * GetReleaseTag();
that returns the system release tag, that unless you are patching your own system code should
give you the revision for the release tag of the system.
This uses a bit of code in the nburn\system\makefile to read the contents of the release tag file and load it into code.
We used to have a system revision code file, but it kept getting forgotten in the update process,
so every single build we do does a release tag, so now its always up to date.
Paul
Re: Embedding Git Revision Number into Code
Posted: Thu Jun 21, 2012 11:19 am
by sigmabeta
tod wrote:I'm in the process of switching from SVN to Mercurial (Hg) so I decided to put more effort into solving this. Since Hg and Git are very similar this might help you. I couldnt' find an equivalent of SubWcRev for Hg. I have been reading up on PowerShell so I thought I could use it with Hg and Eclipse. I'm happy with the results. I wrote up the details in a
new blog post. I'm pretty confident you can easily adapt the script shown for git with minimal effort and no knowledge of PowerShell. If you run into any problems let me know.
Fantastic! That was just what I needed. Here are the adaptations I made to make this work with git:
- $exe needs to be set to the git executable
- $rev-date needs to be set to &$exe describe
Aside from that, following your guide to the T should do the trick. However, in eclipse I had to specify the full absolute path to the script, because ${workspace_loc}, ${project_loc}, etc, were not being recognized by Eclipse and so the script was not running. Not a big deal, though.
Thanks so much!
Re: Embedding Git Revision Number into Code
Posted: Thu Jun 21, 2012 12:00 pm
by tod
sigma - you're welcome, glad it helped.
Paul, thanks for pointing out that method. I don't currently put the NNDK release tag info in the version info I report but I should, and now I will.