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