]> git.ipfire.org Git - thirdparty/git.git/commitdiff
color: return bool from want_color()
authorJeff King <peff@peff.net>
Tue, 16 Sep 2025 20:25:26 +0000 (16:25 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 17 Sep 2025 01:00:25 +0000 (18:00 -0700)
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>
color.c
color.h

diff --git a/color.c b/color.c
index 3348ead534c73d827ff995cc3dc688c94c3a8fd5..07ac8c9d4009066f84dd5d6f809c0398b65b66d9 100644 (file)
--- 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 fcb38c5562f3e282f5cfddf940c3a0917d71f311..43e6c9ad0972b314035dcaf7d5fd6332564bb82e 100644 (file)
--- 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))