1

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?

Community
  • 1
  • 1
Phil Hannent
  • 12,047
  • 17
  • 71
  • 118

3 Answers3

4

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

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
0

No git command give you only the current branch, so you have to modify its output to extract it:

git branch --no-color 2> /dev/null | grep "*" | sed s/*\ //
1                                    2          3

Explanation:

  1. get branch list
  2. select current branch, prefixed with *
  3. remove the *
CharlesB
  • 86,532
  • 28
  • 194
  • 218
0
cat .git/HEAD | sed 's/.*\///'

The sed 's/.*\///' part removes the "ref: refs/heads/" from the beginning of the line.

Lee Netherton
  • 21,347
  • 12
  • 68
  • 102