From: Jeff King Date: Tue, 16 Sep 2025 20:25:26 +0000 (-0400) Subject: color: return bool from want_color() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b978f7803400b9f0c54f5874b085064c44a6c372;p=thirdparty%2Fgit.git color: return bool from want_color() The point of want_color() is to take in a git_colorbool enum value and collapse it down to a single true/false boolean, letting UNKNOWN fall back to the color.ui default and checking isatty() for AUTO. Let's make that more clear in the type system by returning a bool rather than an integer. This sadly still does not help us much with compiler warnings for using the two types interchangeably. But it helps make the intent more clear to a human reader. We still retain the idempotency of want_color(), because in C a bool true/false converts to 1/0 when converted to an integer, which corresponds to GIT_COLOR_ALWAYS and GIT_COLOR_NEVER. So you can store the bool in a git_colorbool and get the right result (something a few pieces of code still do, but which we'll clean up in further patches). Note that we rely on this same bool/int conversion for check_auto_color(). We cache its results in a tristate int with "-1" as "not yet set", but we can assign to it (and return it) with implicit conversions to/from bool. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- diff --git a/color.c b/color.c index 3348ead534..07ac8c9d40 100644 --- a/color.c +++ b/color.c @@ -391,7 +391,7 @@ enum git_colorbool git_config_colorbool(const char *var, const char *value) return GIT_COLOR_AUTO; } -static int check_auto_color(int fd) +static bool check_auto_color(int fd) { static int color_stderr_is_tty = -1; int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty; @@ -399,12 +399,12 @@ static int check_auto_color(int fd) *is_tty_p = isatty(fd); if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) { if (!is_terminal_dumb()) - return 1; + return true; } - return 0; + return false; } -int want_color_fd(int fd, enum git_colorbool var) +bool 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 fcb38c5562..43e6c9ad09 100644 --- 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, enum git_colorbool var); +bool 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))