Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lookup the latest git commit hash from an ant build script

Tags:

git

ant

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 .

like image 489
mchr Avatar asked Jun 04 '10 12:06

mchr


People also ask

How do you find the hash of a commit?

The long string following the word commit is called the commit hash.

How do I get the latest commit of a branch?

git log -n 1 [branch_name] branch_name(may be remote or local branch) is optional. Without branch_name it will show the latest commit of current branch.

What determines git hash?

Hashes are what enable Git to share data efficiently between repositories. If two files are the same, their hashes are guaranteed to be the same. Similarly, if two commits contain the same files and have the same ancestors, their hashes will be the same as well.

How long are git commit hashes?

A commit in git always has a hash that contains 40 characters. But to make the id:s easier to handle it also supports using a short version of the id. The short commit id can actually be any number of characters as long as it's unique for a commit within the same repo.


2 Answers

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> 

It e.g. be used for expanding the token @repository.version@ in a template file:

<target name="index.html" depends="git.revision" description="build index.html from template">     <copy file="index.html.template" tofile="index.html" overwrite="yes">         <filterchain>             <replacetokens>                 <token key="repository.version" value="${repository.version}" />             </replacetokens>         </filterchain>     </copy> </target> 
like image 59
jmuc Avatar answered Sep 19 '22 18:09

jmuc


This command returns always the working folder's last commit SHA1, useful when you don't always build from HEAD. The command should run both on Windows and *nix systems

<exec executable="git" outputproperty="git.revision">     <arg value="log" />     <arg value="-1" />     <arg value="--pretty=format:%H" /> </exec> 
like image 31
Gian Marco Avatar answered Sep 22 '22 18:09

Gian Marco