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 <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Nov 18, 2013 at 23:09 UTC fddb74c94777351d549b2ddaa36612c41b2176f1
3 files changed +30 -2
Documentation/git-for-each-ref.txt
+4
@@ -101,6 +101,10 @@ HEAD::
101 '*' if HEAD matches current ref (the checked out branch), ' '
102 otherwise.
103
104 +color::
105 + Change output color. Followed by `:<colorname>`, where names
106 + are described in `color.branch.*`.
107 +
108 In addition to the above, for commit and tag objects, the header
109 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
110 be used to specify the value in the header field.
builtin/for-each-ref.c
+9 -2
@@ -9,6 +9,7 @@
9 #include "quote.h"
10 #include "parse-options.h"
11 #include "remote.h"
12 +#include "color.h"
13
14 /* Quoting styles */
15 #define QUOTE_NONE 0
@@ -76,6 +77,7 @@ static struct {
77 { "symref" },
78 { "flag" },
79 { "HEAD" },
80 + { "color" },
81 };
82
83 /*
@@ -662,8 +664,13 @@ static void populate_value(struct refinfo *ref)
664 !branch->merge[0]->dst)
665 continue;
666 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")) {
674 char buf[256], *cp = buf;
675 if (ref->flag & REF_ISSYMREF)
676 cp = copy_advance(cp, ",symref");
t/t6300-for-each-ref.sh
+17
@@ -343,6 +343,23 @@ test_expect_success 'Check for invalid refname format' '
343 test_must_fail git for-each-ref --format="%(refname:INVALID)"
344 '
345
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 +
363 cat >expected <<\EOF
364 heads/master
365 tags/master