blob: 542d8d02b2b00a8675d6bff9245019d0251d043f [file] [log] [blame]
Matthias Kestenholze12c0952006-08-02 23:51:59 +02001#include "builtin.h"
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +01002#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Junio C Hamano9ce03522007-11-27 22:41:05 -08004#include "color.h"
Felipe Contrerasd64ec162009-02-21 02:49:25 +02005#include "parse-options.h"
Junio C Hamanod4770962013-07-31 11:14:59 -07006#include "urlmatch.h"
Lars Schneider70bd8792016-02-19 10:16:02 +01007#include "quote.h"
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +02008#include "worktree.h"
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +01009
Felipe Contrerasd64ec162009-02-21 02:49:25 +020010static const char *const builtin_config_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070011 N_("git config [<options>]"),
Felipe Contrerasd64ec162009-02-21 02:49:25 +020012 NULL
13};
Johannes Schindelin4ddba792005-11-20 06:52:22 +010014
David Rientjes96f1e582006-08-15 10:23:48 -070015static char *key;
16static regex_t *key_regexp;
Derrick Stolee3f1bae12020-11-25 22:12:55 +000017static const char *value_pattern;
David Rientjes96f1e582006-08-15 10:23:48 -070018static regex_t *regexp;
19static int show_keys;
SZEDER Gábor578625f2015-08-10 11:46:06 +020020static int omit_values;
David Rientjes96f1e582006-08-15 10:23:48 -070021static int use_key_regexp;
22static int do_all;
23static int do_not_match;
Frank Lichtenheld2275d502007-06-25 16:03:55 +020024static char delim = '=';
25static char key_delim = ' ';
26static char term = '\n';
Johannes Schindelin4ddba792005-11-20 06:52:22 +010027
Sverre Rabbelier57210a62010-08-03 20:59:23 -050028static int use_global_config, use_system_config, use_local_config;
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +020029static int use_worktree_config;
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +020030static struct git_config_source given_config_source;
Taylor Blau0a8950b2018-04-09 15:46:54 -070031static int actions, type;
Taylor Blaueeaa24b2018-04-09 17:18:26 -070032static char *default_value;
Matthew Rogers329e6ec2020-01-24 00:21:01 +000033static int end_nul;
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +070034static int respect_includes_opt = -1;
35static struct config_options config_options;
Lars Schneider70bd8792016-02-19 10:16:02 +010036static int show_origin;
Matthew Rogers145d59f2020-02-10 00:30:59 +000037static int show_scope;
Derrick Stoleefda43942020-11-25 22:12:53 +000038static int fixed_value;
Felipe Contrerasd64ec162009-02-21 02:49:25 +020039
40#define ACTION_GET (1<<0)
41#define ACTION_GET_ALL (1<<1)
42#define ACTION_GET_REGEXP (1<<2)
43#define ACTION_REPLACE_ALL (1<<3)
44#define ACTION_ADD (1<<4)
45#define ACTION_UNSET (1<<5)
46#define ACTION_UNSET_ALL (1<<6)
47#define ACTION_RENAME_SECTION (1<<7)
48#define ACTION_REMOVE_SECTION (1<<8)
49#define ACTION_LIST (1<<9)
50#define ACTION_EDIT (1<<10)
51#define ACTION_SET (1<<11)
52#define ACTION_SET_ALL (1<<12)
53#define ACTION_GET_COLOR (1<<13)
54#define ACTION_GET_COLORBOOL (1<<14)
Junio C Hamanod4770962013-07-31 11:14:59 -070055#define ACTION_GET_URLMATCH (1<<15)
Felipe Contrerasd64ec162009-02-21 02:49:25 +020056
Martin Ågren32888b82018-02-21 19:51:43 +010057/*
58 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
59 * one line of output and which should therefore be paged.
60 */
61#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
62 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
63
Taylor Blau0a8950b2018-04-09 15:46:54 -070064#define TYPE_BOOL 1
65#define TYPE_INT 2
66#define TYPE_BOOL_OR_INT 3
67#define TYPE_PATH 4
68#define TYPE_EXPIRY_DATE 5
Taylor Blau63e2a0f2018-04-09 17:18:31 -070069#define TYPE_COLOR 6
Lin Sundbd8c092020-05-07 07:31:14 +080070#define TYPE_BOOL_OR_STR 7
Felipe Contreras16c1e932009-02-21 02:49:27 +020071
Taylor Blaufb0dc3b2018-04-18 14:43:35 -070072#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
73 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
74 PARSE_OPT_NONEG, option_parse_type, (i) }
75
Beat Bolli6aaded52018-07-05 20:34:45 +020076static NORETURN void usage_builtin_config(void);
Taylor Blaufb0dc3b2018-04-18 14:43:35 -070077
78static int option_parse_type(const struct option *opt, const char *arg,
79 int unset)
80{
81 int new_type, *to_type;
82
83 if (unset) {
84 *((int *) opt->value) = 0;
85 return 0;
86 }
87
88 /*
89 * To support '--<type>' style flags, begin with new_type equal to
90 * opt->defval.
91 */
92 new_type = opt->defval;
93 if (!new_type) {
94 if (!strcmp(arg, "bool"))
95 new_type = TYPE_BOOL;
96 else if (!strcmp(arg, "int"))
97 new_type = TYPE_INT;
98 else if (!strcmp(arg, "bool-or-int"))
99 new_type = TYPE_BOOL_OR_INT;
Lin Sundbd8c092020-05-07 07:31:14 +0800100 else if (!strcmp(arg, "bool-or-str"))
101 new_type = TYPE_BOOL_OR_STR;
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700102 else if (!strcmp(arg, "path"))
103 new_type = TYPE_PATH;
104 else if (!strcmp(arg, "expiry-date"))
105 new_type = TYPE_EXPIRY_DATE;
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700106 else if (!strcmp(arg, "color"))
107 new_type = TYPE_COLOR;
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700108 else
109 die(_("unrecognized --type argument, %s"), arg);
110 }
111
112 to_type = opt->value;
113 if (*to_type && *to_type != new_type) {
114 /*
115 * Complain when there is a new type not equal to the old type.
116 * This allows for combinations like '--int --type=int' and
117 * '--type=int --type=int', but disallows ones like '--type=bool
118 * --int' and '--type=bool
119 * --type=int'.
120 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200121 error(_("only one type at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200122 usage_builtin_config();
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700123 }
124 *to_type = new_type;
125
126 return 0;
127}
128
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200129static struct option builtin_config_options[] = {
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700130 OPT_GROUP(N_("Config file location")),
Stefan Beller21e047d2013-08-03 13:51:24 +0200131 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
132 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
133 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200134 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200135 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
136 OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700137 OPT_GROUP(N_("Action")),
Derrick Stolee247e2f82020-11-25 22:12:50 +0000138 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
139 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
140 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
Junio C Hamanod4770962013-07-31 11:14:59 -0700141 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
Derrick Stolee247e2f82020-11-25 22:12:50 +0000142 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
Nguyễn Thái Ngọc Duyf63cf8c2012-08-20 19:32:55 +0700143 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
Derrick Stolee247e2f82020-11-25 22:12:50 +0000144 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
145 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700146 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
147 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
148 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
Derrick Stoleefda43942020-11-25 22:12:53 +0000149 OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
Nguyễn Thái Ngọc Duyf63cf8c2012-08-20 19:32:55 +0700150 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
Jeff Kingd0e08d62014-11-20 10:15:51 -0500151 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
152 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700153 OPT_GROUP(N_("Type")),
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700154 OPT_CALLBACK('t', "type", &type, "", N_("value is given this type"), option_parse_type),
155 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
156 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
157 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
Lin Sundbd8c092020-05-07 07:31:14 +0800158 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700159 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
160 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700161 OPT_GROUP(N_("Other")),
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000162 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
SZEDER Gábor578625f2015-08-10 11:46:06 +0200163 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +0700164 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
Lars Schneider70bd8792016-02-19 10:16:02 +0100165 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
Matthew Rogers145d59f2020-02-10 00:30:59 +0000166 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700167 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200168 OPT_END(),
169};
170
Beat Bolli6aaded52018-07-05 20:34:45 +0200171static NORETURN void usage_builtin_config(void)
172{
173 usage_with_options(builtin_config_usage, builtin_config_options);
174}
175
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100176static void check_argc(int argc, int min, int max)
177{
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200178 if (argc >= min && argc <= max)
179 return;
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200180 if (min == max)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200181 error(_("wrong number of arguments, should be %d"), min);
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200182 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200183 error(_("wrong number of arguments, should be from %d to %d"),
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200184 min, max);
Beat Bolli6aaded52018-07-05 20:34:45 +0200185 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200186}
187
Lars Schneider70bd8792016-02-19 10:16:02 +0100188static void show_config_origin(struct strbuf *buf)
189{
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000190 const char term = end_nul ? '\0' : '\t';
Lars Schneider70bd8792016-02-19 10:16:02 +0100191
192 strbuf_addstr(buf, current_config_origin_type());
193 strbuf_addch(buf, ':');
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000194 if (end_nul)
Lars Schneider70bd8792016-02-19 10:16:02 +0100195 strbuf_addstr(buf, current_config_name());
196 else
197 quote_c_style(current_config_name(), buf, NULL, 0);
198 strbuf_addch(buf, term);
199}
200
Matthew Rogers145d59f2020-02-10 00:30:59 +0000201static void show_config_scope(struct strbuf *buf)
202{
203 const char term = end_nul ? '\0' : '\t';
204 const char *scope = config_scope_name(current_config_scope());
205
206 strbuf_addstr(buf, N_(scope));
207 strbuf_addch(buf, term);
208}
209
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100210static int show_all_config(const char *key_, const char *value_, void *cb)
Petr Baudisde791f12006-04-25 00:59:25 +0200211{
Matthew Rogers145d59f2020-02-10 00:30:59 +0000212 if (show_origin || show_scope) {
Lars Schneider70bd8792016-02-19 10:16:02 +0100213 struct strbuf buf = STRBUF_INIT;
Matthew Rogers145d59f2020-02-10 00:30:59 +0000214 if (show_scope)
215 show_config_scope(&buf);
216 if (show_origin)
217 show_config_origin(&buf);
Lars Schneider70bd8792016-02-19 10:16:02 +0100218 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
219 fwrite(buf.buf, 1, buf.len, stdout);
220 strbuf_release(&buf);
221 }
SZEDER Gábor578625f2015-08-10 11:46:06 +0200222 if (!omit_values && value_)
Frank Lichtenheld2275d502007-06-25 16:03:55 +0200223 printf("%s%c%s%c", key_, delim, value_, term);
Petr Baudisde791f12006-04-25 00:59:25 +0200224 else
Frank Lichtenheld2275d502007-06-25 16:03:55 +0200225 printf("%s%c", key_, term);
Petr Baudisde791f12006-04-25 00:59:25 +0200226 return 0;
227}
228
Jeff King7acdd6f2012-10-23 15:51:50 -0400229struct strbuf_list {
230 struct strbuf *items;
231 int nr;
232 int alloc;
233};
234
Junio C Hamanod9b91692013-07-29 14:23:16 -0700235static int format_config(struct strbuf *buf, const char *key_, const char *value_)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100236{
Matthew Rogers145d59f2020-02-10 00:30:59 +0000237 if (show_scope)
238 show_config_scope(buf);
Lars Schneider70bd8792016-02-19 10:16:02 +0100239 if (show_origin)
240 show_config_origin(buf);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200241 if (show_keys)
Jeff King7acdd6f2012-10-23 15:51:50 -0400242 strbuf_addstr(buf, key_);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200243 if (!omit_values) {
Jeff Kingf2259872015-08-20 10:47:34 -0400244 if (show_keys)
245 strbuf_addch(buf, key_delim);
Jeff King00b347d2012-10-23 16:52:44 -0400246
Taylor Blau0a8950b2018-04-09 15:46:54 -0700247 if (type == TYPE_INT)
Jeff Kingf2259872015-08-20 10:47:34 -0400248 strbuf_addf(buf, "%"PRId64,
249 git_config_int64(key_, value_ ? value_ : ""));
Taylor Blau0a8950b2018-04-09 15:46:54 -0700250 else if (type == TYPE_BOOL)
Jeff Kingf2259872015-08-20 10:47:34 -0400251 strbuf_addstr(buf, git_config_bool(key_, value_) ?
252 "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700253 else if (type == TYPE_BOOL_OR_INT) {
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200254 int is_bool, v;
255 v = git_config_bool_or_int(key_, value_, &is_bool);
256 if (is_bool)
Jeff Kingf2259872015-08-20 10:47:34 -0400257 strbuf_addstr(buf, v ? "true" : "false");
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200258 else
Jeff Kingf2259872015-08-20 10:47:34 -0400259 strbuf_addf(buf, "%d", v);
Lin Sundbd8c092020-05-07 07:31:14 +0800260 } else if (type == TYPE_BOOL_OR_STR) {
261 int v = git_parse_maybe_bool(value_);
262 if (v < 0)
263 strbuf_addstr(buf, value_);
264 else
265 strbuf_addstr(buf, v ? "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700266 } else if (type == TYPE_PATH) {
Jeff Kingf2259872015-08-20 10:47:34 -0400267 const char *v;
268 if (git_config_pathname(&v, key_, value_) < 0)
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200269 return -1;
Jeff Kingf2259872015-08-20 10:47:34 -0400270 strbuf_addstr(buf, v);
271 free((char *)v);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700272 } else if (type == TYPE_EXPIRY_DATE) {
Haaris Mehmood5f967422017-11-18 02:27:27 +0000273 timestamp_t t;
274 if (git_config_expiry_date(&t, key_, value_) < 0)
275 return -1;
276 strbuf_addf(buf, "%"PRItime, t);
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700277 } else if (type == TYPE_COLOR) {
278 char v[COLOR_MAXLEN];
279 if (git_config_color(v, key_, value_) < 0)
280 return -1;
281 strbuf_addstr(buf, v);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200282 } else if (value_) {
Jeff Kingf2259872015-08-20 10:47:34 -0400283 strbuf_addstr(buf, value_);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200284 } else {
Jeff Kingf2259872015-08-20 10:47:34 -0400285 /* Just show the key name; back out delimiter */
286 if (show_keys)
287 strbuf_setlen(buf, buf->len - 1);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200288 }
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200289 }
Jeff King00b347d2012-10-23 16:52:44 -0400290 strbuf_addch(buf, term);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100291 return 0;
292}
293
Junio C Hamanod9b91692013-07-29 14:23:16 -0700294static int collect_config(const char *key_, const char *value_, void *cb)
295{
296 struct strbuf_list *values = cb;
297
298 if (!use_key_regexp && strcmp(key_, key))
299 return 0;
300 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
301 return 0;
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000302 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
303 return 0;
Junio C Hamanod9b91692013-07-29 14:23:16 -0700304 if (regexp != NULL &&
305 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
306 return 0;
307
308 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
Jeff King9f1429d2015-08-20 10:46:04 -0400309 strbuf_init(&values->items[values->nr], 0);
Junio C Hamanod9b91692013-07-29 14:23:16 -0700310
311 return format_config(&values->items[values->nr++], key_, value_);
312}
313
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000314static int get_value(const char *key_, const char *regex_, unsigned flags)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100315{
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700316 int ret = CONFIG_GENERIC_ERROR;
Ramsay Jones5ba1a8a2012-10-28 21:05:25 +0000317 struct strbuf_list values = {NULL};
Jeff King7acdd6f2012-10-23 15:51:50 -0400318 int i;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100319
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200320 if (use_key_regexp) {
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100321 char *tl;
322
323 /*
324 * NEEDSWORK: this naive pattern lowercasing obviously does not
325 * work for more complex patterns like "^[^.]*Foo.*bar".
326 * Perhaps we should deprecate this altogether someday.
327 */
328
329 key = xstrdup(key_);
330 for (tl = key + strlen(key) - 1;
331 tl >= key && *tl != '.';
332 tl--)
333 *tl = tolower(*tl);
334 for (tl = key; *tl && *tl != '.'; tl++)
335 *tl = tolower(*tl);
336
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200337 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200338 if (regcomp(key_regexp, key, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200339 error(_("invalid key pattern: %s"), key_);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000340 FREE_AND_NULL(key_regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700341 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200342 goto free_strings;
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200343 }
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100344 } else {
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700345 if (git_config_parse_key(key_, &key, NULL)) {
346 ret = CONFIG_INVALID_KEY;
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100347 goto free_strings;
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700348 }
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200349 }
350
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000351 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
352 value_pattern = regex_;
353 else if (regex_) {
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100354 if (regex_[0] == '!') {
355 do_not_match = 1;
356 regex_++;
357 }
358
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200359 regexp = (regex_t*)xmalloc(sizeof(regex_t));
Amos Waterland0a152172006-01-04 19:31:02 -0500360 if (regcomp(regexp, regex_, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200361 error(_("invalid pattern: %s"), regex_);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000362 FREE_AND_NULL(regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700363 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200364 goto free_strings;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100365 }
366 }
367
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700368 config_with_options(collect_config, &values,
369 &given_config_source, &config_options);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200370
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700371 if (!values.nr && default_value) {
372 struct strbuf *item;
373 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
374 item = &values.items[values.nr++];
375 strbuf_init(item, 0);
376 if (format_config(item, key_, default_value) < 0)
377 die(_("failed to format default config value: %s"),
378 default_value);
379 }
380
Jeff King00b347d2012-10-23 16:52:44 -0400381 ret = !values.nr;
Jeff King7acdd6f2012-10-23 15:51:50 -0400382
383 for (i = 0; i < values.nr; i++) {
384 struct strbuf *buf = values.items + i;
Jeff King00b347d2012-10-23 16:52:44 -0400385 if (do_all || i == values.nr - 1)
386 fwrite(buf->buf, 1, buf->len, stdout);
Jeff King7acdd6f2012-10-23 15:51:50 -0400387 strbuf_release(buf);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100388 }
Jeff King7acdd6f2012-10-23 15:51:50 -0400389 free(values.items);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100390
Jeff King97ed50f2012-10-23 15:40:06 -0400391free_strings:
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100392 free(key);
Jeff King35998c82012-10-23 15:36:12 -0400393 if (key_regexp) {
394 regfree(key_regexp);
395 free(key_regexp);
396 }
Amos Waterland0a152172006-01-04 19:31:02 -0500397 if (regexp) {
398 regfree(regexp);
399 free(regexp);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100400 }
401
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200402 return ret;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100403}
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100404
Stephan Beyer186458b2008-07-24 01:09:35 +0200405static char *normalize_value(const char *key, const char *value)
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200406{
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200407 if (!value)
408 return NULL;
409
Taylor Blau0a8950b2018-04-09 15:46:54 -0700410 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
Matthieu Moy13494842009-12-30 17:51:53 +0100411 /*
412 * We don't do normalization for TYPE_PATH here: If
413 * the path is like ~/foobar/, we prefer to store
414 * "~/foobar/" in the config file, and to expand the ~
415 * when retrieving the value.
Haaris Mehmood5f967422017-11-18 02:27:27 +0000416 * Also don't do normalization for expiry dates.
Matthieu Moy13494842009-12-30 17:51:53 +0100417 */
Jeff King3ec832c2015-09-24 17:07:05 -0400418 return xstrdup(value);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700419 if (type == TYPE_INT)
Jeff King3ec832c2015-09-24 17:07:05 -0400420 return xstrfmt("%"PRId64, git_config_int64(key, value));
Taylor Blau0a8950b2018-04-09 15:46:54 -0700421 if (type == TYPE_BOOL)
Jeff King3ec832c2015-09-24 17:07:05 -0400422 return xstrdup(git_config_bool(key, value) ? "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700423 if (type == TYPE_BOOL_OR_INT) {
Jeff King3ec832c2015-09-24 17:07:05 -0400424 int is_bool, v;
425 v = git_config_bool_or_int(key, value, &is_bool);
426 if (!is_bool)
427 return xstrfmt("%d", v);
428 else
429 return xstrdup(v ? "true" : "false");
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200430 }
Lin Sundbd8c092020-05-07 07:31:14 +0800431 if (type == TYPE_BOOL_OR_STR) {
432 int v = git_parse_maybe_bool(value);
433 if (v < 0)
434 return xstrdup(value);
435 else
436 return xstrdup(v ? "true" : "false");
437 }
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700438 if (type == TYPE_COLOR) {
439 char v[COLOR_MAXLEN];
440 if (git_config_color(v, key, value))
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200441 die(_("cannot parse color '%s'"), value);
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700442
443 /*
444 * The contents of `v` now contain an ANSI escape
445 * sequence, not suitable for including within a
446 * configuration file. Treat the above as a
447 * "sanity-check", and return the given value, which we
448 * know is representable as valid color code.
449 */
450 return xstrdup(value);
451 }
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200452
Junio C Hamano50f08db2018-05-30 14:04:07 +0900453 BUG("cannot normalize type %d", type);
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200454}
455
Junio C Hamano9ce03522007-11-27 22:41:05 -0800456static int get_color_found;
457static const char *get_color_slot;
Felipe Contrerasb4084572009-02-21 02:48:56 +0200458static const char *get_colorbool_slot;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800459static char parsed_color[COLOR_MAXLEN];
460
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100461static int git_get_color_config(const char *var, const char *value, void *cb)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800462{
463 if (!strcmp(var, get_color_slot)) {
Junio C Hamanof7699822008-02-11 10:48:12 -0800464 if (!value)
465 config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400466 if (color_parse(value, parsed_color) < 0)
467 return -1;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800468 get_color_found = 1;
469 }
470 return 0;
471}
472
Jeff Kingd0e08d62014-11-20 10:15:51 -0500473static void get_color(const char *var, const char *def_color)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800474{
Jeff Kingd0e08d62014-11-20 10:15:51 -0500475 get_color_slot = var;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800476 get_color_found = 0;
477 parsed_color[0] = '\0';
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700478 config_with_options(git_get_color_config, NULL,
479 &given_config_source, &config_options);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800480
Jeff Kingf6c5a292014-10-07 15:33:09 -0400481 if (!get_color_found && def_color) {
482 if (color_parse(def_color, parsed_color) < 0)
483 die(_("unable to parse default color value"));
484 }
Junio C Hamano9ce03522007-11-27 22:41:05 -0800485
486 fputs(parsed_color, stdout);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800487}
488
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800489static int get_colorbool_found;
Junio C Hamano69243c22007-12-05 22:12:07 -0800490static int get_diff_color_found;
Jeff Kingc659f552011-08-17 22:04:56 -0700491static int get_color_ui_found;
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100492static int git_get_colorbool_config(const char *var, const char *value,
493 void *cb)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800494{
Jeff Kinge269eb72011-08-17 22:03:48 -0700495 if (!strcmp(var, get_colorbool_slot))
496 get_colorbool_found = git_config_colorbool(var, value);
497 else if (!strcmp(var, "diff.color"))
498 get_diff_color_found = git_config_colorbool(var, value);
499 else if (!strcmp(var, "color.ui"))
Jeff Kingc659f552011-08-17 22:04:56 -0700500 get_color_ui_found = git_config_colorbool(var, value);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800501 return 0;
502}
503
Jeff Kingd0e08d62014-11-20 10:15:51 -0500504static int get_colorbool(const char *var, int print)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800505{
Jeff Kingd0e08d62014-11-20 10:15:51 -0500506 get_colorbool_slot = var;
Junio C Hamano69243c22007-12-05 22:12:07 -0800507 get_colorbool_found = -1;
508 get_diff_color_found = -1;
Matthieu Moyb8612b42013-05-15 19:00:55 +0200509 get_color_ui_found = -1;
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700510 config_with_options(git_get_colorbool_config, NULL,
511 &given_config_source, &config_options);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800512
Junio C Hamano69243c22007-12-05 22:12:07 -0800513 if (get_colorbool_found < 0) {
Felipe Contrerasb4084572009-02-21 02:48:56 +0200514 if (!strcmp(get_colorbool_slot, "color.diff"))
Junio C Hamano69243c22007-12-05 22:12:07 -0800515 get_colorbool_found = get_diff_color_found;
516 if (get_colorbool_found < 0)
Jeff Kingc659f552011-08-17 22:04:56 -0700517 get_colorbool_found = get_color_ui_found;
Junio C Hamano69243c22007-12-05 22:12:07 -0800518 }
519
Matthieu Moyb8612b42013-05-15 19:00:55 +0200520 if (get_colorbool_found < 0)
521 /* default value if none found in config */
Matthieu Moy4c7f1812013-06-10 16:26:09 +0200522 get_colorbool_found = GIT_COLOR_AUTO;
Matthieu Moyb8612b42013-05-15 19:00:55 +0200523
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700524 get_colorbool_found = want_color(get_colorbool_found);
525
Felipe Contreras0e854a22009-02-21 02:48:57 +0200526 if (print) {
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800527 printf("%s\n", get_colorbool_found ? "true" : "false");
528 return 0;
Felipe Contreras0e854a22009-02-21 02:48:57 +0200529 } else
530 return get_colorbool_found ? 0 : 1;
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800531}
532
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200533static void check_write(void)
Heiko Voigt1bc88812013-07-12 00:46:47 +0200534{
Johannes Schindelin638fa622016-02-24 13:48:11 +0100535 if (!given_config_source.file && !startup_info->have_repository)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200536 die(_("not in a git directory"));
Johannes Schindelin638fa622016-02-24 13:48:11 +0100537
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200538 if (given_config_source.use_stdin)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200539 die(_("writing to stdin is not supported"));
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200540
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200541 if (given_config_source.blob)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200542 die(_("writing config blobs is not supported"));
Heiko Voigt1bc88812013-07-12 00:46:47 +0200543}
544
Junio C Hamanod4770962013-07-31 11:14:59 -0700545struct urlmatch_current_candidate_value {
546 char value_is_null;
547 struct strbuf value;
548};
549
550static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
551{
552 struct string_list *values = cb;
553 struct string_list_item *item = string_list_insert(values, var);
554 struct urlmatch_current_candidate_value *matched = item->util;
555
556 if (!matched) {
557 matched = xmalloc(sizeof(*matched));
558 strbuf_init(&matched->value, 0);
559 item->util = matched;
560 } else {
561 strbuf_reset(&matched->value);
562 }
563
564 if (value) {
565 strbuf_addstr(&matched->value, value);
566 matched->value_is_null = 0;
567 } else {
568 matched->value_is_null = 1;
569 }
570 return 0;
571}
572
Junio C Hamanod4770962013-07-31 11:14:59 -0700573static int get_urlmatch(const char *var, const char *url)
574{
John Keeping27b30be2016-02-28 11:54:35 +0000575 int ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700576 char *section_tail;
577 struct string_list_item *item;
Ævar Arnfjörð Bjarmason73ee4492021-10-01 12:27:33 +0200578 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
Junio C Hamanod4770962013-07-31 11:14:59 -0700579 struct string_list values = STRING_LIST_INIT_DUP;
580
581 config.collect_fn = urlmatch_collect_fn;
582 config.cascade_fn = NULL;
583 config.cb = &values;
584
585 if (!url_normalize(url, &config.url))
Junio C Hamano6667a6a2013-08-08 21:41:44 -0700586 die("%s", config.url.err);
Junio C Hamanod4770962013-07-31 11:14:59 -0700587
Jeff King88d5a6f2014-05-22 05:44:09 -0400588 config.section = xstrdup_tolower(var);
Junio C Hamanod4770962013-07-31 11:14:59 -0700589 section_tail = strchr(config.section, '.');
590 if (section_tail) {
591 *section_tail = '\0';
592 config.key = section_tail + 1;
593 show_keys = 0;
594 } else {
595 config.key = NULL;
596 show_keys = 1;
597 }
598
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700599 config_with_options(urlmatch_config_entry, &config,
600 &given_config_source, &config_options);
Junio C Hamanod4770962013-07-31 11:14:59 -0700601
John Keeping27b30be2016-02-28 11:54:35 +0000602 ret = !values.nr;
603
Junio C Hamanod4770962013-07-31 11:14:59 -0700604 for_each_string_list_item(item, &values) {
605 struct urlmatch_current_candidate_value *matched = item->util;
Junio C Hamanod4770962013-07-31 11:14:59 -0700606 struct strbuf buf = STRBUF_INIT;
607
Jeff Kinga92330d2015-08-20 10:49:45 -0400608 format_config(&buf, item->string,
Junio C Hamanod4770962013-07-31 11:14:59 -0700609 matched->value_is_null ? NULL : matched->value.buf);
610 fwrite(buf.buf, 1, buf.len, stdout);
Junio C Hamanod4770962013-07-31 11:14:59 -0700611 strbuf_release(&buf);
612
613 strbuf_release(&matched->value);
614 }
615 string_list_clear(&config.vars, 1);
616 string_list_clear(&values, 1);
617 free(config.url.url);
618
619 free((void *)config.section);
John Keeping27b30be2016-02-28 11:54:35 +0000620 return ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700621}
622
Matthieu Moy98305342014-07-25 21:11:34 +0200623static char *default_user_config(void)
624{
625 struct strbuf buf = STRBUF_INIT;
626 strbuf_addf(&buf,
627 _("# This is Git's per-user configuration file.\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300628 "[user]\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200629 "# Please adapt and uncomment the following lines:\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300630 "# name = %s\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200631 "# email = %s\n"),
632 ident_default_name(),
633 ident_default_email());
634 return strbuf_detach(&buf, NULL);
635}
636
Nguyễn Thái Ngọc Duy3ba7e6e2010-08-05 22:15:09 -0500637int cmd_config(int argc, const char **argv, const char *prefix)
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100638{
Nguyễn Thái Ngọc Duy3ba7e6e2010-08-05 22:15:09 -0500639 int nongit = !startup_info->have_repository;
Felipe Contreras4b951b72009-02-21 02:48:53 +0200640 char *value;
Derrick Stoleec90702a2020-11-25 22:12:54 +0000641 int flags = 0;
Petr Baudis7162dff2006-02-12 04:14:48 +0100642
Jeff King423ff9b2019-01-11 17:15:54 -0500643 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
Daniel Barkalowdc871832008-06-30 03:37:47 -0400644
Stephen Boyd37782922009-05-23 11:53:12 -0700645 argc = parse_options(argc, argv, prefix, builtin_config_options,
646 builtin_config_usage,
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200647 PARSE_OPT_STOP_AT_NON_OPTION);
648
Heiko Voigt1bc88812013-07-12 00:46:47 +0200649 if (use_global_config + use_system_config + use_local_config +
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200650 use_worktree_config +
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200651 !!given_config_source.file + !!given_config_source.blob > 1) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200652 error(_("only one config file at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200653 usage_builtin_config();
Felipe Contreras67052c92009-02-21 02:49:26 +0200654 }
655
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300656 if (nongit) {
657 if (use_local_config)
658 die(_("--local can only be used inside a git repository"));
659 if (given_config_source.blob)
660 die(_("--blob can only be used inside a git repository"));
661 if (use_worktree_config)
662 die(_("--worktree can only be used inside a git repository"));
Jeff King25cd2912017-05-12 23:29:31 -0400663
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300664 }
Jeff King17b8a2d2018-05-18 15:27:04 -0700665
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200666 if (given_config_source.file &&
667 !strcmp(given_config_source.file, "-")) {
668 given_config_source.file = NULL;
669 given_config_source.use_stdin = 1;
Matthew Rogerse37efa42020-02-10 00:30:57 +0000670 given_config_source.scope = CONFIG_SCOPE_COMMAND;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200671 }
672
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200673 if (use_global_config) {
Patrick Steinhardt1e06eb92021-04-19 14:31:12 +0200674 char *user_config, *xdg_config;
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +0200675
Patrick Steinhardt1e06eb92021-04-19 14:31:12 +0200676 git_global_config(&user_config, &xdg_config);
Matthieu Moye3ebc352012-07-12 14:04:20 +0200677 if (!user_config)
678 /*
679 * It is unknown if HOME/.gitconfig exists, so
680 * we do not know if we should write to XDG
681 * location; error out even if XDG_CONFIG_HOME
682 * is set and points at a sane location.
683 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200684 die(_("$HOME not set"));
Matthieu Moye3ebc352012-07-12 14:04:20 +0200685
Matthew Rogerse37efa42020-02-10 00:30:57 +0000686 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
687
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700688 if (access_or_warn(user_config, R_OK, 0) &&
Jeff King6c6b08d2017-09-05 09:04:20 -0400689 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200690 given_config_source.file = xdg_config;
Jeff King6c6b08d2017-09-05 09:04:20 -0400691 free(user_config);
692 } else {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200693 given_config_source.file = user_config;
Jeff King6c6b08d2017-09-05 09:04:20 -0400694 free(xdg_config);
695 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200696 }
Matthew Rogerse37efa42020-02-10 00:30:57 +0000697 else if (use_system_config) {
Patrick Steinhardtc62a9992021-04-19 14:31:08 +0200698 given_config_source.file = git_system_config();
Matthew Rogerse37efa42020-02-10 00:30:57 +0000699 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
700 } else if (use_local_config) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200701 given_config_source.file = git_pathdup("config");
Matthew Rogerse37efa42020-02-10 00:30:57 +0000702 given_config_source.scope = CONFIG_SCOPE_LOCAL;
703 } else if (use_worktree_config) {
Eric Sunshine03f24652020-06-19 19:35:44 -0400704 struct worktree **worktrees = get_worktrees();
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200705 if (repository_format_worktree_config)
706 given_config_source.file = git_pathdup("config.worktree");
707 else if (worktrees[0] && worktrees[1])
708 die(_("--worktree cannot be used with multiple "
709 "working trees unless the config\n"
710 "extension worktreeConfig is enabled. "
711 "Please read \"CONFIGURATION FILE\"\n"
712 "section in \"git help worktree\" for details"));
713 else
714 given_config_source.file = git_pathdup("config");
Matthew Rogerse37efa42020-02-10 00:30:57 +0000715 given_config_source.scope = CONFIG_SCOPE_LOCAL;
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200716 free_worktrees(worktrees);
717 } else if (given_config_source.file) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200718 if (!is_absolute_path(given_config_source.file) && prefix)
719 given_config_source.file =
Jeff Kinge4da43b2017-03-20 21:28:49 -0400720 prefix_filename(prefix, given_config_source.file);
Matthew Rogerse37efa42020-02-10 00:30:57 +0000721 given_config_source.scope = CONFIG_SCOPE_COMMAND;
722 } else if (given_config_source.blob) {
723 given_config_source.scope = CONFIG_SCOPE_COMMAND;
Petr Baudis7162dff2006-02-12 04:14:48 +0100724 }
725
Matthew Rogerse37efa42020-02-10 00:30:57 +0000726
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +0700727 if (respect_includes_opt == -1)
728 config_options.respect_includes = !given_config_source.file;
729 else
730 config_options.respect_includes = respect_includes_opt;
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700731 if (!nongit) {
732 config_options.commondir = get_git_common_dir();
733 config_options.git_dir = get_git_dir();
734 }
Jeff King9b25a0b2012-02-06 04:54:04 -0500735
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000736 if (end_nul) {
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200737 term = '\0';
738 delim = '\n';
739 key_delim = '\n';
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100740 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200741
Taylor Blau0a8950b2018-04-09 15:46:54 -0700742 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200743 error(_("--get-color and variable type are incoherent"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200744 usage_builtin_config();
Felipe Contrerasc2387352009-02-21 02:49:29 +0200745 }
746
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200747 if (HAS_MULTI_BITS(actions)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200748 error(_("only one action at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200749 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200750 }
751 if (actions == 0)
752 switch (argc) {
753 case 1: actions = ACTION_GET; break;
754 case 2: actions = ACTION_SET; break;
755 case 3: actions = ACTION_SET_ALL; break;
756 default:
Beat Bolli6aaded52018-07-05 20:34:45 +0200757 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200758 }
SZEDER Gábor578625f2015-08-10 11:46:06 +0200759 if (omit_values &&
760 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200761 error(_("--name-only is only applicable to --list or --get-regexp"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200762 usage_builtin_config();
SZEDER Gábor578625f2015-08-10 11:46:06 +0200763 }
Lars Schneider70bd8792016-02-19 10:16:02 +0100764
765 if (show_origin && !(actions &
766 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200767 error(_("--show-origin is only applicable to --get, --get-all, "
768 "--get-regexp, and --list"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200769 usage_builtin_config();
Lars Schneider70bd8792016-02-19 10:16:02 +0100770 }
771
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700772 if (default_value && !(actions & ACTION_GET)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200773 error(_("--default is only applicable to --get"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200774 usage_builtin_config();
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700775 }
776
Derrick Stoleefda43942020-11-25 22:12:53 +0000777 /* check usage of --fixed-value */
778 if (fixed_value) {
779 int allowed_usage = 0;
780
781 switch (actions) {
782 /* git config --get <name> <value-pattern> */
783 case ACTION_GET:
784 /* git config --get-all <name> <value-pattern> */
785 case ACTION_GET_ALL:
786 /* git config --get-regexp <name-pattern> <value-pattern> */
787 case ACTION_GET_REGEXP:
788 /* git config --unset <name> <value-pattern> */
789 case ACTION_UNSET:
790 /* git config --unset-all <name> <value-pattern> */
791 case ACTION_UNSET_ALL:
792 allowed_usage = argc > 1 && !!argv[1];
793 break;
794
795 /* git config <name> <value> <value-pattern> */
796 case ACTION_SET_ALL:
797 /* git config --replace-all <name> <value> <value-pattern> */
798 case ACTION_REPLACE_ALL:
799 allowed_usage = argc > 2 && !!argv[2];
800 break;
801
802 /* other options don't allow --fixed-value */
803 }
804
805 if (!allowed_usage) {
806 error(_("--fixed-value only applies with 'value-pattern'"));
807 usage_builtin_config();
808 }
Derrick Stoleec90702a2020-11-25 22:12:54 +0000809
810 flags |= CONFIG_FLAGS_FIXED_VALUE;
Derrick Stoleefda43942020-11-25 22:12:53 +0000811 }
812
Martin Ågren32888b82018-02-21 19:51:43 +0100813 if (actions & PAGING_ACTIONS)
Martin Ågrenc0e9f5b2018-02-21 19:51:44 +0100814 setup_auto_pager("config", 1);
Martin Ågren32888b82018-02-21 19:51:43 +0100815
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200816 if (actions == ACTION_LIST) {
Felipe Contreras225a9ca2009-02-21 02:49:28 +0200817 check_argc(argc, 0, 0);
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700818 if (config_with_options(show_all_config, NULL,
819 &given_config_source,
820 &config_options) < 0) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200821 if (given_config_source.file)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200822 die_errno(_("unable to read config file '%s'"),
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200823 given_config_source.file);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200824 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200825 die(_("error processing config file(s)"));
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200826 }
827 }
828 else if (actions == ACTION_EDIT) {
Michael Haggerty3696a7c2014-11-16 08:37:44 +0100829 char *config_file;
830
Felipe Contreras225a9ca2009-02-21 02:49:28 +0200831 check_argc(argc, 0, 0);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200832 if (!given_config_source.file && nongit)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200833 die(_("not in a git directory"));
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200834 if (given_config_source.use_stdin)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200835 die(_("editing stdin is not supported"));
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200836 if (given_config_source.blob)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200837 die(_("editing blobs is not supported"));
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200838 git_config(git_default_config, NULL);
Jeff Kingd9c69642017-04-20 17:09:09 -0400839 config_file = given_config_source.file ?
840 xstrdup(given_config_source.file) :
841 git_pathdup("config");
Matthieu Moy98305342014-07-25 21:11:34 +0200842 if (use_global_config) {
843 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
Jeff Kingaabbd3f2016-07-08 05:06:50 -0400844 if (fd >= 0) {
Matthieu Moy98305342014-07-25 21:11:34 +0200845 char *content = default_user_config();
846 write_str_in_full(fd, content);
847 free(content);
848 close(fd);
849 }
850 else if (errno != EEXIST)
851 die_errno(_("cannot create configuration file %s"), config_file);
852 }
853 launch_editor(config_file, NULL, NULL);
Michael Haggerty3696a7c2014-11-16 08:37:44 +0100854 free(config_file);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200855 }
856 else if (actions == ACTION_SET) {
Michael J Gruber5a2df362011-05-17 17:38:53 +0200857 int ret;
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200858 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200859 check_argc(argc, 2, 2);
860 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400861 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100862 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
Michael J Gruber5a2df362011-05-17 17:38:53 +0200863 if (ret == CONFIG_NOTHING_SET)
Vasco Almeidaccf63802016-09-15 14:59:00 +0000864 error(_("cannot overwrite multiple values with a single value\n"
865 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
Michael J Gruber5a2df362011-05-17 17:38:53 +0200866 return ret;
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200867 }
868 else if (actions == ACTION_SET_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200869 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200870 check_argc(argc, 2, 3);
871 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400872 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100873 return git_config_set_multivar_in_file_gently(given_config_source.file,
Derrick Stoleec90702a2020-11-25 22:12:54 +0000874 argv[0], value, argv[2],
875 flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200876 }
877 else if (actions == ACTION_ADD) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200878 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200879 check_argc(argc, 2, 2);
880 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400881 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100882 return git_config_set_multivar_in_file_gently(given_config_source.file,
883 argv[0], value,
Derrick Stoleec90702a2020-11-25 22:12:54 +0000884 CONFIG_REGEX_NONE,
885 flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200886 }
887 else if (actions == ACTION_REPLACE_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200888 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200889 check_argc(argc, 2, 3);
890 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400891 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100892 return git_config_set_multivar_in_file_gently(given_config_source.file,
Derrick Stolee504ee122020-11-25 22:12:49 +0000893 argv[0], value, argv[2],
Derrick Stoleec90702a2020-11-25 22:12:54 +0000894 flags | CONFIG_FLAGS_MULTI_REPLACE);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200895 }
896 else if (actions == ACTION_GET) {
897 check_argc(argc, 1, 2);
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000898 return get_value(argv[0], argv[1], flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200899 }
900 else if (actions == ACTION_GET_ALL) {
901 do_all = 1;
902 check_argc(argc, 1, 2);
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000903 return get_value(argv[0], argv[1], flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200904 }
905 else if (actions == ACTION_GET_REGEXP) {
906 show_keys = 1;
907 use_key_regexp = 1;
908 do_all = 1;
909 check_argc(argc, 1, 2);
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000910 return get_value(argv[0], argv[1], flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200911 }
Junio C Hamanod4770962013-07-31 11:14:59 -0700912 else if (actions == ACTION_GET_URLMATCH) {
913 check_argc(argc, 2, 2);
914 return get_urlmatch(argv[0], argv[1]);
915 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200916 else if (actions == ACTION_UNSET) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200917 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200918 check_argc(argc, 1, 2);
919 if (argc == 2)
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100920 return git_config_set_multivar_in_file_gently(given_config_source.file,
Derrick Stoleec90702a2020-11-25 22:12:54 +0000921 argv[0], NULL, argv[1],
922 flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200923 else
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100924 return git_config_set_in_file_gently(given_config_source.file,
925 argv[0], NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200926 }
927 else if (actions == ACTION_UNSET_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200928 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200929 check_argc(argc, 1, 2);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100930 return git_config_set_multivar_in_file_gently(given_config_source.file,
Derrick Stolee504ee122020-11-25 22:12:49 +0000931 argv[0], NULL, argv[1],
Derrick Stoleec90702a2020-11-25 22:12:54 +0000932 flags | CONFIG_FLAGS_MULTI_REPLACE);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200933 }
934 else if (actions == ACTION_RENAME_SECTION) {
935 int ret;
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200936 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200937 check_argc(argc, 2, 2);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200938 ret = git_config_rename_section_in_file(given_config_source.file,
Jeff King270a3442012-02-16 03:07:32 -0500939 argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200940 if (ret < 0)
941 return ret;
942 if (ret == 0)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200943 die(_("no such section: %s"), argv[0]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200944 }
945 else if (actions == ACTION_REMOVE_SECTION) {
946 int ret;
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200947 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200948 check_argc(argc, 1, 1);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200949 ret = git_config_rename_section_in_file(given_config_source.file,
Jeff King270a3442012-02-16 03:07:32 -0500950 argv[0], NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200951 if (ret < 0)
952 return ret;
953 if (ret == 0)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200954 die(_("no such section: %s"), argv[0]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200955 }
956 else if (actions == ACTION_GET_COLOR) {
Jeff Kingd0e08d62014-11-20 10:15:51 -0500957 check_argc(argc, 1, 2);
958 get_color(argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200959 }
960 else if (actions == ACTION_GET_COLORBOOL) {
Jeff Kingd0e08d62014-11-20 10:15:51 -0500961 check_argc(argc, 1, 2);
962 if (argc == 2)
963 color_stdout_is_tty = git_config_bool("command line", argv[1]);
964 return get_colorbool(argv[0], argc == 2);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200965 }
966
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100967 return 0;
968}