Re: git-branch --print-current

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Adeodato Simó <dato@xxxxxxxxxxxxxx> writes:
> * Karl Chen [Sun, 04 Jan 2009 04:40:51 -0800]:
> 
> >     Arnaud> $ git branch | awk '/^\*/ {print $2}'
> 
> > 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
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux