tags:

views:

542

answers:

5

We have a git repository which contains source for a few related Java WARs and JARs. It would be nice if the Java code could somehow:

System.err.println("I was built from git commit " + commitID);

(Obviously real code might be putting this into an HTTP header, logging it on startup, or whatever, that's not important right now)

We are using Ant to build (at least for production builds, it seems some programmers do their testing from inside Eclipse which I know even less about) binaries.

Is there a canonical way to get the commit id for the current git checkout into our Java at build time? If not, can people using Ant to build suggest how they'd do it and we'll see if a canonical solution emerges? I'm sure I can invent something myself entirely from whole cloth, but this seems like a re-usable building block, so I'd rather not.

+2  A: 

git rev-parse HEAD will print what you probably want (e.g. id of HEAD commit). You can make ant generate a simple Java class with this id as a static constant.

Piotr Findeisen
+1  A: 

I don't know if there are any Ant task for git (I googled a bit without success), anyway Ant can update a properties file with Piotr's option (git rev-parse HEAD) and then in runtime use that properties to get the revision number. This is cleaner and IDE friendly than having Ant generating a .java file.

victor hugo
I'm going to accept this as the answer for the question. I was really hoping to get more Ant-specific material, the Git side of the equation which got most feedback is the one I already had pretty good ideas for. But the suggestion to use a properties file seems like a good one.
tialaramex
+1  A: 

First, you can use ident gitattribute with $Id$ keyword (although it is not probably what you want; it is hash of file contents, and has nothing to do with current project version).

Second, you can do it the way Linux kernel and Git itself do it: in Makefile (in your case: in Ant file) there is rule which replaces some placeholder, usually '@@VERSION@@' (but in case of Perl it is '++VERSION++') by result of GIT-VERSION-GEN, which in turn uses "git describe". But for that to be useful you have to tag your releases (using annotated / signed tags).

Jakub Narębski
$Id$ attribute is the blob id, so I don't think that's what we want (a change elsewhere won't update the blob)
tialaramex
+3  A: 

You can get the last commit SHA with

git rev-parse HEAD

but it's generally a lot more useful to use

git describe

which will give you something that looks like this:

v0.7.0-185-g83e38c7

This works if you have tags - it will tell you how many commits from the last valid tag your current checkout is at plus a partial SHA for that commit, so you can use it to base a checkout off of later. You can use this identifier just like a SHA in most circumstances, but it's much more human readable.

Scott Chacon
A: 

Has anyone tried using a custom filter driver for this?

The git documentation seems a bit sparse on this and google isn't turning up any good examples.

Brandon Fosdick