Skip to content

Commit 9d241b0

Browse files
peffgitster
authored andcommitted
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 daa0c3d (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b978f78 commit 9d241b0

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

add-interactive.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
#include "prompt.h"
2121
#include "tree.h"
2222

23-
static void init_color(struct repository *r, int use_color,
23+
static void init_color(struct repository *r, enum git_colorbool use_color,
2424
const char *section_and_slot, char *dst,
2525
const char *default_color)
2626
{
2727
char *key = xstrfmt("color.%s", section_and_slot);
2828
const char *value;
2929

30-
if (!use_color)
30+
if (!want_color(use_color))
3131
dst[0] = '\0';
3232
else if (repo_config_get_value(r, key, &value) ||
3333
color_parse(value, dst))
@@ -36,7 +36,7 @@ static void init_color(struct repository *r, int use_color,
3636
free(key);
3737
}
3838

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)
4040
{
4141
const char *value;
4242
enum git_colorbool ret;
@@ -55,7 +55,7 @@ static int check_color_config(struct repository *r, const char *var)
5555
!repo_config_get_value(r, "color.ui", &value))
5656
ret = git_config_colorbool("color.ui", value);
5757

58-
return want_color(ret);
58+
return ret;
5959
}
6060

6161
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,
7676
init_color(r, s->use_color_interactive, "interactive.error",
7777
s->error_color, GIT_COLOR_BOLD_RED);
7878
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);
8080

8181
s->use_color_diff = check_color_config(r, "color.diff");
8282

@@ -93,7 +93,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
9393
init_color(r, s->use_color_diff, "diff.new", s->file_new_color,
9494
diff_get_color(s->use_color_diff, DIFF_FILE_NEW));
9595
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);
9797

9898
FREE_AND_NULL(s->interactive_diff_filter);
9999
repo_config_get_string(r, "interactive.difffilter",
@@ -1211,7 +1211,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps,
12111211
* When color was asked for, use the prompt color for
12121212
* highlighting, otherwise use square brackets.
12131213
*/
1214-
if (s.use_color_interactive) {
1214+
if (want_color(s.use_color_interactive)) {
12151215
data.color = s.prompt_color;
12161216
data.reset = s.reset_color_interactive;
12171217
}

add-interactive.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ struct add_p_opt {
1212

1313
struct add_i_state {
1414
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;
1717
char header_color[COLOR_MAXLEN];
1818
char help_color[COLOR_MAXLEN];
1919
char prompt_color[COLOR_MAXLEN];

0 commit comments

Comments
 (0)