Git fork

color: use GIT_COLOR_* instead of numeric constants

Long ago Git's decision to show color for a subsytem was stored in a
tri-state variable: it could be true (1), false (0), or unknown (-1).
But since daa0c3d971 (color: delay auto-color decision until point of
use, 2011-08-17) we want to carry around a new state, "auto", which
bases the decision on the tty-ness of stdout (rather than collapsing
that "auto" state to a true/false immediately).

That commit introduced a set of GIT_COLOR_* defines to represent each
state: UNKNOWN, ALWAYS, NEVER, and AUTO. But it only used the AUTO
value, and left alone code using bare 0/1/-1 values. And of course since
then we've grown many new spots that use those bare values.

Let's switch all of these to use the named constants. That should make
the code a bit easier to read, as it is more obvious that we're
representing a color decision.

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
3c3e9b83 e335ff31

+42 -40
+5 -4
add-interactive.c
··· 42 42 int ret; 43 43 44 44 if (repo_config_get_value(r, var, &value)) 45 - ret = -1; 45 + ret = GIT_COLOR_UNKNOWN; 46 46 else 47 47 ret = git_config_colorbool(var, value); 48 48 ··· 51 51 * the value parsed by git_color_config(), which may not have been 52 52 * called by the main command. 53 53 */ 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)) 55 56 ret = git_config_colorbool("color.ui", value); 56 57 57 58 return want_color(ret); ··· 130 131 FREE_AND_NULL(s->interactive_diff_filter); 131 132 FREE_AND_NULL(s->interactive_diff_algorithm); 132 133 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; 135 136 } 136 137 137 138 /*
+1 -1
advice.c
··· 7 7 #include "help.h" 8 8 #include "string-list.h" 9 9 10 - static int advice_use_color = -1; 10 + static int 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/add.c
··· 200 200 201 201 argc = setup_revisions(argc, argv, &rev, NULL); 202 202 rev.diffopt.output_format = DIFF_FORMAT_PATCH; 203 - rev.diffopt.use_color = 0; 203 + rev.diffopt.use_color = GIT_COLOR_NEVER; 204 204 rev.diffopt.flags.ignore_dirty_submodules = 1; 205 205 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666); 206 206 rev.diffopt.file = xfdopen(out, "w");
+2 -2
builtin/am.c
··· 1408 1408 rev_info.no_commit_id = 1; 1409 1409 rev_info.diffopt.flags.binary = 1; 1410 1410 rev_info.diffopt.flags.full_index = 1; 1411 - rev_info.diffopt.use_color = 0; 1411 + rev_info.diffopt.use_color = GIT_COLOR_NEVER; 1412 1412 rev_info.diffopt.file = fp; 1413 1413 rev_info.diffopt.close_file = 1; 1414 1414 add_pending_object(&rev_info, &commit->object, ""); ··· 1441 1441 rev_info.disable_stdin = 1; 1442 1442 rev_info.no_commit_id = 1; 1443 1443 rev_info.diffopt.output_format = DIFF_FORMAT_PATCH; 1444 - rev_info.diffopt.use_color = 0; 1444 + rev_info.diffopt.use_color = GIT_COLOR_NEVER; 1445 1445 rev_info.diffopt.file = fp; 1446 1446 rev_info.diffopt.close_file = 1; 1447 1447 add_pending_object(&rev_info, &tree->object, "");
+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 = -1; 49 + static int 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 = -1; 67 + static int 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
··· 1016 1016 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */ 1017 1017 1018 1018 saved_color_setting = s->use_color; 1019 - s->use_color = 0; 1019 + s->use_color = GIT_COLOR_NEVER; 1020 1020 committable = run_status(s->fp, index_file, prefix, 1, s); 1021 1021 s->use_color = saved_color_setting; 1022 1022 string_list_clear_func(&s->change, change_data_free);
+6 -6
builtin/config.c
··· 594 594 { 595 595 struct get_colorbool_config_data data = { 596 596 .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, 600 600 }; 601 601 602 602 config_with_options(git_get_colorbool_config, &data, 603 603 &opts->source, the_repository, 604 604 &opts->options); 605 605 606 - if (data.get_colorbool_found < 0) { 606 + if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) { 607 607 if (!strcmp(data.get_colorbool_slot, "color.diff")) 608 608 data.get_colorbool_found = data.get_diff_color_found; 609 - if (data.get_colorbool_found < 0) 609 + if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) 610 610 data.get_colorbool_found = data.get_color_ui_found; 611 611 } 612 612 613 - if (data.get_colorbool_found < 0) 613 + if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) 614 614 /* default value if none found in config */ 615 615 data.get_colorbool_found = GIT_COLOR_AUTO; 616 616
+1 -1
builtin/grep.c
··· 1091 1091 if (show_in_pager == default_pager) 1092 1092 show_in_pager = git_pager(the_repository, 1); 1093 1093 if (show_in_pager) { 1094 - opt.color = 0; 1094 + opt.color = GIT_COLOR_NEVER; 1095 1095 opt.name_only = 1; 1096 1096 opt.null_following_name = 1; 1097 1097 opt.output_priv = &path_list;
+1 -1
builtin/push.c
··· 27 27 NULL, 28 28 }; 29 29 30 - static int push_use_color = -1; 30 + static int 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 */
+2 -1
builtin/range-diff.c
··· 6 6 #include "parse-options.h" 7 7 #include "range-diff.h" 8 8 #include "config.h" 9 + #include "color.h" 9 10 10 11 11 12 static const char * const builtin_range_diff_usage[] = { ··· 66 67 67 68 /* force color when --dual-color was used */ 68 69 if (!simple_color) 69 - diffopt.use_color = 1; 70 + diffopt.use_color = GIT_COLOR_ALWAYS; 70 71 71 72 /* If `--diff-merges` was specified, imply `--merges` */ 72 73 if (diff_merges_arg.nr) {
+1 -1
builtin/show-branch.c
··· 29 29 NULL 30 30 }; 31 31 32 - static int showbranch_use_color = -1; 32 + static int showbranch_use_color = GIT_COLOR_UNKNOWN; 33 33 34 34 static struct strvec default_args = STRVEC_INIT; 35 35
+6 -6
color.c
··· 373 373 { 374 374 if (value) { 375 375 if (!strcasecmp(value, "never")) 376 - return 0; 376 + return GIT_COLOR_NEVER; 377 377 if (!strcasecmp(value, "always")) 378 - return 1; 378 + return GIT_COLOR_ALWAYS; 379 379 if (!strcasecmp(value, "auto")) 380 380 return GIT_COLOR_AUTO; 381 381 } 382 382 383 383 if (!var) 384 - return -1; 384 + return GIT_COLOR_UNKNOWN; 385 385 386 386 /* Missing or explicit false to turn off colorization */ 387 387 if (!git_config_bool(var, value)) 388 - return 0; 388 + return GIT_COLOR_NEVER; 389 389 390 390 /* any normal truth value defaults to 'auto' */ 391 391 return GIT_COLOR_AUTO; ··· 418 418 if (fd < 1 || fd >= ARRAY_SIZE(want_auto)) 419 419 BUG("file descriptor out of range: %d", fd); 420 420 421 - if (var < 0) 421 + if (var == GIT_COLOR_UNKNOWN) 422 422 var = git_use_color_default; 423 423 424 424 if (var == GIT_COLOR_AUTO) { ··· 426 426 want_auto[fd] = check_auto_color(fd); 427 427 return want_auto[fd]; 428 428 } 429 - return var; 429 + return var == GIT_COLOR_ALWAYS; 430 430 } 431 431 432 432 int git_color_config(const char *var, const char *value, void *cb UNUSED)
+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 = -1; 60 + static int 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; ··· 5259 5259 struct diff_options *options = opt->value; 5260 5260 5261 5261 BUG_ON_OPT_NEG(unset); 5262 - options->use_color = 1; 5262 + options->use_color = GIT_COLOR_ALWAYS; 5263 5263 options->word_diff = DIFF_WORDS_COLOR; 5264 5264 options->word_regex = arg; 5265 5265 return 0; ··· 5581 5581 if (!strcmp(arg, "plain")) 5582 5582 options->word_diff = DIFF_WORDS_PLAIN; 5583 5583 else if (!strcmp(arg, "color")) { 5584 - options->use_color = 1; 5584 + options->use_color = GIT_COLOR_ALWAYS; 5585 5585 options->word_diff = DIFF_WORDS_COLOR; 5586 5586 } 5587 5587 else if (!strcmp(arg, "porcelain"))
+1 -1
grep.h
··· 198 198 [GREP_COLOR_SEP] = GIT_COLOR_CYAN, \ 199 199 }, \ 200 200 .only_matching = 0, \ 201 - .color = -1, \ 201 + .color = GIT_COLOR_UNKNOWN, \ 202 202 .output = std_output, \ 203 203 } 204 204
+1 -1
parse-options-cb.c
··· 55 55 if (!arg) 56 56 arg = unset ? "never" : (const char *)opt->defval; 57 57 value = git_config_colorbool(NULL, arg); 58 - if (value < 0) 58 + if (value == GIT_COLOR_UNKNOWN) 59 59 return error(_("option `%s' expects \"always\", \"auto\", or \"never\""), 60 60 opt->long_name); 61 61 *(int *)opt->value = value;
+1 -1
pretty.c
··· 1462 1462 } else { 1463 1463 int ret = parse_color(sb, placeholder, c); 1464 1464 if (ret) 1465 - c->auto_color = 0; 1465 + c->auto_color = GIT_COLOR_NEVER; 1466 1466 /* 1467 1467 * Otherwise, we decided to treat %C<unknown> 1468 1468 * as a literal string, and the previous
+1 -1
ref-filter.h
··· 111 111 .exclude = STRVEC_INIT, \ 112 112 } 113 113 #define REF_FORMAT_INIT { \ 114 - .use_color = -1, \ 114 + .use_color = GIT_COLOR_UNKNOWN, \ 115 115 } 116 116 117 117 /* Macros for checking --merged and --no-merged options */
+2 -2
sideband.c
··· 29 29 /* Returns a color setting (GIT_COLOR_NEVER, etc). */ 30 30 static int use_sideband_colors(void) 31 31 { 32 - static int use_sideband_colors_cached = -1; 32 + static int use_sideband_colors_cached = GIT_COLOR_UNKNOWN; 33 33 34 34 const char *key = "color.remote"; 35 35 struct strbuf sb = STRBUF_INIT; 36 36 const char *value; 37 37 int i; 38 38 39 - if (use_sideband_colors_cached >= 0) 39 + if (use_sideband_colors_cached != GIT_COLOR_UNKNOWN) 40 40 return use_sideband_colors_cached; 41 41 42 42 if (!repo_config_get_string_tmp(the_repository, key, &value))
+1 -1
transport.c
··· 30 30 #include "color.h" 31 31 #include "bundle-uri.h" 32 32 33 - static int transport_use_color = -1; 33 + static int 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 */
+3 -3
wt-status.c
··· 148 148 memcpy(s->color_palette, default_wt_status_colors, 149 149 sizeof(default_wt_status_colors)); 150 150 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; 151 - s->use_color = -1; 151 + s->use_color = GIT_COLOR_UNKNOWN; 152 152 s->relative_paths = 1; 153 153 s->branch = refs_resolve_refdup(get_main_ref_store(the_repository), 154 154 "HEAD", 0, NULL, NULL); ··· 1164 1164 * before. 1165 1165 */ 1166 1166 if (s->fp != stdout) { 1167 - rev.diffopt.use_color = 0; 1167 + rev.diffopt.use_color = GIT_COLOR_NEVER; 1168 1168 wt_status_add_cut_line(s); 1169 1169 } 1170 1170 if (s->verbose > 1 && s->committable) { ··· 2164 2164 2165 2165 static void wt_porcelain_print(struct wt_status *s) 2166 2166 { 2167 - s->use_color = 0; 2167 + s->use_color = GIT_COLOR_NEVER; 2168 2168 s->relative_paths = 0; 2169 2169 s->prefix = NULL; 2170 2170 s->no_gettext = 1;