7 #include "environment.h"
8 #include "repository.h"
11 #include "parse-options.h"
19 static const char *const builtin_config_usage
[] = {
20 N_("git config [<options>]"),
25 static regex_t
*key_regexp
;
26 static const char *value_pattern
;
27 static regex_t
*regexp
;
29 static int omit_values
;
30 static int use_key_regexp
;
32 static int do_not_match
;
33 static char delim
= '=';
34 static char key_delim
= ' ';
35 static char term
= '\n';
37 static int use_global_config
, use_system_config
, use_local_config
;
38 static int use_worktree_config
;
39 static struct git_config_source given_config_source
;
40 static int actions
, type
;
41 static char *default_value
;
43 static int respect_includes_opt
= -1;
44 static struct config_options config_options
;
45 static int show_origin
;
46 static int show_scope
;
47 static int fixed_value
;
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 struct option builtin_config_options
[] = {
139 OPT_GROUP(N_("Config file location")),
140 OPT_BOOL(0, "global", &use_global_config
, N_("use global config file")),
141 OPT_BOOL(0, "system", &use_system_config
, N_("use system config file")),
142 OPT_BOOL(0, "local", &use_local_config
, N_("use repository config file")),
143 OPT_BOOL(0, "worktree", &use_worktree_config
, N_("use per-worktree config file")),
144 OPT_STRING('f', "file", &given_config_source
.file
, N_("file"), N_("use given config file")),
145 OPT_STRING(0, "blob", &given_config_source
.blob
, N_("blob-id"), N_("read config from given blob object")),
146 OPT_GROUP(N_("Action")),
147 OPT_BIT(0, "get", &actions
, N_("get value: name [value-pattern]"), ACTION_GET
),
148 OPT_BIT(0, "get-all", &actions
, N_("get all values: key [value-pattern]"), ACTION_GET_ALL
),
149 OPT_BIT(0, "get-regexp", &actions
, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP
),
150 OPT_BIT(0, "get-urlmatch", &actions
, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH
),
151 OPT_BIT(0, "replace-all", &actions
, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL
),
152 OPT_BIT(0, "add", &actions
, N_("add a new variable: name value"), ACTION_ADD
),
153 OPT_BIT(0, "unset", &actions
, N_("remove a variable: name [value-pattern]"), ACTION_UNSET
),
154 OPT_BIT(0, "unset-all", &actions
, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL
),
155 OPT_BIT(0, "rename-section", &actions
, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION
),
156 OPT_BIT(0, "remove-section", &actions
, N_("remove a section: name"), ACTION_REMOVE_SECTION
),
157 OPT_BIT('l', "list", &actions
, N_("list all"), ACTION_LIST
),
158 OPT_BOOL(0, "fixed-value", &fixed_value
, N_("use string equality when comparing values to 'value-pattern'")),
159 OPT_BIT('e', "edit", &actions
, N_("open an editor"), ACTION_EDIT
),
160 OPT_BIT(0, "get-color", &actions
, N_("find the color configured: slot [default]"), ACTION_GET_COLOR
),
161 OPT_BIT(0, "get-colorbool", &actions
, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL
),
162 OPT_GROUP(N_("Type")),
163 OPT_CALLBACK('t', "type", &type
, N_("type"), N_("value is given this type"), option_parse_type
),
164 OPT_CALLBACK_VALUE(0, "bool", &type
, N_("value is \"true\" or \"false\""), TYPE_BOOL
),
165 OPT_CALLBACK_VALUE(0, "int", &type
, N_("value is decimal number"), TYPE_INT
),
166 OPT_CALLBACK_VALUE(0, "bool-or-int", &type
, N_("value is --bool or --int"), TYPE_BOOL_OR_INT
),
167 OPT_CALLBACK_VALUE(0, "bool-or-str", &type
, N_("value is --bool or string"), TYPE_BOOL_OR_STR
),
168 OPT_CALLBACK_VALUE(0, "path", &type
, N_("value is a path (file or directory name)"), TYPE_PATH
),
169 OPT_CALLBACK_VALUE(0, "expiry-date", &type
, N_("value is an expiry date"), TYPE_EXPIRY_DATE
),
170 OPT_GROUP(N_("Other")),
171 OPT_BOOL('z', "null", &end_nul
, N_("terminate values with NUL byte")),
172 OPT_BOOL(0, "name-only", &omit_values
, N_("show variable names only")),
173 OPT_BOOL(0, "includes", &respect_includes_opt
, N_("respect include directives on lookup")),
174 OPT_BOOL(0, "show-origin", &show_origin
, N_("show origin of config (file, standard input, blob, command line)")),
175 OPT_BOOL(0, "show-scope", &show_scope
, N_("show scope of config (worktree, local, global, system, command)")),
176 OPT_STRING(0, "default", &default_value
, N_("value"), N_("with --get, use default value when missing entry")),
180 static NORETURN
void usage_builtin_config(void)
182 usage_with_options(builtin_config_usage
, builtin_config_options
);
185 static void check_argc(int argc
, int min
, int max
)
187 if (argc
>= min
&& argc
<= max
)
190 error(_("wrong number of arguments, should be %d"), min
);
192 error(_("wrong number of arguments, should be from %d to %d"),
194 usage_builtin_config();
197 static void show_config_origin(const struct key_value_info
*kvi
,
200 const char term
= end_nul
? '\0' : '\t';
202 strbuf_addstr(buf
, config_origin_type_name(kvi
->origin_type
));
203 strbuf_addch(buf
, ':');
205 strbuf_addstr(buf
, kvi
->filename
? kvi
->filename
: "");
207 quote_c_style(kvi
->filename
? kvi
->filename
: "", buf
, NULL
, 0);
208 strbuf_addch(buf
, term
);
211 static void show_config_scope(const struct key_value_info
*kvi
,
214 const char term
= end_nul
? '\0' : '\t';
215 const char *scope
= config_scope_name(kvi
->scope
);
217 strbuf_addstr(buf
, N_(scope
));
218 strbuf_addch(buf
, term
);
221 static int show_all_config(const char *key_
, const char *value_
,
222 const struct config_context
*ctx
,
225 const struct key_value_info
*kvi
= ctx
->kvi
;
227 if (show_origin
|| show_scope
) {
228 struct strbuf buf
= STRBUF_INIT
;
230 show_config_scope(kvi
, &buf
);
232 show_config_origin(kvi
, &buf
);
233 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
234 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
235 strbuf_release(&buf
);
237 if (!omit_values
&& value_
)
238 printf("%s%c%s%c", key_
, delim
, value_
, term
);
240 printf("%s%c", key_
, term
);
245 struct strbuf
*items
;
250 static int format_config(struct strbuf
*buf
, const char *key_
,
251 const char *value_
, const struct key_value_info
*kvi
)
254 show_config_scope(kvi
, buf
);
256 show_config_origin(kvi
, buf
);
258 strbuf_addstr(buf
, key_
);
261 strbuf_addch(buf
, key_delim
);
263 if (type
== TYPE_INT
)
264 strbuf_addf(buf
, "%"PRId64
,
265 git_config_int64(key_
, value_
? value_
: "", kvi
));
266 else if (type
== TYPE_BOOL
)
267 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
269 else if (type
== TYPE_BOOL_OR_INT
) {
271 v
= git_config_bool_or_int(key_
, value_
, kvi
,
274 strbuf_addstr(buf
, v
? "true" : "false");
276 strbuf_addf(buf
, "%d", v
);
277 } else if (type
== TYPE_BOOL_OR_STR
) {
278 int v
= git_parse_maybe_bool(value_
);
280 strbuf_addstr(buf
, value_
);
282 strbuf_addstr(buf
, v
? "true" : "false");
283 } else if (type
== TYPE_PATH
) {
285 if (git_config_pathname(&v
, key_
, value_
) < 0)
287 strbuf_addstr(buf
, v
);
289 } else if (type
== TYPE_EXPIRY_DATE
) {
291 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
293 strbuf_addf(buf
, "%"PRItime
, t
);
294 } else if (type
== TYPE_COLOR
) {
295 char v
[COLOR_MAXLEN
];
296 if (git_config_color(v
, key_
, value_
) < 0)
298 strbuf_addstr(buf
, v
);
300 strbuf_addstr(buf
, value_
);
302 /* Just show the key name; back out delimiter */
304 strbuf_setlen(buf
, buf
->len
- 1);
307 strbuf_addch(buf
, term
);
311 static int collect_config(const char *key_
, const char *value_
,
312 const struct config_context
*ctx
, void *cb
)
314 struct strbuf_list
*values
= cb
;
315 const struct key_value_info
*kvi
= ctx
->kvi
;
317 if (!use_key_regexp
&& strcmp(key_
, key
))
319 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
321 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
323 if (regexp
!= NULL
&&
324 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
327 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
328 strbuf_init(&values
->items
[values
->nr
], 0);
330 return format_config(&values
->items
[values
->nr
++], key_
, value_
, kvi
);
333 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
335 int ret
= CONFIG_GENERIC_ERROR
;
336 struct strbuf_list values
= {NULL
};
339 if (use_key_regexp
) {
343 * NEEDSWORK: this naive pattern lowercasing obviously does not
344 * work for more complex patterns like "^[^.]*Foo.*bar".
345 * Perhaps we should deprecate this altogether someday.
349 for (tl
= key
+ strlen(key
) - 1;
350 tl
>= key
&& *tl
!= '.';
353 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
356 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
357 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
358 error(_("invalid key pattern: %s"), key_
);
359 FREE_AND_NULL(key_regexp
);
360 ret
= CONFIG_INVALID_PATTERN
;
364 if (git_config_parse_key(key_
, &key
, NULL
)) {
365 ret
= CONFIG_INVALID_KEY
;
370 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
371 value_pattern
= regex_
;
373 if (regex_
[0] == '!') {
378 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
379 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
380 error(_("invalid pattern: %s"), regex_
);
381 FREE_AND_NULL(regexp
);
382 ret
= CONFIG_INVALID_PATTERN
;
387 config_with_options(collect_config
, &values
,
388 &given_config_source
, the_repository
,
391 if (!values
.nr
&& default_value
) {
392 struct key_value_info kvi
= KVI_INIT
;
395 kvi_from_param(&kvi
);
396 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
397 item
= &values
.items
[values
.nr
++];
398 strbuf_init(item
, 0);
399 if (format_config(item
, key_
, default_value
, &kvi
) < 0)
400 die(_("failed to format default config value: %s"),
406 for (i
= 0; i
< values
.nr
; i
++) {
407 struct strbuf
*buf
= values
.items
+ i
;
408 if (do_all
|| i
== values
.nr
- 1)
409 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
428 static char *normalize_value(const char *key
, const char *value
,
429 struct key_value_info
*kvi
)
434 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
436 * We don't do normalization for TYPE_PATH here: If
437 * the path is like ~/foobar/, we prefer to store
438 * "~/foobar/" in the config file, and to expand the ~
439 * when retrieving the value.
440 * Also don't do normalization for expiry dates.
442 return xstrdup(value
);
443 if (type
== TYPE_INT
)
444 return xstrfmt("%"PRId64
, git_config_int64(key
, value
, kvi
));
445 if (type
== TYPE_BOOL
)
446 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
447 if (type
== TYPE_BOOL_OR_INT
) {
449 v
= git_config_bool_or_int(key
, value
, kvi
, &is_bool
);
451 return xstrfmt("%d", v
);
453 return xstrdup(v
? "true" : "false");
455 if (type
== TYPE_BOOL_OR_STR
) {
456 int v
= git_parse_maybe_bool(value
);
458 return xstrdup(value
);
460 return xstrdup(v
? "true" : "false");
462 if (type
== TYPE_COLOR
) {
463 char v
[COLOR_MAXLEN
];
464 if (git_config_color(v
, key
, value
))
465 die(_("cannot parse color '%s'"), value
);
468 * The contents of `v` now contain an ANSI escape
469 * sequence, not suitable for including within a
470 * configuration file. Treat the above as a
471 * "sanity-check", and return the given value, which we
472 * know is representable as valid color code.
474 return xstrdup(value
);
477 BUG("cannot normalize type %d", type
);
480 static int get_color_found
;
481 static const char *get_color_slot
;
482 static const char *get_colorbool_slot
;
483 static char parsed_color
[COLOR_MAXLEN
];
485 static int git_get_color_config(const char *var
, const char *value
,
486 const struct config_context
*ctx UNUSED
,
489 if (!strcmp(var
, get_color_slot
)) {
491 config_error_nonbool(var
);
492 if (color_parse(value
, parsed_color
) < 0)
499 static void get_color(const char *var
, const char *def_color
)
501 get_color_slot
= var
;
503 parsed_color
[0] = '\0';
504 config_with_options(git_get_color_config
, NULL
,
505 &given_config_source
, the_repository
,
508 if (!get_color_found
&& def_color
) {
509 if (color_parse(def_color
, parsed_color
) < 0)
510 die(_("unable to parse default color value"));
513 fputs(parsed_color
, stdout
);
516 static int get_colorbool_found
;
517 static int get_diff_color_found
;
518 static int get_color_ui_found
;
519 static int git_get_colorbool_config(const char *var
, const char *value
,
520 const struct config_context
*ctx UNUSED
,
523 if (!strcmp(var
, get_colorbool_slot
))
524 get_colorbool_found
= git_config_colorbool(var
, value
);
525 else if (!strcmp(var
, "diff.color"))
526 get_diff_color_found
= git_config_colorbool(var
, value
);
527 else if (!strcmp(var
, "color.ui"))
528 get_color_ui_found
= git_config_colorbool(var
, value
);
532 static int get_colorbool(const char *var
, int print
)
534 get_colorbool_slot
= var
;
535 get_colorbool_found
= -1;
536 get_diff_color_found
= -1;
537 get_color_ui_found
= -1;
538 config_with_options(git_get_colorbool_config
, NULL
,
539 &given_config_source
, the_repository
,
542 if (get_colorbool_found
< 0) {
543 if (!strcmp(get_colorbool_slot
, "color.diff"))
544 get_colorbool_found
= get_diff_color_found
;
545 if (get_colorbool_found
< 0)
546 get_colorbool_found
= get_color_ui_found
;
549 if (get_colorbool_found
< 0)
550 /* default value if none found in config */
551 get_colorbool_found
= GIT_COLOR_AUTO
;
553 get_colorbool_found
= want_color(get_colorbool_found
);
556 printf("%s\n", get_colorbool_found
? "true" : "false");
559 return get_colorbool_found
? 0 : 1;
562 static void check_write(void)
564 if (!given_config_source
.file
&& !startup_info
->have_repository
)
565 die(_("not in a git directory"));
567 if (given_config_source
.use_stdin
)
568 die(_("writing to stdin is not supported"));
570 if (given_config_source
.blob
)
571 die(_("writing config blobs is not supported"));
574 struct urlmatch_current_candidate_value
{
577 struct key_value_info kvi
;
580 static int urlmatch_collect_fn(const char *var
, const char *value
,
581 const struct config_context
*ctx
,
584 struct string_list
*values
= cb
;
585 struct string_list_item
*item
= string_list_insert(values
, var
);
586 struct urlmatch_current_candidate_value
*matched
= item
->util
;
587 const struct key_value_info
*kvi
= ctx
->kvi
;
590 matched
= xmalloc(sizeof(*matched
));
591 strbuf_init(&matched
->value
, 0);
592 item
->util
= matched
;
594 strbuf_reset(&matched
->value
);
599 strbuf_addstr(&matched
->value
, value
);
600 matched
->value_is_null
= 0;
602 matched
->value_is_null
= 1;
607 static int get_urlmatch(const char *var
, const char *url
)
611 struct string_list_item
*item
;
612 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
613 struct string_list values
= STRING_LIST_INIT_DUP
;
615 config
.collect_fn
= urlmatch_collect_fn
;
616 config
.cascade_fn
= NULL
;
619 if (!url_normalize(url
, &config
.url
))
620 die("%s", config
.url
.err
);
622 config
.section
= xstrdup_tolower(var
);
623 section_tail
= strchr(config
.section
, '.');
625 *section_tail
= '\0';
626 config
.key
= section_tail
+ 1;
633 config_with_options(urlmatch_config_entry
, &config
,
634 &given_config_source
, the_repository
,
639 for_each_string_list_item(item
, &values
) {
640 struct urlmatch_current_candidate_value
*matched
= item
->util
;
641 struct strbuf buf
= STRBUF_INIT
;
643 format_config(&buf
, item
->string
,
644 matched
->value_is_null
? NULL
: matched
->value
.buf
,
646 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
647 strbuf_release(&buf
);
649 strbuf_release(&matched
->value
);
651 urlmatch_config_release(&config
);
652 string_list_clear(&values
, 1);
653 free(config
.url
.url
);
655 free((void *)config
.section
);
659 static char *default_user_config(void)
661 struct strbuf buf
= STRBUF_INIT
;
663 _("# This is Git's per-user configuration file.\n"
665 "# Please adapt and uncomment the following lines:\n"
668 ident_default_name(),
669 ident_default_email());
670 return strbuf_detach(&buf
, NULL
);
673 int cmd_config(int argc
, const char **argv
, const char *prefix
)
675 int nongit
= !startup_info
->have_repository
;
679 struct key_value_info default_kvi
= KVI_INIT
;
681 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
683 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
684 builtin_config_usage
,
685 PARSE_OPT_STOP_AT_NON_OPTION
);
687 if (use_global_config
+ use_system_config
+ use_local_config
+
688 use_worktree_config
+
689 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
690 error(_("only one config file at a time"));
691 usage_builtin_config();
695 if (use_local_config
)
696 die(_("--local can only be used inside a git repository"));
697 if (given_config_source
.blob
)
698 die(_("--blob can only be used inside a git repository"));
699 if (use_worktree_config
)
700 die(_("--worktree can only be used inside a git repository"));
704 if (given_config_source
.file
&&
705 !strcmp(given_config_source
.file
, "-")) {
706 given_config_source
.file
= NULL
;
707 given_config_source
.use_stdin
= 1;
708 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
711 if (use_global_config
) {
712 char *user_config
, *xdg_config
;
714 git_global_config(&user_config
, &xdg_config
);
717 * It is unknown if HOME/.gitconfig exists, so
718 * we do not know if we should write to XDG
719 * location; error out even if XDG_CONFIG_HOME
720 * is set and points at a sane location.
722 die(_("$HOME not set"));
724 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
726 if (access_or_warn(user_config
, R_OK
, 0) &&
727 xdg_config
&& !access_or_warn(xdg_config
, R_OK
, 0)) {
728 given_config_source
.file
= xdg_config
;
731 given_config_source
.file
= user_config
;
735 else if (use_system_config
) {
736 given_config_source
.file
= git_system_config();
737 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
738 } else if (use_local_config
) {
739 given_config_source
.file
= git_pathdup("config");
740 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
741 } else if (use_worktree_config
) {
742 struct worktree
**worktrees
= get_worktrees();
743 if (the_repository
->repository_format_worktree_config
)
744 given_config_source
.file
= git_pathdup("config.worktree");
745 else if (worktrees
[0] && worktrees
[1])
746 die(_("--worktree cannot be used with multiple "
747 "working trees unless the config\n"
748 "extension worktreeConfig is enabled. "
749 "Please read \"CONFIGURATION FILE\"\n"
750 "section in \"git help worktree\" for details"));
752 given_config_source
.file
= git_pathdup("config");
753 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
754 free_worktrees(worktrees
);
755 } else if (given_config_source
.file
) {
756 if (!is_absolute_path(given_config_source
.file
) && prefix
)
757 given_config_source
.file
=
758 prefix_filename(prefix
, given_config_source
.file
);
759 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
760 } else if (given_config_source
.blob
) {
761 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
765 if (respect_includes_opt
== -1)
766 config_options
.respect_includes
= !given_config_source
.file
;
768 config_options
.respect_includes
= respect_includes_opt
;
770 config_options
.commondir
= get_git_common_dir();
771 config_options
.git_dir
= get_git_dir();
780 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
781 error(_("--get-color and variable type are incoherent"));
782 usage_builtin_config();
785 if (HAS_MULTI_BITS(actions
)) {
786 error(_("only one action at a time"));
787 usage_builtin_config();
791 case 1: actions
= ACTION_GET
; break;
792 case 2: actions
= ACTION_SET
; break;
793 case 3: actions
= ACTION_SET_ALL
; break;
795 usage_builtin_config();
798 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
799 error(_("--name-only is only applicable to --list or --get-regexp"));
800 usage_builtin_config();
803 if (show_origin
&& !(actions
&
804 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
805 error(_("--show-origin is only applicable to --get, --get-all, "
806 "--get-regexp, and --list"));
807 usage_builtin_config();
810 if (default_value
&& !(actions
& ACTION_GET
)) {
811 error(_("--default is only applicable to --get"));
812 usage_builtin_config();
815 /* check usage of --fixed-value */
817 int allowed_usage
= 0;
820 /* git config --get <name> <value-pattern> */
822 /* git config --get-all <name> <value-pattern> */
824 /* git config --get-regexp <name-pattern> <value-pattern> */
825 case ACTION_GET_REGEXP
:
826 /* git config --unset <name> <value-pattern> */
828 /* git config --unset-all <name> <value-pattern> */
829 case ACTION_UNSET_ALL
:
830 allowed_usage
= argc
> 1 && !!argv
[1];
833 /* git config <name> <value> <value-pattern> */
835 /* git config --replace-all <name> <value> <value-pattern> */
836 case ACTION_REPLACE_ALL
:
837 allowed_usage
= argc
> 2 && !!argv
[2];
840 /* other options don't allow --fixed-value */
843 if (!allowed_usage
) {
844 error(_("--fixed-value only applies with 'value-pattern'"));
845 usage_builtin_config();
848 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
851 if (actions
& PAGING_ACTIONS
)
852 setup_auto_pager("config", 1);
854 if (actions
== ACTION_LIST
) {
855 check_argc(argc
, 0, 0);
856 if (config_with_options(show_all_config
, NULL
,
857 &given_config_source
, the_repository
,
858 &config_options
) < 0) {
859 if (given_config_source
.file
)
860 die_errno(_("unable to read config file '%s'"),
861 given_config_source
.file
);
863 die(_("error processing config file(s)"));
866 else if (actions
== ACTION_EDIT
) {
869 check_argc(argc
, 0, 0);
870 if (!given_config_source
.file
&& nongit
)
871 die(_("not in a git directory"));
872 if (given_config_source
.use_stdin
)
873 die(_("editing stdin is not supported"));
874 if (given_config_source
.blob
)
875 die(_("editing blobs is not supported"));
876 git_config(git_default_config
, NULL
);
877 config_file
= given_config_source
.file
?
878 xstrdup(given_config_source
.file
) :
879 git_pathdup("config");
880 if (use_global_config
) {
881 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
883 char *content
= default_user_config();
884 write_str_in_full(fd
, content
);
888 else if (errno
!= EEXIST
)
889 die_errno(_("cannot create configuration file %s"), config_file
);
891 launch_editor(config_file
, NULL
, NULL
);
894 else if (actions
== ACTION_SET
) {
896 check_argc(argc
, 2, 2);
897 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
898 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], value
);
899 if (ret
== CONFIG_NOTHING_SET
)
900 error(_("cannot overwrite multiple values with a single value\n"
901 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
903 else if (actions
== ACTION_SET_ALL
) {
905 check_argc(argc
, 2, 3);
906 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
907 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
908 argv
[0], value
, argv
[2],
911 else if (actions
== ACTION_ADD
) {
913 check_argc(argc
, 2, 2);
914 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
915 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
920 else if (actions
== ACTION_REPLACE_ALL
) {
922 check_argc(argc
, 2, 3);
923 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
924 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
925 argv
[0], value
, argv
[2],
926 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
928 else if (actions
== ACTION_GET
) {
929 check_argc(argc
, 1, 2);
930 return get_value(argv
[0], argv
[1], flags
);
932 else if (actions
== ACTION_GET_ALL
) {
934 check_argc(argc
, 1, 2);
935 return get_value(argv
[0], argv
[1], flags
);
937 else if (actions
== ACTION_GET_REGEXP
) {
941 check_argc(argc
, 1, 2);
942 return get_value(argv
[0], argv
[1], flags
);
944 else if (actions
== ACTION_GET_URLMATCH
) {
945 check_argc(argc
, 2, 2);
946 return get_urlmatch(argv
[0], argv
[1]);
948 else if (actions
== ACTION_UNSET
) {
950 check_argc(argc
, 1, 2);
952 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
953 argv
[0], NULL
, argv
[1],
956 return git_config_set_in_file_gently(given_config_source
.file
,
959 else if (actions
== ACTION_UNSET_ALL
) {
961 check_argc(argc
, 1, 2);
962 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
963 argv
[0], NULL
, argv
[1],
964 flags
| CONFIG_FLAGS_MULTI_REPLACE
);
966 else if (actions
== ACTION_RENAME_SECTION
) {
968 check_argc(argc
, 2, 2);
969 ret
= git_config_rename_section_in_file(given_config_source
.file
,
974 die(_("no such section: %s"), argv
[0]);
978 else if (actions
== ACTION_REMOVE_SECTION
) {
980 check_argc(argc
, 1, 1);
981 ret
= git_config_rename_section_in_file(given_config_source
.file
,
986 die(_("no such section: %s"), argv
[0]);
990 else if (actions
== ACTION_GET_COLOR
) {
991 check_argc(argc
, 1, 2);
992 get_color(argv
[0], argv
[1]);
994 else if (actions
== ACTION_GET_COLORBOOL
) {
995 check_argc(argc
, 1, 2);
997 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
998 return get_colorbool(argv
[0], argc
== 2);