Skip to content

Commit e9330ae

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e9ddd3 commit e9330ae

23 files changed

+37
-33
lines changed

add-interactive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void init_color(struct repository *r, int use_color,
3939
static int check_color_config(struct repository *r, const char *var)
4040
{
4141
const char *value;
42-
int ret;
42+
enum git_colorbool ret;
4343

4444
if (repo_config_get_value(r, var, &value))
4545
ret = GIT_COLOR_UNKNOWN;

advice.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "help.h"
88
#include "string-list.h"
99

10-
static int advice_use_color = GIT_COLOR_UNKNOWN;
10+
static enum git_colorbool advice_use_color = GIT_COLOR_UNKNOWN;
1111
static char advice_colors[][COLOR_MAXLEN] = {
1212
GIT_COLOR_RESET,
1313
GIT_COLOR_YELLOW, /* HINT */

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static struct object_id head_oid;
4646
static int recurse_submodules = 0;
4747
static int submodule_propagate_branches = 0;
4848

49-
static int branch_use_color = GIT_COLOR_UNKNOWN;
49+
static enum git_colorbool branch_use_color = GIT_COLOR_UNKNOWN;
5050
static char branch_colors[][COLOR_MAXLEN] = {
5151
GIT_COLOR_RESET,
5252
GIT_COLOR_NORMAL, /* PLAIN */

builtin/clean.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static const char *color_interactive_slots[] = {
6464
[CLEAN_COLOR_RESET] = "reset",
6565
};
6666

67-
static int clean_use_color = GIT_COLOR_UNKNOWN;
67+
static enum git_colorbool clean_use_color = GIT_COLOR_UNKNOWN;
6868
static char clean_colors[][COLOR_MAXLEN] = {
6969
[CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
7070
[CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
936936
strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
937937
if (use_editor && include_status) {
938938
int ident_shown = 0;
939-
int saved_color_setting;
939+
enum git_colorbool saved_color_setting;
940940
struct ident_split ci, ai;
941941
const char *hint_cleanup_all = allow_empty_message ?
942942
_("Please enter the commit message for your changes."

builtin/config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,9 @@ static void get_color(const struct config_location_options *opts,
568568
}
569569

570570
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;
574574
const char *get_colorbool_slot;
575575
};
576576

builtin/push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static const char * const push_usage[] = {
2727
NULL,
2828
};
2929

30-
static int push_use_color = GIT_COLOR_UNKNOWN;
30+
static enum git_colorbool push_use_color = GIT_COLOR_UNKNOWN;
3131
static char push_colors[][COLOR_MAXLEN] = {
3232
GIT_COLOR_RESET,
3333
GIT_COLOR_RED, /* ERROR */

builtin/show-branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static const char*const show_branch_usage[] = {
2929
NULL
3030
};
3131

32-
static int showbranch_use_color = GIT_COLOR_UNKNOWN;
32+
static enum git_colorbool showbranch_use_color = GIT_COLOR_UNKNOWN;
3333

3434
static struct strvec default_args = STRVEC_INIT;
3535

color.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "pager.h"
1010
#include "strbuf.h"
1111

12-
static int git_use_color_default = GIT_COLOR_AUTO;
12+
static enum git_colorbool git_use_color_default = GIT_COLOR_AUTO;
1313
int color_stdout_is_tty = -1;
1414

1515
/*
@@ -404,7 +404,7 @@ static int check_auto_color(int fd)
404404
return 0;
405405
}
406406

407-
int want_color_fd(int fd, int var)
407+
int want_color_fd(int fd, enum git_colorbool var)
408408
{
409409
/*
410410
* NEEDSWORK: This function is sometimes used from multiple threads, and

color.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ enum git_colorbool git_config_colorbool(const char *var, const char *value);
106106
* Return a boolean whether to use color, where the argument 'var' is
107107
* one of GIT_COLOR_UNKNOWN, GIT_COLOR_NEVER, GIT_COLOR_ALWAYS, GIT_COLOR_AUTO.
108108
*/
109-
int want_color_fd(int fd, int var);
109+
int want_color_fd(int fd, enum git_colorbool var);
110110
#define want_color(colorbool) want_color_fd(1, (colorbool))
111111
#define want_color_stderr(colorbool) want_color_fd(2, (colorbool))
112112

0 commit comments

Comments
 (0)