]> git.ipfire.org Git - thirdparty/git.git/commitdiff
color: use git_colorbool enum type to store colorbools
authorJeff King <peff@peff.net>
Tue, 16 Sep 2025 23:13:59 +0000 (19:13 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 17 Sep 2025 00:59:53 +0000 (17:59 -0700)
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>
23 files changed:
add-interactive.c
advice.c
builtin/branch.c
builtin/clean.c
builtin/commit.c
builtin/config.c
builtin/push.c
builtin/show-branch.c
color.c
color.h
combine-diff.c
diff.c
diff.h
grep.h
log-tree.c
log-tree.h
parse-options-cb.c
pretty.c
pretty.h
ref-filter.h
sideband.c
transport.c
wt-status.h

index 34c020673e0a8909acb05c4dfba7b11926b5c696..000315971eda13982d9e42a901bbc848c112d4d5 100644 (file)
@@ -39,7 +39,7 @@ static void init_color(struct repository *r, int use_color,
 static int check_color_config(struct repository *r, const char *var)
 {
        const char *value;
-       int ret;
+       enum git_colorbool ret;
 
        if (repo_config_get_value(r, var, &value))
                ret = GIT_COLOR_UNKNOWN;
index a00aaad9deb5581ac2cda57d2a26c63e4cf075d0..0018501b7bc103267ac32c94f8869944a921d3de 100644 (file)
--- a/advice.c
+++ b/advice.c
@@ -7,7 +7,7 @@
 #include "help.h"
 #include "string-list.h"
 
-static int advice_use_color = GIT_COLOR_UNKNOWN;
+static enum git_colorbool advice_use_color = GIT_COLOR_UNKNOWN;
 static char advice_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RESET,
        GIT_COLOR_YELLOW,       /* HINT */
index 029223df7b7a7ee0a12539453a3f251c3a0820a9..9fcf04bebb2e72013d3d2b0747d14ff1300d13f1 100644 (file)
@@ -46,7 +46,7 @@ static struct object_id head_oid;
 static int recurse_submodules = 0;
 static int submodule_propagate_branches = 0;
 
-static int branch_use_color = GIT_COLOR_UNKNOWN;
+static enum git_colorbool branch_use_color = GIT_COLOR_UNKNOWN;
 static char branch_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RESET,
        GIT_COLOR_NORMAL,       /* PLAIN */
index 8e3598d03000e71ccd43174ae1034e450c493ff1..f10d984f60570a19613eaaf239551373bd0fac64 100644 (file)
@@ -64,7 +64,7 @@ static const char *color_interactive_slots[] = {
        [CLEAN_COLOR_RESET]  = "reset",
 };
 
-static int clean_use_color = GIT_COLOR_UNKNOWN;
+static enum git_colorbool clean_use_color = GIT_COLOR_UNKNOWN;
 static char clean_colors[][COLOR_MAXLEN] = {
        [CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
        [CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,
index 6c5784646a29c88434b072219d1fcc1cae6530d6..8d40bf86194c587e27fb66d93aa28c279183a27e 100644 (file)
@@ -936,7 +936,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
        strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
        if (use_editor && include_status) {
                int ident_shown = 0;
-               int saved_color_setting;
+               enum git_colorbool saved_color_setting;
                struct ident_split ci, ai;
                const char *hint_cleanup_all = allow_empty_message ?
                        _("Please enter the commit message for your changes."
index c3da3ae21022df2e6bc8ec439c173b260976ca87..9e4e4eb2f18fbe6c394a9a166a1c0dee67193192 100644 (file)
@@ -568,9 +568,9 @@ static void get_color(const struct config_location_options *opts,
 }
 
 struct get_colorbool_config_data {
-       int get_colorbool_found;
-       int get_diff_color_found;
-       int get_color_ui_found;
+       enum git_colorbool get_colorbool_found;
+       enum git_colorbool get_diff_color_found;
+       enum git_colorbool get_color_ui_found;
        const char *get_colorbool_slot;
 };
 
index 0962b122c70ca5ad582faeeb08a2b383f3d077d3..5b6cebbb856cfc569079ad70f6825c76ea8e3cba 100644 (file)
@@ -27,7 +27,7 @@ static const char * const push_usage[] = {
        NULL,
 };
 
-static int push_use_color = GIT_COLOR_UNKNOWN;
+static enum git_colorbool push_use_color = GIT_COLOR_UNKNOWN;
 static char push_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RESET,
        GIT_COLOR_RED,  /* ERROR */
index 970e78bc2d133e4da36f70a253f2d1101b8cfc5f..441babf2e350f94477422e9d1381c7e2b21dd939 100644 (file)
@@ -29,7 +29,7 @@ static const char*const show_branch_usage[] = {
     NULL
 };
 
-static int showbranch_use_color = GIT_COLOR_UNKNOWN;
+static enum git_colorbool showbranch_use_color = GIT_COLOR_UNKNOWN;
 
 static struct strvec default_args = STRVEC_INIT;
 
diff --git a/color.c b/color.c
index f3adce01415b1f36704d6c5adf26506322f41881..3348ead534c73d827ff995cc3dc688c94c3a8fd5 100644 (file)
--- a/color.c
+++ b/color.c
@@ -9,7 +9,7 @@
 #include "pager.h"
 #include "strbuf.h"
 
-static int git_use_color_default = GIT_COLOR_AUTO;
+static enum git_colorbool git_use_color_default = GIT_COLOR_AUTO;
 int color_stdout_is_tty = -1;
 
 /*
@@ -404,7 +404,7 @@ static int check_auto_color(int fd)
        return 0;
 }
 
-int want_color_fd(int fd, int var)
+int want_color_fd(int fd, enum git_colorbool var)
 {
        /*
         * NEEDSWORK: This function is sometimes used from multiple threads, and
diff --git a/color.h b/color.h
index 303e2c9a6dcf2a9bdb28229cde2e521717f9a2aa..fcb38c5562f3e282f5cfddf940c3a0917d71f311 100644 (file)
--- a/color.h
+++ b/color.h
@@ -106,7 +106,7 @@ enum git_colorbool git_config_colorbool(const char *var, const char *value);
  * Return a boolean whether to use color, where the argument 'var' is
  * one of GIT_COLOR_UNKNOWN, GIT_COLOR_NEVER, GIT_COLOR_ALWAYS, GIT_COLOR_AUTO.
  */
-int want_color_fd(int fd, int var);
+int want_color_fd(int fd, enum git_colorbool var);
 #define want_color(colorbool) want_color_fd(1, (colorbool))
 #define want_color_stderr(colorbool) want_color_fd(2, (colorbool))
 
index 4ea2dc93c4f8075042641eeb8b57c131a4313571..9b4deeebeb09ea05dcfaa44459e15339482b4a41 100644 (file)
@@ -749,7 +749,7 @@ static void show_line_to_eol(const char *line, int len, const char *reset)
 
 static void dump_sline(struct sline *sline, const char *line_prefix,
                       unsigned long cnt, int num_parent,
-                      int use_color, int result_deleted)
+                      enum git_colorbool use_color, int result_deleted)
 {
        unsigned long mark = (1UL<<num_parent);
        unsigned long no_pre_delete = (2UL<<num_parent);
diff --git a/diff.c b/diff.c
index 3544be2318ca664f7c1e583b2b5a194099dd4615..5ee63a28902a64ac7e50f249d49001219ee3aede 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -57,7 +57,7 @@ static int diff_detect_rename_default;
 static int diff_indent_heuristic = 1;
 static int diff_rename_limit_default = 1000;
 static int diff_suppress_blank_empty;
-static int diff_use_color_default = GIT_COLOR_UNKNOWN;
+static enum git_colorbool diff_use_color_default = GIT_COLOR_UNKNOWN;
 static int diff_color_moved_default;
 static int diff_color_moved_ws_default;
 static int diff_context_default = 3;
@@ -2309,7 +2309,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
        }
 }
 
-const char *diff_get_color(int diff_use_color, enum color_diff ix)
+const char *diff_get_color(enum git_colorbool diff_use_color, enum color_diff ix)
 {
        if (want_color(diff_use_color))
                return diff_colors[ix];
@@ -4481,7 +4481,7 @@ static void fill_metainfo(struct strbuf *msg,
                          struct diff_options *o,
                          struct diff_filepair *p,
                          int *must_show_header,
-                         int use_color)
+                         enum git_colorbool use_color)
 {
        const char *set = diff_get_color(use_color, DIFF_METAINFO);
        const char *reset = diff_get_color(use_color, DIFF_RESET);
diff --git a/diff.h b/diff.h
index 62e5768a9a379e4f319937e2fc8608a9c2cd3675..8c345de7c8c8dc966748849447cd6aed2bb13644 100644 (file)
--- a/diff.h
+++ b/diff.h
@@ -7,6 +7,7 @@
 #include "hash.h"
 #include "pathspec.h"
 #include "strbuf.h"
+#include "color.h"
 
 struct oidset;
 
@@ -283,7 +284,7 @@ struct diff_options {
        /* diff-filter bits */
        unsigned int filter, filter_not;
 
-       int use_color;
+       enum git_colorbool use_color;
 
        /* Number of context lines to generate in patch output. */
        int context;
@@ -459,7 +460,7 @@ enum color_diff {
        DIFF_FILE_NEW_BOLD = 22,
 };
 
-const char *diff_get_color(int diff_use_color, enum color_diff ix);
+const char *diff_get_color(enum git_colorbool diff_use_color, enum color_diff ix);
 #define diff_get_color_opt(o, ix) \
        diff_get_color((o)->use_color, ix)
 
diff --git a/grep.h b/grep.h
index 43195baab38ec3aaee6e49b3b2134847db55f096..13e26a93189c3b28fc8acfb3aa920618650d212f 100644 (file)
--- a/grep.h
+++ b/grep.h
@@ -159,7 +159,7 @@ struct grep_opt {
        int pathname;
        int null_following_name;
        int only_matching;
-       int color;
+       enum git_colorbool color;
        int max_depth;
        int funcname;
        int funcbody;
index 233bf9f227c61b09bbec910d594f7bcb3c9ff02d..a2cd5c587b209c9c19973fd0011b469e7c0711ed 100644 (file)
@@ -57,7 +57,7 @@ static const char *color_decorate_slots[] = {
        [DECORATION_GRAFTED]    = "grafted",
 };
 
-static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
+static const char *decorate_get_color(enum git_colorbool decorate_use_color, enum decoration_type ix)
 {
        if (want_color(decorate_use_color))
                return decoration_colors[ix];
@@ -341,7 +341,7 @@ static void show_name(struct strbuf *sb, const struct name_decoration *decoratio
  */
 void format_decorations(struct strbuf *sb,
                        const struct commit *commit,
-                       int use_color,
+                       enum git_colorbool use_color,
                        const struct decoration_options *opts)
 {
        const struct name_decoration *decoration;
index ebe491c543cfab55fa03aaa382501f72a2b0119e..07924be8bcea5e47956ba1144df35452a7ea04ca 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef LOG_TREE_H
 #define LOG_TREE_H
 
+#include "color.h"
+
 struct rev_info;
 
 struct log_info {
@@ -26,7 +28,7 @@ int log_tree_diff_flush(struct rev_info *);
 int log_tree_commit(struct rev_info *, struct commit *);
 void show_log(struct rev_info *opt);
 void format_decorations(struct strbuf *sb, const struct commit *commit,
-                       int use_color, const struct decoration_options *opts);
+                       enum git_colorbool use_color, const struct decoration_options *opts);
 void show_decorations(struct rev_info *opt, struct commit *commit);
 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
                             char **extra_headers_p,
index e13e0a9e33c0b91240aa2eca2fd9a1a15c2a2c83..976cc863851fac39778940325d9cc034f48f401e 100644 (file)
@@ -50,7 +50,7 @@ int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
                            int unset)
 {
-       int value;
+       enum git_colorbool value;
 
        if (!arg)
                arg = unset ? "never" : (const char *)opt->defval;
index 86d69bf8772de3e5a66a8f1038acc81dd0aa7b92..e0646bbc5d49cc6b792c4e826b4022282ac7179b 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -470,7 +470,7 @@ static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
 
 static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
                                   const char *line, size_t linelen,
-                                  int color, enum grep_context ctx,
+                                  enum git_colorbool color, enum grep_context ctx,
                                   enum grep_header_field field)
 {
        const char *buf, *eol, *line_color, *match_color;
@@ -899,7 +899,7 @@ struct format_commit_context {
        const char *message;
        char *commit_encoding;
        size_t width, indent1, indent2;
-       int auto_color;
+       enum git_colorbool auto_color;
        int padding;
 
        /* 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)
 }
 
 static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt,
-                                int color, int tabwidth, const char *line,
+                                enum git_colorbool color, int tabwidth, const char *line,
                                 int linelen)
 {
        const char *tab;
index df267afe4a8541d91609ec4cfe91c6fb004168ba..fac699033e943cde998329e8d8b917e71ad8075d 100644 (file)
--- a/pretty.h
+++ b/pretty.h
@@ -3,6 +3,7 @@
 
 #include "date.h"
 #include "string-list.h"
+#include "color.h"
 
 struct commit;
 struct repository;
@@ -46,7 +47,7 @@ struct pretty_print_context {
        struct rev_info *rev;
        const char *output_encoding;
        struct string_list *mailmap;
-       int color;
+       enum git_colorbool color;
        struct ident_split *from_ident;
        unsigned encode_email_headers:1;
        struct pretty_print_describe_status *describe_status;
index 644f5c567c9e3529fbe099b4851bb8ccb30b53e5..81f2c229a981dc0d073c2e96b1ed40bda0085641 100644 (file)
@@ -95,7 +95,7 @@ struct ref_format {
        const char *format;
        const char *rest;
        int quote_style;
-       int use_color;
+       enum git_colorbool use_color;
 
        /* Internal state to ref-filter */
        int need_color_reset_at_eol;
index 3ac87148b9361eac65865a790801285e8cd7a6de..ea7c25211ef7e1c8a80cb60475ee43457c8c13f1 100644 (file)
@@ -27,9 +27,9 @@ static struct keyword_entry keywords[] = {
 };
 
 /* Returns a color setting (GIT_COLOR_NEVER, etc). */
-static int use_sideband_colors(void)
+static enum git_colorbool use_sideband_colors(void)
 {
-       static int use_sideband_colors_cached = GIT_COLOR_UNKNOWN;
+       static enum git_colorbool use_sideband_colors_cached = GIT_COLOR_UNKNOWN;
 
        const char *key = "color.remote";
        struct strbuf sb = STRBUF_INIT;
index 4f54ef1b1267c202126c427a6833bc514f557f66..961f26a9a6e5c939008d3e7ce274de0562a5efa7 100644 (file)
@@ -30,7 +30,7 @@
 #include "color.h"
 #include "bundle-uri.h"
 
-static int transport_use_color = GIT_COLOR_UNKNOWN;
+static enum git_colorbool transport_use_color = GIT_COLOR_UNKNOWN;
 static char transport_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_RESET,
        GIT_COLOR_RED           /* REJECTED */
index 4e377ce62b8b2871bb73cf900acff0a9bab40c6c..e40a27214a700d8a3d62ee67ee8ad5ec461887e1 100644 (file)
@@ -111,7 +111,7 @@ struct wt_status {
        int amend;
        enum commit_whence whence;
        int nowarn;
-       int use_color;
+       enum git_colorbool use_color;
        int no_gettext;
        int display_comment_prefix;
        int relative_paths;