Git fork

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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Jeff King and committed by
Junio C Hamano
b978f780 e9330ae4

+5 -5
+4 -4
color.c
··· 391 391 return GIT_COLOR_AUTO; 392 392 } 393 393 394 - static int check_auto_color(int fd) 394 + static bool check_auto_color(int fd) 395 395 { 396 396 static int color_stderr_is_tty = -1; 397 397 int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty; ··· 399 399 *is_tty_p = isatty(fd); 400 400 if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) { 401 401 if (!is_terminal_dumb()) 402 - return 1; 402 + return true; 403 403 } 404 - return 0; 404 + return false; 405 405 } 406 406 407 - int want_color_fd(int fd, enum git_colorbool var) 407 + bool 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, enum git_colorbool var); 109 + bool 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