1

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?

Thorbjørn Ravn Andersen
  • 68,906
  • 28
  • 171
  • 323
  • 1
    Isn't this related to http://stackoverflow.com/questions/3858563/active-git-branch-is-no-branch-on-hudson-ci? – VonC Oct 13 '10 at 11:46
  • @VonC, yes closely, but I can probably live with the SHA1 code - I want to be able to go back at the exact commit from which the build was made. – Thorbjørn Ravn Andersen Oct 13 '10 at 12:49
  • 1
    ørn SHA1 is good (but not very satisfactory), [`git describe`](http://stackoverflow.com/questions/677436/how-to-get-the-git-commit-count/677888) is better (but means invoking git, which is what you don't want). The concept of (named) branch is a "transient" one, as branches can be deleted/renamed at will. – VonC Oct 13 '10 at 12:55
  • "don't want" is too strong, "prefer not" is better. I'd like Hudson to tell what it knows instead of having to use heuristics to derive something that may or may not be true. – Thorbjørn Ravn Andersen Oct 13 '10 at 18:32

4 Answers4

2

The git plugin exposes a GIT_COMMIT environment variable.

wmbolle
  • 81
  • 1
  • 2
2

Well, if you really want to avoid calling git command (git describe or git rev-parse), then you can do the following:

  1. 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.

  2. 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.

Community
  • 1
  • 1
Jakub Narębski
  • 268,805
  • 58
  • 209
  • 228
2

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.

Thorbjørn Ravn Andersen
  • 68,906
  • 28
  • 171
  • 323
0

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.

alexkasko
  • 4,715
  • 1
  • 24
  • 30