6 #include "environment.h"
7 #include "repository.h"
10 #include "parse-options.h"
18 static const char *const builtin_config_usage
[] = {
19 N_("git config [<options>]"),
24 static regex_t
*key_regexp
;
25 static const char *value_pattern
;
26 static regex_t
*regexp
;
28 static int omit_values
;
29 static int use_key_regexp
;
31 static int do_not_match
;
32 static char delim
= '=';
33 static char key_delim
= ' ';
34 static char term
= '\n';
36 static int use_global_config
, use_system_config
, use_local_config
;
37 static int use_worktree_config
;
38 static struct git_config_source given_config_source
;
39 static int actions
, type
;
40 static char *default_value
;
42 static int respect_includes_opt
= -1;
43 static struct config_options config_options
;
44 static int show_origin
;
45 static int show_scope
;
46 static int fixed_value
;
47 static const char *comment_arg
;
49 #define ACTION_GET (1<<0)
50 #define ACTION_GET_ALL (1<<1)
51 #define ACTION_GET_REGEXP (1<<2)
52 #define ACTION_REPLACE_ALL (1<<3)
53 #define ACTION_ADD (1<<4)
54 #define ACTION_UNSET (1<<5)
55 #define ACTION_UNSET_ALL (1<<6)
56 #define ACTION_RENAME_SECTION (1<<7)
57 #define ACTION_REMOVE_SECTION (1<<8)
58 #define ACTION_LIST (1<<9)
59 #define ACTION_EDIT (1<<10)
60 #define ACTION_SET (1<<11)
61 #define ACTION_SET_ALL (1<<12)
62 #define ACTION_GET_COLOR (1<<13)
63 #define ACTION_GET_COLORBOOL (1<<14)
64 #define ACTION_GET_URLMATCH (1<<15)
67 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
68 * one line of output and which should therefore be paged.
70 #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
71 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
75 #define TYPE_BOOL_OR_INT 3
77 #define TYPE_EXPIRY_DATE 5
79 #define TYPE_BOOL_OR_STR 7
81 #define OPT_CALLBACK_VALUE(s, l, v, h, i) \
82 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
83 PARSE_OPT_NONEG, option_parse_type, (i) }
85 static NORETURN
void usage_builtin_config(void);
87 static int option_parse_type(const struct option
*opt
, const char *arg
,
90 int new_type
, *to_type
;
93 *((int *) opt
->value
) = 0;
98 * To support '--<type>' style flags, begin with new_type equal to
101 new_type
= opt
->defval
;
103 if (!strcmp(arg
, "bool"))
104 new_type
= TYPE_BOOL
;
105 else if (!strcmp(arg
, "int"))
107 else if (!strcmp(arg
, "bool-or-int"))
108 new_type
= TYPE_BOOL_OR_INT
;
109 else if (!strcmp(arg
, "bool-or-str"))
110 new_type
= TYPE_BOOL_OR_STR
;
111 else if (!strcmp(arg
, "path"))
112 new_type
= TYPE_PATH
;
113 else if (!strcmp(arg
, "expiry-date"))
114 new_type
= TYPE_EXPIRY_DATE
;
115 else if (!strcmp(arg
, "color"))
116 new_type
= TYPE_COLOR
;
118 die(_("unrecognized --type argument, %s"), arg
);
121 to_type
= opt
->value
;
122 if (*to_type
&& *to_type
!= new_type
) {
124 * Complain when there is a new type not equal to the old type.
125 * This allows for combinations like '--int --type=int' and
126 * '--type=int --type=int', but disallows ones like '--type=bool
127 * --int' and '--type=bool
130 error(_("only one type at a time"));
131 usage_builtin_config();
138 static void check_argc(int argc
, int min
, int max
)
140 if (argc
>= min
&& argc
<= max
)
143 error(_("wrong number of arguments, should be %d"), min
);
145 error(_("wrong number of arguments, should be from %d to %d"),
147 usage_builtin_config();
150 static void show_config_origin(const struct key_value_info
*kvi
,
153 const char term
= end_nul
? '\0' : '\t';
155 strbuf_addstr(buf
, config_origin_type_name(kvi
->origin_type
));
156 strbuf_addch(buf
, ':');
158 strbuf_addstr(buf
, kvi
->filename
? kvi
->filename
: "");
160 quote_c_style(kvi
->filename
? kvi
->filename
: "", buf
, NULL
, 0);
161 strbuf_addch(buf
, term
);
164 static void show_config_scope(const struct key_value_info
*kvi
,
167 const char term
= end_nul
? '\0' : '\t';
168 const char *scope
= config_scope_name(kvi
->scope
);
170 strbuf_addstr(buf
, N_(scope
));
171 strbuf_addch(buf
, term
);
174 static int show_all_config(const char *key_
, const char *value_
,
175 const struct config_context
*ctx
,
178 const struct key_value_info
*kvi
= ctx
->kvi
;
180 if (show_origin
|| show_scope
) {
181 struct strbuf buf
= STRBUF_INIT
;
183 show_config_scope(kvi
, &buf
);
185 show_config_origin(kvi
, &buf
);
186 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
187 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
188 strbuf_release(&buf
);
190 if (!omit_values
&& value_
)
191 printf("%s%c%s%c", key_
, delim
, value_
, term
);
193 printf("%s%c", key_
, term
);
198 struct strbuf
*items
;
203 static int format_config(struct strbuf
*buf
, const char *key_
,
204 const char *value_
, const struct key_value_info
*kvi
)
207 show_config_scope(kvi
, buf
);
209 show_config_origin(kvi
, buf
);
211 strbuf_addstr(buf
, key_
);
214 strbuf_addch(buf
, key_delim
);
216 if (type
== TYPE_INT
)
217 strbuf_addf(buf
, "%"PRId64
,
218 git_config_int64(key_
, value_
? value_
: "", kvi
));
219 else if (type
== TYPE_BOOL
)
220 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
222 else if (type
== TYPE_BOOL_OR_INT
) {
224 v
= git_config_bool_or_int(key_
, value_
, kvi
,
227 strbuf_addstr(buf
, v
? "true" : "false");
229 strbuf_addf(buf
, "%d", v
);
230 } else if (type
== TYPE_BOOL_OR_STR
) {
231 int v
= git_parse_maybe_bool(value_
);
233 strbuf_addstr(buf
, value_
);
235 strbuf_addstr(buf
, v
? "true" : "false");
236 } else if (type
== TYPE_PATH
) {
238 if (git_config_pathname(&v
, key_
, value_
) < 0)
240 strbuf_addstr(buf
, v
);
242 } else if (type
== TYPE_EXPIRY_DATE
) {
244 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
246 strbuf_addf(buf
, "%"PRItime
, t
);
247 } else if (type
== TYPE_COLOR
) {
248 char v
[COLOR_MAXLEN
];
249 if (git_config_color(v
, key_
, value_
) < 0)
251 strbuf_addstr(buf
, v
);
253 strbuf_addstr(buf
, value_
);
255 /* Just show the key name; back out delimiter */
257 strbuf_setlen(buf
, buf
->len
- 1);
260 strbuf_addch(buf
, term
);
264 static int collect_config(const char *key_
, const char *value_
,
265 const struct config_context
*ctx
, void *cb
)
267 struct strbuf_list
*values
= cb
;
268 const struct key_value_info
*kvi
= ctx
->kvi
;
270 if (!use_key_regexp
&& strcmp(key_
, key
))
272 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
274 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
276 if (regexp
!= NULL
&&
277 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
280 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
281 strbuf_init(&values
->items
[values
->nr
], 0);
283 return format_config(&values
->items
[values
->nr
++], key_
, value_
, kvi
);
286 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
288 int ret
= CONFIG_GENERIC_ERROR
;
289 struct strbuf_list values
= {NULL
};
292 if (use_key_regexp
) {
296 * NEEDSWORK: this naive pattern lowercasing obviously does not
297 * work for more complex patterns like "^[^.]*Foo.*bar".
298 * Perhaps we should deprecate this altogether someday.
302 for (tl
= key
+ strlen(key
) - 1;
303 tl
>= key
&& *tl
!= '.';
306 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
309 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
310 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
311 error(_("invalid key pattern: %s"), key_
);
312 FREE_AND_NULL(key_regexp
);
313 ret
= CONFIG_INVALID_PATTERN
;
317 if (git_config_parse_key(key_
, &key
, NULL
)) {
318 ret
= CONFIG_INVALID_KEY
;
323 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
324 value_pattern
= regex_
;
326 if (regex_
[0] == '!') {
331 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
332 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
333 error(_("invalid pattern: %s"), regex_
);
334 FREE_AND_NULL(regexp
);
335 ret
= CONFIG_INVALID_PATTERN
;
340 config_with_options(collect_config
, &values
,
341 &given_config_source
, the_repository
,
344 if (!values
.nr
&& default_value
) {
345 struct key_value_info kvi
= KVI_INIT
;
348 kvi_from_param(&kvi
);
349 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
350 item
= &values
.items
[values
.nr
++];
351 strbuf_init(item
, 0);
352 if (format_config(item
, key_
, default_value
, &kvi
) < 0)
353 die(_("failed to format default config value: %s"),
359 for (i
= 0; i
< values
.nr
; i
++) {
360 struct strbuf
*buf
= values
.items
+ i
;
361 if (do_all
|| i
== values
.nr
- 1)
362 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
381 static char *normalize_value(const char *key
, const char *value
,
382 struct key_value_info
*kvi
)
387 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
389 * We don't do normalization for TYPE_PATH here: If
390 * the path is like ~/foobar/, we prefer to store
391 * "~/foobar/" in the config file, and to expand the ~
392 * when retrieving the value.
393 * Also don't do normalization for expiry dates.
395 return xstrdup(value
);
396 if (type
== TYPE_INT
)
397 return xstrfmt("%"PRId64
, git_config_int64(key
, value
, kvi
));
398 if (type
== TYPE_BOOL
)
399 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
400 if (type
== TYPE_BOOL_OR_INT
) {
402 v
= git_config_bool_or_int(key
, value
, kvi
, &is_bool
);
404 return xstrfmt("%d", v
);
406 return xstrdup(v
? "true" : "false");
408 if (type
== TYPE_BOOL_OR_STR
) {
409 int v
= git_parse_maybe_bool(value
);
411 return xstrdup(value
);
413 return xstrdup(v
? "true" : "false");
415 if (type
== TYPE_COLOR
) {
416 char v
[COLOR_MAXLEN
];
417 if (git_config_color(v
, key
, value
))
418 die(_("cannot parse color '%s'"), value
);
421 * The contents of `v` now contain an ANSI escape
422 * sequence, not suitable for including within a
423 * configuration file. Treat the above as a
424 * "sanity-check", and return the given value, which we
425 * know is representable as valid color code.
427 return xstrdup(value
);
430 BUG("cannot normalize type %d", type
);
433 static int get_color_found
;
434 static const char *get_color_slot
;
435 static const char *get_colorbool_slot
;
436 static char parsed_color
[COLOR_MAXLEN
];
438 static int git_get_color_config(const char *var
, const char *value
,
439 const struct config_context
*ctx UNUSED
,
442 if (!strcmp(var
, get_color_slot
)) {
444 config_error_nonbool(var
);
445 if (color_parse(value
, parsed_color
) < 0)
452 static void get_color(const char *var
, const char *def_color
)
454 get_color_slot
= var
;
456 parsed_color
[0] = '\0';
457 config_with_options(git_get_color_config
, NULL
,
458 &given_config_source
, the_repository
,
461 if (!get_color_found
&& def_color
) {
462 if (color_parse(def_color
, parsed_color
) < 0)
463 die(_("unable to parse default color value"));
466 fputs(parsed_color
, stdout
);
469 static int get_colorbool_found
;
470 static int get_diff_color_found
;
471 static int get_color_ui_found
;
472 static int git_get_colorbool_config(const char *var
, const char *value
,
473 const struct config_context
*ctx UNUSED
,
476 if (!strcmp(var
, get_colorbool_slot
))
477 get_colorbool_found
= git_config_colorbool(var
, value
);
478 else if (!strcmp(var
, "diff.color"))
479 get_diff_color_found
= git_config_colorbool(var
, value
);
480 else if (!strcmp(var
, "color.ui"))
481 get_color_ui_found
= git_config_colorbool(var
, value
);
485 static int get_colorbool(const char *var
, int print
)
487 get_colorbool_slot
= var
;
488 get_colorbool_found
= -1;
489 get_diff_color_found
= -1;
490 get_color_ui_found
= -1;
491 config_with_options(git_get_colorbool_config
, NULL
,
492 &given_config_source
, the_repository
,
495 if (get_colorbool_found
< 0) {
496 if (!strcmp(get_colorbool_slot
, "color.diff"))
497 get_colorbool_found
= get_diff_color_found
;
498 if (get_colorbool_found
< 0)
499 get_colorbool_found
= get_color_ui_found
;
502 if (get_colorbool_found
< 0)
503 /* default value if none found in config */
504 get_colorbool_found
= GIT_COLOR_AUTO
;
506 get_colorbool_found
= want_color(get_colorbool_found
);
509 printf("%s\n", get_colorbool_found
? "true" : "false");
512 return get_colorbool_found
? 0 : 1;
515 static void check_write(void)
517 if (!given_config_source
.file
&& !startup_info
->have_repository
)
518 die(_("not in a git directory"));
520 if (given_config_source
.use_stdin
)
521 die(_("writing to stdin is not supported"));
523 if (given_config_source
.blob
)
524 die(_("writing config blobs is not supported"));
527 struct urlmatch_current_candidate_value
{
530 struct key_value_info kvi
;
533 static int urlmatch_collect_fn(const char *var
, const char *value
,
534 const struct config_context
*ctx
,
537 struct string_list
*values
= cb
;
538 struct string_list_item
*item
= string_list_insert(values
, var
);
539 struct urlmatch_current_candidate_value
*matched
= item
->util
;
540 const struct key_value_info
*kvi
= ctx
->kvi
;
543 matched
= xmalloc(sizeof(*matched
));
544 strbuf_init(&matched
->value
, 0);
545 item
->util
= matched
;
547 strbuf_reset(&matched
->value
);
552 strbuf_addstr(&matched
->value
, value
);
553 matched
->value_is_null
= 0;
555 matched
->value_is_null
= 1;
560 static int get_urlmatch(const char *var
, const char *url
)
564 struct string_list_item
*item
;
565 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
566 struct string_list values
= STRING_LIST_INIT_DUP
;
568 config
.collect_fn
= urlmatch_collect_fn
;
569 config
.cascade_fn
= NULL
;
572 if (!url_normalize(url
, &config
.url
))
573 die("%s", config
.url
.err
);
575 config
.section
= xstrdup_tolower(var
);
576 section_tail
= strchr(config
.section
, '.');
578 *section_tail
= '\0';
579 config
.key
= section_tail
+ 1;
586 config_with_options(urlmatch_config_entry
, &config
,
587 &given_config_source
, the_repository
,
592 for_each_string_list_item(item
, &values
) {
593 struct urlmatch_current_candidate_value
*matched
= item
->util
;
594 struct strbuf buf
= STRBUF_INIT
;
596 format_config(&buf
, item
->string
,
597 matched
->value_is_null
? NULL
: matched
->value
.buf
,
599 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
600 strbuf_release(&buf
);
602 strbuf_release(&matched
->value
);
604 urlmatch_config_release(&config
);
605 string_list_clear(&values
, 1);
606 free(config
.url
.url
);
608 free((void *)config
.section
);
612 static char *default_user_config(void)
614 struct strbuf buf
= STRBUF_INIT
;
616 _("# This is Git's per-user configuration file.\n"
618 "# Please adapt and uncomment the following lines:\n"
621 ident_default_name(),
622 ident_default_email());
623 return strbuf_detach(&buf
, NULL
);
626 static void handle_config_location(const char *prefix
)
628 if (use_global_config
+ use_system_config
+ use_local_config
+
629 use_worktree_config
+
630 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
631 error(_("only one config file at a time"));
632 usage_builtin_config();
635 if (!startup_info
->have_repository
) {
636 if (use_local_config
)
637 die(_("--local can only be used inside a git repository"));
638 if (given_config_source
.blob
)
639 die(_("--blob can only be used inside a git repository"));
640 if (use_worktree_config
)
641 die(_("--worktree can only be used inside a git repository"));
644 if (given_config_source
.file
&&
645 !strcmp(given_config_source
.file
, "-")) {
646 given_config_source
.file
= NULL
;
647 given_config_source
.use_stdin
= 1;
648 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
651 if (use_global_config
) {
652 given_config_source
.file
= git_global_config();
653 if (!given_config_source
.file
)
655 * It is unknown if HOME/.gitconfig exists, so
656 * we do not know if we should write to XDG
657 * location; error out even if XDG_CONFIG_HOME
658 * is set and points at a sane location.
660 die(_("$HOME not set"));
661 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
662 } else if (use_system_config
) {
663 given_config_source
.file
= git_system_config();
664 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
665 } else if (use_local_config
) {
666 given_config_source
.file
= git_pathdup("config");
667 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
668 } else if (use_worktree_config
) {
669 struct worktree
**worktrees
= get_worktrees();
670 if (the_repository
->repository_format_worktree_config
)
671 given_config_source
.file
= git_pathdup("config.worktree");
672 else if (worktrees
[0] && worktrees
[1])
673 die(_("--worktree cannot be used with multiple "
674 "working trees unless the config\n"
675 "extension worktreeConfig is enabled. "
676 "Please read \"CONFIGURATION FILE\"\n"
677 "section in \"git help worktree\" for details"));
679 given_config_source
.file
= git_pathdup("config");
680 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
681 free_worktrees(worktrees
);
682 } else if (given_config_source
.file
) {
683 if (!is_absolute_path(given_config_source
.file
) && prefix
)
684 given_config_source
.file
=
685 prefix_filename(prefix
, given_config_source
.file
);
686 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
687 } else if (given_config_source
.blob
) {
688 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
691 if (respect_includes_opt
== -1)
692 config_options
.respect_includes
= !given_config_source
.file
;
694 config_options
.respect_includes
= respect_includes_opt
;
695 if (startup_info
->have_repository
) {
696 config_options
.commondir
= get_git_common_dir();
697 config_options
.git_dir
= get_git_dir();
701 static struct option builtin_config_options
[] = {
702 OPT_GROUP(N_("Config file location")),
703 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
704 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
705 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
706 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
707 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
708 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
709 OPT_GROUP(N_("Action")),
710 OPT_CMDMODE(0, "get", &actions
, N_("get value: name [<value-pattern>]"), ACTION_GET
),
711 OPT_CMDMODE(0, "get-all", &actions
, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL
),
712 OPT_CMDMODE(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP
),
713 OPT_CMDMODE(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
714 OPT_CMDMODE(0, "replace-all", &actions
, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL
),
715 OPT_CMDMODE(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
716 OPT_CMDMODE(0, "unset", &actions
, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET
),
717 OPT_CMDMODE(0, "unset-all", &actions
, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL
),
718 OPT_CMDMODE(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
719 OPT_CMDMODE(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
720 OPT_CMDMODE('l', "list", &actions
, N_("list all"), ACTION_LIST
),
721 OPT_CMDMODE('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
722 OPT_CMDMODE(0, "get-color", &actions
, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR
),
723 OPT_CMDMODE(0, "get-colorbool", &actions
, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL
),
724 OPT_GROUP(N_("Type")),
725 OPT_CALLBACK('t', "type", &type
, N_("type"), N_("value is given this type"), option_parse_type
),
726 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
727 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
728 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
729 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
730 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
731 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
732 OPT_GROUP(N_("Other")),
733 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
734 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
735 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
736 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
737 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
738 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
739 OPT_STRING(0, "comment", &comment_arg
, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
740 OPT_BOOL(0, "fixed-value", &fixed_value
, N_("use string equality when comparing values to 'value-pattern'")),
744 static NORETURN
void usage_builtin_config(void)
746 usage_with_options(builtin_config_usage
, builtin_config_options
);
749 int cmd_config(int argc
, const char **argv
, const char *prefix
)
751 char *value
= NULL
, *comment
= NULL
;
754 struct key_value_info default_kvi
= KVI_INIT
;
756 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
758 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
759 builtin_config_usage
,
760 PARSE_OPT_STOP_AT_NON_OPTION
);
762 handle_config_location(prefix
);
770 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
771 error(_("--get-color and variable type are incoherent"));
772 usage_builtin_config();
777 case 1: actions
= ACTION_GET
; break;
778 case 2: actions
= ACTION_SET
; break;
779 case 3: actions
= ACTION_SET_ALL
; break;
781 usage_builtin_config();
784 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
785 error(_("--name-only is only applicable to --list or --get-regexp"));
786 usage_builtin_config();
789 if (show_origin
&& !(actions
&
790 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
791 error(_("--show-origin is only applicable to --get, --get-all, "
792 "--get-regexp, and --list"));
793 usage_builtin_config();
796 if (default_value
&& !(actions
& ACTION_GET
)) {
797 error(_("--default is only applicable to --get"));
798 usage_builtin_config();
802 !(actions
& (ACTION_ADD
|ACTION_SET
|ACTION_SET_ALL
|ACTION_REPLACE_ALL
))) {
803 error(_("--comment is only applicable to add/set/replace operations"));
804 usage_builtin_config();
807 /* check usage of --fixed-value */
809 int allowed_usage
= 0;
812 /* git config --get <name> <value-pattern> */
814 /* git config --get-all <name> <value-pattern> */
816 /* git config --get-regexp <name-pattern> <value-pattern> */
817 case ACTION_GET_REGEXP
:
818 /* git config --unset <name> <value-pattern> */
820 /* git config --unset-all <name> <value-pattern> */
821 case ACTION_UNSET_ALL
:
822 allowed_usage
= argc
> 1 && !!argv
[1];
825 /* git config <name> <value> <value-pattern> */
827 /* git config --replace-all <name> <value> <value-pattern> */
828 case ACTION_REPLACE_ALL
:
829 allowed_usage
= argc
> 2 && !!argv
[2];
832 /* other options don't allow --fixed-value */
835 if (!allowed_usage
) {
836 error(_("--fixed-value only applies with 'value-pattern'"));
837 usage_builtin_config();
840 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
843 comment
= git_config_prepare_comment_string(comment_arg
);
845 if (actions
& PAGING_ACTIONS
)
846 setup_auto_pager("config", 1);
848 if (actions
== ACTION_LIST
) {
849 check_argc(argc
, 0, 0);
850 if (config_with_options(show_all_config
, NULL
,
851 &given_config_source
, the_repository
,
852 &config_options
) < 0) {
853 if (given_config_source
.file
)
854 die_errno(_("unable to read config file '%s'"),
855 given_config_source
.file
);
857 die(_("error processing config file(s)"));
860 else if (actions
== ACTION_EDIT
) {
863 check_argc(argc
, 0, 0);
864 if (!given_config_source
.file
&& !startup_info
->have_repository
)
865 die(_("not in a git directory"));
866 if (given_config_source
.use_stdin
)
867 die(_("editing stdin is not supported"));
868 if (given_config_source
.blob
)
869 die(_("editing blobs is not supported"));
870 git_config(git_default_config
, NULL
);
871 config_file
= given_config_source
.file
?
872 xstrdup(given_config_source
.file
) :
873 git_pathdup("config");
874 if (use_global_config
) {
875 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
877 char *content
= default_user_config();
878 write_str_in_full(fd
, content
);
882 else if (errno
!= EEXIST
)
883 die_errno(_("cannot create configuration file %s"), config_file
);
885 launch_editor(config_file
, NULL
, NULL
);
888 else if (actions
== ACTION_SET
) {
890 check_argc(argc
, 2, 2);
891 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
892 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], comment
, value
);
893 if (ret
== CONFIG_NOTHING_SET
)
894 error(_("cannot overwrite multiple values with a single value\n"
895 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
897 else if (actions
== ACTION_SET_ALL
) {
899 check_argc(argc
, 2, 3);
900 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
901 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
902 argv
[0], value
, argv
[2],
905 else if (actions
== ACTION_ADD
) {
907 check_argc(argc
, 2, 2);
908 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
909 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
914 else if (actions
== ACTION_REPLACE_ALL
) {
916 check_argc(argc
, 2, 3);
917 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
918 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
919 argv
[0], value
, argv
[2],
920 comment
, flags
| CONFIG_FLAGS_MULTI_REPLACE
);
922 else if (actions
== ACTION_GET
) {
923 check_argc(argc
, 1, 2);
924 return get_value(argv
[0], argv
[1], flags
);
926 else if (actions
== ACTION_GET_ALL
) {
928 check_argc(argc
, 1, 2);
929 return get_value(argv
[0], argv
[1], flags
);
931 else if (actions
== ACTION_GET_REGEXP
) {
935 check_argc(argc
, 1, 2);
936 return get_value(argv
[0], argv
[1], flags
);
938 else if (actions
== ACTION_GET_URLMATCH
) {
939 check_argc(argc
, 2, 2);
940 return get_urlmatch(argv
[0], argv
[1]);
942 else if (actions
== ACTION_UNSET
) {
944 check_argc(argc
, 1, 2);
946 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
947 argv
[0], NULL
, argv
[1],
950 return git_config_set_in_file_gently(given_config_source
.file
,
951 argv
[0], NULL
, NULL
);
953 else if (actions
== ACTION_UNSET_ALL
) {
955 check_argc(argc
, 1, 2);
956 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
957 argv
[0], NULL
, argv
[1],
958 NULL
, flags
| CONFIG_FLAGS_MULTI_REPLACE
);
960 else if (actions
== ACTION_RENAME_SECTION
) {
962 check_argc(argc
, 2, 2);
963 ret
= git_config_rename_section_in_file(given_config_source
.file
,
968 die(_("no such section: %s"), argv
[0]);
972 else if (actions
== ACTION_REMOVE_SECTION
) {
974 check_argc(argc
, 1, 1);
975 ret
= git_config_rename_section_in_file(given_config_source
.file
,
980 die(_("no such section: %s"), argv
[0]);
984 else if (actions
== ACTION_GET_COLOR
) {
985 check_argc(argc
, 1, 2);
986 get_color(argv
[0], argv
[1]);
988 else if (actions
== ACTION_GET_COLORBOOL
) {
989 check_argc(argc
, 1, 2);
991 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
992 return get_colorbool(argv
[0], argc
== 2);