Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git for-each-ref with a coloured format string

Tags:

git

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?

like image 576
akhaku Avatar asked Aug 06 '13 01:08

akhaku


1 Answers

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).

like image 156
VonC Avatar answered Oct 04 '22 00:10

VonC