Subject: Re: [PATCH v2 3/3] for-each-ref: introduce %(color:...) for color

From: Junio C Hamano <[email-masked]>
Date: Wed, 13 Nov 2013 12:19:25 -0800

Junio C Hamano <[email-masked]> writes:

> Ramkumar Ramachandra <[email-masked]> writes:
>
>> +		} else if (!prefixcmp(name, "color")) {
>> +			;
>
> How is "%(color:short)" parsed with this code?
>
> This part says, "Yeah, I see something starting with color", and
> then later strchr(name, ':') will point formatp to "short".
>
> Luckily, "%(colorgarbage:short)" does not even come this far because
> parse_atom() would not have allowed the codeflow to, but comparing
> with "color:" here may be a lot more defensive and safe, I think.
>
> And find the color-value here, stuffing v->s inside this "else if",
> continue, without letting the formatp part work on refname this
> piece of code does not even set.  Just like how we handle "flag"
> without falling thru to the formatp code.

Perhaps like this (obviously not tested as these three patches did
not add any tests ;-)

I also think that there should be a mechanism to do "color:reset"
after each record is issued automatically, and also have the color
output honor --color=auto from the command line, i.e.

	git for-each-ref --color=auto --format='%(color:blue)%(subject)' | cat

should turn the coloring off.

So I think this patch may be a first step in the right direction,
but there are quite a lot more work that is needed before it gets
ready for production use.

Thanks.

 builtin/for-each-ref.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 9e07571..07a9385 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -664,8 +664,12 @@ static void populate_value(struct refinfo *ref)
 			    !branch->merge[0]->dst)
 				continue;
 			refname = branch->merge[0]->dst;
-		} else if (!prefixcmp(name, "color")) {
-			;
+		} else if (!prefixcmp(name, "color:")) {
+			char color[COLOR_MAXLEN] = "";
+
+			color_parse(name + 6, "--format", color);
+			v->s = xstrdup(color);
+			continue;
 		} else if (!strcmp(name, "flag")) {
 			char buf[256], *cp = buf;
 			if (ref->flag & REF_ISSYMREF)
@@ -733,12 +737,6 @@ static void populate_value(struct refinfo *ref)
 				else
 					v->s = "<>";
 				continue;
-			} else if (!prefixcmp(name, "color")) {
-				char color[COLOR_MAXLEN] = "";
-
-				color_parse(formatp, "--format", color);
-				v->s = xstrdup(color);
-				continue;
 			} else
 				die("unknown %.*s format %s",
 				    (int)(formatp - name), name, formatp);