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

Re: [PATCH] Pretty formats: skip color codes if !want_color()

From
Jeff King <peff@peff.net>
Date
Dec 17, 2012, 23:10 UTC
Message-ID
<20121217231042.GB1809@sigill.intra.peff.net>
In-Reply-To
<20121217225703.GC25103@orenhe-laptop>
On Tue, Dec 18, 2012 at 12:57:03AM +0200, Oren Held wrote:
> Avoid color escape codes if colors are disabled, just like the
> behavior of other git commands.  This solves the case of color escape
> codes in stdout when piping or redirecting, e.g.: $ git log
> --format=%Cred%h > out

You may be interested in this thread from today, which is attacking the same problem:

  http://thread.gmane.org/gmane.comp.version-control.git/211370/focus=211714
Two issues with your patch:
Show 6 quoted lines
> @@ -956,6 +962,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
>  	struct commit_list *p;
>  	int h1, h2;
>  
> +	int colors_enabled = want_color(GIT_COLOR_UNKNOWN);
> +

I think this would want to use the regular diff color variable to be consistent with other coloring of "git log".

Show 8 quoted lines
> @@ -967,20 +975,20 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
>  			color_parse_mem(placeholder + 2,
>  					end - (placeholder + 2),
>  					"--pretty format", color);
> -			strbuf_addstr(sb, color);
> +			conditional_strbuf_addstr(colors_enabled, sb, color);
>  			return end - placeholder + 1;
>  		}

This breaks backwards compatibility for callers who expect the coloring to be unconditional; adding new syntax would solve that.

The patch I linked to above solves both.
-Peff
Previous: Oren Held
Message 2 of 2 in “Pretty formats: skip color codes if !want_color()”
  1. Pretty formats: skip color codes if !want_color()Oren Held, Dec 17, 2012
  2. Jeff KingDec 17, 2012

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.