add-interactive: retain colorbool values longer

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 16, 2025 at 16:26 UTC 9d241b01132c17a44adda2d762b37adf3625bdd7
2 files changed +9 -9
add-interactive.c
+7 -7
@@ -20,14 +20,14 @@
20 #include "prompt.h"
21 #include "tree.h"
22
23 -static void init_color(struct repository *r, int use_color,
23 +static void init_color(struct repository *r, enum git_colorbool use_color,
24 const char *section_and_slot, char *dst,
25 const char *default_color)
26 {
27 char *key = xstrfmt("color.%s", section_and_slot);
28 const char *value;
29
30 - if (!use_color)
30 + if (!want_color(use_color))
31 dst[0] = '\0';
32 else if (repo_config_get_value(r, key, &value) ||
33 color_parse(value, dst))
@@ -36,7 +36,7 @@ static void init_color(struct repository *r, int use_color,
36 free(key);
37 }
38
39 -static int check_color_config(struct repository *r, const char *var)
39 +static enum git_colorbool check_color_config(struct repository *r, const char *var)
40 {
41 const char *value;
42 enum git_colorbool ret;
@@ -55,7 +55,7 @@ static int check_color_config(struct repository *r, const char *var)
55 !repo_config_get_value(r, "color.ui", &value))
56 ret = git_config_colorbool("color.ui", value);
57
58 - return want_color(ret);
58 + return ret;
59 }
60
61 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,
76 init_color(r, s->use_color_interactive, "interactive.error",
77 s->error_color, GIT_COLOR_BOLD_RED);
78 strlcpy(s->reset_color_interactive,
79 - s->use_color_interactive ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
79 + want_color(s->use_color_interactive) ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
80
81 s->use_color_diff = check_color_config(r, "color.diff");
82
@@ -93,7 +93,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
93 init_color(r, s->use_color_diff, "diff.new", s->file_new_color,
94 diff_get_color(s->use_color_diff, DIFF_FILE_NEW));
95 strlcpy(s->reset_color_diff,
96 - s->use_color_diff ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
96 + want_color(s->use_color_diff) ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
97
98 FREE_AND_NULL(s->interactive_diff_filter);
99 repo_config_get_string(r, "interactive.difffilter",
@@ -1211,7 +1211,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps,
1211 * When color was asked for, use the prompt color for
1212 * highlighting, otherwise use square brackets.
1213 */
1214 - if (s.use_color_interactive) {
1214 + if (want_color(s.use_color_interactive)) {
1215 data.color = s.prompt_color;
1216 data.reset = s.reset_color_interactive;
1217 }
add-interactive.h
+2 -2
@@ -12,8 +12,8 @@ struct add_p_opt {
12
13 struct add_i_state {
14 struct repository *r;
15 - int use_color_interactive;
16 - int use_color_diff;
15 + enum git_colorbool use_color_interactive;
16 + enum git_colorbool use_color_diff;
17 char header_color[COLOR_MAXLEN];
18 char help_color[COLOR_MAXLEN];
19 char prompt_color[COLOR_MAXLEN];