tags:

views:

140

answers:

5

How can I lookup the latest git commit hash from an ant build script?

I am currently working on a new open source project which I store on github. I would like to extend my existing ANT build file to allow me to create numbered builds. I am imagining that I would launch the build with something like "ant buildnum -Dnum=12".

I would like the resulting jar to have two crucial bits of information in it's manifest file:

  • build.number=12
  • build.gitcommit=

I know how to create the build.number line. However, I am unsure of the best ant plumbing to lookup the latest git commit hash which is the value I want to fill in for .

+2  A: 

Would that be what you are looking for?

git rev-parse HEAD
Olivier
+1  A: 

You should tag a version (starting with a 0.1 or similar) and then just use git describe.

This will give you readable unique identifiers as reference points from your tag. When you release, this version number will be whatever you specified it to be.

Dustin
A: 

I actually used both answers. The ant code I wrote was as follows.

  <target name="getgitdetails" >
    <exec executable="git" outputproperty="git.tagstring">
      <arg value="describe"/>
    </exec>
    <exec executable="git" outputproperty="git.revision">
      <arg value="rev-parse"/>
      <arg value="HEAD"/>
    </exec>
    <if>
      <contains string="${git.tagstring}" substring="cannot"/>
      <then>
        <property name="git.tag" value="none"/>
      </then>
      <else>
        <property name="git.tag" value="${git.tagstring}"/>
      </else>
    </if>
  </target>

mchr
A: 

This command returns always the working folder's last commit SHA1, useful when you don't always build from HEAD.

On Windows with msysgit:

<exec executable="cmd" outputproperty="git.revision">
    <arg value="/c" />
    <arg value="git.cmd log -1 --pretty=format:%H" />
</exec>

On unix:

<exec executable="git" outputproperty="git.revision">
    <arg value="log -1 --pretty=format:%H" />
</exec>
Gian Marco Gherardi
A: 

I wrote the following ant target for a project on github. Usage:

  • stores version in property "repository.version"
  • works if no git is installed or no .git directory is present (fallback)
  • other targets must depend on this target if they need the git version
  • only one git command gets executed (--always)

<available file=".git" type="dir" property="git.present"/>

<target name="git.revision" description="Store git revision in ${repository.version}" if="git.present">
    <exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="">
        <arg value="describe"/>
        <arg value="--tags"/>
        <arg value="--always"/>
        <arg value="HEAD"/>
    </exec>
    <condition property="repository.version" value="${git.revision}" else="unknown">
        <and>
            <isset property="git.revision"/>
            <length string="${git.revision}" trim="yes" length="0" when="greater"/>
        </and>
    </condition>
</target>
jmuc