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
;
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")),
177 OPT_STRING(0, "comment", &comment
, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
181 static NORETURN
void usage_builtin_config(void)
183 usage_with_options(builtin_config_usage
, builtin_config_options
);
186 static void check_argc(int argc
, int min
, int max
)
188 if (argc
>= min
&& argc
<= max
)
191 error(_("wrong number of arguments, should be %d"), min
);
193 error(_("wrong number of arguments, should be from %d to %d"),
195 usage_builtin_config();
198 static void show_config_origin(const struct key_value_info
*kvi
,
201 const char term
= end_nul
? '\0' : '\t';
203 strbuf_addstr(buf
, config_origin_type_name(kvi
->origin_type
));
204 strbuf_addch(buf
, ':');
206 strbuf_addstr(buf
, kvi
->filename
? kvi
->filename
: "");
208 quote_c_style(kvi
->filename
? kvi
->filename
: "", buf
, NULL
, 0);
209 strbuf_addch(buf
, term
);
212 static void show_config_scope(const struct key_value_info
*kvi
,
215 const char term
= end_nul
? '\0' : '\t';
216 const char *scope
= config_scope_name(kvi
->scope
);
218 strbuf_addstr(buf
, N_(scope
));
219 strbuf_addch(buf
, term
);
222 static int show_all_config(const char *key_
, const char *value_
,
223 const struct config_context
*ctx
,
226 const struct key_value_info
*kvi
= ctx
->kvi
;
228 if (show_origin
|| show_scope
) {
229 struct strbuf buf
= STRBUF_INIT
;
231 show_config_scope(kvi
, &buf
);
233 show_config_origin(kvi
, &buf
);
234 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
235 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
236 strbuf_release(&buf
);
238 if (!omit_values
&& value_
)
239 printf("%s%c%s%c", key_
, delim
, value_
, term
);
241 printf("%s%c", key_
, term
);
246 struct strbuf
*items
;
251 static int format_config(struct strbuf
*buf
, const char *key_
,
252 const char *value_
, const struct key_value_info
*kvi
)
255 show_config_scope(kvi
, buf
);
257 show_config_origin(kvi
, buf
);
259 strbuf_addstr(buf
, key_
);
262 strbuf_addch(buf
, key_delim
);
264 if (type
== TYPE_INT
)
265 strbuf_addf(buf
, "%"PRId64
,
266 git_config_int64(key_
, value_
? value_
: "", kvi
));
267 else if (type
== TYPE_BOOL
)
268 strbuf_addstr(buf
, git_config_bool(key_
, value_
) ?
270 else if (type
== TYPE_BOOL_OR_INT
) {
272 v
= git_config_bool_or_int(key_
, value_
, kvi
,
275 strbuf_addstr(buf
, v
? "true" : "false");
277 strbuf_addf(buf
, "%d", v
);
278 } else if (type
== TYPE_BOOL_OR_STR
) {
279 int v
= git_parse_maybe_bool(value_
);
281 strbuf_addstr(buf
, value_
);
283 strbuf_addstr(buf
, v
? "true" : "false");
284 } else if (type
== TYPE_PATH
) {
286 if (git_config_pathname(&v
, key_
, value_
) < 0)
288 strbuf_addstr(buf
, v
);
290 } else if (type
== TYPE_EXPIRY_DATE
) {
292 if (git_config_expiry_date(&t
, key_
, value_
) < 0)
294 strbuf_addf(buf
, "%"PRItime
, t
);
295 } else if (type
== TYPE_COLOR
) {
296 char v
[COLOR_MAXLEN
];
297 if (git_config_color(v
, key_
, value_
) < 0)
299 strbuf_addstr(buf
, v
);
301 strbuf_addstr(buf
, value_
);
303 /* Just show the key name; back out delimiter */
305 strbuf_setlen(buf
, buf
->len
- 1);
308 strbuf_addch(buf
, term
);
312 static int collect_config(const char *key_
, const char *value_
,
313 const struct config_context
*ctx
, void *cb
)
315 struct strbuf_list
*values
= cb
;
316 const struct key_value_info
*kvi
= ctx
->kvi
;
318 if (!use_key_regexp
&& strcmp(key_
, key
))
320 if (use_key_regexp
&& regexec(key_regexp
, key_
, 0, NULL
, 0))
322 if (fixed_value
&& strcmp(value_pattern
, (value_
?value_
:"")))
324 if (regexp
!= NULL
&&
325 (do_not_match
^ !!regexec(regexp
, (value_
?value_
:""), 0, NULL
, 0)))
328 ALLOC_GROW(values
->items
, values
->nr
+ 1, values
->alloc
);
329 strbuf_init(&values
->items
[values
->nr
], 0);
331 return format_config(&values
->items
[values
->nr
++], key_
, value_
, kvi
);
334 static int get_value(const char *key_
, const char *regex_
, unsigned flags
)
336 int ret
= CONFIG_GENERIC_ERROR
;
337 struct strbuf_list values
= {NULL
};
340 if (use_key_regexp
) {
344 * NEEDSWORK: this naive pattern lowercasing obviously does not
345 * work for more complex patterns like "^[^.]*Foo.*bar".
346 * Perhaps we should deprecate this altogether someday.
350 for (tl
= key
+ strlen(key
) - 1;
351 tl
>= key
&& *tl
!= '.';
354 for (tl
= key
; *tl
&& *tl
!= '.'; tl
++)
357 key_regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
358 if (regcomp(key_regexp
, key
, REG_EXTENDED
)) {
359 error(_("invalid key pattern: %s"), key_
);
360 FREE_AND_NULL(key_regexp
);
361 ret
= CONFIG_INVALID_PATTERN
;
365 if (git_config_parse_key(key_
, &key
, NULL
)) {
366 ret
= CONFIG_INVALID_KEY
;
371 if (regex_
&& (flags
& CONFIG_FLAGS_FIXED_VALUE
))
372 value_pattern
= regex_
;
374 if (regex_
[0] == '!') {
379 regexp
= (regex_t
*)xmalloc(sizeof(regex_t
));
380 if (regcomp(regexp
, regex_
, REG_EXTENDED
)) {
381 error(_("invalid pattern: %s"), regex_
);
382 FREE_AND_NULL(regexp
);
383 ret
= CONFIG_INVALID_PATTERN
;
388 config_with_options(collect_config
, &values
,
389 &given_config_source
, the_repository
,
392 if (!values
.nr
&& default_value
) {
393 struct key_value_info kvi
= KVI_INIT
;
396 kvi_from_param(&kvi
);
397 ALLOC_GROW(values
.items
, values
.nr
+ 1, values
.alloc
);
398 item
= &values
.items
[values
.nr
++];
399 strbuf_init(item
, 0);
400 if (format_config(item
, key_
, default_value
, &kvi
) < 0)
401 die(_("failed to format default config value: %s"),
407 for (i
= 0; i
< values
.nr
; i
++) {
408 struct strbuf
*buf
= values
.items
+ i
;
409 if (do_all
|| i
== values
.nr
- 1)
410 fwrite(buf
->buf
, 1, buf
->len
, stdout
);
429 static char *normalize_value(const char *key
, const char *value
,
430 struct key_value_info
*kvi
)
435 if (type
== 0 || type
== TYPE_PATH
|| type
== TYPE_EXPIRY_DATE
)
437 * We don't do normalization for TYPE_PATH here: If
438 * the path is like ~/foobar/, we prefer to store
439 * "~/foobar/" in the config file, and to expand the ~
440 * when retrieving the value.
441 * Also don't do normalization for expiry dates.
443 return xstrdup(value
);
444 if (type
== TYPE_INT
)
445 return xstrfmt("%"PRId64
, git_config_int64(key
, value
, kvi
));
446 if (type
== TYPE_BOOL
)
447 return xstrdup(git_config_bool(key
, value
) ? "true" : "false");
448 if (type
== TYPE_BOOL_OR_INT
) {
450 v
= git_config_bool_or_int(key
, value
, kvi
, &is_bool
);
452 return xstrfmt("%d", v
);
454 return xstrdup(v
? "true" : "false");
456 if (type
== TYPE_BOOL_OR_STR
) {
457 int v
= git_parse_maybe_bool(value
);
459 return xstrdup(value
);
461 return xstrdup(v
? "true" : "false");
463 if (type
== TYPE_COLOR
) {
464 char v
[COLOR_MAXLEN
];
465 if (git_config_color(v
, key
, value
))
466 die(_("cannot parse color '%s'"), value
);
469 * The contents of `v` now contain an ANSI escape
470 * sequence, not suitable for including within a
471 * configuration file. Treat the above as a
472 * "sanity-check", and return the given value, which we
473 * know is representable as valid color code.
475 return xstrdup(value
);
478 BUG("cannot normalize type %d", type
);
481 static int get_color_found
;
482 static const char *get_color_slot
;
483 static const char *get_colorbool_slot
;
484 static char parsed_color
[COLOR_MAXLEN
];
486 static int git_get_color_config(const char *var
, const char *value
,
487 const struct config_context
*ctx UNUSED
,
490 if (!strcmp(var
, get_color_slot
)) {
492 config_error_nonbool(var
);
493 if (color_parse(value
, parsed_color
) < 0)
500 static void get_color(const char *var
, const char *def_color
)
502 get_color_slot
= var
;
504 parsed_color
[0] = '\0';
505 config_with_options(git_get_color_config
, NULL
,
506 &given_config_source
, the_repository
,
509 if (!get_color_found
&& def_color
) {
510 if (color_parse(def_color
, parsed_color
) < 0)
511 die(_("unable to parse default color value"));
514 fputs(parsed_color
, stdout
);
517 static int get_colorbool_found
;
518 static int get_diff_color_found
;
519 static int get_color_ui_found
;
520 static int git_get_colorbool_config(const char *var
, const char *value
,
521 const struct config_context
*ctx UNUSED
,
524 if (!strcmp(var
, get_colorbool_slot
))
525 get_colorbool_found
= git_config_colorbool(var
, value
);
526 else if (!strcmp(var
, "diff.color"))
527 get_diff_color_found
= git_config_colorbool(var
, value
);
528 else if (!strcmp(var
, "color.ui"))
529 get_color_ui_found
= git_config_colorbool(var
, value
);
533 static int get_colorbool(const char *var
, int print
)
535 get_colorbool_slot
= var
;
536 get_colorbool_found
= -1;
537 get_diff_color_found
= -1;
538 get_color_ui_found
= -1;
539 config_with_options(git_get_colorbool_config
, NULL
,
540 &given_config_source
, the_repository
,
543 if (get_colorbool_found
< 0) {
544 if (!strcmp(get_colorbool_slot
, "color.diff"))
545 get_colorbool_found
= get_diff_color_found
;
546 if (get_colorbool_found
< 0)
547 get_colorbool_found
= get_color_ui_found
;
550 if (get_colorbool_found
< 0)
551 /* default value if none found in config */
552 get_colorbool_found
= GIT_COLOR_AUTO
;
554 get_colorbool_found
= want_color(get_colorbool_found
);
557 printf("%s\n", get_colorbool_found
? "true" : "false");
560 return get_colorbool_found
? 0 : 1;
563 static void check_write(void)
565 if (!given_config_source
.file
&& !startup_info
->have_repository
)
566 die(_("not in a git directory"));
568 if (given_config_source
.use_stdin
)
569 die(_("writing to stdin is not supported"));
571 if (given_config_source
.blob
)
572 die(_("writing config blobs is not supported"));
575 struct urlmatch_current_candidate_value
{
578 struct key_value_info kvi
;
581 static int urlmatch_collect_fn(const char *var
, const char *value
,
582 const struct config_context
*ctx
,
585 struct string_list
*values
= cb
;
586 struct string_list_item
*item
= string_list_insert(values
, var
);
587 struct urlmatch_current_candidate_value
*matched
= item
->util
;
588 const struct key_value_info
*kvi
= ctx
->kvi
;
591 matched
= xmalloc(sizeof(*matched
));
592 strbuf_init(&matched
->value
, 0);
593 item
->util
= matched
;
595 strbuf_reset(&matched
->value
);
600 strbuf_addstr(&matched
->value
, value
);
601 matched
->value_is_null
= 0;
603 matched
->value_is_null
= 1;
608 static int get_urlmatch(const char *var
, const char *url
)
612 struct string_list_item
*item
;
613 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
614 struct string_list values
= STRING_LIST_INIT_DUP
;
616 config
.collect_fn
= urlmatch_collect_fn
;
617 config
.cascade_fn
= NULL
;
620 if (!url_normalize(url
, &config
.url
))
621 die("%s", config
.url
.err
);
623 config
.section
= xstrdup_tolower(var
);
624 section_tail
= strchr(config
.section
, '.');
626 *section_tail
= '\0';
627 config
.key
= section_tail
+ 1;
634 config_with_options(urlmatch_config_entry
, &config
,
635 &given_config_source
, the_repository
,
640 for_each_string_list_item(item
, &values
) {
641 struct urlmatch_current_candidate_value
*matched
= item
->util
;
642 struct strbuf buf
= STRBUF_INIT
;
644 format_config(&buf
, item
->string
,
645 matched
->value_is_null
? NULL
: matched
->value
.buf
,
647 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
648 strbuf_release(&buf
);
650 strbuf_release(&matched
->value
);
652 urlmatch_config_release(&config
);
653 string_list_clear(&values
, 1);
654 free(config
.url
.url
);
656 free((void *)config
.section
);
660 static char *default_user_config(void)
662 struct strbuf buf
= STRBUF_INIT
;
664 _("# This is Git's per-user configuration file.\n"
666 "# Please adapt and uncomment the following lines:\n"
669 ident_default_name(),
670 ident_default_email());
671 return strbuf_detach(&buf
, NULL
);
674 int cmd_config(int argc
, const char **argv
, const char *prefix
)
676 int nongit
= !startup_info
->have_repository
;
680 struct key_value_info default_kvi
= KVI_INIT
;
682 given_config_source
.file
= xstrdup_or_null(getenv(CONFIG_ENVIRONMENT
));
684 argc
= parse_options(argc
, argv
, prefix
, builtin_config_options
,
685 builtin_config_usage
,
686 PARSE_OPT_STOP_AT_NON_OPTION
);
688 if (use_global_config
+ use_system_config
+ use_local_config
+
689 use_worktree_config
+
690 !!given_config_source
.file
+ !!given_config_source
.blob
> 1) {
691 error(_("only one config file at a time"));
692 usage_builtin_config();
696 if (use_local_config
)
697 die(_("--local can only be used inside a git repository"));
698 if (given_config_source
.blob
)
699 die(_("--blob can only be used inside a git repository"));
700 if (use_worktree_config
)
701 die(_("--worktree can only be used inside a git repository"));
705 if (given_config_source
.file
&&
706 !strcmp(given_config_source
.file
, "-")) {
707 given_config_source
.file
= NULL
;
708 given_config_source
.use_stdin
= 1;
709 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
712 if (use_global_config
) {
713 given_config_source
.file
= git_global_config();
714 if (!given_config_source
.file
)
716 * It is unknown if HOME/.gitconfig exists, so
717 * we do not know if we should write to XDG
718 * location; error out even if XDG_CONFIG_HOME
719 * is set and points at a sane location.
721 die(_("$HOME not set"));
722 given_config_source
.scope
= CONFIG_SCOPE_GLOBAL
;
723 } else if (use_system_config
) {
724 given_config_source
.file
= git_system_config();
725 given_config_source
.scope
= CONFIG_SCOPE_SYSTEM
;
726 } else if (use_local_config
) {
727 given_config_source
.file
= git_pathdup("config");
728 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
729 } else if (use_worktree_config
) {
730 struct worktree
**worktrees
= get_worktrees();
731 if (the_repository
->repository_format_worktree_config
)
732 given_config_source
.file
= git_pathdup("config.worktree");
733 else if (worktrees
[0] && worktrees
[1])
734 die(_("--worktree cannot be used with multiple "
735 "working trees unless the config\n"
736 "extension worktreeConfig is enabled. "
737 "Please read \"CONFIGURATION FILE\"\n"
738 "section in \"git help worktree\" for details"));
740 given_config_source
.file
= git_pathdup("config");
741 given_config_source
.scope
= CONFIG_SCOPE_LOCAL
;
742 free_worktrees(worktrees
);
743 } else if (given_config_source
.file
) {
744 if (!is_absolute_path(given_config_source
.file
) && prefix
)
745 given_config_source
.file
=
746 prefix_filename(prefix
, given_config_source
.file
);
747 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
748 } else if (given_config_source
.blob
) {
749 given_config_source
.scope
= CONFIG_SCOPE_COMMAND
;
752 if (respect_includes_opt
== -1)
753 config_options
.respect_includes
= !given_config_source
.file
;
755 config_options
.respect_includes
= respect_includes_opt
;
757 config_options
.commondir
= get_git_common_dir();
758 config_options
.git_dir
= get_git_dir();
767 if ((actions
& (ACTION_GET_COLOR
|ACTION_GET_COLORBOOL
)) && type
) {
768 error(_("--get-color and variable type are incoherent"));
769 usage_builtin_config();
772 if (HAS_MULTI_BITS(actions
)) {
773 error(_("only one action at a time"));
774 usage_builtin_config();
778 case 1: actions
= ACTION_GET
; break;
779 case 2: actions
= ACTION_SET
; break;
780 case 3: actions
= ACTION_SET_ALL
; break;
782 usage_builtin_config();
785 !(actions
== ACTION_LIST
|| actions
== ACTION_GET_REGEXP
)) {
786 error(_("--name-only is only applicable to --list or --get-regexp"));
787 usage_builtin_config();
790 if (show_origin
&& !(actions
&
791 (ACTION_GET
|ACTION_GET_ALL
|ACTION_GET_REGEXP
|ACTION_LIST
))) {
792 error(_("--show-origin is only applicable to --get, --get-all, "
793 "--get-regexp, and --list"));
794 usage_builtin_config();
797 if (default_value
&& !(actions
& ACTION_GET
)) {
798 error(_("--default is only applicable to --get"));
799 usage_builtin_config();
803 !(actions
& (ACTION_ADD
|ACTION_SET
|ACTION_SET_ALL
|ACTION_REPLACE_ALL
))) {
804 error(_("--comment is only applicable to add/set/replace operations"));
805 usage_builtin_config();
808 /* check usage of --fixed-value */
810 int allowed_usage
= 0;
813 /* git config --get <name> <value-pattern> */
815 /* git config --get-all <name> <value-pattern> */
817 /* git config --get-regexp <name-pattern> <value-pattern> */
818 case ACTION_GET_REGEXP
:
819 /* git config --unset <name> <value-pattern> */
821 /* git config --unset-all <name> <value-pattern> */
822 case ACTION_UNSET_ALL
:
823 allowed_usage
= argc
> 1 && !!argv
[1];
826 /* git config <name> <value> <value-pattern> */
828 /* git config --replace-all <name> <value> <value-pattern> */
829 case ACTION_REPLACE_ALL
:
830 allowed_usage
= argc
> 2 && !!argv
[2];
833 /* other options don't allow --fixed-value */
836 if (!allowed_usage
) {
837 error(_("--fixed-value only applies with 'value-pattern'"));
838 usage_builtin_config();
841 flags
|= CONFIG_FLAGS_FIXED_VALUE
;
844 comment
= git_config_prepare_comment_string(comment
);
846 if (actions
& PAGING_ACTIONS
)
847 setup_auto_pager("config", 1);
849 if (actions
== ACTION_LIST
) {
850 check_argc(argc
, 0, 0);
851 if (config_with_options(show_all_config
, NULL
,
852 &given_config_source
, the_repository
,
853 &config_options
) < 0) {
854 if (given_config_source
.file
)
855 die_errno(_("unable to read config file '%s'"),
856 given_config_source
.file
);
858 die(_("error processing config file(s)"));
861 else if (actions
== ACTION_EDIT
) {
864 check_argc(argc
, 0, 0);
865 if (!given_config_source
.file
&& nongit
)
866 die(_("not in a git directory"));
867 if (given_config_source
.use_stdin
)
868 die(_("editing stdin is not supported"));
869 if (given_config_source
.blob
)
870 die(_("editing blobs is not supported"));
871 git_config(git_default_config
, NULL
);
872 config_file
= given_config_source
.file
?
873 xstrdup(given_config_source
.file
) :
874 git_pathdup("config");
875 if (use_global_config
) {
876 int fd
= open(config_file
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
878 char *content
= default_user_config();
879 write_str_in_full(fd
, content
);
883 else if (errno
!= EEXIST
)
884 die_errno(_("cannot create configuration file %s"), config_file
);
886 launch_editor(config_file
, NULL
, NULL
);
889 else if (actions
== ACTION_SET
) {
891 check_argc(argc
, 2, 2);
892 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
893 ret
= git_config_set_in_file_gently(given_config_source
.file
, argv
[0], comment
, value
);
894 if (ret
== CONFIG_NOTHING_SET
)
895 error(_("cannot overwrite multiple values with a single value\n"
896 " Use a regexp, --add or --replace-all to change %s."), argv
[0]);
898 else if (actions
== ACTION_SET_ALL
) {
900 check_argc(argc
, 2, 3);
901 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
902 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
903 argv
[0], value
, argv
[2],
906 else if (actions
== ACTION_ADD
) {
908 check_argc(argc
, 2, 2);
909 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
910 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
915 else if (actions
== ACTION_REPLACE_ALL
) {
917 check_argc(argc
, 2, 3);
918 value
= normalize_value(argv
[0], argv
[1], &default_kvi
);
919 ret
= git_config_set_multivar_in_file_gently(given_config_source
.file
,
920 argv
[0], value
, argv
[2],
921 comment
, flags
| CONFIG_FLAGS_MULTI_REPLACE
);
923 else if (actions
== ACTION_GET
) {
924 check_argc(argc
, 1, 2);
925 return get_value(argv
[0], argv
[1], flags
);
927 else if (actions
== ACTION_GET_ALL
) {
929 check_argc(argc
, 1, 2);
930 return get_value(argv
[0], argv
[1], flags
);
932 else if (actions
== ACTION_GET_REGEXP
) {
936 check_argc(argc
, 1, 2);
937 return get_value(argv
[0], argv
[1], flags
);
939 else if (actions
== ACTION_GET_URLMATCH
) {
940 check_argc(argc
, 2, 2);
941 return get_urlmatch(argv
[0], argv
[1]);
943 else if (actions
== ACTION_UNSET
) {
945 check_argc(argc
, 1, 2);
947 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
948 argv
[0], NULL
, argv
[1],
951 return git_config_set_in_file_gently(given_config_source
.file
,
952 argv
[0], NULL
, NULL
);
954 else if (actions
== ACTION_UNSET_ALL
) {
956 check_argc(argc
, 1, 2);
957 return git_config_set_multivar_in_file_gently(given_config_source
.file
,
958 argv
[0], NULL
, argv
[1],
959 NULL
, flags
| CONFIG_FLAGS_MULTI_REPLACE
);
961 else if (actions
== ACTION_RENAME_SECTION
) {
963 check_argc(argc
, 2, 2);
964 ret
= git_config_rename_section_in_file(given_config_source
.file
,
969 die(_("no such section: %s"), argv
[0]);
973 else if (actions
== ACTION_REMOVE_SECTION
) {
975 check_argc(argc
, 1, 1);
976 ret
= git_config_rename_section_in_file(given_config_source
.file
,
981 die(_("no such section: %s"), argv
[0]);
985 else if (actions
== ACTION_GET_COLOR
) {
986 check_argc(argc
, 1, 2);
987 get_color(argv
[0], argv
[1]);
989 else if (actions
== ACTION_GET_COLORBOOL
) {
990 check_argc(argc
, 1, 2);
992 color_stdout_is_tty
= git_config_bool("command line", argv
[1]);
993 return get_colorbool(argv
[0], argc
== 2);