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

[PATCH 11/13] add-interactive: retain colorbool values longer

From
Jeff King <peff@peff.net>
Date
Sep 16, 2025, 20:26 UTC
Message-ID
<20250916202624.GK612873@coredump.intra.peff.net>
In-Reply-To
<20250916201036.GA612463@coredump.intra.peff.net>

Most of the diff code stores the decision about whether to show color as a git_colorbool, and evaluates it at point-of-use with want_color(). This timing is important for reasons explained in daa0c3d971 (color: delay auto-color decision until point of use, 2011-08-17).

The add-interactive code instead converts immediately to strict boolean values using want_color(), and then evaluates those. This isn't wrong. Even though we pass the bool values to diff_use_color(), which expects a colorbool, the values are compatible. But it is unlike the rest of the color code, and is questionable from a type-system perspective (but C's typing between enums, ints, and bools is weak enough that the compiler does not complain).

Let's switch it to the more usual way of calling want_color() at the point of use.

Signed-off-by: Jeff King <peff@peff.net>
---
I guess this could also come earlier in the series, but I found it
easier to understand the conversion if we start using the git_colorbool
value.
 add-interactive.c | 14 +++++++-------
 add-interactive.h |  4 ++--
 2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/add-interactive.c b/add-interactive.c
index 000315971e..6ffe64c38d 100644
--- a/add-interactive.c
+++ b/add-interactive.c
@@ -20,14 +20,14 @@
 #include "prompt.h"
 #include "tree.h"
 
-static void init_color(struct repository *r, int use_color,
+static void init_color(struct repository *r, enum git_colorbool use_color,
 		       const char *section_and_slot, char *dst,
 		       const char *default_color)
 {
 	char *key = xstrfmt("color.%s", section_and_slot);
 	const char *value;
 
-	if (!use_color)
+	if (!want_color(use_color))
 		dst[0] = '\0';
 	else if (repo_config_get_value(r, key, &value) ||
 		 color_parse(value, dst))
@@ -36,7 +36,7 @@ static void init_color(struct repository *r, int use_color,
 	free(key);
 }
 
-static int check_color_config(struct repository *r, const char *var)
+static enum git_colorbool check_color_config(struct repository *r, const char *var)
 {
 	const char *value;
 	enum git_colorbool ret;
@@ -55,7 +55,7 @@ static int check_color_config(struct repository *r, const char *var)
 	    !repo_config_get_value(r, "color.ui", &value))
 		ret = git_config_colorbool("color.ui", value);
 
-	return want_color(ret);
+	return ret;
 }
 
 void init_add_i_state(struct add_i_state *s, struct repository *r,
@@ -76,7 +76,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
 	init_color(r, s->use_color_interactive, "interactive.error",
 		   s->error_color, GIT_COLOR_BOLD_RED);
 	strlcpy(s->reset_color_interactive,
-		s->use_color_interactive ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
+		want_color(s->use_color_interactive) ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
 
 	s->use_color_diff = check_color_config(r, "color.diff");
 
@@ -93,7 +93,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
 	init_color(r, s->use_color_diff, "diff.new", s->file_new_color,
 		   diff_get_color(s->use_color_diff, DIFF_FILE_NEW));
 	strlcpy(s->reset_color_diff,
-		s->use_color_diff ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
+		want_color(s->use_color_diff) ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
 
 	FREE_AND_NULL(s->interactive_diff_filter);
 	repo_config_get_string(r, "interactive.difffilter",
@@ -1211,7 +1211,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps,
 	 * When color was asked for, use the prompt color for
 	 * highlighting, otherwise use square brackets.
 	 */
-	if (s.use_color_interactive) {
+	if (want_color(s.use_color_interactive)) {
 		data.color = s.prompt_color;
 		data.reset = s.reset_color_interactive;
 	}
diff --git a/add-interactive.h b/add-interactive.h
index ceadfa6bb6..da49502b76 100644
--- a/add-interactive.h
+++ b/add-interactive.h
@@ -12,8 +12,8 @@ struct add_p_opt {
 
 struct add_i_state {
 	struct repository *r;
-	int use_color_interactive;
-	int use_color_diff;
+	enum git_colorbool use_color_interactive;
+	enum git_colorbool use_color_diff;
 	char header_color[COLOR_MAXLEN];
 	char help_color[COLOR_MAXLEN];
 	char prompt_color[COLOR_MAXLEN];
-- 
2.51.0.527.g34bc42dacd
Previous: Jeff KingNext: Jeff King
Message 14 of 18 in “unraveling the mysteries of color variables”
  1. 0/13 unraveling the mysteries of color variablesJeff King, Sep 16, 2025
  2. Jeff KingSep 16, 2025
  3. 01/13 color: use GIT_COLOR_* instead of numeric constantsJeff King, Sep 16, 2025
  4. 02/13 color: return enum from git_config_colorbool()Jeff King, Sep 16, 2025
  5. 03/13 grep: don't treat grep_opt.color as a strict boolJeff King, Sep 16, 2025
  6. 04/13 diff: simplify color_moved check when flushingJeff King, Sep 16, 2025
  7. 05/13 diff: don't use diff_options.use_color as a strict boolJeff King, Sep 16, 2025
  8. 06/13 diff: pass o->use_color directly to fill_metainfo()Jeff King, Sep 16, 2025
  9. 07/13 diff: stop passing ecbdata->use_color as booleanJeff King, Sep 16, 2025
  10. 08/13 pretty: use format_commit_context.auto_color as colorboolJeff King, Sep 16, 2025
  11. 09/13 color: use git_colorbool enum to type to store colorboolsJeff King, Sep 16, 2025
  12. 9/13 color: use git_colorbool enum type to store colorboolsJeff King, Sep 16, 2025
  13. 10/13 color: return bool from want_color()Jeff King, Sep 16, 2025
  14. 11/13 add-interactive: retain colorbool values longerJeff King, Sep 16, 2025
  15. 12/13 config: store want_color() result in a separate boolJeff King, Sep 16, 2025
  16. 13/13 color: convert git_colorbool into a structJeff King, Sep 16, 2025
  17. Junio C HamanoSep 16, 2025
  18. Jeff KingSep 16, 2025

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.