Skip to content

Commit a89fa2f

Browse files
committed
Merge branch 'jk/color-variable-fixes'
Some places in the code confused a variable that is *not* a boolean to enable color but is an enum that records what the user requested to do about color. A couple of bugs of this sort have been fixed, while the code has been cleaned up to prevent similar bugs in the future. * jk/color-variable-fixes: config: store want_color() result in a separate bool add-interactive: retain colorbool values longer color: return bool from want_color() color: use git_colorbool enum type to store colorbools pretty: use format_commit_context.auto_color as colorbool diff: stop passing ecbdata->use_color as boolean diff: pass o->use_color directly to fill_metainfo() diff: don't use diff_options.use_color as a strict bool diff: simplify color_moved check when flushing grep: don't treat grep_opt.color as a strict bool color: return enum from git_config_colorbool() color: use GIT_COLOR_* instead of numeric constants
2 parents a5d4779 + 69a7e8d commit a89fa2f

30 files changed

+116
-111
lines changed

add-interactive.c

Lines changed: 13 additions & 12 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,13 +36,13 @@ 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;
42-
int ret;
42+
enum git_colorbool ret;
4343

4444
if (repo_config_get_value(r, var, &value))
45-
ret = -1;
45+
ret = GIT_COLOR_UNKNOWN;
4646
else
4747
ret = git_config_colorbool(var, value);
4848

@@ -51,10 +51,11 @@ static int check_color_config(struct repository *r, const char *var)
5151
* the value parsed by git_color_config(), which may not have been
5252
* called by the main command.
5353
*/
54-
if (ret < 0 && !repo_config_get_value(r, "color.ui", &value))
54+
if (ret == GIT_COLOR_UNKNOWN &&
55+
!repo_config_get_value(r, "color.ui", &value))
5556
ret = git_config_colorbool("color.ui", value);
5657

57-
return want_color(ret);
58+
return ret;
5859
}
5960

6061
void init_add_i_state(struct add_i_state *s, struct repository *r,
@@ -75,7 +76,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
7576
init_color(r, s->use_color_interactive, "interactive.error",
7677
s->error_color, GIT_COLOR_BOLD_RED);
7778
strlcpy(s->reset_color_interactive,
78-
s->use_color_interactive ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
79+
want_color(s->use_color_interactive) ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
7980

8081
s->use_color_diff = check_color_config(r, "color.diff");
8182

@@ -92,7 +93,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
9293
init_color(r, s->use_color_diff, "diff.new", s->file_new_color,
9394
diff_get_color(s->use_color_diff, DIFF_FILE_NEW));
9495
strlcpy(s->reset_color_diff,
95-
s->use_color_diff ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
96+
want_color(s->use_color_diff) ? GIT_COLOR_RESET : "", COLOR_MAXLEN);
9697

9798
FREE_AND_NULL(s->interactive_diff_filter);
9899
repo_config_get_string(r, "interactive.difffilter",
@@ -130,8 +131,8 @@ void clear_add_i_state(struct add_i_state *s)
130131
FREE_AND_NULL(s->interactive_diff_filter);
131132
FREE_AND_NULL(s->interactive_diff_algorithm);
132133
memset(s, 0, sizeof(*s));
133-
s->use_color_interactive = -1;
134-
s->use_color_diff = -1;
134+
s->use_color_interactive = GIT_COLOR_UNKNOWN;
135+
s->use_color_diff = GIT_COLOR_UNKNOWN;
135136
}
136137

137138
/*
@@ -1210,7 +1211,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps,
12101211
* When color was asked for, use the prompt color for
12111212
* highlighting, otherwise use square brackets.
12121213
*/
1213-
if (s.use_color_interactive) {
1214+
if (want_color(s.use_color_interactive)) {
12141215
data.color = s.prompt_color;
12151216
data.reset = s.reset_color_interactive;
12161217
}

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];

advice.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "help.h"
88
#include "string-list.h"
99

10-
static int advice_use_color = -1;
10+
static enum git_colorbool advice_use_color = GIT_COLOR_UNKNOWN;
1111
static char advice_colors[][COLOR_MAXLEN] = {
1212
GIT_COLOR_RESET,
1313
GIT_COLOR_YELLOW, /* HINT */

builtin/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static int edit_patch(struct repository *repo,
200200

201201
argc = setup_revisions(argc, argv, &rev, NULL);
202202
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
203-
rev.diffopt.use_color = 0;
203+
rev.diffopt.use_color = GIT_COLOR_NEVER;
204204
rev.diffopt.flags.ignore_dirty_submodules = 1;
205205
out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
206206
rev.diffopt.file = xfdopen(out, "w");

builtin/am.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ static void write_commit_patch(const struct am_state *state, struct commit *comm
14081408
rev_info.no_commit_id = 1;
14091409
rev_info.diffopt.flags.binary = 1;
14101410
rev_info.diffopt.flags.full_index = 1;
1411-
rev_info.diffopt.use_color = 0;
1411+
rev_info.diffopt.use_color = GIT_COLOR_NEVER;
14121412
rev_info.diffopt.file = fp;
14131413
rev_info.diffopt.close_file = 1;
14141414
add_pending_object(&rev_info, &commit->object, "");
@@ -1441,7 +1441,7 @@ static void write_index_patch(const struct am_state *state)
14411441
rev_info.disable_stdin = 1;
14421442
rev_info.no_commit_id = 1;
14431443
rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
1444-
rev_info.diffopt.use_color = 0;
1444+
rev_info.diffopt.use_color = GIT_COLOR_NEVER;
14451445
rev_info.diffopt.file = fp;
14461446
rev_info.diffopt.close_file = 1;
14471447
add_pending_object(&rev_info, &tree->object, "");

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static struct object_id head_oid;
4646
static int recurse_submodules = 0;
4747
static int submodule_propagate_branches = 0;
4848

49-
static int branch_use_color = -1;
49+
static enum git_colorbool branch_use_color = GIT_COLOR_UNKNOWN;
5050
static char branch_colors[][COLOR_MAXLEN] = {
5151
GIT_COLOR_RESET,
5252
GIT_COLOR_NORMAL, /* PLAIN */

builtin/clean.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static const char *color_interactive_slots[] = {
6464
[CLEAN_COLOR_RESET] = "reset",
6565
};
6666

67-
static int clean_use_color = -1;
67+
static enum git_colorbool clean_use_color = GIT_COLOR_UNKNOWN;
6868
static char clean_colors[][COLOR_MAXLEN] = {
6969
[CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
7070
[CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
940940
strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
941941
if (use_editor && include_status) {
942942
int ident_shown = 0;
943-
int saved_color_setting;
943+
enum git_colorbool saved_color_setting;
944944
struct ident_split ci, ai;
945945
const char *hint_cleanup_all = allow_empty_message ?
946946
_("Please enter the commit message for your changes."
@@ -1020,7 +1020,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10201020
status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
10211021

10221022
saved_color_setting = s->use_color;
1023-
s->use_color = 0;
1023+
s->use_color = GIT_COLOR_NEVER;
10241024
committable = run_status(s->fp, index_file, prefix, 1, s);
10251025
s->use_color = saved_color_setting;
10261026
string_list_clear_func(&s->change, change_data_free);

builtin/config.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,9 @@ static void get_color(const struct config_location_options *opts,
568568
}
569569

570570
struct get_colorbool_config_data {
571-
int get_colorbool_found;
572-
int get_diff_color_found;
573-
int get_color_ui_found;
571+
enum git_colorbool get_colorbool_found;
572+
enum git_colorbool get_diff_color_found;
573+
enum git_colorbool get_color_ui_found;
574574
const char *get_colorbool_slot;
575575
};
576576

@@ -594,33 +594,34 @@ static int get_colorbool(const struct config_location_options *opts,
594594
{
595595
struct get_colorbool_config_data data = {
596596
.get_colorbool_slot = var,
597-
.get_colorbool_found = -1,
598-
.get_diff_color_found = -1,
599-
.get_color_ui_found = -1,
597+
.get_colorbool_found = GIT_COLOR_UNKNOWN,
598+
.get_diff_color_found = GIT_COLOR_UNKNOWN,
599+
.get_color_ui_found = GIT_COLOR_UNKNOWN,
600600
};
601+
bool result;
601602

602603
config_with_options(git_get_colorbool_config, &data,
603604
&opts->source, the_repository,
604605
&opts->options);
605606

606-
if (data.get_colorbool_found < 0) {
607+
if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) {
607608
if (!strcmp(data.get_colorbool_slot, "color.diff"))
608609
data.get_colorbool_found = data.get_diff_color_found;
609-
if (data.get_colorbool_found < 0)
610+
if (data.get_colorbool_found == GIT_COLOR_UNKNOWN)
610611
data.get_colorbool_found = data.get_color_ui_found;
611612
}
612613

613-
if (data.get_colorbool_found < 0)
614+
if (data.get_colorbool_found == GIT_COLOR_UNKNOWN)
614615
/* default value if none found in config */
615616
data.get_colorbool_found = GIT_COLOR_AUTO;
616617

617-
data.get_colorbool_found = want_color(data.get_colorbool_found);
618+
result = want_color(data.get_colorbool_found);
618619

619620
if (print) {
620-
printf("%s\n", data.get_colorbool_found ? "true" : "false");
621+
printf("%s\n", result ? "true" : "false");
621622
return 0;
622623
} else
623-
return data.get_colorbool_found ? 0 : 1;
624+
return result ? 0 : 1;
624625
}
625626

626627
static void check_write(const struct git_config_source *source)

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ int cmd_grep(int argc,
10911091
if (show_in_pager == default_pager)
10921092
show_in_pager = git_pager(the_repository, 1);
10931093
if (show_in_pager) {
1094-
opt.color = 0;
1094+
opt.color = GIT_COLOR_NEVER;
10951095
opt.name_only = 1;
10961096
opt.null_following_name = 1;
10971097
opt.output_priv = &path_list;

0 commit comments

Comments
 (0)