Skip to content

Commit 5ef8d77

Browse files
peffgitster
authored andcommitted
color: make it easier for non-config to parse color specs
We have very featureful color-parsing routines which are used for color.diff.* and other options. Let's make it easier to use those routines from other parts of the code. This patch adds a color_parse_mem() helper function which takes a length-bounded string instead of a NUL-terminated one. While the helper is only a few lines long, it is nice to abstract this out so that: - callers don't forget to free() the temporary buffer - right now, it is implemented in terms of color_parse(). But it would be more efficient to reverse this and implement color_parse in terms of color_parse_mem. This also changes the error string for an invalid color not to mention the word "config", since it is not always appropriate (and when it is, the context is obvious since the offending config variable is given). Finally, while we are in the area, we clean up the parameter names in the declaration of color_parse; the var and value parameters were reversed from the actual implementation. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bbd8d6 commit 5ef8d77

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

color.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void color_parse(const char *value, const char *var, char *dst)
115115
*dst = 0;
116116
return;
117117
bad:
118-
die("bad config value '%s' for variable '%s'", value, var);
118+
die("bad color value '%s' for variable '%s'", value, var);
119119
}
120120

121121
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
@@ -191,3 +191,10 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
191191
va_end(args);
192192
return r;
193193
}
194+
195+
void color_parse_mem(const char *value, int len, const char *var, char *dst)
196+
{
197+
char *tmp = xmemdupz(value, len);
198+
color_parse(tmp, var, dst);
199+
free(tmp);
200+
}

color.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ extern int git_use_color_default;
1616
int git_color_default_config(const char *var, const char *value, void *cb);
1717

1818
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
19-
void color_parse(const char *var, const char *value, char *dst);
19+
void color_parse(const char *value, const char *var, char *dst);
20+
void color_parse_mem(const char *value, int len, const char *var, char *dst);
2021
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
2122
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);
2223

0 commit comments

Comments
 (0)