5 #include "parse-options.h"
9 static const char *const builtin_config_usage
[] = {
10 N_("git config [<options>]"),
15 static regex_t
*key_regexp
;
16 static regex_t
*regexp
;
18 static int omit_values
;
19 static int use_key_regexp
;
21 static int do_not_match
;
22 static char delim
= '=';
23 static char key_delim
= ' ';
24 static char term
= '\n';
26 static int use_global_config
, use_system_config
, use_local_config
;
27 static struct git_config_source given_config_source
;
28 static int actions
, types
;
30 static int respect_includes_opt
= -1;
31 static struct config_options config_options
;
32 static int show_origin
;
34 #define ACTION_GET (1<<0)
35 #define ACTION_GET_ALL (1<<1)
36 #define ACTION_GET_REGEXP (1<<2)
37 #define ACTION_REPLACE_ALL (1<<3)
38 #define ACTION_ADD (1<<4)
39 #define ACTION_UNSET (1<<5)
40 #define ACTION_UNSET_ALL (1<<6)
41 #define ACTION_RENAME_SECTION (1<<7)
42 #define ACTION_REMOVE_SECTION (1<<8)
43 #define ACTION_LIST (1<<9)
44 #define ACTION_EDIT (1<<10)
45 #define ACTION_SET (1<<11)
46 #define ACTION_SET_ALL (1<<12)
47 #define ACTION_GET_COLOR (1<<13)
48 #define ACTION_GET_COLORBOOL (1<<14)
49 #define ACTION_GET_URLMATCH (1<<15)
51 #define TYPE_BOOL (1<<0)
52 #define TYPE_INT (1<<1)
53 #define TYPE_BOOL_OR_INT (1<<2)
54 #define TYPE_PATH (1<<3)
56 static struct option builtin_config_options
[] = {
57 OPT_GROUP(N_("Config file location")),
58 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
59 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
60 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
61 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
62 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
63 OPT_GROUP(N_("Action")),
64 OPT_BIT(0, "get", &actions
, N_("get value: name [value-regex]"), ACTION_GET
),
65 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-regex]"), ACTION_GET_ALL
),
66 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP
),
67 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
68 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL
),
69 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
70 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-regex]"), ACTION_UNSET
),
71 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL
),
72 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
73 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
74 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
75 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
76 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
77 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
78 OPT_GROUP(N_("Type")),
79 OPT_BIT(0, "bool", &types
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
80 OPT_BIT(0, "int", &types
, N_("value is decimal number"), TYPE_INT
),
81 OPT_BIT(0, "bool-or-int", &types
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
82 OPT_BIT(0, "path", &types
, N_("value is a path (file or directory name)"), TYPE_PATH
),
83 OPT_GROUP(N_("Other")),
84 OPT_BOOL('z', "null", &end_null
, N_("terminate values with NUL byte")),
85 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
86 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
87 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
91 static void check_argc(int argc
, int min
, int max
) {
92 if (argc
>= min
&& argc
<= max
)
94 error("wrong number of arguments");
95 usage_with_options(builtin_config_usage
, builtin_config_options
);
98 static void show_config_origin(struct strbuf
*buf
)
100 const char term
= end_null
? '\0' : '\t';
102 strbuf_addstr(buf
, current_config_origin_type());
103 strbuf_addch(buf
, ':');
105 strbuf_addstr(buf
, current_config_name());
107 quote_c_style(current_config_name(), buf
, NULL
, 0);
108 strbuf_addch(buf
, term
);
111 static int show_all_config(const char *key_
, const char *value_
, void *cb
)
114 struct strbuf buf
= STRBUF_INIT
;
115 show_config_origin(&buf
);
116 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
117 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
118 strbuf_release(&buf
);
120 if (!omit_values
&& value_
)
121 printf("%s%c%s%c", key_
, delim
, value_
, term
);
123 printf("%s%c", key_
, term
);
128 struct strbuf
*items
;
133 static int format_config(struct strbuf
*buf
, const char *key_
, const char *value_
)
136 show_config_origin(buf
);
138 strbuf_addstr(buf
, key_
);
141 strbuf_addch(buf
, key_delim
);
143 if (types
== TYPE_INT
)
144 strbuf_addf(buf
, "%"PRId64
,
145 git_config_int64(key_
, value_
? value_
: ""));
146 else if (types
== TYPE_BOOL
)
147 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
149 else if (types
== TYPE_BOOL_OR_INT
) {
151 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
153 strbuf_addstr(buf
, v
? "true" : "false");
155 strbuf_addf(buf
, "%d", v
);
156 } else if (types
== TYPE_PATH
) {
158 if (git_config_pathname(&v
, key_
, value_
) < 0)
160 strbuf_addstr(buf
, v
);
163 strbuf_addstr(buf
, value_
);
165 /* Just show the key name; back out delimiter */
167 strbuf_setlen(buf
, buf
->len
- 1);
170 strbuf_addch(buf
, term
);
174 static int collect_config(const char *key_
, const char *value_
, void *cb
)
176 struct strbuf_list
*values
= cb
;
178 if (!use_key_regexp
&& strcmp(key_
, key
))
180 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
182 if (regexp
!= NULL
&&
183 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
186 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
187 strbuf_init(&values
->items
[values
->nr
], 0);
189 return format_config(&values
->items
[values
->nr
++], key_
, value_
);
192 static int get_value(const char *key_
, const char *regex_
)
194 int ret
= CONFIG_GENERIC_ERROR
;
195 struct strbuf_list values
= {NULL
};
198 if (use_key_regexp
) {
202 * NEEDSWORK: this naive pattern lowercasing obviously does not
203 * work for more complex patterns like "^[^.]*Foo.*bar".
204 * Perhaps we should deprecate this altogether someday.
208 for (tl
= key
+ strlen(key
) - 1;
209 tl
>= key
&& *tl
!= '.';
212 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
215 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
216 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
217 error("invalid key pattern: %s", key_
);
218 FREE_AND_NULL(key_regexp
);
219 ret
= CONFIG_INVALID_PATTERN
;
223 if (git_config_parse_key(key_
, &key
, NULL
)) {
224 ret
= CONFIG_INVALID_KEY
;
230 if (regex_
[0] == '!') {
235 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
236 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
237 error("invalid pattern: %s", regex_
);
238 FREE_AND_NULL(regexp
);
239 ret
= CONFIG_INVALID_PATTERN
;
244 config_with_options(collect_config
, &values
,
245 &given_config_source
, &config_options
);
249 for (i
= 0; i
< values
.nr
; i
++) {
250 struct strbuf
*buf
= values
.items
+ i
;
251 if (do_all
|| i
== values
.nr
- 1)
252 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
271 static char *normalize_value(const char *key
, const char *value
)
276 if (types
== 0 || types
== TYPE_PATH
)
278 * We don't do normalization for TYPE_PATH here: If
279 * the path is like ~/foobar/, we prefer to store
280 * "~/foobar/" in the config file, and to expand the ~
281 * when retrieving the value.
283 return xstrdup(value
);
284 if (types
== TYPE_INT
)
285 return xstrfmt("%"PRId64
, git_config_int64(key
, value
));
286 if (types
== TYPE_BOOL
)
287 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
288 if (types
== TYPE_BOOL_OR_INT
) {
290 v
= git_config_bool_or_int(key
, value
, &is_bool
);
292 return xstrfmt("%d", v
);
294 return xstrdup(v
? "true" : "false");
297 die("BUG: cannot normalize type %d", types
);
300 static int get_color_found
;
301 static const char *get_color_slot
;
302 static const char *get_colorbool_slot
;
303 static char parsed_color
[COLOR_MAXLEN
];
305 static int git_get_color_config(const char *var
, const char *value
, void *cb
)
307 if (!strcmp(var
, get_color_slot
)) {
309 config_error_nonbool(var
);
310 if (color_parse(value
, parsed_color
) < 0)
317 static void get_color(const char *var
, const char *def_color
)
319 get_color_slot
= var
;
321 parsed_color
[0] = '\0';
322 config_with_options(git_get_color_config
, NULL
,
323 &given_config_source
, &config_options
);
325 if (!get_color_found
&& def_color
) {
326 if (color_parse(def_color
, parsed_color
) < 0)
327 die(_("unable to parse default color value"));
330 fputs(parsed_color
, stdout
);
333 static int get_colorbool_found
;
334 static int get_diff_color_found
;
335 static int get_color_ui_found
;
336 static int git_get_colorbool_config(const char *var
, const char *value
,
339 if (!strcmp(var
, get_colorbool_slot
))
340 get_colorbool_found
= git_config_colorbool(var
, value
);
341 else if (!strcmp(var
, "diff.color"))
342 get_diff_color_found
= git_config_colorbool(var
, value
);
343 else if (!strcmp(var
, "color.ui"))
344 get_color_ui_found
= git_config_colorbool(var
, value
);
348 static int get_colorbool(const char *var
, int print
)
350 get_colorbool_slot
= var
;
351 get_colorbool_found
= -1;
352 get_diff_color_found
= -1;
353 get_color_ui_found
= -1;
354 config_with_options(git_get_colorbool_config
, NULL
,
355 &given_config_source
, &config_options
);
357 if (get_colorbool_found
< 0) {
358 if (!strcmp(get_colorbool_slot
, "color.diff"))
359 get_colorbool_found
= get_diff_color_found
;
360 if (get_colorbool_found
< 0)
361 get_colorbool_found
= get_color_ui_found
;
364 if (get_colorbool_found
< 0)
365 /* default value if none found in config */
366 get_colorbool_found
= GIT_COLOR_AUTO
;
368 get_colorbool_found
= want_color(get_colorbool_found
);
371 printf("%s\n", get_colorbool_found
? "true" : "false");
374 return get_colorbool_found
? 0 : 1;
377 static void check_write(void)
379 if (!given_config_source
.file
&& !startup_info
->have_repository
)
380 die("not in a git directory");
382 if (given_config_source
.use_stdin
)
383 die("writing to stdin is not supported");
385 if (given_config_source
.blob
)
386 die("writing config blobs is not supported");
389 struct urlmatch_current_candidate_value
{
394 static int urlmatch_collect_fn(const char *var
, const char *value
, void *cb
)
396 struct string_list
*values
= cb
;
397 struct string_list_item
*item
= string_list_insert(values
, var
);
398 struct urlmatch_current_candidate_value
*matched
= item
->util
;
401 matched
= xmalloc(sizeof(*matched
));
402 strbuf_init(&matched
->value
, 0);
403 item
->util
= matched
;
405 strbuf_reset(&matched
->value
);
409 strbuf_addstr(&matched
->value
, value
);
410 matched
->value_is_null
= 0;
412 matched
->value_is_null
= 1;
417 static int get_urlmatch(const char *var
, const char *url
)
421 struct string_list_item
*item
;
422 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
423 struct string_list values
= STRING_LIST_INIT_DUP
;
425 config
.collect_fn
= urlmatch_collect_fn
;
426 config
.cascade_fn
= NULL
;
429 if (!url_normalize(url
, &config
.url
))
430 die("%s", config
.url
.err
);
432 config
.section
= xstrdup_tolower(var
);
433 section_tail
= strchr(config
.section
, '.');
435 *section_tail
= '\0';
436 config
.key
= section_tail
+ 1;
443 config_with_options(urlmatch_config_entry
, &config
,
444 &given_config_source
, &config_options
);
448 for_each_string_list_item(item
, &values
) {
449 struct urlmatch_current_candidate_value
*matched
= item
->util
;
450 struct strbuf buf
= STRBUF_INIT
;
452 format_config(&buf
, item
->string
,
453 matched
->value_is_null
? NULL
: matched
->value
.buf
);
454 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
455 strbuf_release(&buf
);
457 strbuf_release(&matched
->value
);
459 string_list_clear(&config
.vars
, 1);
460 string_list_clear(&values
, 1);
461 free(config
.url
.url
);
463 free((void *)config
.section
);
467 static char *default_user_config(void)
469 struct strbuf buf
= STRBUF_INIT
;
471 _("# This is Git's per-user configuration file.\n"
473 "# Please adapt and uncomment the following lines:\n"
476 ident_default_name(),
477 ident_default_email());
478 return strbuf_detach(&buf
, NULL
);
481 int cmd_config(int argc
, const char **argv
, const char *prefix
)
483 int nongit
= !startup_info
->have_repository
;
486 given_config_source
.file
= getenv(CONFIG_ENVIRONMENT
);
488 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
489 builtin_config_usage
,
490 PARSE_OPT_STOP_AT_NON_OPTION
);
492 if (use_global_config
+ use_system_config
+ use_local_config
+
493 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
494 error("only one config file at a time.");
495 usage_with_options(builtin_config_usage
, builtin_config_options
);
498 if (use_local_config
&& nongit
)
499 die(_("--local can only be used inside a git repository"));
501 if (given_config_source
.file
&&
502 !strcmp(given_config_source
.file
, "-")) {
503 given_config_source
.file
= NULL
;
504 given_config_source
.use_stdin
= 1;
507 if (use_global_config
) {
508 char *user_config
= expand_user_path("~/.gitconfig", 0);
509 char *xdg_config
= xdg_config_home("config");
513 * It is unknown if HOME/.gitconfig exists, so
514 * we do not know if we should write to XDG
515 * location; error out even if XDG_CONFIG_HOME
516 * is set and points at a sane location.
518 die("$HOME not set");
520 if (access_or_warn(user_config
, R_OK
, 0) &&
521 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0)) {
522 given_config_source
.file
= xdg_config
;
525 given_config_source
.file
= user_config
;
529 else if (use_system_config
)
530 given_config_source
.file
= git_etc_gitconfig();
531 else if (use_local_config
)
532 given_config_source
.file
= git_pathdup("config");
533 else if (given_config_source
.file
) {
534 if (!is_absolute_path(given_config_source
.file
) && prefix
)
535 given_config_source
.file
=
536 prefix_filename(prefix
, given_config_source
.file
);
539 if (respect_includes_opt
== -1)
540 config_options
.respect_includes
= !given_config_source
.file
;
542 config_options
.respect_includes
= respect_includes_opt
;
544 config_options
.commondir
= get_git_common_dir();
545 config_options
.git_dir
= get_git_dir();
554 if (HAS_MULTI_BITS(types
)) {
555 error("only one type at a time.");
556 usage_with_options(builtin_config_usage
, builtin_config_options
);
559 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && types
) {
560 error("--get-color and variable type are incoherent");
561 usage_with_options(builtin_config_usage
, builtin_config_options
);
564 if (HAS_MULTI_BITS(actions
)) {
565 error("only one action at a time.");
566 usage_with_options(builtin_config_usage
, builtin_config_options
);
570 case 1: actions
= ACTION_GET
; break;
571 case 2: actions
= ACTION_SET
; break;
572 case 3: actions
= ACTION_SET_ALL
; break;
574 usage_with_options(builtin_config_usage
, builtin_config_options
);
577 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
578 error("--name-only is only applicable to --list or --get-regexp");
579 usage_with_options(builtin_config_usage
, builtin_config_options
);
582 if (show_origin
&& !(actions
&
583 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
584 error("--show-origin is only applicable to --get, --get-all, "
585 "--get-regexp, and --list.");
586 usage_with_options(builtin_config_usage
, builtin_config_options
);
589 if (actions
== ACTION_LIST
) {
590 check_argc(argc
, 0, 0);
591 if (config_with_options(show_all_config
, NULL
,
592 &given_config_source
,
593 &config_options
) < 0) {
594 if (given_config_source
.file
)
595 die_errno("unable to read config file '%s'",
596 given_config_source
.file
);
598 die("error processing config file(s)");
601 else if (actions
== ACTION_EDIT
) {
604 check_argc(argc
, 0, 0);
605 if (!given_config_source
.file
&& nongit
)
606 die("not in a git directory");
607 if (given_config_source
.use_stdin
)
608 die("editing stdin is not supported");
609 if (given_config_source
.blob
)
610 die("editing blobs is not supported");
611 git_config(git_default_config
, NULL
);
612 config_file
= given_config_source
.file
?
613 xstrdup(given_config_source
.file
) :
614 git_pathdup("config");
615 if (use_global_config
) {
616 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
618 char *content
= default_user_config();
619 write_str_in_full(fd
, content
);
623 else if (errno
!= EEXIST
)
624 die_errno(_("cannot create configuration file %s"), config_file
);
626 launch_editor(config_file
, NULL
, NULL
);
629 else if (actions
== ACTION_SET
) {
632 check_argc(argc
, 2, 2);
633 value
= normalize_value(argv
[0], argv
[1]);
635 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
636 if (ret
== CONFIG_NOTHING_SET
)
637 error(_("cannot overwrite multiple values with a single value\n"
638 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
641 else if (actions
== ACTION_SET_ALL
) {
643 check_argc(argc
, 2, 3);
644 value
= normalize_value(argv
[0], argv
[1]);
646 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
647 argv
[0], value
, argv
[2], 0);
649 else if (actions
== ACTION_ADD
) {
651 check_argc(argc
, 2, 2);
652 value
= normalize_value(argv
[0], argv
[1]);
654 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
656 CONFIG_REGEX_NONE
, 0);
658 else if (actions
== ACTION_REPLACE_ALL
) {
660 check_argc(argc
, 2, 3);
661 value
= normalize_value(argv
[0], argv
[1]);
663 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
664 argv
[0], value
, argv
[2], 1);
666 else if (actions
== ACTION_GET
) {
667 check_argc(argc
, 1, 2);
668 return get_value(argv
[0], argv
[1]);
670 else if (actions
== ACTION_GET_ALL
) {
672 check_argc(argc
, 1, 2);
673 return get_value(argv
[0], argv
[1]);
675 else if (actions
== ACTION_GET_REGEXP
) {
679 check_argc(argc
, 1, 2);
680 return get_value(argv
[0], argv
[1]);
682 else if (actions
== ACTION_GET_URLMATCH
) {
683 check_argc(argc
, 2, 2);
684 return get_urlmatch(argv
[0], argv
[1]);
686 else if (actions
== ACTION_UNSET
) {
688 check_argc(argc
, 1, 2);
690 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
691 argv
[0], NULL
, argv
[1], 0);
693 return git_config_set_in_file_gently(given_config_source
.file
,
696 else if (actions
== ACTION_UNSET_ALL
) {
698 check_argc(argc
, 1, 2);
699 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
700 argv
[0], NULL
, argv
[1], 1);
702 else if (actions
== ACTION_RENAME_SECTION
) {
705 check_argc(argc
, 2, 2);
706 ret
= git_config_rename_section_in_file(given_config_source
.file
,
711 die("No such section!");
713 else if (actions
== ACTION_REMOVE_SECTION
) {
716 check_argc(argc
, 1, 1);
717 ret
= git_config_rename_section_in_file(given_config_source
.file
,
722 die("No such section!");
724 else if (actions
== ACTION_GET_COLOR
) {
725 check_argc(argc
, 1, 2);
726 get_color(argv
[0], argv
[1]);
728 else if (actions
== ACTION_GET_COLORBOOL
) {
729 check_argc(argc
, 1, 2);
731 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
732 return get_colorbool(argv
[0], argc
== 2);