blob: b55bfae7d66df0cea54313f677e1a924a4a579b3 [file] [log] [blame]
Matthias Kestenholze12c0952006-08-02 23:51:59 +02001#include "builtin.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00002#include "abspath.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Junio C Hamano9ce03522007-11-27 22:41:05 -08004#include "color.h"
Elijah Newren4e120822023-04-11 00:41:57 -07005#include "editor.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00006#include "environment.h"
Victoria Dye3867f6d2023-05-26 01:33:00 +00007#include "repository.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00008#include "gettext.h"
Elijah Newrenb5fa6082023-02-24 00:09:29 +00009#include "ident.h"
Felipe Contrerasd64ec162009-02-21 02:49:25 +020010#include "parse-options.h"
Junio C Hamanod4770962013-07-31 11:14:59 -070011#include "urlmatch.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +000012#include "path.h"
Lars Schneider70bd8792016-02-19 10:16:02 +010013#include "quote.h"
Elijah Newrene38da482023-03-21 06:26:05 +000014#include "setup.h"
Elijah Newren88e4e182023-05-16 06:34:02 +000015#include "strbuf.h"
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +020016#include "worktree.h"
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +010017
Felipe Contrerasd64ec162009-02-21 02:49:25 +020018static const char *const builtin_config_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070019 N_("git config [<options>]"),
Felipe Contrerasd64ec162009-02-21 02:49:25 +020020 NULL
21};
Johannes Schindelin4ddba792005-11-20 06:52:22 +010022
David Rientjes96f1e582006-08-15 10:23:48 -070023static char *key;
24static regex_t *key_regexp;
Derrick Stolee3f1bae12020-11-25 22:12:55 +000025static const char *value_pattern;
David Rientjes96f1e582006-08-15 10:23:48 -070026static regex_t *regexp;
27static int show_keys;
SZEDER Gábor578625f2015-08-10 11:46:06 +020028static int omit_values;
David Rientjes96f1e582006-08-15 10:23:48 -070029static int use_key_regexp;
30static int do_all;
31static int do_not_match;
Frank Lichtenheld2275d502007-06-25 16:03:55 +020032static char delim = '=';
33static char key_delim = ' ';
34static char term = '\n';
Johannes Schindelin4ddba792005-11-20 06:52:22 +010035
Sverre Rabbelier57210a62010-08-03 20:59:23 -050036static int use_global_config, use_system_config, use_local_config;
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +020037static int use_worktree_config;
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +020038static struct git_config_source given_config_source;
Taylor Blau0a8950b2018-04-09 15:46:54 -070039static int actions, type;
Taylor Blaueeaa24b2018-04-09 17:18:26 -070040static char *default_value;
Matthew Rogers329e6ec2020-01-24 00:21:01 +000041static int end_nul;
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +070042static int respect_includes_opt = -1;
43static struct config_options config_options;
Lars Schneider70bd8792016-02-19 10:16:02 +010044static int show_origin;
Matthew Rogers145d59f2020-02-10 00:30:59 +000045static int show_scope;
Derrick Stoleefda43942020-11-25 22:12:53 +000046static int fixed_value;
Felipe Contrerasd64ec162009-02-21 02:49:25 +020047
48#define ACTION_GET (1<<0)
49#define ACTION_GET_ALL (1<<1)
50#define ACTION_GET_REGEXP (1<<2)
51#define ACTION_REPLACE_ALL (1<<3)
52#define ACTION_ADD (1<<4)
53#define ACTION_UNSET (1<<5)
54#define ACTION_UNSET_ALL (1<<6)
55#define ACTION_RENAME_SECTION (1<<7)
56#define ACTION_REMOVE_SECTION (1<<8)
57#define ACTION_LIST (1<<9)
58#define ACTION_EDIT (1<<10)
59#define ACTION_SET (1<<11)
60#define ACTION_SET_ALL (1<<12)
61#define ACTION_GET_COLOR (1<<13)
62#define ACTION_GET_COLORBOOL (1<<14)
Junio C Hamanod4770962013-07-31 11:14:59 -070063#define ACTION_GET_URLMATCH (1<<15)
Felipe Contrerasd64ec162009-02-21 02:49:25 +020064
Martin Ågren32888b82018-02-21 19:51:43 +010065/*
66 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
67 * one line of output and which should therefore be paged.
68 */
69#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
70 ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
71
Taylor Blau0a8950b2018-04-09 15:46:54 -070072#define TYPE_BOOL 1
73#define TYPE_INT 2
74#define TYPE_BOOL_OR_INT 3
75#define TYPE_PATH 4
76#define TYPE_EXPIRY_DATE 5
Taylor Blau63e2a0f2018-04-09 17:18:31 -070077#define TYPE_COLOR 6
Lin Sundbd8c092020-05-07 07:31:14 +080078#define TYPE_BOOL_OR_STR 7
Felipe Contreras16c1e932009-02-21 02:49:27 +020079
Taylor Blaufb0dc3b2018-04-18 14:43:35 -070080#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
81 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
82 PARSE_OPT_NONEG, option_parse_type, (i) }
83
Beat Bolli6aaded52018-07-05 20:34:45 +020084static NORETURN void usage_builtin_config(void);
Taylor Blaufb0dc3b2018-04-18 14:43:35 -070085
86static int option_parse_type(const struct option *opt, const char *arg,
87 int unset)
88{
89 int new_type, *to_type;
90
91 if (unset) {
92 *((int *) opt->value) = 0;
93 return 0;
94 }
95
96 /*
97 * To support '--<type>' style flags, begin with new_type equal to
98 * opt->defval.
99 */
100 new_type = opt->defval;
101 if (!new_type) {
102 if (!strcmp(arg, "bool"))
103 new_type = TYPE_BOOL;
104 else if (!strcmp(arg, "int"))
105 new_type = TYPE_INT;
106 else if (!strcmp(arg, "bool-or-int"))
107 new_type = TYPE_BOOL_OR_INT;
Lin Sundbd8c092020-05-07 07:31:14 +0800108 else if (!strcmp(arg, "bool-or-str"))
109 new_type = TYPE_BOOL_OR_STR;
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700110 else if (!strcmp(arg, "path"))
111 new_type = TYPE_PATH;
112 else if (!strcmp(arg, "expiry-date"))
113 new_type = TYPE_EXPIRY_DATE;
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700114 else if (!strcmp(arg, "color"))
115 new_type = TYPE_COLOR;
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700116 else
117 die(_("unrecognized --type argument, %s"), arg);
118 }
119
120 to_type = opt->value;
121 if (*to_type && *to_type != new_type) {
122 /*
123 * Complain when there is a new type not equal to the old type.
124 * This allows for combinations like '--int --type=int' and
125 * '--type=int --type=int', but disallows ones like '--type=bool
126 * --int' and '--type=bool
127 * --type=int'.
128 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200129 error(_("only one type at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200130 usage_builtin_config();
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700131 }
132 *to_type = new_type;
133
134 return 0;
135}
136
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200137static struct option builtin_config_options[] = {
Nguyễn Thái Ngọc Duy1bd31ce2012-08-20 19:32:05 +0700138 OPT_GROUP(N_("Config file location")),
Stefan Beller21e047d2013-08-03 13:51:24 +0200139 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
140 OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
141 OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200142 OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200143 OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
144 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 +0700145 OPT_GROUP(N_("Action")),
Derrick Stolee247e2f82020-11-25 22:12:50 +0000146 OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
147 OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
148 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 -0700149 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 +0000150 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 +0700151 OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
Derrick Stolee247e2f82020-11-25 22:12:50 +0000152 OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
153 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 +0700154 OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
155 OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
156 OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
Derrick Stoleefda43942020-11-25 22:12:53 +0000157 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 +0700158 OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
Jeff Kingd0e08d62014-11-20 10:15:51 -0500159 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
160 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 +0700161 OPT_GROUP(N_("Type")),
Matheus Felipe54451242022-03-04 04:31:53 +0000162 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700163 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
164 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
165 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 +0800166 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 -0700167 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
168 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 +0700169 OPT_GROUP(N_("Other")),
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000170 OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
SZEDER Gábor578625f2015-08-10 11:46:06 +0200171 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +0700172 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
Lars Schneider70bd8792016-02-19 10:16:02 +0100173 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 +0000174 OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700175 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200176 OPT_END(),
177};
178
Beat Bolli6aaded52018-07-05 20:34:45 +0200179static NORETURN void usage_builtin_config(void)
180{
181 usage_with_options(builtin_config_usage, builtin_config_options);
182}
183
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100184static void check_argc(int argc, int min, int max)
185{
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200186 if (argc >= min && argc <= max)
187 return;
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200188 if (min == max)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200189 error(_("wrong number of arguments, should be %d"), min);
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200190 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200191 error(_("wrong number of arguments, should be from %d to %d"),
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200192 min, max);
Beat Bolli6aaded52018-07-05 20:34:45 +0200193 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200194}
195
Glen Choo26b66932023-06-28 19:26:25 +0000196static void show_config_origin(const struct key_value_info *kvi,
197 struct strbuf *buf)
Lars Schneider70bd8792016-02-19 10:16:02 +0100198{
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000199 const char term = end_nul ? '\0' : '\t';
Lars Schneider70bd8792016-02-19 10:16:02 +0100200
Glen Choo26b66932023-06-28 19:26:25 +0000201 strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
Lars Schneider70bd8792016-02-19 10:16:02 +0100202 strbuf_addch(buf, ':');
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000203 if (end_nul)
Glen Choo26b66932023-06-28 19:26:25 +0000204 strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
Lars Schneider70bd8792016-02-19 10:16:02 +0100205 else
Glen Choo26b66932023-06-28 19:26:25 +0000206 quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
Lars Schneider70bd8792016-02-19 10:16:02 +0100207 strbuf_addch(buf, term);
208}
209
Glen Choo26b66932023-06-28 19:26:25 +0000210static void show_config_scope(const struct key_value_info *kvi,
211 struct strbuf *buf)
Matthew Rogers145d59f2020-02-10 00:30:59 +0000212{
213 const char term = end_nul ? '\0' : '\t';
Glen Choo26b66932023-06-28 19:26:25 +0000214 const char *scope = config_scope_name(kvi->scope);
Matthew Rogers145d59f2020-02-10 00:30:59 +0000215
216 strbuf_addstr(buf, N_(scope));
217 strbuf_addch(buf, term);
218}
219
Jeff King783a86c2022-08-19 06:08:44 -0400220static int show_all_config(const char *key_, const char *value_,
Glen Choo26b66932023-06-28 19:26:25 +0000221 const struct config_context *ctx,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200222 void *cb UNUSED)
Petr Baudisde791f12006-04-25 00:59:25 +0200223{
Glen Choo26b66932023-06-28 19:26:25 +0000224 const struct key_value_info *kvi = ctx->kvi;
225
Matthew Rogers145d59f2020-02-10 00:30:59 +0000226 if (show_origin || show_scope) {
Lars Schneider70bd8792016-02-19 10:16:02 +0100227 struct strbuf buf = STRBUF_INIT;
Matthew Rogers145d59f2020-02-10 00:30:59 +0000228 if (show_scope)
Glen Choo26b66932023-06-28 19:26:25 +0000229 show_config_scope(kvi, &buf);
Matthew Rogers145d59f2020-02-10 00:30:59 +0000230 if (show_origin)
Glen Choo26b66932023-06-28 19:26:25 +0000231 show_config_origin(kvi, &buf);
Lars Schneider70bd8792016-02-19 10:16:02 +0100232 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
233 fwrite(buf.buf, 1, buf.len, stdout);
234 strbuf_release(&buf);
235 }
SZEDER Gábor578625f2015-08-10 11:46:06 +0200236 if (!omit_values && value_)
Frank Lichtenheld2275d502007-06-25 16:03:55 +0200237 printf("%s%c%s%c", key_, delim, value_, term);
Petr Baudisde791f12006-04-25 00:59:25 +0200238 else
Frank Lichtenheld2275d502007-06-25 16:03:55 +0200239 printf("%s%c", key_, term);
Petr Baudisde791f12006-04-25 00:59:25 +0200240 return 0;
241}
242
Jeff King7acdd6f2012-10-23 15:51:50 -0400243struct strbuf_list {
244 struct strbuf *items;
245 int nr;
246 int alloc;
247};
248
Glen Choo26b66932023-06-28 19:26:25 +0000249static int format_config(struct strbuf *buf, const char *key_,
250 const char *value_, const struct key_value_info *kvi)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100251{
Matthew Rogers145d59f2020-02-10 00:30:59 +0000252 if (show_scope)
Glen Choo26b66932023-06-28 19:26:25 +0000253 show_config_scope(kvi, buf);
Lars Schneider70bd8792016-02-19 10:16:02 +0100254 if (show_origin)
Glen Choo26b66932023-06-28 19:26:25 +0000255 show_config_origin(kvi, buf);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200256 if (show_keys)
Jeff King7acdd6f2012-10-23 15:51:50 -0400257 strbuf_addstr(buf, key_);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200258 if (!omit_values) {
Jeff Kingf2259872015-08-20 10:47:34 -0400259 if (show_keys)
260 strbuf_addch(buf, key_delim);
Jeff King00b347d2012-10-23 16:52:44 -0400261
Taylor Blau0a8950b2018-04-09 15:46:54 -0700262 if (type == TYPE_INT)
Jeff Kingf2259872015-08-20 10:47:34 -0400263 strbuf_addf(buf, "%"PRId64,
Glen Choo8868b1e2023-06-28 19:26:27 +0000264 git_config_int64(key_, value_ ? value_ : "", kvi));
Taylor Blau0a8950b2018-04-09 15:46:54 -0700265 else if (type == TYPE_BOOL)
Jeff Kingf2259872015-08-20 10:47:34 -0400266 strbuf_addstr(buf, git_config_bool(key_, value_) ?
267 "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700268 else if (type == TYPE_BOOL_OR_INT) {
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200269 int is_bool, v;
Glen Choo8868b1e2023-06-28 19:26:27 +0000270 v = git_config_bool_or_int(key_, value_, kvi,
271 &is_bool);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200272 if (is_bool)
Jeff Kingf2259872015-08-20 10:47:34 -0400273 strbuf_addstr(buf, v ? "true" : "false");
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200274 else
Jeff Kingf2259872015-08-20 10:47:34 -0400275 strbuf_addf(buf, "%d", v);
Lin Sundbd8c092020-05-07 07:31:14 +0800276 } else if (type == TYPE_BOOL_OR_STR) {
277 int v = git_parse_maybe_bool(value_);
278 if (v < 0)
279 strbuf_addstr(buf, value_);
280 else
281 strbuf_addstr(buf, v ? "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700282 } else if (type == TYPE_PATH) {
Jeff Kingf2259872015-08-20 10:47:34 -0400283 const char *v;
284 if (git_config_pathname(&v, key_, value_) < 0)
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200285 return -1;
Jeff Kingf2259872015-08-20 10:47:34 -0400286 strbuf_addstr(buf, v);
287 free((char *)v);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700288 } else if (type == TYPE_EXPIRY_DATE) {
Haaris Mehmood5f967422017-11-18 02:27:27 +0000289 timestamp_t t;
290 if (git_config_expiry_date(&t, key_, value_) < 0)
291 return -1;
292 strbuf_addf(buf, "%"PRItime, t);
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700293 } else if (type == TYPE_COLOR) {
294 char v[COLOR_MAXLEN];
295 if (git_config_color(v, key_, value_) < 0)
296 return -1;
297 strbuf_addstr(buf, v);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200298 } else if (value_) {
Jeff Kingf2259872015-08-20 10:47:34 -0400299 strbuf_addstr(buf, value_);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200300 } else {
Jeff Kingf2259872015-08-20 10:47:34 -0400301 /* Just show the key name; back out delimiter */
302 if (show_keys)
303 strbuf_setlen(buf, buf->len - 1);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200304 }
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200305 }
Jeff King00b347d2012-10-23 16:52:44 -0400306 strbuf_addch(buf, term);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100307 return 0;
308}
309
Glen Chooa4e7e312023-06-28 19:26:22 +0000310static int collect_config(const char *key_, const char *value_,
Glen Choo26b66932023-06-28 19:26:25 +0000311 const struct config_context *ctx, void *cb)
Junio C Hamanod9b91692013-07-29 14:23:16 -0700312{
313 struct strbuf_list *values = cb;
Glen Choo26b66932023-06-28 19:26:25 +0000314 const struct key_value_info *kvi = ctx->kvi;
Junio C Hamanod9b91692013-07-29 14:23:16 -0700315
316 if (!use_key_regexp && strcmp(key_, key))
317 return 0;
318 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
319 return 0;
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000320 if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
321 return 0;
Junio C Hamanod9b91692013-07-29 14:23:16 -0700322 if (regexp != NULL &&
323 (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
324 return 0;
325
326 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
Jeff King9f1429d2015-08-20 10:46:04 -0400327 strbuf_init(&values->items[values->nr], 0);
Junio C Hamanod9b91692013-07-29 14:23:16 -0700328
Glen Choo26b66932023-06-28 19:26:25 +0000329 return format_config(&values->items[values->nr++], key_, value_, kvi);
Junio C Hamanod9b91692013-07-29 14:23:16 -0700330}
331
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000332static int get_value(const char *key_, const char *regex_, unsigned flags)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100333{
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700334 int ret = CONFIG_GENERIC_ERROR;
Ramsay Jones5ba1a8a2012-10-28 21:05:25 +0000335 struct strbuf_list values = {NULL};
Jeff King7acdd6f2012-10-23 15:51:50 -0400336 int i;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100337
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200338 if (use_key_regexp) {
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100339 char *tl;
340
341 /*
342 * NEEDSWORK: this naive pattern lowercasing obviously does not
343 * work for more complex patterns like "^[^.]*Foo.*bar".
344 * Perhaps we should deprecate this altogether someday.
345 */
346
347 key = xstrdup(key_);
348 for (tl = key + strlen(key) - 1;
349 tl >= key && *tl != '.';
350 tl--)
351 *tl = tolower(*tl);
352 for (tl = key; *tl && *tl != '.'; tl++)
353 *tl = tolower(*tl);
354
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200355 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200356 if (regcomp(key_regexp, key, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200357 error(_("invalid key pattern: %s"), key_);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000358 FREE_AND_NULL(key_regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700359 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200360 goto free_strings;
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200361 }
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100362 } else {
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700363 if (git_config_parse_key(key_, &key, NULL)) {
364 ret = CONFIG_INVALID_KEY;
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100365 goto free_strings;
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700366 }
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200367 }
368
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000369 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
370 value_pattern = regex_;
371 else if (regex_) {
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100372 if (regex_[0] == '!') {
373 do_not_match = 1;
374 regex_++;
375 }
376
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200377 regexp = (regex_t*)xmalloc(sizeof(regex_t));
Amos Waterland0a152172006-01-04 19:31:02 -0500378 if (regcomp(regexp, regex_, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200379 error(_("invalid pattern: %s"), regex_);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000380 FREE_AND_NULL(regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700381 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200382 goto free_strings;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100383 }
384 }
385
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700386 config_with_options(collect_config, &values,
Victoria Dye9b6b06c2023-05-26 01:32:59 +0000387 &given_config_source, the_repository,
388 &config_options);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200389
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700390 if (!values.nr && default_value) {
Glen Choo26b66932023-06-28 19:26:25 +0000391 struct key_value_info kvi = KVI_INIT;
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700392 struct strbuf *item;
Glen Choo26b66932023-06-28 19:26:25 +0000393
394 kvi_from_param(&kvi);
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700395 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
396 item = &values.items[values.nr++];
397 strbuf_init(item, 0);
Glen Choo26b66932023-06-28 19:26:25 +0000398 if (format_config(item, key_, default_value, &kvi) < 0)
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700399 die(_("failed to format default config value: %s"),
400 default_value);
401 }
402
Jeff King00b347d2012-10-23 16:52:44 -0400403 ret = !values.nr;
Jeff King7acdd6f2012-10-23 15:51:50 -0400404
405 for (i = 0; i < values.nr; i++) {
406 struct strbuf *buf = values.items + i;
Jeff King00b347d2012-10-23 16:52:44 -0400407 if (do_all || i == values.nr - 1)
408 fwrite(buf->buf, 1, buf->len, stdout);
Jeff King7acdd6f2012-10-23 15:51:50 -0400409 strbuf_release(buf);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100410 }
Jeff King7acdd6f2012-10-23 15:51:50 -0400411 free(values.items);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100412
Jeff King97ed50f2012-10-23 15:40:06 -0400413free_strings:
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100414 free(key);
Jeff King35998c82012-10-23 15:36:12 -0400415 if (key_regexp) {
416 regfree(key_regexp);
417 free(key_regexp);
418 }
Amos Waterland0a152172006-01-04 19:31:02 -0500419 if (regexp) {
420 regfree(regexp);
421 free(regexp);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100422 }
423
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200424 return ret;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100425}
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100426
Glen Choo8868b1e2023-06-28 19:26:27 +0000427static char *normalize_value(const char *key, const char *value,
428 struct key_value_info *kvi)
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200429{
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200430 if (!value)
431 return NULL;
432
Taylor Blau0a8950b2018-04-09 15:46:54 -0700433 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
Matthieu Moy13494842009-12-30 17:51:53 +0100434 /*
435 * We don't do normalization for TYPE_PATH here: If
436 * the path is like ~/foobar/, we prefer to store
437 * "~/foobar/" in the config file, and to expand the ~
438 * when retrieving the value.
Haaris Mehmood5f967422017-11-18 02:27:27 +0000439 * Also don't do normalization for expiry dates.
Matthieu Moy13494842009-12-30 17:51:53 +0100440 */
Jeff King3ec832c2015-09-24 17:07:05 -0400441 return xstrdup(value);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700442 if (type == TYPE_INT)
Glen Choo8868b1e2023-06-28 19:26:27 +0000443 return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
Taylor Blau0a8950b2018-04-09 15:46:54 -0700444 if (type == TYPE_BOOL)
Jeff King3ec832c2015-09-24 17:07:05 -0400445 return xstrdup(git_config_bool(key, value) ? "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700446 if (type == TYPE_BOOL_OR_INT) {
Jeff King3ec832c2015-09-24 17:07:05 -0400447 int is_bool, v;
Glen Choo8868b1e2023-06-28 19:26:27 +0000448 v = git_config_bool_or_int(key, value, kvi, &is_bool);
Jeff King3ec832c2015-09-24 17:07:05 -0400449 if (!is_bool)
450 return xstrfmt("%d", v);
451 else
452 return xstrdup(v ? "true" : "false");
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200453 }
Lin Sundbd8c092020-05-07 07:31:14 +0800454 if (type == TYPE_BOOL_OR_STR) {
455 int v = git_parse_maybe_bool(value);
456 if (v < 0)
457 return xstrdup(value);
458 else
459 return xstrdup(v ? "true" : "false");
460 }
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700461 if (type == TYPE_COLOR) {
462 char v[COLOR_MAXLEN];
463 if (git_config_color(v, key, value))
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200464 die(_("cannot parse color '%s'"), value);
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700465
466 /*
467 * The contents of `v` now contain an ANSI escape
468 * sequence, not suitable for including within a
469 * configuration file. Treat the above as a
470 * "sanity-check", and return the given value, which we
471 * know is representable as valid color code.
472 */
473 return xstrdup(value);
474 }
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200475
Junio C Hamano50f08db2018-05-30 14:04:07 +0900476 BUG("cannot normalize type %d", type);
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200477}
478
Junio C Hamano9ce03522007-11-27 22:41:05 -0800479static int get_color_found;
480static const char *get_color_slot;
Felipe Contrerasb4084572009-02-21 02:48:56 +0200481static const char *get_colorbool_slot;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800482static char parsed_color[COLOR_MAXLEN];
483
Jeff King783a86c2022-08-19 06:08:44 -0400484static int git_get_color_config(const char *var, const char *value,
Glen Chooa4e7e312023-06-28 19:26:22 +0000485 const struct config_context *ctx UNUSED,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200486 void *cb UNUSED)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800487{
488 if (!strcmp(var, get_color_slot)) {
Junio C Hamanof7699822008-02-11 10:48:12 -0800489 if (!value)
490 config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400491 if (color_parse(value, parsed_color) < 0)
492 return -1;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800493 get_color_found = 1;
494 }
495 return 0;
496}
497
Jeff Kingd0e08d62014-11-20 10:15:51 -0500498static void get_color(const char *var, const char *def_color)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800499{
Jeff Kingd0e08d62014-11-20 10:15:51 -0500500 get_color_slot = var;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800501 get_color_found = 0;
502 parsed_color[0] = '\0';
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700503 config_with_options(git_get_color_config, NULL,
Victoria Dye9b6b06c2023-05-26 01:32:59 +0000504 &given_config_source, the_repository,
505 &config_options);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800506
Jeff Kingf6c5a292014-10-07 15:33:09 -0400507 if (!get_color_found && def_color) {
508 if (color_parse(def_color, parsed_color) < 0)
509 die(_("unable to parse default color value"));
510 }
Junio C Hamano9ce03522007-11-27 22:41:05 -0800511
512 fputs(parsed_color, stdout);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800513}
514
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800515static int get_colorbool_found;
Junio C Hamano69243c22007-12-05 22:12:07 -0800516static int get_diff_color_found;
Jeff Kingc659f552011-08-17 22:04:56 -0700517static int get_color_ui_found;
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100518static int git_get_colorbool_config(const char *var, const char *value,
Glen Chooa4e7e312023-06-28 19:26:22 +0000519 const struct config_context *ctx UNUSED,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200520 void *data UNUSED)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800521{
Jeff Kinge269eb72011-08-17 22:03:48 -0700522 if (!strcmp(var, get_colorbool_slot))
523 get_colorbool_found = git_config_colorbool(var, value);
524 else if (!strcmp(var, "diff.color"))
525 get_diff_color_found = git_config_colorbool(var, value);
526 else if (!strcmp(var, "color.ui"))
Jeff Kingc659f552011-08-17 22:04:56 -0700527 get_color_ui_found = git_config_colorbool(var, value);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800528 return 0;
529}
530
Jeff Kingd0e08d62014-11-20 10:15:51 -0500531static int get_colorbool(const char *var, int print)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800532{
Jeff Kingd0e08d62014-11-20 10:15:51 -0500533 get_colorbool_slot = var;
Junio C Hamano69243c22007-12-05 22:12:07 -0800534 get_colorbool_found = -1;
535 get_diff_color_found = -1;
Matthieu Moyb8612b42013-05-15 19:00:55 +0200536 get_color_ui_found = -1;
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700537 config_with_options(git_get_colorbool_config, NULL,
Victoria Dye9b6b06c2023-05-26 01:32:59 +0000538 &given_config_source, the_repository,
539 &config_options);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800540
Junio C Hamano69243c22007-12-05 22:12:07 -0800541 if (get_colorbool_found < 0) {
Felipe Contrerasb4084572009-02-21 02:48:56 +0200542 if (!strcmp(get_colorbool_slot, "color.diff"))
Junio C Hamano69243c22007-12-05 22:12:07 -0800543 get_colorbool_found = get_diff_color_found;
544 if (get_colorbool_found < 0)
Jeff Kingc659f552011-08-17 22:04:56 -0700545 get_colorbool_found = get_color_ui_found;
Junio C Hamano69243c22007-12-05 22:12:07 -0800546 }
547
Matthieu Moyb8612b42013-05-15 19:00:55 +0200548 if (get_colorbool_found < 0)
549 /* default value if none found in config */
Matthieu Moy4c7f1812013-06-10 16:26:09 +0200550 get_colorbool_found = GIT_COLOR_AUTO;
Matthieu Moyb8612b42013-05-15 19:00:55 +0200551
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700552 get_colorbool_found = want_color(get_colorbool_found);
553
Felipe Contreras0e854a22009-02-21 02:48:57 +0200554 if (print) {
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800555 printf("%s\n", get_colorbool_found ? "true" : "false");
556 return 0;
Felipe Contreras0e854a22009-02-21 02:48:57 +0200557 } else
558 return get_colorbool_found ? 0 : 1;
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800559}
560
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200561static void check_write(void)
Heiko Voigt1bc88812013-07-12 00:46:47 +0200562{
Johannes Schindelin638fa622016-02-24 13:48:11 +0100563 if (!given_config_source.file && !startup_info->have_repository)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200564 die(_("not in a git directory"));
Johannes Schindelin638fa622016-02-24 13:48:11 +0100565
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200566 if (given_config_source.use_stdin)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200567 die(_("writing to stdin is not supported"));
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200568
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200569 if (given_config_source.blob)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200570 die(_("writing config blobs is not supported"));
Heiko Voigt1bc88812013-07-12 00:46:47 +0200571}
572
Junio C Hamanod4770962013-07-31 11:14:59 -0700573struct urlmatch_current_candidate_value {
574 char value_is_null;
575 struct strbuf value;
Glen Choo26b66932023-06-28 19:26:25 +0000576 struct key_value_info kvi;
Junio C Hamanod4770962013-07-31 11:14:59 -0700577};
578
Glen Chooa4e7e312023-06-28 19:26:22 +0000579static int urlmatch_collect_fn(const char *var, const char *value,
Glen Choo26b66932023-06-28 19:26:25 +0000580 const struct config_context *ctx,
Glen Chooa4e7e312023-06-28 19:26:22 +0000581 void *cb)
Junio C Hamanod4770962013-07-31 11:14:59 -0700582{
583 struct string_list *values = cb;
584 struct string_list_item *item = string_list_insert(values, var);
585 struct urlmatch_current_candidate_value *matched = item->util;
Glen Choo26b66932023-06-28 19:26:25 +0000586 const struct key_value_info *kvi = ctx->kvi;
Junio C Hamanod4770962013-07-31 11:14:59 -0700587
588 if (!matched) {
589 matched = xmalloc(sizeof(*matched));
590 strbuf_init(&matched->value, 0);
591 item->util = matched;
592 } else {
593 strbuf_reset(&matched->value);
594 }
Glen Choo26b66932023-06-28 19:26:25 +0000595 matched->kvi = *kvi;
Junio C Hamanod4770962013-07-31 11:14:59 -0700596
597 if (value) {
598 strbuf_addstr(&matched->value, value);
599 matched->value_is_null = 0;
600 } else {
601 matched->value_is_null = 1;
602 }
603 return 0;
604}
605
Junio C Hamanod4770962013-07-31 11:14:59 -0700606static int get_urlmatch(const char *var, const char *url)
607{
John Keeping27b30be2016-02-28 11:54:35 +0000608 int ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700609 char *section_tail;
610 struct string_list_item *item;
Ævar Arnfjörð Bjarmason73ee4492021-10-01 12:27:33 +0200611 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
Junio C Hamanod4770962013-07-31 11:14:59 -0700612 struct string_list values = STRING_LIST_INIT_DUP;
613
614 config.collect_fn = urlmatch_collect_fn;
615 config.cascade_fn = NULL;
616 config.cb = &values;
617
618 if (!url_normalize(url, &config.url))
Junio C Hamano6667a6a2013-08-08 21:41:44 -0700619 die("%s", config.url.err);
Junio C Hamanod4770962013-07-31 11:14:59 -0700620
Jeff King88d5a6f2014-05-22 05:44:09 -0400621 config.section = xstrdup_tolower(var);
Junio C Hamanod4770962013-07-31 11:14:59 -0700622 section_tail = strchr(config.section, '.');
623 if (section_tail) {
624 *section_tail = '\0';
625 config.key = section_tail + 1;
626 show_keys = 0;
627 } else {
628 config.key = NULL;
629 show_keys = 1;
630 }
631
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700632 config_with_options(urlmatch_config_entry, &config,
Victoria Dye9b6b06c2023-05-26 01:32:59 +0000633 &given_config_source, the_repository,
634 &config_options);
Junio C Hamanod4770962013-07-31 11:14:59 -0700635
John Keeping27b30be2016-02-28 11:54:35 +0000636 ret = !values.nr;
637
Junio C Hamanod4770962013-07-31 11:14:59 -0700638 for_each_string_list_item(item, &values) {
639 struct urlmatch_current_candidate_value *matched = item->util;
Junio C Hamanod4770962013-07-31 11:14:59 -0700640 struct strbuf buf = STRBUF_INIT;
641
Jeff Kinga92330d2015-08-20 10:49:45 -0400642 format_config(&buf, item->string,
Glen Choo26b66932023-06-28 19:26:25 +0000643 matched->value_is_null ? NULL : matched->value.buf,
644 &matched->kvi);
Junio C Hamanod4770962013-07-31 11:14:59 -0700645 fwrite(buf.buf, 1, buf.len, stdout);
Junio C Hamanod4770962013-07-31 11:14:59 -0700646 strbuf_release(&buf);
647
648 strbuf_release(&matched->value);
649 }
Ævar Arnfjörð Bjarmasona41e8e72022-03-04 19:32:07 +0100650 urlmatch_config_release(&config);
Junio C Hamanod4770962013-07-31 11:14:59 -0700651 string_list_clear(&values, 1);
652 free(config.url.url);
653
654 free((void *)config.section);
John Keeping27b30be2016-02-28 11:54:35 +0000655 return ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700656}
657
Matthieu Moy98305342014-07-25 21:11:34 +0200658static char *default_user_config(void)
659{
660 struct strbuf buf = STRBUF_INIT;
661 strbuf_addf(&buf,
662 _("# This is Git's per-user configuration file.\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300663 "[user]\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200664 "# Please adapt and uncomment the following lines:\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300665 "# name = %s\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200666 "# email = %s\n"),
667 ident_default_name(),
668 ident_default_email());
669 return strbuf_detach(&buf, NULL);
670}
671
Nguyễn Thái Ngọc Duy3ba7e6e2010-08-05 22:15:09 -0500672int cmd_config(int argc, const char **argv, const char *prefix)
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100673{
Nguyễn Thái Ngọc Duy3ba7e6e2010-08-05 22:15:09 -0500674 int nongit = !startup_info->have_repository;
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100675 char *value = NULL;
Derrick Stoleec90702a2020-11-25 22:12:54 +0000676 int flags = 0;
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100677 int ret = 0;
Glen Choo8868b1e2023-06-28 19:26:27 +0000678 struct key_value_info default_kvi = KVI_INIT;
Petr Baudis7162dff2006-02-12 04:14:48 +0100679
Jeff King423ff9b2019-01-11 17:15:54 -0500680 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
Daniel Barkalowdc871832008-06-30 03:37:47 -0400681
Stephen Boyd37782922009-05-23 11:53:12 -0700682 argc = parse_options(argc, argv, prefix, builtin_config_options,
683 builtin_config_usage,
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200684 PARSE_OPT_STOP_AT_NON_OPTION);
685
Heiko Voigt1bc88812013-07-12 00:46:47 +0200686 if (use_global_config + use_system_config + use_local_config +
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200687 use_worktree_config +
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200688 !!given_config_source.file + !!given_config_source.blob > 1) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200689 error(_("only one config file at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200690 usage_builtin_config();
Felipe Contreras67052c92009-02-21 02:49:26 +0200691 }
692
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300693 if (nongit) {
694 if (use_local_config)
695 die(_("--local can only be used inside a git repository"));
696 if (given_config_source.blob)
697 die(_("--blob can only be used inside a git repository"));
698 if (use_worktree_config)
699 die(_("--worktree can only be used inside a git repository"));
Jeff King25cd2912017-05-12 23:29:31 -0400700
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300701 }
Jeff King17b8a2d2018-05-18 15:27:04 -0700702
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200703 if (given_config_source.file &&
704 !strcmp(given_config_source.file, "-")) {
705 given_config_source.file = NULL;
706 given_config_source.use_stdin = 1;
Matthew Rogerse37efa42020-02-10 00:30:57 +0000707 given_config_source.scope = CONFIG_SCOPE_COMMAND;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200708 }
709
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200710 if (use_global_config) {
Kristoffer Haugsbakkc15129b2024-01-18 17:12:51 +0100711 given_config_source.file = git_global_config();
712 if (!given_config_source.file)
Kristoffer Haugsbakk1cb3b922024-01-28 19:31:40 +0100713 /*
714 * It is unknown if HOME/.gitconfig exists, so
715 * we do not know if we should write to XDG
716 * location; error out even if XDG_CONFIG_HOME
717 * is set and points at a sane location.
718 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200719 die(_("$HOME not set"));
Matthew Rogerse37efa42020-02-10 00:30:57 +0000720 given_config_source.scope = CONFIG_SCOPE_GLOBAL;
Kristoffer Haugsbakkc15129b2024-01-18 17:12:51 +0100721 } else if (use_system_config) {
Patrick Steinhardtc62a9992021-04-19 14:31:08 +0200722 given_config_source.file = git_system_config();
Matthew Rogerse37efa42020-02-10 00:30:57 +0000723 given_config_source.scope = CONFIG_SCOPE_SYSTEM;
724 } else if (use_local_config) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200725 given_config_source.file = git_pathdup("config");
Matthew Rogerse37efa42020-02-10 00:30:57 +0000726 given_config_source.scope = CONFIG_SCOPE_LOCAL;
727 } else if (use_worktree_config) {
Eric Sunshine03f24652020-06-19 19:35:44 -0400728 struct worktree **worktrees = get_worktrees();
Victoria Dye3867f6d2023-05-26 01:33:00 +0000729 if (the_repository->repository_format_worktree_config)
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200730 given_config_source.file = git_pathdup("config.worktree");
731 else if (worktrees[0] && worktrees[1])
732 die(_("--worktree cannot be used with multiple "
733 "working trees unless the config\n"
734 "extension worktreeConfig is enabled. "
735 "Please read \"CONFIGURATION FILE\"\n"
736 "section in \"git help worktree\" for details"));
737 else
738 given_config_source.file = git_pathdup("config");
Matthew Rogerse37efa42020-02-10 00:30:57 +0000739 given_config_source.scope = CONFIG_SCOPE_LOCAL;
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200740 free_worktrees(worktrees);
741 } else if (given_config_source.file) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200742 if (!is_absolute_path(given_config_source.file) && prefix)
743 given_config_source.file =
Jeff Kinge4da43b2017-03-20 21:28:49 -0400744 prefix_filename(prefix, given_config_source.file);
Matthew Rogerse37efa42020-02-10 00:30:57 +0000745 given_config_source.scope = CONFIG_SCOPE_COMMAND;
746 } else if (given_config_source.blob) {
747 given_config_source.scope = CONFIG_SCOPE_COMMAND;
Petr Baudis7162dff2006-02-12 04:14:48 +0100748 }
749
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +0700750 if (respect_includes_opt == -1)
751 config_options.respect_includes = !given_config_source.file;
752 else
753 config_options.respect_includes = respect_includes_opt;
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700754 if (!nongit) {
755 config_options.commondir = get_git_common_dir();
756 config_options.git_dir = get_git_dir();
757 }
Jeff King9b25a0b2012-02-06 04:54:04 -0500758
Matthew Rogers329e6ec2020-01-24 00:21:01 +0000759 if (end_nul) {
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200760 term = '\0';
761 delim = '\n';
762 key_delim = '\n';
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100763 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200764
Taylor Blau0a8950b2018-04-09 15:46:54 -0700765 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200766 error(_("--get-color and variable type are incoherent"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200767 usage_builtin_config();
Felipe Contrerasc2387352009-02-21 02:49:29 +0200768 }
769
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200770 if (HAS_MULTI_BITS(actions)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200771 error(_("only one action at a time"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200772 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200773 }
774 if (actions == 0)
775 switch (argc) {
776 case 1: actions = ACTION_GET; break;
777 case 2: actions = ACTION_SET; break;
778 case 3: actions = ACTION_SET_ALL; break;
779 default:
Beat Bolli6aaded52018-07-05 20:34:45 +0200780 usage_builtin_config();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200781 }
SZEDER Gábor578625f2015-08-10 11:46:06 +0200782 if (omit_values &&
783 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200784 error(_("--name-only is only applicable to --list or --get-regexp"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200785 usage_builtin_config();
SZEDER Gábor578625f2015-08-10 11:46:06 +0200786 }
Lars Schneider70bd8792016-02-19 10:16:02 +0100787
788 if (show_origin && !(actions &
789 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200790 error(_("--show-origin is only applicable to --get, --get-all, "
791 "--get-regexp, and --list"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200792 usage_builtin_config();
Lars Schneider70bd8792016-02-19 10:16:02 +0100793 }
794
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700795 if (default_value && !(actions & ACTION_GET)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200796 error(_("--default is only applicable to --get"));
Beat Bolli6aaded52018-07-05 20:34:45 +0200797 usage_builtin_config();
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700798 }
799
Derrick Stoleefda43942020-11-25 22:12:53 +0000800 /* check usage of --fixed-value */
801 if (fixed_value) {
802 int allowed_usage = 0;
803
804 switch (actions) {
805 /* git config --get <name> <value-pattern> */
806 case ACTION_GET:
807 /* git config --get-all <name> <value-pattern> */
808 case ACTION_GET_ALL:
809 /* git config --get-regexp <name-pattern> <value-pattern> */
810 case ACTION_GET_REGEXP:
811 /* git config --unset <name> <value-pattern> */
812 case ACTION_UNSET:
813 /* git config --unset-all <name> <value-pattern> */
814 case ACTION_UNSET_ALL:
815 allowed_usage = argc > 1 && !!argv[1];
816 break;
817
818 /* git config <name> <value> <value-pattern> */
819 case ACTION_SET_ALL:
820 /* git config --replace-all <name> <value> <value-pattern> */
821 case ACTION_REPLACE_ALL:
822 allowed_usage = argc > 2 && !!argv[2];
823 break;
824
825 /* other options don't allow --fixed-value */
826 }
827
828 if (!allowed_usage) {
829 error(_("--fixed-value only applies with 'value-pattern'"));
830 usage_builtin_config();
831 }
Derrick Stoleec90702a2020-11-25 22:12:54 +0000832
833 flags |= CONFIG_FLAGS_FIXED_VALUE;
Derrick Stoleefda43942020-11-25 22:12:53 +0000834 }
835
Martin Ågren32888b82018-02-21 19:51:43 +0100836 if (actions & PAGING_ACTIONS)
Martin Ågrenc0e9f5b2018-02-21 19:51:44 +0100837 setup_auto_pager("config", 1);
Martin Ågren32888b82018-02-21 19:51:43 +0100838
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200839 if (actions == ACTION_LIST) {
Felipe Contreras225a9ca2009-02-21 02:49:28 +0200840 check_argc(argc, 0, 0);
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700841 if (config_with_options(show_all_config, NULL,
Victoria Dye9b6b06c2023-05-26 01:32:59 +0000842 &given_config_source, the_repository,
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700843 &config_options) < 0) {
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200844 if (given_config_source.file)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200845 die_errno(_("unable to read config file '%s'"),
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200846 given_config_source.file);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200847 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200848 die(_("error processing config file(s)"));
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200849 }
850 }
851 else if (actions == ACTION_EDIT) {
Michael Haggerty3696a7c2014-11-16 08:37:44 +0100852 char *config_file;
853
Felipe Contreras225a9ca2009-02-21 02:49:28 +0200854 check_argc(argc, 0, 0);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200855 if (!given_config_source.file && nongit)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200856 die(_("not in a git directory"));
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200857 if (given_config_source.use_stdin)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200858 die(_("editing stdin is not supported"));
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200859 if (given_config_source.blob)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200860 die(_("editing blobs is not supported"));
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200861 git_config(git_default_config, NULL);
Jeff Kingd9c69642017-04-20 17:09:09 -0400862 config_file = given_config_source.file ?
863 xstrdup(given_config_source.file) :
864 git_pathdup("config");
Matthieu Moy98305342014-07-25 21:11:34 +0200865 if (use_global_config) {
866 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
Jeff Kingaabbd3f2016-07-08 05:06:50 -0400867 if (fd >= 0) {
Matthieu Moy98305342014-07-25 21:11:34 +0200868 char *content = default_user_config();
869 write_str_in_full(fd, content);
870 free(content);
871 close(fd);
872 }
873 else if (errno != EEXIST)
874 die_errno(_("cannot create configuration file %s"), config_file);
875 }
876 launch_editor(config_file, NULL, NULL);
Michael Haggerty3696a7c2014-11-16 08:37:44 +0100877 free(config_file);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200878 }
879 else if (actions == ACTION_SET) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200880 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200881 check_argc(argc, 2, 2);
Glen Choo8868b1e2023-06-28 19:26:27 +0000882 value = normalize_value(argv[0], argv[1], &default_kvi);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100883 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
Michael J Gruber5a2df362011-05-17 17:38:53 +0200884 if (ret == CONFIG_NOTHING_SET)
Vasco Almeidaccf63802016-09-15 14:59:00 +0000885 error(_("cannot overwrite multiple values with a single value\n"
886 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200887 }
888 else if (actions == ACTION_SET_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200889 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200890 check_argc(argc, 2, 3);
Glen Choo8868b1e2023-06-28 19:26:27 +0000891 value = normalize_value(argv[0], argv[1], &default_kvi);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100892 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
893 argv[0], value, argv[2],
894 flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200895 }
896 else if (actions == ACTION_ADD) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200897 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200898 check_argc(argc, 2, 2);
Glen Choo8868b1e2023-06-28 19:26:27 +0000899 value = normalize_value(argv[0], argv[1], &default_kvi);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100900 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
901 argv[0], value,
902 CONFIG_REGEX_NONE,
903 flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200904 }
905 else if (actions == ACTION_REPLACE_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200906 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200907 check_argc(argc, 2, 3);
Glen Choo8868b1e2023-06-28 19:26:27 +0000908 value = normalize_value(argv[0], argv[1], &default_kvi);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100909 ret = git_config_set_multivar_in_file_gently(given_config_source.file,
910 argv[0], value, argv[2],
911 flags | CONFIG_FLAGS_MULTI_REPLACE);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200912 }
913 else if (actions == ACTION_GET) {
914 check_argc(argc, 1, 2);
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000915 return get_value(argv[0], argv[1], flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200916 }
917 else if (actions == ACTION_GET_ALL) {
918 do_all = 1;
919 check_argc(argc, 1, 2);
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000920 return get_value(argv[0], argv[1], flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200921 }
922 else if (actions == ACTION_GET_REGEXP) {
923 show_keys = 1;
924 use_key_regexp = 1;
925 do_all = 1;
926 check_argc(argc, 1, 2);
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000927 return get_value(argv[0], argv[1], flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200928 }
Junio C Hamanod4770962013-07-31 11:14:59 -0700929 else if (actions == ACTION_GET_URLMATCH) {
930 check_argc(argc, 2, 2);
931 return get_urlmatch(argv[0], argv[1]);
932 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200933 else if (actions == ACTION_UNSET) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200934 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200935 check_argc(argc, 1, 2);
936 if (argc == 2)
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100937 return git_config_set_multivar_in_file_gently(given_config_source.file,
Derrick Stoleec90702a2020-11-25 22:12:54 +0000938 argv[0], NULL, argv[1],
939 flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200940 else
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100941 return git_config_set_in_file_gently(given_config_source.file,
942 argv[0], NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200943 }
944 else if (actions == ACTION_UNSET_ALL) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200945 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200946 check_argc(argc, 1, 2);
Patrick Steinhardt30598ad2016-02-22 12:23:35 +0100947 return git_config_set_multivar_in_file_gently(given_config_source.file,
Derrick Stolee504ee122020-11-25 22:12:49 +0000948 argv[0], NULL, argv[1],
Derrick Stoleec90702a2020-11-25 22:12:54 +0000949 flags | CONFIG_FLAGS_MULTI_REPLACE);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200950 }
951 else if (actions == ACTION_RENAME_SECTION) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200952 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200953 check_argc(argc, 2, 2);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200954 ret = git_config_rename_section_in_file(given_config_source.file,
Jeff King270a3442012-02-16 03:07:32 -0500955 argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200956 if (ret < 0)
957 return ret;
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100958 else if (!ret)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200959 die(_("no such section: %s"), argv[0]);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100960 else
961 ret = 0;
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200962 }
963 else if (actions == ACTION_REMOVE_SECTION) {
Kirill A. Shutemov6aea9f02014-02-19 00:58:53 +0200964 check_write();
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200965 check_argc(argc, 1, 1);
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +0200966 ret = git_config_rename_section_in_file(given_config_source.file,
Jeff King270a3442012-02-16 03:07:32 -0500967 argv[0], NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200968 if (ret < 0)
969 return ret;
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100970 else if (!ret)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200971 die(_("no such section: %s"), argv[0]);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100972 else
973 ret = 0;
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200974 }
975 else if (actions == ACTION_GET_COLOR) {
Jeff Kingd0e08d62014-11-20 10:15:51 -0500976 check_argc(argc, 1, 2);
977 get_color(argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200978 }
979 else if (actions == ACTION_GET_COLORBOOL) {
Jeff Kingd0e08d62014-11-20 10:15:51 -0500980 check_argc(argc, 1, 2);
981 if (argc == 2)
982 color_stdout_is_tty = git_config_bool("command line", argv[1]);
983 return get_colorbool(argv[0], argc == 2);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200984 }
985
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +0100986 free(value);
987 return ret;
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100988}