]> git.ipfire.org Git - thirdparty/git.git/commitdiff
color: add color_parse_quietly()
authorDerrick Stolee <stolee@gmail.com>
Mon, 23 Feb 2026 12:26:52 +0000 (12:26 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Feb 2026 21:23:41 +0000 (13:23 -0800)
When parsing colors, a failed parse leads to an error message due to the
result returning error(). To allow for quiet parsing, create
color_parse_quietly(). This is in contrast to an ..._gently() version
because the original does not die(), so both options are technically
'gentle'.

To accomplish this, convert the implementation of color_parse_mem() into
a static color_parse_mem_1() helper that adds a 'quiet' parameter. The
color_parse_quietly() method can then use this. Since it is a near
equivalent to color_parse(), move that method down in the file so they
can be nearby while also appearing after color_parse_mem_1().

Signed-off-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
color.c
color.h

diff --git a/color.c b/color.c
index 07ac8c9d4009066f84dd5d6f809c0398b65b66d9..00b53f97acbcc7c5358946e5f7635ff7a5c08428 100644 (file)
--- a/color.c
+++ b/color.c
@@ -223,11 +223,6 @@ static int parse_attr(const char *name, size_t len)
        return -1;
 }
 
-int color_parse(const char *value, char *dst)
-{
-       return color_parse_mem(value, strlen(value), dst);
-}
-
 /*
  * Write the ANSI color codes for "c" to "out"; the string should
  * already have the ANSI escape code in it. "out" should have enough
@@ -264,7 +259,8 @@ static int color_empty(const struct color *c)
        return c->type <= COLOR_NORMAL;
 }
 
-int color_parse_mem(const char *value, int value_len, char *dst)
+static int color_parse_mem_1(const char *value, int value_len,
+                            char *dst, int quiet)
 {
        const char *ptr = value;
        int len = value_len;
@@ -365,10 +361,25 @@ int color_parse_mem(const char *value, int value_len, char *dst)
        OUT(0);
        return 0;
 bad:
-       return error(_("invalid color value: %.*s"), value_len, value);
+       return quiet ? -1 : error(_("invalid color value: %.*s"), value_len, value);
 #undef OUT
 }
 
+int color_parse_mem(const char *value, int value_len, char *dst)
+{
+       return color_parse_mem_1(value, value_len, dst, 0);
+}
+
+int color_parse(const char *value, char *dst)
+{
+       return color_parse_mem(value, strlen(value), dst);
+}
+
+int color_parse_quietly(const char *value, char *dst)
+{
+       return color_parse_mem_1(value, strlen(value), dst, 1);
+}
+
 enum git_colorbool git_config_colorbool(const char *var, const char *value)
 {
        if (value) {
diff --git a/color.h b/color.h
index 43e6c9ad0972b314035dcaf7d5fd6332564bb82e..0d7254030003f050a0cd74afb54f0245a5a61c81 100644 (file)
--- a/color.h
+++ b/color.h
@@ -118,6 +118,7 @@ bool want_color_fd(int fd, enum git_colorbool var);
  * terminal.
  */
 int color_parse(const char *value, char *dst);
+int color_parse_quietly(const char *value, char *dst);
 int color_parse_mem(const char *value, int len, char *dst);
 
 /*