Saving Git SHA1 when building with Hudson similar to the CVS_BRANCH tag for CVS
We have migrated from CVS to Git. Our Hudson build script used to save the value of the CVS_BRANCH environment variable in the generated build along with the Hudson BUILD_ID to allow for later investigation.
I cannot figure out how Hudson (or the Git plugin) presents the Git SHA1 name of the current commit to the ant script, as I cannot locate any definite source saying where I should look.
I'd prefer not to invoke git to get it if it is present in the environment, but can do if that is necessary.
What have I missed?
Well, if you really want to avoid calling git command (git describe or git rev-parse), then you can do the following:
Look up $GIT_DIR/HEAD file. If it is symbolic link, its target is fully qualified name of current branch (e.g. 'refs/heads/master' if current branch is 'master'); shouldn't happen except in very old repositories managed by very old git.
If it is ordinary file, it is either of the form ref: refs/heads/<branch> (so called symref), or it contains SHA-1 id of current commit (so called "detached HEAD" aka. anonymous branch: '(no branch)' in git branch output.
Current commit is either in $GIT_DIR/refs/head/branch file, or it can be found in the $GIT_DIR/packed-refs file. If both exist, then loose ref (in a seperate file named after fully qualified branch name) wins.
But I am not sure if it is worth it.
It turned out that jGit supports the "rev-parse HEAD" command, which in combination with the <java jar="jgit...jar" fork="true" args="rev-parse HEAD" outputproperty="git.SHA1" /> did exactly what I wanted, namely got the SHA1 into an ant property so I could use it later.
The git plugin exposes a GIT_COMMIT environment variable.
To get buildnumber from git using Ant and JGit you can use jgit-buildnumber-ant-task. It can give you tag name, branch name, commit sha1, and commit's count in current branch as Ant variables.