git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 1/4] config.c: Add git_config_bool_or_int to handle bool/int variable

From
Junio C Hamano <junio@pobox.com>
Date
Apr 13, 2008, 06:26 UTC
Message-ID
<7vmynye0cz.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<1208012733-18211-2-git-send-email-pkufranky@gmail.com>
Ping Yin <pkufranky@gmail.com> writes:
Show 31 quoted lines
> With git_config_bool_or_int, the caller can differentiate boolean true
> and integer 1 etc.
>
> Signed-off-by: Ping Yin <pkufranky@gmail.com>
> ---
>  config.c |   15 +++++++++++++++
>  1 files changed, 15 insertions(+), 0 deletions(-)
>
> diff --git a/config.c b/config.c
> index 0624494..e614456 100644
> --- a/config.c
> +++ b/config.c
> @@ -316,6 +316,21 @@ int git_config_bool(const char *name, const char *value)
>  	return git_config_int(name, value) != 0;
>  }
>  
> +int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
> +{
> +	*is_bool = 1;
> +	if (!value)
> +		return 1;
> +	if (!*value)
> +		return 0;
> +	if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
> +		return 1;
> +	if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
> +		return 0;
> +	*is_bool = 0;
> +	return git_config_int(name, value);
> +}
> +

I expected git_config_bool() to be implemented in terms of this new function to avoid code duplication if we were actually going to do this.

You also need an external declaration in a header file for its users.
---
 cache.h  |    1 +
 config.c |   10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletions(-)
diff --git a/cache.h b/cache.h
index 2a1e7ec..50b28fa 100644
--- a/cache.h
+++ b/cache.h
@@ -692,6 +692,7 @@ extern int git_parse_long(const char *, long *);
 extern int git_parse_ulong(const char *, unsigned long *);
 extern int git_config_int(const char *, const char *);
 extern unsigned long git_config_ulong(const char *, const char *);
+extern int git_config_bool_or_int(const char *, const char *, int *);
 extern int git_config_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
 extern int git_config_set(const char *, const char *);
diff --git a/config.c b/config.c
index 0624494..5ea18ef 100644
--- a/config.c
+++ b/config.c
@@ -303,8 +303,9 @@ unsigned long git_config_ulong(const char *name, const char *value)
 	return ret;
 }
 
-int git_config_bool(const char *name, const char *value)
+int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
 {
+	*is_bool = 1;
 	if (!value)
 		return 1;
 	if (!*value)
@@ -313,9 +314,16 @@ int git_config_bool(const char *name, const char *value)
 		return 1;
 	if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
 		return 0;
+	*is_bool = 0;
 	return git_config_int(name, value) != 0;
 }
 
+int git_config_bool(const char *name, const char *value)
+{
+	int discard;
+	return git_config_bool_or_int(name, value, &discard);
+}
+
 int git_config_string(const char **dest, const char *var, const char *value)
 {
 	if (!value)
Previous: Ping YinNext: Ping Yin
Message 7 of 11 in “builtin-status: submodule summary support”
  1. 0/4 builtin-status: submodule summary supportPing Yin, Apr 12, 2008
  2. 1/4 config.c: Add git_config_bool_or_int to handle bool/int variablePing Yin, Apr 12, 2008
  3. 2/4 git-submodule summary: --for-status optionPing Yin, Apr 12, 2008
  4. 3/4 builtin-status: submodule summary supportPing Yin, Apr 12, 2008
  5. 4/4 buitin-status: Add tests for submodule summaryPing Yin, Apr 12, 2008
  6. Ping YinApr 12, 2008
  7. Junio C HamanoApr 13, 2008
  8. Ping YinApr 13, 2008
  9. Ping YinApr 13, 2008
  10. Junio C HamanoApr 13, 2008
  11. Junio C HamanoApr 13, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.