11

I'm using git for-each-ref as a git alias to display a branch name and the subject of the last commit on that branch. That said, it's hard to tell where the branch name ends and the commit message subject starts, so I'm trying to colourize the branch name to more easily tell the difference between the two. Below is the working alias without colour:

[alias]
  logbranch = for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short) %(subject)'

To add colour, I tried using shell colour escapes (I'm using bash) like so:

[alias]
  logbranch = for-each-ref --sort=-committerdate refs/heads/ --format='[\033[0;31m]%(refname:short)[\e[m]   %(subject)'

which gives me a git config error. I also tried replacing the single quotes with double quotes, and escaping the square brackets, but no dice.

Ideas?

akhaku
  • 1,107
  • 7
  • 16
  • According to the [docs for `git for-each-ref`](https://www.kernel.org/pub/software/scm/git/docs/git-for-each-ref.html), you can use `%xx`, where `xx` is a hex digit, in the format string, so that it's evaluated by the host language (in this case Bash?). I've been trying it out, not sure if it works. –  Aug 06 '13 at 01:50
  • Doesn't `git branch -vv` give you the same information about branches? – Roman Aug 06 '13 at 03:48
  • @R0MANARMY /facepalm thanks, that's exactly what I was trying to emulate - should have read the git branch manpage more carefully. – akhaku Aug 06 '13 at 16:42
  • @akhaku You might be interested in this question too: [how do I get git to show me which branches are tracking what?](http://stackoverflow.com/questions/4950725/how-do-i-get-git-to-show-me-which-branches-are-tracking-what/4952368) – Roman Aug 06 '13 at 17:03
  • 1
    Note: this is coming! See [my answer below](http://stackoverflow.com/a/20499556/6309). – VonC Dec 10 '13 at 16:16
  • related: http://stackoverflow.com/questions/31984968/how-can-i-color-git-branches-based-on-their-names – jub0bs Aug 20 '15 at 09:04

2 Answers2

28

Git 1.9/2.0 (Q1 2014) will introduce color formatting for git for-each-ref.
See commit fddb74c from Ramkumar Ramachandra (artagnon):

for-each-ref: introduce %(color:...) for color

Enhance 'git for-each-ref' with color formatting options.
You can now use the following format in for-each-ref:

%(color:green)%(refname:short)%(color:reset)

where color names are described in color.branch.*.


With Git 2.15 (Q4 2017), you will be able to turn those colors on or off.

See commit 0c88bf5 (03 Oct 2017) by Jeff King (peff).
(Merged 04 Oct 2017)

provide --color option for all ref-filter users

When ref-filter learned about want_color() in 11b087a (ref-filter: consult want_color() before emitting colors, 2017-07-13), it became useful to be able to turn colors off and on for specific commands. For git-branch, you can do so with --color/--no-color.

But for git-for-each-ref and git-tag, the other users of ref-filter, you have no option except to tweak the "color.ui" config setting. Let's give both of these commands the usual color command-line options.

This is a bit more obvious as a method for overriding the config. And it also prepares us for the behavior of "always" changing (so that we are still left with a way of forcing color when our output goes to a non-terminal).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
6

I don't see anything in the for-each-ref man page that suggests it supports backslash-escape sequences like \033. If you replace \033 (and \e) with a literal escape character, it seems to work just fine.

Cupcake says that for-each-ref also supports %xx hex escape sequences, which would look like:

[alias]
    logbranch = "for-each-ref --sort=-committerdate refs/heads/ --format='[%1B[0;31m]%(refname:short)[%1B[m]   %(subject)' "

This also works fine on my system.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • The last format string seems to have extra square brackets. I tried `'%1B[0;31m%(refname:short)%1B[m %(subject)'` instead, which seems to work. But putting double-quotes around the whole alias seems to be the most important part, otherwise I get the "bad config" message that the original poster got. –  Aug 06 '13 at 01:57
  • Thanks - I had to remove the extra square brackets too, but this answers my question exactly. Just when I thought I was finally beginning to grok git... – akhaku Aug 06 '13 at 16:46