Skip to content

Commit 8420ccd

Browse files
committed
git_config_maybe_bool()
Some configuration variables can take boolean values in addition to enumeration specific to them. Introduce git_config_maybe_bool() that returns 0 or 1 if the given value is boolean, or -1 if not, so that a parser for such a variable can check for boolean first and then parse other kinds of values as a fallback. Signed-off-by: Junio C Hamano <[email protected]>
1 parent e923eae commit 8420ccd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ extern int git_config_int(const char *, const char *);
924924
extern unsigned long git_config_ulong(const char *, const char *);
925925
extern int git_config_bool_or_int(const char *, const char *, int *);
926926
extern int git_config_bool(const char *, const char *);
927+
extern int git_config_maybe_bool(const char *, const char *);
927928
extern int git_config_string(const char **, const char *, const char *);
928929
extern int git_config_pathname(const char **, const char *, const char *);
929930
extern int git_config_set(const char *, const char *);

config.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,30 @@ unsigned long git_config_ulong(const char *name, const char *value)
322322
return ret;
323323
}
324324

325-
int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
325+
int git_config_maybe_bool(const char *name, const char *value)
326326
{
327-
*is_bool = 1;
328327
if (!value)
329328
return 1;
330329
if (!*value)
331330
return 0;
332-
if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
331+
if (!strcasecmp(value, "true")
332+
|| !strcasecmp(value, "yes")
333+
|| !strcasecmp(value, "on"))
333334
return 1;
334-
if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
335+
if (!strcasecmp(value, "false")
336+
|| !strcasecmp(value, "no")
337+
|| !strcasecmp(value, "off"))
335338
return 0;
339+
return -1;
340+
}
341+
342+
int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
343+
{
344+
int v = git_config_maybe_bool(name, value);
345+
if (0 <= v) {
346+
*is_bool = 1;
347+
return v;
348+
}
336349
*is_bool = 0;
337350
return git_config_int(name, value);
338351
}

0 commit comments

Comments
 (0)