config: change git_config_with_options() interface

We're going to have more options for config source. Let's alter git_config_with_options() interface to accept struct with all source options. Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kirill A. Shutemov committed Feb 19, 2014 at 00:58 UTC c8985ce05360857733738561dd6cdf964470cbdf
3 files changed +47 -49
builtin/config.c
+35 -40
@@ -21,8 +21,7 @@ static char key_delim = ' ';
21 static char term = '\n';
22
23 static int use_global_config, use_system_config, use_local_config;
24 -static const char *given_config_file;
25 -static const char *given_config_blob;
24 +static struct git_config_source given_config_source;
25 static int actions, types;
26 static const char *get_color_slot, *get_colorbool_slot;
27 static int end_null;
@@ -55,8 +54,8 @@ static struct option builtin_config_options[] = {
54 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
55 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
56 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
58 - OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
59 - OPT_STRING(0, "blob", &given_config_blob, N_("blob-id"), N_("read config from given blob object")),
57 + OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
58 + OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
59 OPT_GROUP(N_("Action")),
60 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
61 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
@@ -224,8 +223,7 @@ static int get_value(const char *key_, const char *regex_)
223 }
224
225 git_config_with_options(collect_config, &values,
227 - given_config_file, given_config_blob,
228 - respect_includes);
226 + &given_config_source, respect_includes);
227
228 ret = !values.nr;
229
@@ -309,8 +307,7 @@ static void get_color(const char *def_color)
307 get_color_found = 0;
308 parsed_color[0] = '\0';
309 git_config_with_options(git_get_color_config, NULL,
312 - given_config_file, given_config_blob,
313 - respect_includes);
310 + &given_config_source, respect_includes);
311
312 if (!get_color_found && def_color)
313 color_parse(def_color, "command line", parsed_color);
@@ -339,8 +336,7 @@ static int get_colorbool(int print)
336 get_diff_color_found = -1;
337 get_color_ui_found = -1;
338 git_config_with_options(git_get_colorbool_config, NULL,
342 - given_config_file, given_config_blob,
343 - respect_includes);
339 + &given_config_source, respect_includes);
340
341 if (get_colorbool_found < 0) {
342 if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -364,7 +360,7 @@ static int get_colorbool(int print)
360
361 static void check_write(void)
362 {
367 - if (given_config_blob)
363 + if (given_config_source.blob)
364 die("writing config blobs is not supported");
365 }
366
@@ -435,7 +431,7 @@ static int get_urlmatch(const char *var, const char *url)
431 }
432
433 git_config_with_options(urlmatch_config_entry, &config,
438 - given_config_file, NULL, respect_includes);
434 + &given_config_source, respect_includes);
435
436 for_each_string_list_item(item, &values) {
437 struct urlmatch_current_candidate_value *matched = item->util;
@@ -464,14 +460,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
460 int nongit = !startup_info->have_repository;
461 char *value;
462
467 - given_config_file = getenv(CONFIG_ENVIRONMENT);
463 + given_config_source.file = getenv(CONFIG_ENVIRONMENT);
464
465 argc = parse_options(argc, argv, prefix, builtin_config_options,
466 builtin_config_usage,
467 PARSE_OPT_STOP_AT_NON_OPTION);
468
469 if (use_global_config + use_system_config + use_local_config +
474 - !!given_config_file + !!given_config_blob > 1) {
470 + !!given_config_source.file + !!given_config_source.blob > 1) {
471 error("only one config file at a time.");
472 usage_with_options(builtin_config_usage, builtin_config_options);
473 }
@@ -493,24 +489,24 @@ int cmd_config(int argc, const char **argv, const char *prefix)
489
490 if (access_or_warn(user_config, R_OK, 0) &&
491 xdg_config && !access_or_warn(xdg_config, R_OK, 0))
496 - given_config_file = xdg_config;
492 + given_config_source.file = xdg_config;
493 else
498 - given_config_file = user_config;
494 + given_config_source.file = user_config;
495 }
496 else if (use_system_config)
501 - given_config_file = git_etc_gitconfig();
497 + given_config_source.file = git_etc_gitconfig();
498 else if (use_local_config)
503 - given_config_file = git_pathdup("config");
504 - else if (given_config_file) {
505 - if (!is_absolute_path(given_config_file) && prefix)
506 - given_config_file =
499 + given_config_source.file = git_pathdup("config");
500 + else if (given_config_source.file) {
501 + if (!is_absolute_path(given_config_source.file) && prefix)
502 + given_config_source.file =
503 xstrdup(prefix_filename(prefix,
504 strlen(prefix),
509 - given_config_file));
505 + given_config_source.file));
506 }
507
508 if (respect_includes == -1)
513 - respect_includes = !given_config_file;
509 + respect_includes = !given_config_source.file;
510
511 if (end_null) {
512 term = '\0';
@@ -549,25 +545,24 @@ int cmd_config(int argc, const char **argv, const char *prefix)
545 if (actions == ACTION_LIST) {
546 check_argc(argc, 0, 0);
547 if (git_config_with_options(show_all_config, NULL,
552 - given_config_file,
553 - given_config_blob,
548 + &given_config_source,
549 respect_includes) < 0) {
555 - if (given_config_file)
550 + if (given_config_source.file)
551 die_errno("unable to read config file '%s'",
557 - given_config_file);
552 + given_config_source.file);
553 else
554 die("error processing config file(s)");
555 }
556 }
557 else if (actions == ACTION_EDIT) {
558 check_argc(argc, 0, 0);
564 - if (!given_config_file && nongit)
559 + if (!given_config_source.file && nongit)
560 die("not in a git directory");
566 - if (given_config_blob)
561 + if (given_config_source.blob)
562 die("editing blobs is not supported");
563 git_config(git_default_config, NULL);
569 - launch_editor(given_config_file ?
570 - given_config_file : git_path("config"),
564 + launch_editor(given_config_source.file ?
565 + given_config_source.file : git_path("config"),
566 NULL, NULL);
567 }
568 else if (actions == ACTION_SET) {
@@ -575,7 +570,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
570 check_write();
571 check_argc(argc, 2, 2);
572 value = normalize_value(argv[0], argv[1]);
578 - ret = git_config_set_in_file(given_config_file, argv[0], value);
573 + ret = git_config_set_in_file(given_config_source.file, argv[0], value);
574 if (ret == CONFIG_NOTHING_SET)
575 error("cannot overwrite multiple values with a single value\n"
576 " Use a regexp, --add or --replace-all to change %s.", argv[0]);
@@ -585,21 +580,21 @@ int cmd_config(int argc, const char **argv, const char *prefix)
580 check_write();
581 check_argc(argc, 2, 3);
582 value = normalize_value(argv[0], argv[1]);
588 - return git_config_set_multivar_in_file(given_config_file,
583 + return git_config_set_multivar_in_file(given_config_source.file,
584 argv[0], value, argv[2], 0);
585 }
586 else if (actions == ACTION_ADD) {
587 check_write();
588 check_argc(argc, 2, 2);
589 value = normalize_value(argv[0], argv[1]);
595 - return git_config_set_multivar_in_file(given_config_file,
590 + return git_config_set_multivar_in_file(given_config_source.file,
591 argv[0], value, "^$", 0);
592 }
593 else if (actions == ACTION_REPLACE_ALL) {
594 check_write();
595 check_argc(argc, 2, 3);
596 value = normalize_value(argv[0], argv[1]);
602 - return git_config_set_multivar_in_file(given_config_file,
597 + return git_config_set_multivar_in_file(given_config_source.file,
598 argv[0], value, argv[2], 1);
599 }
600 else if (actions == ACTION_GET) {
@@ -626,23 +621,23 @@ int cmd_config(int argc, const char **argv, const char *prefix)
621 check_write();
622 check_argc(argc, 1, 2);
623 if (argc == 2)
629 - return git_config_set_multivar_in_file(given_config_file,
624 + return git_config_set_multivar_in_file(given_config_source.file,
625 argv[0], NULL, argv[1], 0);
626 else
632 - return git_config_set_in_file(given_config_file,
627 + return git_config_set_in_file(given_config_source.file,
628 argv[0], NULL);
629 }
630 else if (actions == ACTION_UNSET_ALL) {
631 check_write();
632 check_argc(argc, 1, 2);
638 - return git_config_set_multivar_in_file(given_config_file,
633 + return git_config_set_multivar_in_file(given_config_source.file,
634 argv[0], NULL, argv[1], 1);
635 }
636 else if (actions == ACTION_RENAME_SECTION) {
637 int ret;
638 check_write();
639 check_argc(argc, 2, 2);
645 - ret = git_config_rename_section_in_file(given_config_file,
640 + ret = git_config_rename_section_in_file(given_config_source.file,
641 argv[0], argv[1]);
642 if (ret < 0)
643 return ret;
@@ -653,7 +648,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
648 int ret;
649 check_write();
650 check_argc(argc, 1, 1);
656 - ret = git_config_rename_section_in_file(given_config_file,
651 + ret = git_config_rename_section_in_file(given_config_source.file,
652 argv[0], NULL);
653 if (ret < 0)
654 return ret;
cache.h
+6 -2
@@ -1146,6 +1146,11 @@ extern int update_server_info(int);
1146 #define CONFIG_INVALID_PATTERN 6
1147 #define CONFIG_GENERIC_ERROR 7
1148
1149 +struct git_config_source {
1150 + const char *file;
1151 + const char *blob;
1152 +};
1153 +
1154 typedef int (*config_fn_t)(const char *, const char *, void *);
1155 extern int git_default_config(const char *, const char *, void *);
1156 extern int git_config_from_file(config_fn_t fn, const char *, void *);
@@ -1155,8 +1160,7 @@ extern void git_config_push_parameter(const char *text);
1160 extern int git_config_from_parameters(config_fn_t fn, void *data);
1161 extern int git_config(config_fn_t fn, void *);
1162 extern int git_config_with_options(config_fn_t fn, void *,
1158 - const char *filename,
1159 - const char *blob_ref,
1163 + struct git_config_source *config_source,
1164 int respect_includes);
1165 extern int git_config_early(config_fn_t fn, void *, const char *repo_config);
1166 extern int git_parse_ulong(const char *, unsigned long *);
config.c
+6 -7
@@ -1172,8 +1172,7 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
1172 }
1173
1174 int git_config_with_options(config_fn_t fn, void *data,
1175 - const char *filename,
1176 - const char *blob_ref,
1175 + struct git_config_source *config_source,
1176 int respect_includes)
1177 {
1178 char *repo_config = NULL;
@@ -1191,10 +1190,10 @@ int git_config_with_options(config_fn_t fn, void *data,
1190 * If we have a specific filename, use it. Otherwise, follow the
1191 * regular lookup sequence.
1192 */
1194 - if (filename)
1195 - return git_config_from_file(fn, filename, data);
1196 - else if (blob_ref)
1197 - return git_config_from_blob_ref(fn, blob_ref, data);
1193 + if (config_source && config_source->file)
1194 + return git_config_from_file(fn, config_source->file, data);
1195 + else if (config_source && config_source->blob)
1196 + return git_config_from_blob_ref(fn, config_source->blob, data);
1197
1198 repo_config = git_pathdup("config");
1199 ret = git_config_early(fn, data, repo_config);
@@ -1205,7 +1204,7 @@ int git_config_with_options(config_fn_t fn, void *data,
1204
1205 int git_config(config_fn_t fn, void *data)
1206 {
1208 - return git_config_with_options(fn, data, NULL, NULL, 1);
1207 + return git_config_with_options(fn, data, NULL, 1);
1208 }
1209
1210 /*