Skip to content

Commit

Permalink
config: add 'git_config_string' to refactor string config variables.
Browse files Browse the repository at this point in the history
In many places we just check if a value from the config file is not
NULL, then we duplicate it and return 0. This patch introduces the new
'git_config_string' function to do that.

This function is also used to refactor some code in 'config.c'.
Refactoring other files is left for other patches.

Also not all the code in "config.c" is refactored, because the function
takes a "const char **" as its first parameter, but in many places a
"char *" is used instead of a "const char *". (And C does not allow
using a "char **" instead of a "const char **" without a warning.)

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Christian Couder authored and Junio C Hamano committed Feb 16, 2008
1 parent 2c77821 commit ea5105a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ 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(const char *, const char *);
extern int git_config_string(const char **, const char *, const char *);
extern int git_config_set(const char *, const char *);
extern int git_config_set_multivar(const char *, const char *, const char *, int);
extern int git_config_rename_section(const char *, const char *);
Expand Down
25 changes: 12 additions & 13 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ int git_config_bool(const char *name, const char *value)
return git_config_int(name, value) != 0;
}

int git_config_string(const char **dest, const char *var, const char *value)
{
if (!value)
return config_error_nonbool(var);
*dest = xstrdup(value);
return 0;
}

int git_default_config(const char *var, const char *value)
{
/* This needs a better name */
Expand Down Expand Up @@ -421,20 +429,11 @@ int git_default_config(const char *var, const char *value)
return 0;
}

if (!strcmp(var, "i18n.commitencoding")) {
if (!value)
return config_error_nonbool(var);
git_commit_encoding = xstrdup(value);
return 0;
}

if (!strcmp(var, "i18n.logoutputencoding")) {
if (!value)
return config_error_nonbool(var);
git_log_output_encoding = xstrdup(value);
return 0;
}
if (!strcmp(var, "i18n.commitencoding"))
return git_config_string(&git_commit_encoding, var, value);

if (!strcmp(var, "i18n.logoutputencoding"))
return git_config_string(&git_log_output_encoding, var, value);

if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
pager_use_color = git_config_bool(var,value);
Expand Down

0 comments on commit ea5105a

Please sign in to comment.