git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git-branch --print-current

From
KCKarl Chen <quarl@cs.berkeley.edu>
Date
Jan 4, 2009, 04:26 UTC
Message-ID
<quack.20090103T2026.lth3afzg0hx@roar.cs.berkeley.edu>
In-Reply-To
<20090104033839.GD21154@genesis.frugalware.org>
>>>>> On 2009-01-03 19:38 PST, Miklos Vajna writes:
    Miklos> On Sat, Jan 03, 2009 at 06:18:36PM -0800, Karl Chen <quarl@cs.berkeley.edu> wrote:
    >> How about an option to git-symbolic-ref that gets rid of
    >> the refs/heads/ ?
    Miklos> Make an alias?

Thanks for the suggestion. I don't have any problems making aliases or using git-branch for interactive output; it's not an issue of typing less.

I guess the broader point is that people use these "porcelain" commands in scripts and parse their output even when they shouldn't, and it's better to take action to prevent that. This reminds me of the issue of debugfs supposedly not being an ABI but people rely on anyway since it's stable enough - people are starting to rely on 'git branch' output just to print the current branch name. Better to create or at least publicly point out a good alternative to nip this in the bud.

I suppose "user education" in the form of a big warning in the git-branch man page would also help. How do you even tell in the man page whether a command is porcelain or not? Still, I think something like this is worth making slightly easier. Another minor argument for something like git branch --print-name is that it's annoying to check the exit code inside a pipeline.

For example: Google for how to add the name of the git branch to
the bash prompt and you'll find countless examples of people using
git-branch.  And they're all different, so people aren't just
blindly copying one guy; here is a small sample:
     
    export PS1='...`git branch 2> /dev/null | grep -e ^* | sed
    -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\
     
    $(git branch &>/dev/null; if [ $? -eq 0 ]; then echo "
    ($(git branch | grep '^*' |sed s/\*\ //))"; fi)
     
    `ruby -e \"print (%x{git branch 2>
    /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1)
    ')\"\`
     
    parse_git_branch() {
      git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
    }
     
    `git branch 2>/dev/null|cut -f2 -d\* -s`
     
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
     
    git_branch=`git branch 2>/dev/null | grep -e '^*' | sed -E 's/^\* (.+)$/(\1) /'`
     
There were a few using git-symbolic-ref but most used git-branch.
Previous: Miklos VajnaNext: Junio C Hamano
Message 5 of 28 in “git-branch --print-current”
  1. Karl ChenJan 2, 2009
  2. David AguilarJan 2, 2009
  3. Karl ChenJan 4, 2009
  4. Miklos VajnaJan 4, 2009
  5. Karl ChenJan 4, 2009
  6. Junio C HamanoJan 4, 2009
  7. git-rev-parse --symbolic-abbrev-name [was Re: git-branch --print-current]Karl Chen, Jan 4, 2009
  8. demerphqJan 4, 2009
  9. Junio C HamanoJan 4, 2009
  10. Arnaud LacombeJan 4, 2009
  11. Miklos VajnaJan 4, 2009
  12. Arnaud LacombeJan 5, 2009
  13. Miklos VajnaJan 5, 2009
  14. Junio C HamanoJan 6, 2009
  15. Arnaud LacombeJan 7, 2009
  16. demerphqJan 4, 2009
  17. Junio C HamanoJan 5, 2009
  18. Shawn O. PearceJan 5, 2009
  19. Junio C HamanoJan 5, 2009
  20. Jeff KingJan 5, 2009
  21. Arnaud LacombeJan 4, 2009
  22. Karl ChenJan 4, 2009
  23. demerphqJan 4, 2009
  24. Arnaud LacombeJan 4, 2009
  25. Adeodato SimóJan 4, 2009
  26. Jakub NarebskiJan 4, 2009
  27. Alexandre DulaunoyJan 4, 2009
  28. demerphqJan 4, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.