Search code examples
gitcontinuous-integrationqmakebuildbotmonotone

How do I get just the branch from a Git command?


Possible Duplicate:
Show just the current branch in Git

I am moving my monotone DVCS to git. In my build setup I have qmake get the current revision and the current branch (because these are build on buildbot) so that it can be used as a define.

exists(_MTN):DEFINES += BUILDREVISION=\\\"$$system(mtn automate get_base_revision_id)\\\"
else:DEFINES += BUILDREVISION=\\\"NOT \
    BUILT \
    FROM \
    SOURCE \
    REPOSITORY\\\"

# Check which branch we are building
exists(_MTN):DEFINES += BUILDBRANCH=\\\"$$system(mtn au get_option branch)\\\"
else:DEFINES += BUILDBRANCH=\\\"UNKNOWN\\\"

In git I can do:

exists(.git):DEFINES += BUILDREVISION=\\\"$$system(git rev-parse HEAD)\\\"
else:DEFINES += BUILDREVISION=\\\"NOT \
    BUILT \
    FROM \
    SOURCE \
    REPOSITORY\\\"

# Check which branch we are building
exists(.git):DEFINES += BUILDBRANCH=\\\"$$system(git show-branch --current)\\\"
else:DEFINES += BUILDBRANCH=\\\"UNKNOWN\\\"

My problems are that "git show-branch --current" has the commit message with it and a new line character in it, which my compiler doesn't like. How can I get git to be more succinct?


Solution

  • The following works for me:

    $ git rev-parse --symbolic-full-name --abbrev-ref HEAD
    master
    

    If you're not on any branch (i.e. you've detached HEAD) then this will just return HEAD, but it sounds as if in your use case you're always expecting to be on a branch.

    Update: In fact it's even simpler, you can just do: git rev-parse --abbrev-ref HEAD