blob: 52a904cfb1ee654e16aa5a6a038e749b6b145750 [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;
17static regex_t *regexp;
18static int show_keys;
SZEDER Gábor578625f2015-08-10 11:46:06 +020019static int omit_values;
David Rientjes96f1e582006-08-15 10:23:48 -070020static int use_key_regexp;
21static int do_all;
22static int do_not_match;
Frank Lichtenheld2275d502007-06-25 16:03:55 +020023static char delim = '=';
24static char key_delim = ' ';
25static char term = '\n';
Johannes Schindelin4ddba792005-11-20 06:52:22 +010026
Sverre Rabbelier57210a62010-08-03 20:59:23 -050027static int use_global_config, use_system_config, use_local_config;
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +020028static int use_worktree_config;
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +020029static struct git_config_source given_config_source;
Taylor Blau0a8950b2018-04-09 15:46:54 -070030static int actions, type;
Taylor Blaueeaa24b2018-04-09 17:18:26 -070031static char *default_value;
Matthew Rogers329e6ec2020-01-24 00:21:01 +000032static int end_nul;
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +070033static int respect_includes_opt = -1;
34static struct config_options config_options;
Lars Schneider70bd8792016-02-19 10:16:02 +010035static int show_origin;
Felipe Contrerasd64ec162009-02-21 02:49:25 +020036
37#define ACTION_GET (1<<0)
38#define ACTION_GET_ALL (1<<1)
39#define ACTION_GET_REGEXP (1<<2)
40#define ACTION_REPLACE_ALL (1<<3)
41#define ACTION_ADD (1<<4)
42#define ACTION_UNSET (1<<5)
43#define ACTION_UNSET_ALL (1<<6)
44#define ACTION_RENAME_SECTION (1<<7)
45#define ACTION_REMOVE_SECTION (1<<8)
46#define ACTION_LIST (1<<9)
47#define ACTION_EDIT (1<<10)
48#define ACTION_SET (1<<11)
49#define ACTION_SET_ALL (1<<12)
50#define ACTION_GET_COLOR (1<<13)
51#define ACTION_GET_COLORBOOL (1<<14)
Junio C Hamanod4770962013-07-31 11:14:59 -070052#define ACTION_GET_URLMATCH (1<<15)
Felipe Contrerasd64ec162009-02-21 02:49:25 +020053
Martin Ågren32888b82018-02-21 19:51:43 +010054/*
55 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
56 * one line of output and which should therefore be paged.
57 */
58#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
59 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
60
Taylor Blau0a8950b2018-04-09 15:46:54 -070061#define TYPE_BOOL 1
62#define TYPE_INT 2
63#define TYPE_BOOL_OR_INT 3
64#define TYPE_PATH 4
65#define TYPE_EXPIRY_DATE 5
Taylor Blau63e2a0f2018-04-09 17:18:31 -070066#define TYPE_COLOR 6
Felipe Contreras16c1e932009-02-21 02:49:27 +020067
Taylor Blaufb0dc3b2018-04-18 14:43:35 -070068#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
69 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
70 PARSE_OPT_NONEG, option_parse_type, (i) }
71
Beat Bolli6aaded52018-07-05 20:34:45 +020072static NORETURN void usage_builtin_config(void);
Taylor Blaufb0dc3b2018-04-18 14:43:35 -070073
74static int option_parse_type(const struct option *opt, const char *arg,
75 int unset)
76{
77 int new_type, *to_type;
78
79 if (unset) {
80 *((int *) opt->value) = 0;
81 return 0;
82 }
83
84 /*
85 * To support '--<type>' style flags, begin with new_type equal to
86 * opt->defval.
87 */
88 new_type = opt->defval;
89 if (!new_type) {
90 if (!strcmp(arg, "bool"))
91 new_type = TYPE_BOOL;
92 else if (!strcmp(arg, "int"))
93 new_type = TYPE_INT;
94 else if (!strcmp(arg, "bool-or-int"))
95 new_type = TYPE_BOOL_OR_INT;
96 else if (!strcmp(arg, "path"))
97 new_type = TYPE_PATH;
98 else if (!strcmp(arg, "expiry-date"))
99 new_type = TYPE_EXPIRY_DATE;
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700100 else if (!strcmp(arg, "color"))
101 new_type = TYPE_COLOR;
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700102 else
103 die(_("unrecognized --type argument, %s"), arg);
104 }
105
106 to_type = opt->value;
107 if (*to_type && *to_type != new_type) {
108 /*
109 * Complain when there is a new type not equal to the old type.
110 * This allows for combinations like '--int --type=int' and
111 * '--type=int --type=int', but disallows ones like '--type=bool
112 * --int' and '--type=bool
113 * --type=int'.
114 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200115 error(_("only one type at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200116 usage_builtin_config();
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700117 }
118 *to_type = new_type;
119
120 return 0;
121}
122
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200123static struct option builtin_config_options[] = {
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700124 OPT_GROUP(N_("Config file location")),
Stefan Beller21e047d2013-08-03 13:51:24 +0200125 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
126 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
127 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200128 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200129 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
130 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 +0700131 OPT_GROUP(N_("Action")),
132 OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
133 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
134 OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
Junio C Hamanod4770962013-07-31 11:14:59 -0700135 OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700136 OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
Nguyễn Thái Ngọc Duyf63cf8c2012-08-20 19:32:55 +0700137 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
138 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
139 OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700140 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
141 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
142 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
Nguyễn Thái Ngọc Duyf63cf8c2012-08-20 19:32:55 +0700143 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
Jeff Kingd0e08d62014-11-20 10:15:51 -0500144 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
145 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 +0700146 OPT_GROUP(N_("Type")),
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700147 OPT_CALLBACK('t', "type", &type, "", N_("value is given this type"), option_parse_type),
148 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
149 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
150 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
151 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
152 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 +0700153 OPT_GROUP(N_("Other")),
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000154 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
SZEDER Gábor578625f2015-08-10 11:46:06 +0200155 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +0700156 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
Lars Schneider70bd8792016-02-19 10:16:02 +0100157 OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700158 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200159 OPT_END(),
160};
161
Beat Bolli6aaded52018-07-05 20:34:45 +0200162static NORETURN void usage_builtin_config(void)
163{
164 usage_with_options(builtin_config_usage, builtin_config_options);
165}
166
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100167static void check_argc(int argc, int min, int max)
168{
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200169 if (argc >= min && argc <= max)
170 return;
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200171 if (min == max)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200172 error(_("wrong number of arguments, should be %d"), min);
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200173 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200174 error(_("wrong number of arguments, should be from %d to %d"),
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200175 min, max);
Beat Bolli6aaded52018-07-05 20:34:45 +0200176 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200177}
178
Lars Schneider70bd8792016-02-19 10:16:02 +0100179static void show_config_origin(struct strbuf *buf)
180{
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000181 const char term = end_nul ? '\0' : '\t';
Lars Schneider70bd8792016-02-19 10:16:02 +0100182
183 strbuf_addstr(buf, current_config_origin_type());
184 strbuf_addch(buf, ':');
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000185 if (end_nul)
Lars Schneider70bd8792016-02-19 10:16:02 +0100186 strbuf_addstr(buf, current_config_name());
187 else
188 quote_c_style(current_config_name(), buf, NULL, 0);
189 strbuf_addch(buf, term);
190}
191
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100192static int show_all_config(const char *key_, const char *value_, void *cb)
Petr Baudisde791f12006-04-25 00:59:25 +0200193{
Lars Schneider70bd8792016-02-19 10:16:02 +0100194 if (show_origin) {
195 struct strbuf buf = STRBUF_INIT;
196 show_config_origin(&buf);
197 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
198 fwrite(buf.buf, 1, buf.len, stdout);
199 strbuf_release(&buf);
200 }
SZEDER Gábor578625f2015-08-10 11:46:06 +0200201 if (!omit_values && value_)
Frank Lichtenheld2275d502007-06-25 16:03:55 +0200202 printf("%s%c%s%c", key_, delim, value_, term);
Petr Baudisde791f12006-04-25 00:59:25 +0200203 else
Frank Lichtenheld2275d502007-06-25 16:03:55 +0200204 printf("%s%c", key_, term);
Petr Baudisde791f12006-04-25 00:59:25 +0200205 return 0;
206}
207
Jeff King7acdd6f2012-10-23 15:51:50 -0400208struct strbuf_list {
209 struct strbuf *items;
210 int nr;
211 int alloc;
212};
213
Junio C Hamanod9b91692013-07-29 14:23:16 -0700214static int format_config(struct strbuf *buf, const char *key_, const char *value_)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100215{
Lars Schneider70bd8792016-02-19 10:16:02 +0100216 if (show_origin)
217 show_config_origin(buf);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200218 if (show_keys)
Jeff King7acdd6f2012-10-23 15:51:50 -0400219 strbuf_addstr(buf, key_);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200220 if (!omit_values) {
Jeff Kingf2259872015-08-20 10:47:34 -0400221 if (show_keys)
222 strbuf_addch(buf, key_delim);
Jeff King00b347d2012-10-23 16:52:44 -0400223
Taylor Blau0a8950b2018-04-09 15:46:54 -0700224 if (type == TYPE_INT)
Jeff Kingf2259872015-08-20 10:47:34 -0400225 strbuf_addf(buf, "%"PRId64,
226 git_config_int64(key_, value_ ? value_ : ""));
Taylor Blau0a8950b2018-04-09 15:46:54 -0700227 else if (type == TYPE_BOOL)
Jeff Kingf2259872015-08-20 10:47:34 -0400228 strbuf_addstr(buf, git_config_bool(key_, value_) ?
229 "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700230 else if (type == TYPE_BOOL_OR_INT) {
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200231 int is_bool, v;
232 v = git_config_bool_or_int(key_, value_, &is_bool);
233 if (is_bool)
Jeff Kingf2259872015-08-20 10:47:34 -0400234 strbuf_addstr(buf, v ? "true" : "false");
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200235 else
Jeff Kingf2259872015-08-20 10:47:34 -0400236 strbuf_addf(buf, "%d", v);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700237 } else if (type == TYPE_PATH) {
Jeff Kingf2259872015-08-20 10:47:34 -0400238 const char *v;
239 if (git_config_pathname(&v, key_, value_) < 0)
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200240 return -1;
Jeff Kingf2259872015-08-20 10:47:34 -0400241 strbuf_addstr(buf, v);
242 free((char *)v);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700243 } else if (type == TYPE_EXPIRY_DATE) {
Haaris Mehmood5f967422017-11-18 02:27:27 +0000244 timestamp_t t;
245 if (git_config_expiry_date(&t, key_, value_) < 0)
246 return -1;
247 strbuf_addf(buf, "%"PRItime, t);
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700248 } else if (type == TYPE_COLOR) {
249 char v[COLOR_MAXLEN];
250 if (git_config_color(v, key_, value_) < 0)
251 return -1;
252 strbuf_addstr(buf, v);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200253 } else if (value_) {
Jeff Kingf2259872015-08-20 10:47:34 -0400254 strbuf_addstr(buf, value_);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200255 } else {
Jeff Kingf2259872015-08-20 10:47:34 -0400256 /* Just show the key name; back out delimiter */
257 if (show_keys)
258 strbuf_setlen(buf, buf->len - 1);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200259 }
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200260 }
Jeff King00b347d2012-10-23 16:52:44 -0400261 strbuf_addch(buf, term);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100262 return 0;
263}
264
Junio C Hamanod9b91692013-07-29 14:23:16 -0700265static int collect_config(const char *key_, const char *value_, void *cb)
266{
267 struct strbuf_list *values = cb;
268
269 if (!use_key_regexp && strcmp(key_, key))
270 return 0;
271 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
272 return 0;
273 if (regexp != NULL &&
274 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
275 return 0;
276
277 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
Jeff King9f1429d2015-08-20 10:46:04 -0400278 strbuf_init(&values->items[values->nr], 0);
Junio C Hamanod9b91692013-07-29 14:23:16 -0700279
280 return format_config(&values->items[values->nr++], key_, value_);
281}
282
Felipe Contreras4b951b72009-02-21 02:48:53 +0200283static int get_value(const char *key_, const char *regex_)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100284{
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700285 int ret = CONFIG_GENERIC_ERROR;
Ramsay Jones5ba1a8a2012-10-28 21:05:25 +0000286 struct strbuf_list values = {NULL};
Jeff King7acdd6f2012-10-23 15:51:50 -0400287 int i;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100288
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200289 if (use_key_regexp) {
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100290 char *tl;
291
292 /*
293 * NEEDSWORK: this naive pattern lowercasing obviously does not
294 * work for more complex patterns like "^[^.]*Foo.*bar".
295 * Perhaps we should deprecate this altogether someday.
296 */
297
298 key = xstrdup(key_);
299 for (tl = key + strlen(key) - 1;
300 tl >= key && *tl != '.';
301 tl--)
302 *tl = tolower(*tl);
303 for (tl = key; *tl && *tl != '.'; tl++)
304 *tl = tolower(*tl);
305
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200306 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200307 if (regcomp(key_regexp, key, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200308 error(_("invalid key pattern: %s"), key_);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000309 FREE_AND_NULL(key_regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700310 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200311 goto free_strings;
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200312 }
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100313 } else {
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700314 if (git_config_parse_key(key_, &key, NULL)) {
315 ret = CONFIG_INVALID_KEY;
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100316 goto free_strings;
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700317 }
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200318 }
319
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100320 if (regex_) {
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100321 if (regex_[0] == '!') {
322 do_not_match = 1;
323 regex_++;
324 }
325
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200326 regexp = (regex_t*)xmalloc(sizeof(regex_t));
Amos Waterland0a152172006-01-04 19:31:02 -0500327 if (regcomp(regexp, regex_, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200328 error(_("invalid pattern: %s"), regex_);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000329 FREE_AND_NULL(regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700330 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200331 goto free_strings;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100332 }
333 }
334
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700335 config_with_options(collect_config, &values,
336 &given_config_source, &config_options);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200337
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700338 if (!values.nr && default_value) {
339 struct strbuf *item;
340 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
341 item = &values.items[values.nr++];
342 strbuf_init(item, 0);
343 if (format_config(item, key_, default_value) < 0)
344 die(_("failed to format default config value: %s"),
345 default_value);
346 }
347
Jeff King00b347d2012-10-23 16:52:44 -0400348 ret = !values.nr;
Jeff King7acdd6f2012-10-23 15:51:50 -0400349
350 for (i = 0; i < values.nr; i++) {
351 struct strbuf *buf = values.items + i;
Jeff King00b347d2012-10-23 16:52:44 -0400352 if (do_all || i == values.nr - 1)
353 fwrite(buf->buf, 1, buf->len, stdout);
Jeff King7acdd6f2012-10-23 15:51:50 -0400354 strbuf_release(buf);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100355 }
Jeff King7acdd6f2012-10-23 15:51:50 -0400356 free(values.items);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100357
Jeff King97ed50f2012-10-23 15:40:06 -0400358free_strings:
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100359 free(key);
Jeff King35998c82012-10-23 15:36:12 -0400360 if (key_regexp) {
361 regfree(key_regexp);
362 free(key_regexp);
363 }
Amos Waterland0a152172006-01-04 19:31:02 -0500364 if (regexp) {
365 regfree(regexp);
366 free(regexp);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100367 }
368
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200369 return ret;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100370}
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100371
Stephan Beyer186458b2008-07-24 01:09:35 +0200372static char *normalize_value(const char *key, const char *value)
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200373{
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200374 if (!value)
375 return NULL;
376
Taylor Blau0a8950b2018-04-09 15:46:54 -0700377 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
Matthieu Moy13494842009-12-30 17:51:53 +0100378 /*
379 * We don't do normalization for TYPE_PATH here: If
380 * the path is like ~/foobar/, we prefer to store
381 * "~/foobar/" in the config file, and to expand the ~
382 * when retrieving the value.
Haaris Mehmood5f967422017-11-18 02:27:27 +0000383 * Also don't do normalization for expiry dates.
Matthieu Moy13494842009-12-30 17:51:53 +0100384 */
Jeff King3ec832c2015-09-24 17:07:05 -0400385 return xstrdup(value);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700386 if (type == TYPE_INT)
Jeff King3ec832c2015-09-24 17:07:05 -0400387 return xstrfmt("%"PRId64, git_config_int64(key, value));
Taylor Blau0a8950b2018-04-09 15:46:54 -0700388 if (type == TYPE_BOOL)
Jeff King3ec832c2015-09-24 17:07:05 -0400389 return xstrdup(git_config_bool(key, value) ? "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700390 if (type == TYPE_BOOL_OR_INT) {
Jeff King3ec832c2015-09-24 17:07:05 -0400391 int is_bool, v;
392 v = git_config_bool_or_int(key, value, &is_bool);
393 if (!is_bool)
394 return xstrfmt("%d", v);
395 else
396 return xstrdup(v ? "true" : "false");
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200397 }
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700398 if (type == TYPE_COLOR) {
399 char v[COLOR_MAXLEN];
400 if (git_config_color(v, key, value))
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200401 die(_("cannot parse color '%s'"), value);
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700402
403 /*
404 * The contents of `v` now contain an ANSI escape
405 * sequence, not suitable for including within a
406 * configuration file. Treat the above as a
407 * "sanity-check", and return the given value, which we
408 * know is representable as valid color code.
409 */
410 return xstrdup(value);
411 }
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200412
Junio C Hamano50f08db2018-05-30 14:04:07 +0900413 BUG("cannot normalize type %d", type);
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200414}
415
Junio C Hamano9ce03522007-11-27 22:41:05 -0800416static int get_color_found;
417static const char *get_color_slot;
Felipe Contrerasb4084572009-02-21 02:48:56 +0200418static const char *get_colorbool_slot;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800419static char parsed_color[COLOR_MAXLEN];
420
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100421static int git_get_color_config(const char *var, const char *value, void *cb)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800422{
423 if (!strcmp(var, get_color_slot)) {
Junio C Hamanof7699822008-02-11 10:48:12 -0800424 if (!value)
425 config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400426 if (color_parse(value, parsed_color) < 0)
427 return -1;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800428 get_color_found = 1;
429 }
430 return 0;
431}
432
Jeff Kingd0e08d62014-11-20 10:15:51 -0500433static void get_color(const char *var, const char *def_color)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800434{
Jeff Kingd0e08d62014-11-20 10:15:51 -0500435 get_color_slot = var;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800436 get_color_found = 0;
437 parsed_color[0] = '\0';
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700438 config_with_options(git_get_color_config, NULL,
439 &given_config_source, &config_options);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800440
Jeff Kingf6c5a292014-10-07 15:33:09 -0400441 if (!get_color_found && def_color) {
442 if (color_parse(def_color, parsed_color) < 0)
443 die(_("unable to parse default color value"));
444 }
Junio C Hamano9ce03522007-11-27 22:41:05 -0800445
446 fputs(parsed_color, stdout);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800447}
448
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800449static int get_colorbool_found;
Junio C Hamano69243c22007-12-05 22:12:07 -0800450static int get_diff_color_found;
Jeff Kingc659f552011-08-17 22:04:56 -0700451static int get_color_ui_found;
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100452static int git_get_colorbool_config(const char *var, const char *value,
453 void *cb)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800454{
Jeff Kinge269eb72011-08-17 22:03:48 -0700455 if (!strcmp(var, get_colorbool_slot))
456 get_colorbool_found = git_config_colorbool(var, value);
457 else if (!strcmp(var, "diff.color"))
458 get_diff_color_found = git_config_colorbool(var, value);
459 else if (!strcmp(var, "color.ui"))
Jeff Kingc659f552011-08-17 22:04:56 -0700460 get_color_ui_found = git_config_colorbool(var, value);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800461 return 0;
462}
463
Jeff Kingd0e08d62014-11-20 10:15:51 -0500464static int get_colorbool(const char *var, int print)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800465{
Jeff Kingd0e08d62014-11-20 10:15:51 -0500466 get_colorbool_slot = var;
Junio C Hamano69243c22007-12-05 22:12:07 -0800467 get_colorbool_found = -1;
468 get_diff_color_found = -1;
Matthieu Moyb8612b42013-05-15 19:00:55 +0200469 get_color_ui_found = -1;
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700470 config_with_options(git_get_colorbool_config, NULL,
471 &given_config_source, &config_options);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800472
Junio C Hamano69243c22007-12-05 22:12:07 -0800473 if (get_colorbool_found < 0) {
Felipe Contrerasb4084572009-02-21 02:48:56 +0200474 if (!strcmp(get_colorbool_slot, "color.diff"))
Junio C Hamano69243c22007-12-05 22:12:07 -0800475 get_colorbool_found = get_diff_color_found;
476 if (get_colorbool_found < 0)
Jeff Kingc659f552011-08-17 22:04:56 -0700477 get_colorbool_found = get_color_ui_found;
Junio C Hamano69243c22007-12-05 22:12:07 -0800478 }
479
Matthieu Moyb8612b42013-05-15 19:00:55 +0200480 if (get_colorbool_found < 0)
481 /* default value if none found in config */
Matthieu Moy4c7f1812013-06-10 16:26:09 +0200482 get_colorbool_found = GIT_COLOR_AUTO;
Matthieu Moyb8612b42013-05-15 19:00:55 +0200483
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700484 get_colorbool_found = want_color(get_colorbool_found);
485
Felipe Contreras0e854a22009-02-21 02:48:57 +0200486 if (print) {
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800487 printf("%s\n", get_colorbool_found ? "true" : "false");
488 return 0;
Felipe Contreras0e854a22009-02-21 02:48:57 +0200489 } else
490 return get_colorbool_found ? 0 : 1;
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800491}
492
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200493static void check_write(void)
Heiko Voigt1bc88812013-07-12 00:46:47 +0200494{
Johannes Schindelin638fa622016-02-24 13:48:11 +0100495 if (!given_config_source.file && !startup_info->have_repository)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200496 die(_("not in a git directory"));
Johannes Schindelin638fa622016-02-24 13:48:11 +0100497
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200498 if (given_config_source.use_stdin)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200499 die(_("writing to stdin is not supported"));
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200500
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200501 if (given_config_source.blob)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200502 die(_("writing config blobs is not supported"));
Heiko Voigt1bc88812013-07-12 00:46:47 +0200503}
504
Junio C Hamanod4770962013-07-31 11:14:59 -0700505struct urlmatch_current_candidate_value {
506 char value_is_null;
507 struct strbuf value;
508};
509
510static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
511{
512 struct string_list *values = cb;
513 struct string_list_item *item = string_list_insert(values, var);
514 struct urlmatch_current_candidate_value *matched = item->util;
515
516 if (!matched) {
517 matched = xmalloc(sizeof(*matched));
518 strbuf_init(&matched->value, 0);
519 item->util = matched;
520 } else {
521 strbuf_reset(&matched->value);
522 }
523
524 if (value) {
525 strbuf_addstr(&matched->value, value);
526 matched->value_is_null = 0;
527 } else {
528 matched->value_is_null = 1;
529 }
530 return 0;
531}
532
Junio C Hamanod4770962013-07-31 11:14:59 -0700533static int get_urlmatch(const char *var, const char *url)
534{
John Keeping27b30be2016-02-28 11:54:35 +0000535 int ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700536 char *section_tail;
537 struct string_list_item *item;
538 struct urlmatch_config config = { STRING_LIST_INIT_DUP };
539 struct string_list values = STRING_LIST_INIT_DUP;
540
541 config.collect_fn = urlmatch_collect_fn;
542 config.cascade_fn = NULL;
543 config.cb = &values;
544
545 if (!url_normalize(url, &config.url))
Junio C Hamano6667a6a2013-08-08 21:41:44 -0700546 die("%s", config.url.err);
Junio C Hamanod4770962013-07-31 11:14:59 -0700547
Jeff King88d5a6f2014-05-22 05:44:09 -0400548 config.section = xstrdup_tolower(var);
Junio C Hamanod4770962013-07-31 11:14:59 -0700549 section_tail = strchr(config.section, '.');
550 if (section_tail) {
551 *section_tail = '\0';
552 config.key = section_tail + 1;
553 show_keys = 0;
554 } else {
555 config.key = NULL;
556 show_keys = 1;
557 }
558
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700559 config_with_options(urlmatch_config_entry, &config,
560 &given_config_source, &config_options);
Junio C Hamanod4770962013-07-31 11:14:59 -0700561
John Keeping27b30be2016-02-28 11:54:35 +0000562 ret = !values.nr;
563
Junio C Hamanod4770962013-07-31 11:14:59 -0700564 for_each_string_list_item(item, &values) {
565 struct urlmatch_current_candidate_value *matched = item->util;
Junio C Hamanod4770962013-07-31 11:14:59 -0700566 struct strbuf buf = STRBUF_INIT;
567
Jeff Kinga92330d2015-08-20 10:49:45 -0400568 format_config(&buf, item->string,
Junio C Hamanod4770962013-07-31 11:14:59 -0700569 matched->value_is_null ? NULL : matched->value.buf);
570 fwrite(buf.buf, 1, buf.len, stdout);
Junio C Hamanod4770962013-07-31 11:14:59 -0700571 strbuf_release(&buf);
572
573 strbuf_release(&matched->value);
574 }
575 string_list_clear(&config.vars, 1);
576 string_list_clear(&values, 1);
577 free(config.url.url);
578
579 free((void *)config.section);
John Keeping27b30be2016-02-28 11:54:35 +0000580 return ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700581}
582
Matthieu Moy98305342014-07-25 21:11:34 +0200583static char *default_user_config(void)
584{
585 struct strbuf buf = STRBUF_INIT;
586 strbuf_addf(&buf,
587 _("# This is Git's per-user configuration file.\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300588 "[user]\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200589 "# Please adapt and uncomment the following lines:\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300590 "# name = %s\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200591 "# email = %s\n"),
592 ident_default_name(),
593 ident_default_email());
594 return strbuf_detach(&buf, NULL);
595}
596
Nguyễn Thái Ngọc Duy3ba7e6e2010-08-05 22:15:09 -0500597int cmd_config(int argc, const char **argv, const char *prefix)
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100598{
Nguyễn Thái Ngọc Duy3ba7e6e2010-08-05 22:15:09 -0500599 int nongit = !startup_info->have_repository;
Felipe Contreras4b951b72009-02-21 02:48:53 +0200600 char *value;
Petr Baudis7162dff2006-02-12 04:14:48 +0100601
Jeff King423ff9b2019-01-11 17:15:54 -0500602 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
Daniel Barkalowdc871832008-06-30 03:37:47 -0400603
Stephen Boyd37782922009-05-23 11:53:12 -0700604 argc = parse_options(argc, argv, prefix, builtin_config_options,
605 builtin_config_usage,
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200606 PARSE_OPT_STOP_AT_NON_OPTION);
607
Heiko Voigt1bc88812013-07-12 00:46:47 +0200608 if (use_global_config + use_system_config + use_local_config +
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200609 use_worktree_config +
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200610 !!given_config_source.file + !!given_config_source.blob > 1) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200611 error(_("only one config file at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200612 usage_builtin_config();
Felipe Contreras67052c92009-02-21 02:49:26 +0200613 }
614
Jeff King25cd2912017-05-12 23:29:31 -0400615 if (use_local_config && nongit)
616 die(_("--local can only be used inside a git repository"));
617
Jeff King17b8a2d2018-05-18 15:27:04 -0700618 if (given_config_source.blob && nongit)
619 die(_("--blob can only be used inside a git repository"));
620
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200621 if (given_config_source.file &&
622 !strcmp(given_config_source.file, "-")) {
623 given_config_source.file = NULL;
624 given_config_source.use_stdin = 1;
625 }
626
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200627 if (use_global_config) {
Nguyễn Thái Ngọc Duy4aad2f12017-04-05 17:24:38 +0700628 char *user_config = expand_user_path("~/.gitconfig", 0);
Paul Tan509adc32015-05-06 16:01:03 +0800629 char *xdg_config = xdg_config_home("config");
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +0200630
Matthieu Moye3ebc352012-07-12 14:04:20 +0200631 if (!user_config)
632 /*
633 * It is unknown if HOME/.gitconfig exists, so
634 * we do not know if we should write to XDG
635 * location; error out even if XDG_CONFIG_HOME
636 * is set and points at a sane location.
637 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200638 die(_("$HOME not set"));
Matthieu Moye3ebc352012-07-12 14:04:20 +0200639
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700640 if (access_or_warn(user_config, R_OK, 0) &&
Jeff King6c6b08d2017-09-05 09:04:20 -0400641 xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200642 given_config_source.file = xdg_config;
Jeff King6c6b08d2017-09-05 09:04:20 -0400643 free(user_config);
644 } else {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200645 given_config_source.file = user_config;
Jeff King6c6b08d2017-09-05 09:04:20 -0400646 free(xdg_config);
647 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200648 }
649 else if (use_system_config)
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200650 given_config_source.file = git_etc_gitconfig();
Sverre Rabbelier57210a62010-08-03 20:59:23 -0500651 else if (use_local_config)
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200652 given_config_source.file = git_pathdup("config");
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200653 else if (use_worktree_config) {
654 struct worktree **worktrees = get_worktrees(0);
655 if (repository_format_worktree_config)
656 given_config_source.file = git_pathdup("config.worktree");
657 else if (worktrees[0] && worktrees[1])
658 die(_("--worktree cannot be used with multiple "
659 "working trees unless the config\n"
660 "extension worktreeConfig is enabled. "
661 "Please read \"CONFIGURATION FILE\"\n"
662 "section in \"git help worktree\" for details"));
663 else
664 given_config_source.file = git_pathdup("config");
665 free_worktrees(worktrees);
666 } else if (given_config_source.file) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200667 if (!is_absolute_path(given_config_source.file) && prefix)
668 given_config_source.file =
Jeff Kinge4da43b2017-03-20 21:28:49 -0400669 prefix_filename(prefix, given_config_source.file);
Petr Baudis7162dff2006-02-12 04:14:48 +0100670 }
671
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +0700672 if (respect_includes_opt == -1)
673 config_options.respect_includes = !given_config_source.file;
674 else
675 config_options.respect_includes = respect_includes_opt;
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700676 if (!nongit) {
677 config_options.commondir = get_git_common_dir();
678 config_options.git_dir = get_git_dir();
679 }
Jeff King9b25a0b2012-02-06 04:54:04 -0500680
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000681 if (end_nul) {
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200682 term = '\0';
683 delim = '\n';
684 key_delim = '\n';
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100685 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200686
Taylor Blau0a8950b2018-04-09 15:46:54 -0700687 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200688 error(_("--get-color and variable type are incoherent"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200689 usage_builtin_config();
Felipe Contrerasc2387352009-02-21 02:49:29 +0200690 }
691
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200692 if (HAS_MULTI_BITS(actions)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200693 error(_("only one action at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200694 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200695 }
696 if (actions == 0)
697 switch (argc) {
698 case 1: actions = ACTION_GET; break;
699 case 2: actions = ACTION_SET; break;
700 case 3: actions = ACTION_SET_ALL; break;
701 default:
Beat Bolli6aaded52018-07-05 20:34:45 +0200702 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200703 }
SZEDER Gábor578625f2015-08-10 11:46:06 +0200704 if (omit_values &&
705 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200706 error(_("--name-only is only applicable to --list or --get-regexp"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200707 usage_builtin_config();
SZEDER Gábor578625f2015-08-10 11:46:06 +0200708 }
Lars Schneider70bd8792016-02-19 10:16:02 +0100709
710 if (show_origin && !(actions &
711 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200712 error(_("--show-origin is only applicable to --get, --get-all, "
713 "--get-regexp, and --list"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200714 usage_builtin_config();
Lars Schneider70bd8792016-02-19 10:16:02 +0100715 }
716
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700717 if (default_value && !(actions & ACTION_GET)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200718 error(_("--default is only applicable to --get"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200719 usage_builtin_config();
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700720 }
721
Martin Ågren32888b82018-02-21 19:51:43 +0100722 if (actions & PAGING_ACTIONS)
Martin Ågrenc0e9f5b2018-02-21 19:51:44 +0100723 setup_auto_pager("config", 1);
Martin Ågren32888b82018-02-21 19:51:43 +0100724
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200725 if (actions == ACTION_LIST) {
Felipe Contreras225a9ca2009-02-21 02:49:28 +0200726 check_argc(argc, 0, 0);
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700727 if (config_with_options(show_all_config, NULL,
728 &given_config_source,
729 &config_options) < 0) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200730 if (given_config_source.file)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200731 die_errno(_("unable to read config file '%s'"),
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200732 given_config_source.file);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200733 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200734 die(_("error processing config file(s)"));
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200735 }
736 }
737 else if (actions == ACTION_EDIT) {
Michael Haggerty3696a7c2014-11-16 08:37:44 +0100738 char *config_file;
739
Felipe Contreras225a9ca2009-02-21 02:49:28 +0200740 check_argc(argc, 0, 0);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200741 if (!given_config_source.file && nongit)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200742 die(_("not in a git directory"));
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200743 if (given_config_source.use_stdin)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200744 die(_("editing stdin is not supported"));
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200745 if (given_config_source.blob)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200746 die(_("editing blobs is not supported"));
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200747 git_config(git_default_config, NULL);
Jeff Kingd9c69642017-04-20 17:09:09 -0400748 config_file = given_config_source.file ?
749 xstrdup(given_config_source.file) :
750 git_pathdup("config");
Matthieu Moy98305342014-07-25 21:11:34 +0200751 if (use_global_config) {
752 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
Jeff Kingaabbd3f2016-07-08 05:06:50 -0400753 if (fd >= 0) {
Matthieu Moy98305342014-07-25 21:11:34 +0200754 char *content = default_user_config();
755 write_str_in_full(fd, content);
756 free(content);
757 close(fd);
758 }
759 else if (errno != EEXIST)
760 die_errno(_("cannot create configuration file %s"), config_file);
761 }
762 launch_editor(config_file, NULL, NULL);
Michael Haggerty3696a7c2014-11-16 08:37:44 +0100763 free(config_file);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200764 }
765 else if (actions == ACTION_SET) {
Michael J Gruber5a2df362011-05-17 17:38:53 +0200766 int ret;
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200767 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200768 check_argc(argc, 2, 2);
769 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400770 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100771 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
Michael J Gruber5a2df362011-05-17 17:38:53 +0200772 if (ret == CONFIG_NOTHING_SET)
Vasco Almeidaccf63802016-09-15 14:59:00 +0000773 error(_("cannot overwrite multiple values with a single value\n"
774 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
Michael J Gruber5a2df362011-05-17 17:38:53 +0200775 return ret;
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200776 }
777 else if (actions == ACTION_SET_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200778 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200779 check_argc(argc, 2, 3);
780 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400781 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100782 return git_config_set_multivar_in_file_gently(given_config_source.file,
783 argv[0], value, argv[2], 0);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200784 }
785 else if (actions == ACTION_ADD) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200786 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200787 check_argc(argc, 2, 2);
788 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400789 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100790 return git_config_set_multivar_in_file_gently(given_config_source.file,
791 argv[0], value,
792 CONFIG_REGEX_NONE, 0);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200793 }
794 else if (actions == ACTION_REPLACE_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200795 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200796 check_argc(argc, 2, 3);
797 value = normalize_value(argv[0], argv[1]);
Jeff King0e5bba52017-09-08 02:38:41 -0400798 UNLEAK(value);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100799 return git_config_set_multivar_in_file_gently(given_config_source.file,
800 argv[0], value, argv[2], 1);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200801 }
802 else if (actions == ACTION_GET) {
803 check_argc(argc, 1, 2);
804 return get_value(argv[0], argv[1]);
805 }
806 else if (actions == ACTION_GET_ALL) {
807 do_all = 1;
808 check_argc(argc, 1, 2);
809 return get_value(argv[0], argv[1]);
810 }
811 else if (actions == ACTION_GET_REGEXP) {
812 show_keys = 1;
813 use_key_regexp = 1;
814 do_all = 1;
815 check_argc(argc, 1, 2);
816 return get_value(argv[0], argv[1]);
817 }
Junio C Hamanod4770962013-07-31 11:14:59 -0700818 else if (actions == ACTION_GET_URLMATCH) {
819 check_argc(argc, 2, 2);
820 return get_urlmatch(argv[0], argv[1]);
821 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200822 else if (actions == ACTION_UNSET) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200823 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200824 check_argc(argc, 1, 2);
825 if (argc == 2)
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100826 return git_config_set_multivar_in_file_gently(given_config_source.file,
827 argv[0], NULL, argv[1], 0);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200828 else
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100829 return git_config_set_in_file_gently(given_config_source.file,
830 argv[0], NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200831 }
832 else if (actions == ACTION_UNSET_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200833 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200834 check_argc(argc, 1, 2);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100835 return git_config_set_multivar_in_file_gently(given_config_source.file,
836 argv[0], NULL, argv[1], 1);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200837 }
838 else if (actions == ACTION_RENAME_SECTION) {
839 int ret;
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200840 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200841 check_argc(argc, 2, 2);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200842 ret = git_config_rename_section_in_file(given_config_source.file,
Jeff King270a3442012-02-16 03:07:32 -0500843 argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200844 if (ret < 0)
845 return ret;
846 if (ret == 0)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200847 die(_("no such section: %s"), argv[0]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200848 }
849 else if (actions == ACTION_REMOVE_SECTION) {
850 int ret;
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200851 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200852 check_argc(argc, 1, 1);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200853 ret = git_config_rename_section_in_file(given_config_source.file,
Jeff King270a3442012-02-16 03:07:32 -0500854 argv[0], NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200855 if (ret < 0)
856 return ret;
857 if (ret == 0)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200858 die(_("no such section: %s"), argv[0]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200859 }
860 else if (actions == ACTION_GET_COLOR) {
Jeff Kingd0e08d62014-11-20 10:15:51 -0500861 check_argc(argc, 1, 2);
862 get_color(argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200863 }
864 else if (actions == ACTION_GET_COLORBOOL) {
Jeff Kingd0e08d62014-11-20 10:15:51 -0500865 check_argc(argc, 1, 2);
866 if (argc == 2)
867 color_stdout_is_tty = git_config_bool("command line", argv[1]);
868 return get_colorbool(argv[0], argc == 2);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200869 }
870
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100871 return 0;
872}