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

Re: What happened to "git status --color=(always|auto|never)"?

From
Jeff King <peff@peff.net>
Date
Oct 10, 2017, 10:25 UTC
Message-ID
<20171010102509.e7ucbyon6ka6722l@sigill.intra.peff.net>
In-Reply-To
<CAEY4ZpMKE6yf2baaJt+x6c_esorFnyWvLZ=_KS1iRs6XbL42hw@mail.gmail.com>
On Tue, Oct 10, 2017 at 12:42:43PM +0800, Nazri Ramliy wrote:
Show 23 quoted lines
> >         commit 6be4595edb8e5b616c6e8b9fbc78b0f831fa2a87
> >         Author: Jeff King <peff@peff.net>
> >         Date:   Tue Oct 3 09:46:06 2017 -0400
> >
> >             color: make "always" the same as "auto" in config
> >
> > Would you like to take a stab at adding it?  builtin/commit.c and
> > Documentation/git-{commit,status}.txt would be my best guesses at
> > where to start.
> 
> Perhaps, seeing that that commit intentionally "broke" the color
> output of my tool[1], because it parses the output of `git -c
> color.status=always status`, which now no longer work the way it used
> to. I know, I know... shame on me for parsing the output of a
> porcelain command :)
> 
> But this also means that I will have to modify [1] to cope with this,
> given that it may be used with an older version of git (parse
> git-version and shell out to different git command - either `git -c
> color.ui=always status`, or the not-yet-exist `git status
> --color=always`), or make it use the plumbing output of `git status`,
> but that would just add additional work that  I really don't look
> forward to doing at this moment.
:( I was worried that this might hit some third-party scripts.
One workaround you can do that should work with any version of Git is:
  GIT_PAGER_IN_USE=1 git status | your-parser

That tells Git that even though stdout isn't a tty, you're expecting to present the data to the user and it should be colored appropriately. It has the additional upside that it doesn't override the user's color config.

It does have the potential downside that other non-color changes could kick in (e.g., somebody recently proposed that auto-columns kick in with GIT_PAGER_IN_USE).

All that said, should we revisit the decision from 6be4595edb? The two code changes we could make are:

  1. Adding a "--color" option to "git status". Commit 0c88bf5050
     (provide --color option for all ref-filter users, 2017-10-03) from
     that same series shows some prior art.
     This is a clean solution, but it does mean that scripts have to
     adapt (and would potentially need to care about which Git version
     they're relying on).
  2. Re-allow "color.always" config from the command-line. It's actually
     on-disk config that we want to downgrade, but I wanted to avoid
     making complicated rules about how the config would behave in
     different scopes. The patch for this would look something like the
     one below.
  3. Revert the original series, and revisit the original "respect
     color.ui via porcelain" commit which broke add--interactive in
     v2.14.2 (136c8c8b8fa).

I dunno. I think for your use case, PAGER_IN_USE may actually be the "right" solution, because it most closely expresses what you're doing. We probably ought to have (1) as a general rule for commands which handle color.

But (2) and (3) are the only ones that will work seamlessly with existing scripts. I'm not excited about either of them, though.

-Peff
diff --git a/color.c b/color.c
index 9c0dc82370..3870d3e395 100644
--- a/color.c
+++ b/color.c
@@ -307,8 +307,21 @@ int git_config_colorbool(const char *var, const char *value)
 	if (value) {
 		if (!strcasecmp(value, "never"))
 			return 0;
-		if (!strcasecmp(value, "always"))
-			return var ? GIT_COLOR_AUTO : 1;
+		if (!strcasecmp(value, "always")) {
+			/*
+			 * Command-line options always respect "always".
+			 * Likewise for "-c" config on the command-line.
+			 */
+			if (!var ||
+			    current_config_scope() == CONFIG_SCOPE_CMDLINE)
+				return 1;
+
+			/*
+			 * Otherwise, we're looking at on-disk config;
+			 * downgrade to auto.
+			 */
+			return GIT_COLOR_AUTO;
+		}
 		if (!strcasecmp(value, "auto"))
 			return GIT_COLOR_AUTO;
 	}
Previous: Nazri RamliyNext: Junio C Hamano
Message 6 of 42 in “What happened to "git status --color=(always|auto|never)"?”
  1. Nazri RamliyOct 9, 2017
  2. Jonathan NiederOct 10, 2017
  3. Nazri RamliyOct 10, 2017
  4. Jonathan NiederOct 10, 2017
  5. Nazri RamliyOct 10, 2017
  6. Jeff KingOct 10, 2017
  7. Junio C HamanoOct 10, 2017
  8. Jeff KingOct 10, 2017
  9. Jonathan NiederOct 10, 2017
  10. Jeff KingOct 10, 2017
  11. Junio C HamanoOct 11, 2017
  12. 0/2 Piling more kludge on top of color.uiJunio C Hamano, Oct 12, 2017
  13. 2/2 color: discourage use of ui.color=alwaysJunio C Hamano, Oct 12, 2017
  14. Jonathan NiederOct 12, 2017
  15. Jeff KingOct 12, 2017
  16. Junio C HamanoOct 13, 2017
  17. 1/2 color: downgrade "always" to "auto" only for on-disk configurationJunio C Hamano, Oct 12, 2017
  18. Jonathan NiederOct 12, 2017
  19. Junio C HamanoOct 12, 2017
  20. Jonathan NiederOct 12, 2017
  21. Junio C HamanoOct 12, 2017
  22. Junio C HamanoOct 12, 2017
  23. Jeff KingOct 12, 2017
  24. Jeff KingOct 12, 2017
  25. Jeff KingOct 12, 2017
  26. Junio C HamanoOct 13, 2017
  27. Jeff KingOct 13, 2017
  28. Junio C HamanoOct 13, 2017
  29. Jeff KingOct 13, 2017
  30. 0/4 peeling back color.ui=always hacksJeff King, Oct 13, 2017
  31. 1/4 Revert "color: make "always" the same as "auto" in config"Jeff King, Oct 13, 2017
  32. 2/4 Revert "t6006: drop "always" color config tests"Jeff King, Oct 13, 2017
  33. 3/4 Revert "color: check color.ui in git_default_config()"Jeff King, Oct 13, 2017
  34. 4/4 tag: respect color.ui configJeff King, Oct 13, 2017
  35. Junio C HamanoOct 14, 2017
  36. Jeff KingOct 16, 2017
  37. Junio C HamanoOct 17, 2017
  38. Junio C HamanoOct 17, 2017
  39. Jeff KingOct 18, 2017
  40. Junio C HamanoOct 18, 2017
  41. Jonathan NiederOct 17, 2017
  42. Jeff KingOct 18, 2017

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.