From ea5105a5e3c6629ee64b499ea918c2b80882fc22 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 16 Feb 2008 06:00:24 +0100 Subject: config: add 'git_config_string' to refactor string config variables. 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 Signed-off-by: Junio C Hamano --- config.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 3f4d3b160..7b8615d87 100644 --- a/config.c +++ b/config.c @@ -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 */ @@ -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); -- cgit v1.2.1 From 872da32d80c004c26a19a5d6cb31eb3e7f034094 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 16 Feb 2008 06:01:11 +0100 Subject: Add "const" qualifier to "char *pager_program". Also use "git_config_string" to simplify "config.c" code where "pager_program" is set. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- config.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 7b8615d87..eb6d2ad78 100644 --- a/config.c +++ b/config.c @@ -440,12 +440,8 @@ int git_default_config(const char *var, const char *value) return 0; } - if (!strcmp(var, "core.pager")) { - if (!value) - return config_error_nonbool(var); - pager_program = xstrdup(value); - return 0; - } + if (!strcmp(var, "core.pager")) + return git_config_string(&pager_program, var, value); if (!strcmp(var, "core.editor")) { if (!value) -- cgit v1.2.1 From ee9601e6bef2281e3183e127e1e4e36ed257af7a Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 16 Feb 2008 06:01:41 +0100 Subject: Add "const" qualifier to "char *editor_program". Also use "git_config_string" to simplify "config.c" code where "editor_program" is set. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- config.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index eb6d2ad78..703c2085a 100644 --- a/config.c +++ b/config.c @@ -443,12 +443,8 @@ int git_default_config(const char *var, const char *value) if (!strcmp(var, "core.pager")) return git_config_string(&pager_program, var, value); - if (!strcmp(var, "core.editor")) { - if (!value) - return config_error_nonbool(var); - editor_program = xstrdup(value); - return 0; - } + if (!strcmp(var, "core.editor")) + return git_config_string(&editor_program, var, value); if (!strcmp(var, "core.excludesfile")) { if (!value) -- cgit v1.2.1 From dfb068be8deef2065970b2a7889acc51abf4dd78 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sat, 16 Feb 2008 06:01:59 +0100 Subject: Add "const" qualifier to "char *excludes_file". Also use "git_config_string" to simplify "config.c" code where "excludes_file" is set. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- config.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 703c2085a..b82907cb8 100644 --- a/config.c +++ b/config.c @@ -446,12 +446,8 @@ int git_default_config(const char *var, const char *value) if (!strcmp(var, "core.editor")) return git_config_string(&editor_program, var, value); - if (!strcmp(var, "core.excludesfile")) { - if (!value) - return config_error_nonbool(var); - excludes_file = xstrdup(value); - return 0; - } + if (!strcmp(var, "core.excludesfile")) + return git_config_string(&excludes_file, var, value); if (!strcmp(var, "core.whitespace")) { if (!value) -- cgit v1.2.1