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 <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Feb 16, 2008 at 06:00 UTC
ea5105a5e3c6629ee64b499ea918c2b80882fc22
2 files changed
+13
-13
cache.h
+1
@@ -584,6 +584,7 @@ extern int git_parse_ulong(const char *, unsigned long *);
584
extern int git_config_int(const char *, const char *);
585
extern unsigned long git_config_ulong(const char *, const char *);
586
extern int git_config_bool(const char *, const char *);
587
+extern int git_config_string(const char **, const char *, const char *);
588
extern int git_config_set(const char *, const char *);
589
extern int git_config_set_multivar(const char *, const char *, const char *, int);
590
extern int git_config_rename_section(const char *, const char *);
config.c
+12
-13
@@ -309,6 +309,14 @@ int git_config_bool(const char *name, const char *value)
309
return git_config_int(name, value) != 0;
310
}
311
312
+int git_config_string(const char **dest, const char *var, const char *value)
313
+{
314
+ if (!value)
315
+ return config_error_nonbool(var);
316
+ *dest = xstrdup(value);
317
+ return 0;
318
+}
319
+
320
int git_default_config(const char *var, const char *value)
321
{
322
/* This needs a better name */
@@ -421,20 +429,11 @@ int git_default_config(const char *var, const char *value)
429
return 0;
430
}
431
424
- if (!strcmp(var, "i18n.commitencoding")) {
425
- if (!value)
426
- return config_error_nonbool(var);
427
- git_commit_encoding = xstrdup(value);
428
- return 0;
429
- }
430
-
431
- if (!strcmp(var, "i18n.logoutputencoding")) {
432
- if (!value)
433
- return config_error_nonbool(var);
434
- git_log_output_encoding = xstrdup(value);
435
- return 0;
436
- }
432
+ if (!strcmp(var, "i18n.commitencoding"))
433
+ return git_config_string(&git_commit_encoding, var, value);
434
435
+ if (!strcmp(var, "i18n.logoutputencoding"))
436
+ return git_config_string(&git_log_output_encoding, var, value);
437
438
if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
439
pager_use_color = git_config_bool(var,value);