color: use git_colorbool enum type to store colorbools

We traditionally used "int" to store and pass around the values defined by "enum git_colorbool" (which were originally just #define macros). Using an int doesn't produce incorrect results, but using the actual enum makes the intent of the code more clear. It would be nice if the compiler could catch cases where we used the enum and an int interchangeably, since it's very easy to accidentally check the boolean true/false of a colorbool like: if (branch_use_color) This is wrong because GIT_COLOR_UNKNOWN and GIT_COLOR_AUTO evaluate to true in C, even though we may ultimately decide not to use color. But C is pretty happy to convert between ints and enums (even with various -Wenum-* warnings). So this sadly doesn't protect us from such mistakes, but it hopefully does make the code easier to read. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 16, 2025 at 19:13 UTC e9330ae4b820147c98e723399e9438c8bee60a80
23 files changed +37 -33
add-interactive.c
+1 -1
@@ -39,7 +39,7 @@ static void init_color(struct repository *r, int use_color,
39 static int check_color_config(struct repository *r, const char *var)
40 {
41 const char *value;
42 - int ret;
42 + enum git_colorbool ret;
43
44 if (repo_config_get_value(r, var, &value))
45 ret = GIT_COLOR_UNKNOWN;
advice.c
+1 -1
@@ -7,7 +7,7 @@
7 #include "help.h"
8 #include "string-list.h"
9
10 -static int advice_use_color = GIT_COLOR_UNKNOWN;
10 +static enum git_colorbool advice_use_color = GIT_COLOR_UNKNOWN;
11 static char advice_colors[][COLOR_MAXLEN] = {
12 GIT_COLOR_RESET,
13 GIT_COLOR_YELLOW, /* HINT */
builtin/branch.c
+1 -1
@@ -46,7 +46,7 @@ static struct object_id head_oid;
46 static int recurse_submodules = 0;
47 static int submodule_propagate_branches = 0;
48
49 -static int branch_use_color = GIT_COLOR_UNKNOWN;
49 +static enum git_colorbool branch_use_color = GIT_COLOR_UNKNOWN;
50 static char branch_colors[][COLOR_MAXLEN] = {
51 GIT_COLOR_RESET,
52 GIT_COLOR_NORMAL, /* PLAIN */
builtin/clean.c
+1 -1
@@ -64,7 +64,7 @@ static const char *color_interactive_slots[] = {
64 [CLEAN_COLOR_RESET] = "reset",
65 };
66
67 -static int clean_use_color = GIT_COLOR_UNKNOWN;
67 +static enum git_colorbool clean_use_color = GIT_COLOR_UNKNOWN;
68 static char clean_colors[][COLOR_MAXLEN] = {
69 [CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
70 [CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,
builtin/commit.c
+1 -1
@@ -936,7 +936,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
936 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
937 if (use_editor && include_status) {
938 int ident_shown = 0;
939 - int saved_color_setting;
939 + enum git_colorbool saved_color_setting;
940 struct ident_split ci, ai;
941 const char *hint_cleanup_all = allow_empty_message ?
942 _("Please enter the commit message for your changes."
builtin/config.c
+3 -3
@@ -568,9 +568,9 @@ static void get_color(const struct config_location_options *opts,
568 }
569
570 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;
574 const char *get_colorbool_slot;
575 };
576
builtin/push.c
+1 -1
@@ -27,7 +27,7 @@ static const char * const push_usage[] = {
27 NULL,
28 };
29
30 -static int push_use_color = GIT_COLOR_UNKNOWN;
30 +static enum git_colorbool push_use_color = GIT_COLOR_UNKNOWN;
31 static char push_colors[][COLOR_MAXLEN] = {
32 GIT_COLOR_RESET,
33 GIT_COLOR_RED, /* ERROR */
builtin/show-branch.c
+1 -1
@@ -29,7 +29,7 @@ static const char*const show_branch_usage[] = {
29 NULL
30 };
31
32 -static int showbranch_use_color = GIT_COLOR_UNKNOWN;
32 +static enum git_colorbool showbranch_use_color = GIT_COLOR_UNKNOWN;
33
34 static struct strvec default_args = STRVEC_INIT;
35
color.c
+2 -2
@@ -9,7 +9,7 @@
9 #include "pager.h"
10 #include "strbuf.h"
11
12 -static int git_use_color_default = GIT_COLOR_AUTO;
12 +static enum git_colorbool git_use_color_default = GIT_COLOR_AUTO;
13 int color_stdout_is_tty = -1;
14
15 /*
@@ -404,7 +404,7 @@ static int check_auto_color(int fd)
404 return 0;
405 }
406
407 -int want_color_fd(int fd, int var)
407 +int want_color_fd(int fd, enum git_colorbool var)
408 {
409 /*
410 * NEEDSWORK: This function is sometimes used from multiple threads, and
color.h
+1 -1
@@ -106,7 +106,7 @@ enum git_colorbool git_config_colorbool(const char *var, const char *value);
106 * Return a boolean whether to use color, where the argument 'var' is
107 * one of GIT_COLOR_UNKNOWN, GIT_COLOR_NEVER, GIT_COLOR_ALWAYS, GIT_COLOR_AUTO.
108 */
109 -int want_color_fd(int fd, int var);
109 +int want_color_fd(int fd, enum git_colorbool var);
110 #define want_color(colorbool) want_color_fd(1, (colorbool))
111 #define want_color_stderr(colorbool) want_color_fd(2, (colorbool))
112
combine-diff.c
+1 -1
@@ -749,7 +749,7 @@ static void show_line_to_eol(const char *line, int len, const char *reset)
749
750 static void dump_sline(struct sline *sline, const char *line_prefix,
751 unsigned long cnt, int num_parent,
752 - int use_color, int result_deleted)
752 + enum git_colorbool use_color, int result_deleted)
753 {
754 unsigned long mark = (1UL<<num_parent);
755 unsigned long no_pre_delete = (2UL<<num_parent);
diff.c
+3 -3
@@ -57,7 +57,7 @@ static int diff_detect_rename_default;
57 static int diff_indent_heuristic = 1;
58 static int diff_rename_limit_default = 1000;
59 static int diff_suppress_blank_empty;
60 -static int diff_use_color_default = GIT_COLOR_UNKNOWN;
60 +static enum git_colorbool diff_use_color_default = GIT_COLOR_UNKNOWN;
61 static int diff_color_moved_default;
62 static int diff_color_moved_ws_default;
63 static int diff_context_default = 3;
@@ -2309,7 +2309,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
2309 }
2310 }
2311
2312 -const char *diff_get_color(int diff_use_color, enum color_diff ix)
2312 +const char *diff_get_color(enum git_colorbool diff_use_color, enum color_diff ix)
2313 {
2314 if (want_color(diff_use_color))
2315 return diff_colors[ix];
@@ -4481,7 +4481,7 @@ static void fill_metainfo(struct strbuf *msg,
4481 struct diff_options *o,
4482 struct diff_filepair *p,
4483 int *must_show_header,
4484 - int use_color)
4484 + enum git_colorbool use_color)
4485 {
4486 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4487 const char *reset = diff_get_color(use_color, DIFF_RESET);
diff.h
+3 -2
@@ -7,6 +7,7 @@
7 #include "hash.h"
8 #include "pathspec.h"
9 #include "strbuf.h"
10 +#include "color.h"
11
12 struct oidset;
13
@@ -283,7 +284,7 @@ struct diff_options {
284 /* diff-filter bits */
285 unsigned int filter, filter_not;
286
286 - int use_color;
287 + enum git_colorbool use_color;
288
289 /* Number of context lines to generate in patch output. */
290 int context;
@@ -459,7 +460,7 @@ enum color_diff {
460 DIFF_FILE_NEW_BOLD = 22,
461 };
462
462 -const char *diff_get_color(int diff_use_color, enum color_diff ix);
463 +const char *diff_get_color(enum git_colorbool diff_use_color, enum color_diff ix);
464 #define diff_get_color_opt(o, ix) \
465 diff_get_color((o)->use_color, ix)
466
grep.h
+1 -1
@@ -159,7 +159,7 @@ struct grep_opt {
159 int pathname;
160 int null_following_name;
161 int only_matching;
162 - int color;
162 + enum git_colorbool color;
163 int max_depth;
164 int funcname;
165 int funcbody;
log-tree.c
+2 -2
@@ -57,7 +57,7 @@ static const char *color_decorate_slots[] = {
57 [DECORATION_GRAFTED] = "grafted",
58 };
59
60 -static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
60 +static const char *decorate_get_color(enum git_colorbool decorate_use_color, enum decoration_type ix)
61 {
62 if (want_color(decorate_use_color))
63 return decoration_colors[ix];
@@ -341,7 +341,7 @@ static void show_name(struct strbuf *sb, const struct name_decoration *decoratio
341 */
342 void format_decorations(struct strbuf *sb,
343 const struct commit *commit,
344 - int use_color,
344 + enum git_colorbool use_color,
345 const struct decoration_options *opts)
346 {
347 const struct name_decoration *decoration;
log-tree.h
+3 -1
@@ -1,6 +1,8 @@
1 #ifndef LOG_TREE_H
2 #define LOG_TREE_H
3
4 +#include "color.h"
5 +
6 struct rev_info;
7
8 struct log_info {
@@ -26,7 +28,7 @@ int log_tree_diff_flush(struct rev_info *);
28 int log_tree_commit(struct rev_info *, struct commit *);
29 void show_log(struct rev_info *opt);
30 void format_decorations(struct strbuf *sb, const struct commit *commit,
29 - int use_color, const struct decoration_options *opts);
31 + enum git_colorbool use_color, const struct decoration_options *opts);
32 void show_decorations(struct rev_info *opt, struct commit *commit);
33 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
34 char **extra_headers_p,
parse-options-cb.c
+1 -1
@@ -50,7 +50,7 @@ int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
50 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
51 int unset)
52 {
53 - int value;
53 + enum git_colorbool value;
54
55 if (!arg)
56 arg = unset ? "never" : (const char *)opt->defval;
pretty.c
+3 -3
@@ -470,7 +470,7 @@ static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
470
471 static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
472 const char *line, size_t linelen,
473 - int color, enum grep_context ctx,
473 + enum git_colorbool color, enum grep_context ctx,
474 enum grep_header_field field)
475 {
476 const char *buf, *eol, *line_color, *match_color;
@@ -899,7 +899,7 @@ struct format_commit_context {
899 const char *message;
900 char *commit_encoding;
901 size_t width, indent1, indent2;
902 - int auto_color;
902 + enum git_colorbool auto_color;
903 int padding;
904
905 /* These offsets are relative to the start of the commit message. */
@@ -2167,7 +2167,7 @@ static int pp_utf8_width(const char *start, const char *end)
2167 }
2168
2169 static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt,
2170 - int color, int tabwidth, const char *line,
2170 + enum git_colorbool color, int tabwidth, const char *line,
2171 int linelen)
2172 {
2173 const char *tab;
pretty.h
+2 -1
@@ -3,6 +3,7 @@
3
4 #include "date.h"
5 #include "string-list.h"
6 +#include "color.h"
7
8 struct commit;
9 struct repository;
@@ -46,7 +47,7 @@ struct pretty_print_context {
47 struct rev_info *rev;
48 const char *output_encoding;
49 struct string_list *mailmap;
49 - int color;
50 + enum git_colorbool color;
51 struct ident_split *from_ident;
52 unsigned encode_email_headers:1;
53 struct pretty_print_describe_status *describe_status;
ref-filter.h
+1 -1
@@ -95,7 +95,7 @@ struct ref_format {
95 const char *format;
96 const char *rest;
97 int quote_style;
98 - int use_color;
98 + enum git_colorbool use_color;
99
100 /* Internal state to ref-filter */
101 int need_color_reset_at_eol;
sideband.c
+2 -2
@@ -27,9 +27,9 @@ static struct keyword_entry keywords[] = {
27 };
28
29 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
30 -static int use_sideband_colors(void)
30 +static enum git_colorbool use_sideband_colors(void)
31 {
32 - static int use_sideband_colors_cached = GIT_COLOR_UNKNOWN;
32 + static enum git_colorbool use_sideband_colors_cached = GIT_COLOR_UNKNOWN;
33
34 const char *key = "color.remote";
35 struct strbuf sb = STRBUF_INIT;
transport.c
+1 -1
@@ -30,7 +30,7 @@
30 #include "color.h"
31 #include "bundle-uri.h"
32
33 -static int transport_use_color = GIT_COLOR_UNKNOWN;
33 +static enum git_colorbool transport_use_color = GIT_COLOR_UNKNOWN;
34 static char transport_colors[][COLOR_MAXLEN] = {
35 GIT_COLOR_RESET,
36 GIT_COLOR_RED /* REJECTED */
wt-status.h
+1 -1
@@ -111,7 +111,7 @@ struct wt_status {
111 int amend;
112 enum commit_whence whence;
113 int nowarn;
114 - int use_color;
114 + enum git_colorbool use_color;
115 int no_gettext;
116 int display_comment_prefix;
117 int relative_paths;