4 #include "parse-options.h"
7 static const char *const builtin_config_usage
[] = {
8 N_("git config [<options>]"),
13 static regex_t
*key_regexp
;
14 static regex_t
*regexp
;
16 static int omit_values
;
17 static int use_key_regexp
;
19 static int do_not_match
;
20 static char delim
= '=';
21 static char key_delim
= ' ';
22 static char term
= '\n';
24 static int use_global_config
, use_system_config
, use_local_config
;
25 static struct git_config_source given_config_source
;
26 static int actions
, types
;
27 static const char *get_color_slot
, *get_colorbool_slot
;
29 static int respect_includes
= -1;
31 #define ACTION_GET (1<<0)
32 #define ACTION_GET_ALL (1<<1)
33 #define ACTION_GET_REGEXP (1<<2)
34 #define ACTION_REPLACE_ALL (1<<3)
35 #define ACTION_ADD (1<<4)
36 #define ACTION_UNSET (1<<5)
37 #define ACTION_UNSET_ALL (1<<6)
38 #define ACTION_RENAME_SECTION (1<<7)
39 #define ACTION_REMOVE_SECTION (1<<8)
40 #define ACTION_LIST (1<<9)
41 #define ACTION_EDIT (1<<10)
42 #define ACTION_SET (1<<11)
43 #define ACTION_SET_ALL (1<<12)
44 #define ACTION_GET_COLOR (1<<13)
45 #define ACTION_GET_COLORBOOL (1<<14)
46 #define ACTION_GET_URLMATCH (1<<15)
48 #define TYPE_BOOL (1<<0)
49 #define TYPE_INT (1<<1)
50 #define TYPE_BOOL_OR_INT (1<<2)
51 #define TYPE_PATH (1<<3)
53 static struct option builtin_config_options
[] = {
54 OPT_GROUP(N_("Config file location")),
55 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
56 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
57 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
58 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
59 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
60 OPT_GROUP(N_("Action")),
61 OPT_BIT(0, "get", &actions
, N_("get value: name [value-regex]"), ACTION_GET
),
62 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-regex]"), ACTION_GET_ALL
),
63 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP
),
64 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
65 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL
),
66 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
67 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-regex]"), ACTION_UNSET
),
68 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL
),
69 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
70 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
71 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
72 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
73 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
74 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
75 OPT_GROUP(N_("Type")),
76 OPT_BIT(0, "bool", &types
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
77 OPT_BIT(0, "int", &types
, N_("value is decimal number"), TYPE_INT
),
78 OPT_BIT(0, "bool-or-int", &types
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
79 OPT_BIT(0, "path", &types
, N_("value is a path (file or directory name)"), TYPE_PATH
),
80 OPT_GROUP(N_("Other")),
81 OPT_BOOL('z', "null", &end_null
, N_("terminate values with NUL byte")),
82 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
83 OPT_BOOL(0, "includes", &respect_includes
, N_("respect include directives on lookup")),
87 static void check_argc(int argc
, int min
, int max
) {
88 if (argc
>= min
&& argc
<= max
)
90 error("wrong number of arguments");
91 usage_with_options(builtin_config_usage
, builtin_config_options
);
94 static int show_all_config(const char *key_
, const char *value_
, void *cb
)
96 if (!omit_values
&& value_
)
97 printf("%s%c%s%c", key_
, delim
, value_
, term
);
99 printf("%s%c", key_
, term
);
104 struct strbuf
*items
;
109 static int format_config(struct strbuf
*buf
, const char *key_
, const char *value_
)
112 strbuf_addstr(buf
, key_
);
115 strbuf_addch(buf
, key_delim
);
117 if (types
== TYPE_INT
)
118 strbuf_addf(buf
, "%"PRId64
,
119 git_config_int64(key_
, value_
? value_
: ""));
120 else if (types
== TYPE_BOOL
)
121 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
123 else if (types
== TYPE_BOOL_OR_INT
) {
125 v
= git_config_bool_or_int(key_
, value_
, &is_bool
);
127 strbuf_addstr(buf
, v
? "true" : "false");
129 strbuf_addf(buf
, "%d", v
);
130 } else if (types
== TYPE_PATH
) {
132 if (git_config_pathname(&v
, key_
, value_
) < 0)
134 strbuf_addstr(buf
, v
);
137 strbuf_addstr(buf
, value_
);
139 /* Just show the key name; back out delimiter */
141 strbuf_setlen(buf
, buf
->len
- 1);
144 strbuf_addch(buf
, term
);
148 static int collect_config(const char *key_
, const char *value_
, void *cb
)
150 struct strbuf_list
*values
= cb
;
152 if (!use_key_regexp
&& strcmp(key_
, key
))
154 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
156 if (regexp
!= NULL
&&
157 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
160 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
161 strbuf_init(&values
->items
[values
->nr
], 0);
163 return format_config(&values
->items
[values
->nr
++], key_
, value_
);
166 static int get_value(const char *key_
, const char *regex_
)
168 int ret
= CONFIG_GENERIC_ERROR
;
169 struct strbuf_list values
= {NULL
};
172 if (use_key_regexp
) {
176 * NEEDSWORK: this naive pattern lowercasing obviously does not
177 * work for more complex patterns like "^[^.]*Foo.*bar".
178 * Perhaps we should deprecate this altogether someday.
182 for (tl
= key
+ strlen(key
) - 1;
183 tl
>= key
&& *tl
!= '.';
186 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
189 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
190 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
191 error("invalid key pattern: %s", key_
);
194 ret
= CONFIG_INVALID_PATTERN
;
198 if (git_config_parse_key(key_
, &key
, NULL
)) {
199 ret
= CONFIG_INVALID_KEY
;
205 if (regex_
[0] == '!') {
210 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
211 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
212 error("invalid pattern: %s", regex_
);
215 ret
= CONFIG_INVALID_PATTERN
;
220 git_config_with_options(collect_config
, &values
,
221 &given_config_source
, respect_includes
);
225 for (i
= 0; i
< values
.nr
; i
++) {
226 struct strbuf
*buf
= values
.items
+ i
;
227 if (do_all
|| i
== values
.nr
- 1)
228 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
247 static char *normalize_value(const char *key
, const char *value
)
252 if (types
== 0 || types
== TYPE_PATH
)
254 * We don't do normalization for TYPE_PATH here: If
255 * the path is like ~/foobar/, we prefer to store
256 * "~/foobar/" in the config file, and to expand the ~
257 * when retrieving the value.
259 return xstrdup(value
);
260 if (types
== TYPE_INT
)
261 return xstrfmt("%"PRId64
, git_config_int64(key
, value
));
262 if (types
== TYPE_BOOL
)
263 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
264 if (types
== TYPE_BOOL_OR_INT
) {
266 v
= git_config_bool_or_int(key
, value
, &is_bool
);
268 return xstrfmt("%d", v
);
270 return xstrdup(v
? "true" : "false");
273 die("BUG: cannot normalize type %d", types
);
276 static int get_color_found
;
277 static const char *get_color_slot
;
278 static const char *get_colorbool_slot
;
279 static char parsed_color
[COLOR_MAXLEN
];
281 static int git_get_color_config(const char *var
, const char *value
, void *cb
)
283 if (!strcmp(var
, get_color_slot
)) {
285 config_error_nonbool(var
);
286 if (color_parse(value
, parsed_color
) < 0)
293 static void get_color(const char *var
, const char *def_color
)
295 get_color_slot
= var
;
297 parsed_color
[0] = '\0';
298 git_config_with_options(git_get_color_config
, NULL
,
299 &given_config_source
, respect_includes
);
301 if (!get_color_found
&& def_color
) {
302 if (color_parse(def_color
, parsed_color
) < 0)
303 die(_("unable to parse default color value"));
306 fputs(parsed_color
, stdout
);
309 static int get_colorbool_found
;
310 static int get_diff_color_found
;
311 static int get_color_ui_found
;
312 static int git_get_colorbool_config(const char *var
, const char *value
,
315 if (!strcmp(var
, get_colorbool_slot
))
316 get_colorbool_found
= git_config_colorbool(var
, value
);
317 else if (!strcmp(var
, "diff.color"))
318 get_diff_color_found
= git_config_colorbool(var
, value
);
319 else if (!strcmp(var
, "color.ui"))
320 get_color_ui_found
= git_config_colorbool(var
, value
);
324 static int get_colorbool(const char *var
, int print
)
326 get_colorbool_slot
= var
;
327 get_colorbool_found
= -1;
328 get_diff_color_found
= -1;
329 get_color_ui_found
= -1;
330 git_config_with_options(git_get_colorbool_config
, NULL
,
331 &given_config_source
, respect_includes
);
333 if (get_colorbool_found
< 0) {
334 if (!strcmp(get_colorbool_slot
, "color.diff"))
335 get_colorbool_found
= get_diff_color_found
;
336 if (get_colorbool_found
< 0)
337 get_colorbool_found
= get_color_ui_found
;
340 if (get_colorbool_found
< 0)
341 /* default value if none found in config */
342 get_colorbool_found
= GIT_COLOR_AUTO
;
344 get_colorbool_found
= want_color(get_colorbool_found
);
347 printf("%s\n", get_colorbool_found
? "true" : "false");
350 return get_colorbool_found
? 0 : 1;
353 static void check_write(void)
355 if (!given_config_source
.file
&& !startup_info
->have_repository
)
356 die("not in a git directory");
358 if (given_config_source
.use_stdin
)
359 die("writing to stdin is not supported");
361 if (given_config_source
.blob
)
362 die("writing config blobs is not supported");
365 struct urlmatch_current_candidate_value
{
370 static int urlmatch_collect_fn(const char *var
, const char *value
, void *cb
)
372 struct string_list
*values
= cb
;
373 struct string_list_item
*item
= string_list_insert(values
, var
);
374 struct urlmatch_current_candidate_value
*matched
= item
->util
;
377 matched
= xmalloc(sizeof(*matched
));
378 strbuf_init(&matched
->value
, 0);
379 item
->util
= matched
;
381 strbuf_reset(&matched
->value
);
385 strbuf_addstr(&matched
->value
, value
);
386 matched
->value_is_null
= 0;
388 matched
->value_is_null
= 1;
393 static int get_urlmatch(const char *var
, const char *url
)
396 struct string_list_item
*item
;
397 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
398 struct string_list values
= STRING_LIST_INIT_DUP
;
400 config
.collect_fn
= urlmatch_collect_fn
;
401 config
.cascade_fn
= NULL
;
404 if (!url_normalize(url
, &config
.url
))
405 die("%s", config
.url
.err
);
407 config
.section
= xstrdup_tolower(var
);
408 section_tail
= strchr(config
.section
, '.');
410 *section_tail
= '\0';
411 config
.key
= section_tail
+ 1;
418 git_config_with_options(urlmatch_config_entry
, &config
,
419 &given_config_source
, respect_includes
);
421 for_each_string_list_item(item
, &values
) {
422 struct urlmatch_current_candidate_value
*matched
= item
->util
;
423 struct strbuf buf
= STRBUF_INIT
;
425 format_config(&buf
, item
->string
,
426 matched
->value_is_null
? NULL
: matched
->value
.buf
);
427 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
428 strbuf_release(&buf
);
430 strbuf_release(&matched
->value
);
432 string_list_clear(&config
.vars
, 1);
433 string_list_clear(&values
, 1);
434 free(config
.url
.url
);
436 free((void *)config
.section
);
440 static char *default_user_config(void)
442 struct strbuf buf
= STRBUF_INIT
;
444 _("# This is Git's per-user configuration file.\n"
446 "# Please adapt and uncomment the following lines:\n"
449 ident_default_name(),
450 ident_default_email());
451 return strbuf_detach(&buf
, NULL
);
454 int cmd_config(int argc
, const char **argv
, const char *prefix
)
456 int nongit
= !startup_info
->have_repository
;
459 given_config_source
.file
= getenv(CONFIG_ENVIRONMENT
);
461 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
462 builtin_config_usage
,
463 PARSE_OPT_STOP_AT_NON_OPTION
);
465 if (use_global_config
+ use_system_config
+ use_local_config
+
466 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
467 error("only one config file at a time.");
468 usage_with_options(builtin_config_usage
, builtin_config_options
);
471 if (given_config_source
.file
&&
472 !strcmp(given_config_source
.file
, "-")) {
473 given_config_source
.file
= NULL
;
474 given_config_source
.use_stdin
= 1;
477 if (use_global_config
) {
478 char *user_config
= expand_user_path("~/.gitconfig");
479 char *xdg_config
= xdg_config_home("config");
483 * It is unknown if HOME/.gitconfig exists, so
484 * we do not know if we should write to XDG
485 * location; error out even if XDG_CONFIG_HOME
486 * is set and points at a sane location.
488 die("$HOME not set");
490 if (access_or_warn(user_config
, R_OK
, 0) &&
491 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0))
492 given_config_source
.file
= xdg_config
;
494 given_config_source
.file
= user_config
;
496 else if (use_system_config
)
497 given_config_source
.file
= git_etc_gitconfig();
498 else if (use_local_config
)
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
,
505 given_config_source
.file
));
508 if (respect_includes
== -1)
509 respect_includes
= !given_config_source
.file
;
517 if (HAS_MULTI_BITS(types
)) {
518 error("only one type at a time.");
519 usage_with_options(builtin_config_usage
, builtin_config_options
);
522 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && types
) {
523 error("--get-color and variable type are incoherent");
524 usage_with_options(builtin_config_usage
, builtin_config_options
);
527 if (HAS_MULTI_BITS(actions
)) {
528 error("only one action at a time.");
529 usage_with_options(builtin_config_usage
, builtin_config_options
);
533 case 1: actions
= ACTION_GET
; break;
534 case 2: actions
= ACTION_SET
; break;
535 case 3: actions
= ACTION_SET_ALL
; break;
537 usage_with_options(builtin_config_usage
, builtin_config_options
);
540 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
541 error("--name-only is only applicable to --list or --get-regexp");
542 usage_with_options(builtin_config_usage
, builtin_config_options
);
544 if (actions
== ACTION_LIST
) {
545 check_argc(argc
, 0, 0);
546 if (git_config_with_options(show_all_config
, NULL
,
547 &given_config_source
,
548 respect_includes
) < 0) {
549 if (given_config_source
.file
)
550 die_errno("unable to read config file '%s'",
551 given_config_source
.file
);
553 die("error processing config file(s)");
556 else if (actions
== ACTION_EDIT
) {
559 check_argc(argc
, 0, 0);
560 if (!given_config_source
.file
&& nongit
)
561 die("not in a git directory");
562 if (given_config_source
.use_stdin
)
563 die("editing stdin is not supported");
564 if (given_config_source
.blob
)
565 die("editing blobs is not supported");
566 git_config(git_default_config
, NULL
);
567 config_file
= xstrdup(given_config_source
.file
?
568 given_config_source
.file
: git_path("config"));
569 if (use_global_config
) {
570 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
572 char *content
= default_user_config();
573 write_str_in_full(fd
, content
);
577 else if (errno
!= EEXIST
)
578 die_errno(_("cannot create configuration file %s"), config_file
);
580 launch_editor(config_file
, NULL
, NULL
);
583 else if (actions
== ACTION_SET
) {
586 check_argc(argc
, 2, 2);
587 value
= normalize_value(argv
[0], argv
[1]);
588 ret
= git_config_set_in_file(given_config_source
.file
, argv
[0], value
);
589 if (ret
== CONFIG_NOTHING_SET
)
590 error("cannot overwrite multiple values with a single value\n"
591 " Use a regexp, --add or --replace-all to change %s.", argv
[0]);
594 else if (actions
== ACTION_SET_ALL
) {
596 check_argc(argc
, 2, 3);
597 value
= normalize_value(argv
[0], argv
[1]);
598 return git_config_set_multivar_in_file(given_config_source
.file
,
599 argv
[0], value
, argv
[2], 0);
601 else if (actions
== ACTION_ADD
) {
603 check_argc(argc
, 2, 2);
604 value
= normalize_value(argv
[0], argv
[1]);
605 return git_config_set_multivar_in_file(given_config_source
.file
,
607 CONFIG_REGEX_NONE
, 0);
609 else if (actions
== ACTION_REPLACE_ALL
) {
611 check_argc(argc
, 2, 3);
612 value
= normalize_value(argv
[0], argv
[1]);
613 return git_config_set_multivar_in_file(given_config_source
.file
,
614 argv
[0], value
, argv
[2], 1);
616 else if (actions
== ACTION_GET
) {
617 check_argc(argc
, 1, 2);
618 return get_value(argv
[0], argv
[1]);
620 else if (actions
== ACTION_GET_ALL
) {
622 check_argc(argc
, 1, 2);
623 return get_value(argv
[0], argv
[1]);
625 else if (actions
== ACTION_GET_REGEXP
) {
629 check_argc(argc
, 1, 2);
630 return get_value(argv
[0], argv
[1]);
632 else if (actions
== ACTION_GET_URLMATCH
) {
633 check_argc(argc
, 2, 2);
634 return get_urlmatch(argv
[0], argv
[1]);
636 else if (actions
== ACTION_UNSET
) {
638 check_argc(argc
, 1, 2);
640 return git_config_set_multivar_in_file(given_config_source
.file
,
641 argv
[0], NULL
, argv
[1], 0);
643 return git_config_set_in_file(given_config_source
.file
,
646 else if (actions
== ACTION_UNSET_ALL
) {
648 check_argc(argc
, 1, 2);
649 return git_config_set_multivar_in_file(given_config_source
.file
,
650 argv
[0], NULL
, argv
[1], 1);
652 else if (actions
== ACTION_RENAME_SECTION
) {
655 check_argc(argc
, 2, 2);
656 ret
= git_config_rename_section_in_file(given_config_source
.file
,
661 die("No such section!");
663 else if (actions
== ACTION_REMOVE_SECTION
) {
666 check_argc(argc
, 1, 1);
667 ret
= git_config_rename_section_in_file(given_config_source
.file
,
672 die("No such section!");
674 else if (actions
== ACTION_GET_COLOR
) {
675 check_argc(argc
, 1, 2);
676 get_color(argv
[0], argv
[1]);
678 else if (actions
== ACTION_GET_COLORBOOL
) {
679 check_argc(argc
, 1, 2);
681 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
682 return get_colorbool(argv
[0], argc
== 2);