Re: git-branch --print-current
From: Jakub Narebski <hidden>
Date: 2016-06-15 22:45:52
Adeodato Simó [off-list ref] writes:
* Karl Chen [Sun, 04 Jan 2009 04:40:51 -0800]:quoted
Arnaud> $ git branch | awk '/^\*/ {print $2}'quoted
Yet another addition to the list of ways to pipeline it, this one probably the shortest :)Heh, if we're playing golf: $ git branch | sed -n 's/^\* //p'
Even if you want to reimplement __git_ps1 provided with bash
completion in completion/git-completion.bash instead of reusing it,
you still have to deal with many situations: not being in git
repository, being on detached HEAD, being in intermediate state
(during git-am, git-rebase, git-bisect etc.), etc. Additionally you
would probably want name of git repository and relative path inside
git repository in prompt.
Therefore you need to use script anyway. And for scripting you should
use plumbing (which output format shouldn't change) and not porcelain
git-branch (which output might change, for example having '-v' on by
default, or something; and you might have color.ui set to true by
mistake and have to deal with color codes). And then you don't need
sed nor awk: POSIX shell features would be enough:
BR=$(git symbolic-ref HEAD 2>/dev/null)
BR=${BR#refs/heads/}
BR=${BR:-HEAD} # one of possibilities to show detached HEAD / no branch
--
Jakub Narebski
Poland
ShadeHawk on #git