1

I'd like to embed the Git revision (SHA1) in my executables at build time. I can do it locally like this:

 git log -1 --format='%h' >version
 objcopy --input binary --output elf64-x86-64 version version.o
 # link version.o into the executable

The problem is, I'm using NetBeans (on Windows) to build remotely (on Linux). It's a bit of a paradox: the git command seems to need to run on the local machine (where the source is), but the objcopy command needs to run on the remote server (where the build happens). This is a problem because while NetBeans does let me edit the project's Makefile to insert a pre-build step, I can't figure out how or where to put the git command so that it runs on the local machine before the remote build steps.

I tried making a separate NetBeans project to run the git command on localhost, thinking that I'd then make the "real" project depend on that, but NetBeans doesn't seem to allow me to create a project that "does nothing" (i.e. it has no source files, and does not produce a library nor an executable--it would only generate the version text file).

I'm using NetBeans 7.3 for C++ with the "Automatic Copying" option for remote builds. A possible solution might be to use the NFS/Samba mode with the Git repository stored on the server, but that would be fairly disruptive because the project has multiple developers using Automatic Copying for a long time already. So it would be great to have a solution to generate some files locally in NetBeans before starting the remote build.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436

1 Answers1

1

One solution would be to have that version file already up-to-date when you launch your Netbeans job.

A post-commit hook could generate that file at the right place, for your Netbean job to read.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for this idea. The biggest downside I see now is that Git hooks are not published to the remote (server), so each user will need to run a script to set up the hooks after cloning. Still, I'll give it a try. – John Zwinck Aug 20 '13 at 00:30
  • @JohnZwinck no: I meant setup that post-commit hook on the repo to which you push to, and which is read by your NetBeans job. – VonC Aug 20 '13 at 05:24
  • Now I'm confused. It needs to update a file on the client side, and if the client checks out an old revision, the script needs to run again. So it cannot be a hook on the git "server" side only. Anyway, I have implemented the idea by using hooks on the client side which each user must install after cloning. If you know a way around that I'd love to hear it. Thanks either way! – John Zwinck Aug 20 '13 at 06:14
  • So it mostly works, but I ran into a glitch, so I posted a follow-up question here: http://stackoverflow.com/questions/18393567/git-hook-for-any-action-that-updates-the-working-directory – John Zwinck Aug 23 '13 at 02:11