git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/RFC 7/9] add long form %(color:...) for %C(...)

From
Will Palmer <wmpalmer@gmail.com>
Date
Mar 28, 2011, 23:17 UTC
Message-ID
<1301354251-23380-8-git-send-email-wmpalmer@gmail.com>
In-Reply-To
<1301354251-23380-1-git-send-email-wmpalmer@gmail.com>

this adds %(color:...) as an alternative to %C(...) in the "git log" family of commands. We now have a "long form" for all of the existing complex placeholders.

Signed-off-by: Will Palmer <wmpalmer@gmail.com>
---
 Documentation/pretty-formats.txt |    1 +
 pretty.c                         |   22 ++++++++++++++++++++++
 test-pretty.c                    |    1 +
 3 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index c9f3fb6..d987102 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -138,6 +138,7 @@ The placeholders are:
 - '%Cblue': switch color to blue
 - '%Creset': reset color
 - '%C(...)': color specification, as described in color.branch.* config option
+- '%(color:...)': alternative form of %C(...)
 - '%m': left, right or boundary mark
 - '%n': newline
 - '%%': a raw '%'
diff --git a/pretty.c b/pretty.c
index 8301008..616b857 100644
--- a/pretty.c
+++ b/pretty.c
@@ -126,9 +126,31 @@ static struct format_part *parse_extended(const char *unparsed)
 {
 	struct format_part *part = format_part_alloc();
 	const char *c = unparsed + 2; /* "%(..." + strlen("%(") */
+	const char *e;
 
 	c += strspn(c, WHITESPACE);
 
+	if (!prefixcmp(c, "color")) {
+		part->type = FORMAT_PART_LITERAL;
+		c += 5 + strspn(c + 5, WHITESPACE);
+		if (*c == ')') {
+			part->literal = xstrdup(GIT_COLOR_RESET);
+			part->literal_len = strlen(part->literal);
+			goto success;
+		}
+		if (*c != ':')
+			goto fail;
+		c++;
+		e = strchr(c, ')');
+		part->literal = xcalloc(1, COLOR_MAXLEN);
+		if (!e || !color_parse_len(c, e - c,
+					   part->literal))
+			goto fail;
+		part->literal_len = strlen(part->literal);
+		c = e;
+		goto success;
+	}
+
 	if (!prefixcmp(c, "wrap")) {
 		part->type = FORMAT_PART_WRAP;
 		c += 4;
diff --git a/test-pretty.c b/test-pretty.c
index 64a8218..eb88e3a 100644
--- a/test-pretty.c
+++ b/test-pretty.c
@@ -18,6 +18,7 @@ static const char *all = "a"
 "%Cred%Cgreen%Cblue%Creset%C(reset)"
 "%m%w()%w(1)%w(1,2)%w(1,2,3)"
 "%(wrap)%(wrap:1)%(wrap:1,2)%(wrap:1,2,3)"
+"%(color)%(color:red)%(color:red bold)%(color:red green bold)"
 "%x0a%n%%%@";
 
 static struct strbuf *parts_debug(struct format_parts *parts,
-- 
1.7.4.2
Previous: Will PalmerNext: Will Palmer
Message 8 of 14 in “add long forms for format placeholders”
  1. 0/9 add long forms for format placeholdersWill Palmer, Mar 28, 2011
  2. 1/9 mention --date=raw in rev-list and blame helpWill Palmer, Mar 28, 2011
  3. 2/9 add support for --date=unix to complement %atWill Palmer, Mar 28, 2011
  4. 3/9 interpret %C(invalid) as we would %%C(invalid)Will Palmer, Mar 28, 2011
  5. 4/9 add sanity length check to format_person_partWill Palmer, Mar 28, 2011
  6. 5/9 refactor pretty.c into "parse" and "format" stepsWill Palmer, Mar 28, 2011
  7. 6/9 add long-form %(wrap:...) for %w(...)Will Palmer, Mar 28, 2011
  8. 7/9 add long form %(color:...) for %C(...)Will Palmer, Mar 28, 2011
  9. 8/9 add long forms %(authordate) and %(committerdate)Will Palmer, Mar 28, 2011
  10. 9/9 add long forms for author and committer identityWill Palmer, Mar 28, 2011
  11. Junio C HamanoMar 29, 2011
  12. Will PalmerMar 29, 2011
  13. Michael J GruberMar 29, 2011
  14. Will PalmerMar 29, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.