Git fork

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>

authored by

Jeff King and committed by
Junio C Hamano
e9330ae4 5e9ddd3c

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