Skip to content

Commit fddb74c

Browse files
artagnongitster
authored andcommitted
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.*. Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b28061c commit fddb74c

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ HEAD::
101101
'*' if HEAD matches current ref (the checked out branch), ' '
102102
otherwise.
103103

104+
color::
105+
Change output color. Followed by `:<colorname>`, where names
106+
are described in `color.branch.*`.
107+
104108
In addition to the above, for commit and tag objects, the header
105109
field names (`tree`, `parent`, `object`, `type`, and `tag`) can
106110
be used to specify the value in the header field.

builtin/for-each-ref.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "quote.h"
1010
#include "parse-options.h"
1111
#include "remote.h"
12+
#include "color.h"
1213

1314
/* Quoting styles */
1415
#define QUOTE_NONE 0
@@ -76,6 +77,7 @@ static struct {
7677
{ "symref" },
7778
{ "flag" },
7879
{ "HEAD" },
80+
{ "color" },
7981
};
8082

8183
/*
@@ -662,8 +664,13 @@ static void populate_value(struct refinfo *ref)
662664
!branch->merge[0]->dst)
663665
continue;
664666
refname = branch->merge[0]->dst;
665-
}
666-
else if (!strcmp(name, "flag")) {
667+
} else if (!prefixcmp(name, "color:")) {
668+
char color[COLOR_MAXLEN] = "";
669+
670+
color_parse(name + 6, "--format", color);
671+
v->s = xstrdup(color);
672+
continue;
673+
} else if (!strcmp(name, "flag")) {
667674
char buf[256], *cp = buf;
668675
if (ref->flag & REF_ISSYMREF)
669676
cp = copy_advance(cp, ",symref");

t/t6300-for-each-ref.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,23 @@ test_expect_success 'Check for invalid refname format' '
343343
test_must_fail git for-each-ref --format="%(refname:INVALID)"
344344
'
345345

346+
get_color ()
347+
{
348+
git config --get-color no.such.slot "$1"
349+
}
350+
351+
cat >expected <<EOF
352+
$(git rev-parse --short refs/heads/master) $(get_color green)master$(get_color reset)
353+
$(git rev-parse --short refs/remotes/origin/master) $(get_color green)origin/master$(get_color reset)
354+
$(git rev-parse --short refs/tags/testtag) $(get_color green)testtag$(get_color reset)
355+
$(git rev-parse --short refs/tags/two) $(get_color green)two$(get_color reset)
356+
EOF
357+
358+
test_expect_success 'Check %(color:...) ' '
359+
git for-each-ref --format="%(objectname:short) %(color:green)%(refname:short)%(color:reset)" >actual &&
360+
test_cmp expected actual
361+
'
362+
346363
cat >expected <<\EOF
347364
heads/master
348365
tags/master

0 commit comments

Comments
 (0)