blob: 20a0b64090eab56d3b6a54073e338fdbf5db4448 [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[] = {
Patrick Steinhardt14970502024-05-06 10:56:24 +020019 N_("git config list [<file-option>] [<display-option>] [--includes]"),
Patrick Steinhardt4e513892024-05-06 10:56:29 +020020 N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +020021 N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +020022 N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
Patrick Steinhardt3418e962024-05-06 10:56:43 +020023 N_("git config rename-section [<file-option>] <old-name> <new-name>"),
Patrick Steinhardt15dad202024-05-06 10:56:47 +020024 N_("git config remove-section [<file-option>] <name>"),
Patrick Steinhardt3cbace52024-05-06 10:56:52 +020025 N_("git config edit [<file-option>]"),
Patrick Steinhardt7b91d312024-05-06 10:56:57 +020026 N_("git config [<file-option>] --get-colorbool <name> [<stdout-is-tty>]"),
Felipe Contrerasd64ec162009-02-21 02:49:25 +020027 NULL
28};
Johannes Schindelin4ddba792005-11-20 06:52:22 +010029
Patrick Steinhardt14970502024-05-06 10:56:24 +020030static const char *const builtin_config_list_usage[] = {
31 N_("git config list [<file-option>] [<display-option>] [--includes]"),
32 NULL
33};
34
Patrick Steinhardt4e513892024-05-06 10:56:29 +020035static const char *const builtin_config_get_usage[] = {
36 N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
37 NULL
38};
39
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +020040static const char *const builtin_config_set_usage[] = {
41 N_("git config set [<file-option>] [--type=<type>] [--comment=<message>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
42 NULL
43};
44
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +020045static const char *const builtin_config_unset_usage[] = {
46 N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
47 NULL
48};
49
Patrick Steinhardt3418e962024-05-06 10:56:43 +020050static const char *const builtin_config_rename_section_usage[] = {
51 N_("git config rename-section [<file-option>] <old-name> <new-name>"),
52 NULL
53};
54
Patrick Steinhardt15dad202024-05-06 10:56:47 +020055static const char *const builtin_config_remove_section_usage[] = {
56 N_("git config remove-section [<file-option>] <name>"),
57 NULL
58};
59
Patrick Steinhardt3cbace52024-05-06 10:56:52 +020060static const char *const builtin_config_edit_usage[] = {
61 N_("git config edit [<file-option>]"),
62 NULL
63};
64
Patrick Steinhardtddb103c2024-05-15 08:42:16 +020065#define CONFIG_LOCATION_OPTIONS(opts) \
66 OPT_GROUP(N_("Config file location")), \
67 OPT_BOOL(0, "global", &opts.use_global_config, N_("use global config file")), \
68 OPT_BOOL(0, "system", &opts.use_system_config, N_("use system config file")), \
69 OPT_BOOL(0, "local", &opts.use_local_config, N_("use repository config file")), \
70 OPT_BOOL(0, "worktree", &opts.use_worktree_config, N_("use per-worktree config file")), \
71 OPT_STRING('f', "file", &opts.source.file, N_("file"), N_("use given config file")), \
72 OPT_STRING(0, "blob", &opts.source.blob, N_("blob-id"), N_("read config from given blob object"))
73
74struct config_location_options {
75 struct git_config_source source;
76 struct config_options options;
77 char *file_to_free;
78 int use_global_config;
79 int use_system_config;
80 int use_local_config;
81 int use_worktree_config;
Patrick Steinhardt8c869812024-05-15 08:42:35 +020082 int respect_includes_opt;
Patrick Steinhardtddb103c2024-05-15 08:42:16 +020083};
Patrick Steinhardt8c869812024-05-15 08:42:35 +020084#define CONFIG_LOCATION_OPTIONS_INIT { \
85 .respect_includes_opt = -1, \
86}
Patrick Steinhardtddb103c2024-05-15 08:42:16 +020087
Patrick Steinhardt94c46932024-05-15 08:42:25 +020088#define CONFIG_TYPE_OPTIONS(type) \
89 OPT_GROUP(N_("Type")), \
90 OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
91 OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
92 OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
93 OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
94 OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
95 OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
96 OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
97
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +020098#define CONFIG_DISPLAY_OPTIONS(opts) \
99 OPT_GROUP(N_("Display options")), \
100 OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \
101 OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \
102 OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \
103 OPT_BOOL(0, "show-scope", &opts.show_scope, N_("show scope of config (worktree, local, global, system, command)")), \
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200104 OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \
105 CONFIG_TYPE_OPTIONS(opts.type)
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200106
107struct config_display_options {
108 int end_nul;
109 int omit_values;
110 int show_origin;
111 int show_scope;
112 int show_keys;
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200113 int type;
Patrick Steinhardt4090a9c2024-05-15 08:42:30 +0200114 char *default_value;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200115 /* Populated via `display_options_init()`. */
116 int term;
117 int delim;
118 int key_delim;
119};
120#define CONFIG_DISPLAY_OPTIONS_INIT { \
121 .term = '\n', \
122 .delim = '=', \
123 .key_delim = ' ', \
124}
125
Taylor Blau0a8950b2018-04-09 15:46:54 -0700126#define TYPE_BOOL 1
127#define TYPE_INT 2
128#define TYPE_BOOL_OR_INT 3
129#define TYPE_PATH 4
130#define TYPE_EXPIRY_DATE 5
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700131#define TYPE_COLOR 6
Lin Sundbd8c092020-05-07 07:31:14 +0800132#define TYPE_BOOL_OR_STR 7
Felipe Contreras16c1e932009-02-21 02:49:27 +0200133
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700134#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
135 { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
136 PARSE_OPT_NONEG, option_parse_type, (i) }
137
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700138static int option_parse_type(const struct option *opt, const char *arg,
139 int unset)
140{
141 int new_type, *to_type;
142
143 if (unset) {
144 *((int *) opt->value) = 0;
145 return 0;
146 }
147
148 /*
149 * To support '--<type>' style flags, begin with new_type equal to
150 * opt->defval.
151 */
152 new_type = opt->defval;
153 if (!new_type) {
154 if (!strcmp(arg, "bool"))
155 new_type = TYPE_BOOL;
156 else if (!strcmp(arg, "int"))
157 new_type = TYPE_INT;
158 else if (!strcmp(arg, "bool-or-int"))
159 new_type = TYPE_BOOL_OR_INT;
Lin Sundbd8c092020-05-07 07:31:14 +0800160 else if (!strcmp(arg, "bool-or-str"))
161 new_type = TYPE_BOOL_OR_STR;
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700162 else if (!strcmp(arg, "path"))
163 new_type = TYPE_PATH;
164 else if (!strcmp(arg, "expiry-date"))
165 new_type = TYPE_EXPIRY_DATE;
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700166 else if (!strcmp(arg, "color"))
167 new_type = TYPE_COLOR;
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700168 else
169 die(_("unrecognized --type argument, %s"), arg);
170 }
171
172 to_type = opt->value;
173 if (*to_type && *to_type != new_type) {
174 /*
175 * Complain when there is a new type not equal to the old type.
176 * This allows for combinations like '--int --type=int' and
177 * '--type=int --type=int', but disallows ones like '--type=bool
178 * --int' and '--type=bool
179 * --type=int'.
180 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200181 error(_("only one type at a time"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +0200182 exit(129);
Taylor Blaufb0dc3b2018-04-18 14:43:35 -0700183 }
184 *to_type = new_type;
185
186 return 0;
187}
188
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100189static void check_argc(int argc, int min, int max)
190{
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200191 if (argc >= min && argc <= max)
192 return;
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200193 if (min == max)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200194 error(_("wrong number of arguments, should be %d"), min);
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200195 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200196 error(_("wrong number of arguments, should be from %d to %d"),
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200197 min, max);
Patrick Steinhardta577d2f2024-05-15 08:41:38 +0200198 exit(129);
Felipe Contrerasd64ec162009-02-21 02:49:25 +0200199}
200
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200201static void show_config_origin(const struct config_display_options *opts,
202 const struct key_value_info *kvi,
Glen Choo26b66932023-06-28 19:26:25 +0000203 struct strbuf *buf)
Lars Schneider70bd8792016-02-19 10:16:02 +0100204{
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200205 const char term = opts->end_nul ? '\0' : '\t';
Lars Schneider70bd8792016-02-19 10:16:02 +0100206
Glen Choo26b66932023-06-28 19:26:25 +0000207 strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
Lars Schneider70bd8792016-02-19 10:16:02 +0100208 strbuf_addch(buf, ':');
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200209 if (opts->end_nul)
Glen Choo26b66932023-06-28 19:26:25 +0000210 strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
Lars Schneider70bd8792016-02-19 10:16:02 +0100211 else
Glen Choo26b66932023-06-28 19:26:25 +0000212 quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
Lars Schneider70bd8792016-02-19 10:16:02 +0100213 strbuf_addch(buf, term);
214}
215
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200216static void show_config_scope(const struct config_display_options *opts,
217 const struct key_value_info *kvi,
Glen Choo26b66932023-06-28 19:26:25 +0000218 struct strbuf *buf)
Matthew Rogers145d59f2020-02-10 00:30:59 +0000219{
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200220 const char term = opts->end_nul ? '\0' : '\t';
Glen Choo26b66932023-06-28 19:26:25 +0000221 const char *scope = config_scope_name(kvi->scope);
Matthew Rogers145d59f2020-02-10 00:30:59 +0000222
223 strbuf_addstr(buf, N_(scope));
224 strbuf_addch(buf, term);
225}
226
Jeff King783a86c2022-08-19 06:08:44 -0400227static int show_all_config(const char *key_, const char *value_,
Glen Choo26b66932023-06-28 19:26:25 +0000228 const struct config_context *ctx,
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200229 void *cb)
Petr Baudisde791f12006-04-25 00:59:25 +0200230{
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200231 const struct config_display_options *opts = cb;
Glen Choo26b66932023-06-28 19:26:25 +0000232 const struct key_value_info *kvi = ctx->kvi;
233
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200234 if (opts->show_origin || opts->show_scope) {
Lars Schneider70bd8792016-02-19 10:16:02 +0100235 struct strbuf buf = STRBUF_INIT;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200236 if (opts->show_scope)
237 show_config_scope(opts, kvi, &buf);
238 if (opts->show_origin)
239 show_config_origin(opts, kvi, &buf);
Lars Schneider70bd8792016-02-19 10:16:02 +0100240 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
241 fwrite(buf.buf, 1, buf.len, stdout);
242 strbuf_release(&buf);
243 }
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200244 if (!opts->omit_values && value_)
245 printf("%s%c%s%c", key_, opts->delim, value_, opts->term);
Petr Baudisde791f12006-04-25 00:59:25 +0200246 else
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200247 printf("%s%c", key_, opts->term);
Petr Baudisde791f12006-04-25 00:59:25 +0200248 return 0;
249}
250
Jeff King7acdd6f2012-10-23 15:51:50 -0400251struct strbuf_list {
252 struct strbuf *items;
253 int nr;
254 int alloc;
255};
256
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200257static int format_config(const struct config_display_options *opts,
258 struct strbuf *buf, const char *key_,
Glen Choo26b66932023-06-28 19:26:25 +0000259 const char *value_, const struct key_value_info *kvi)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100260{
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200261 if (opts->show_scope)
262 show_config_scope(opts, kvi, buf);
263 if (opts->show_origin)
264 show_config_origin(opts, kvi, buf);
265 if (opts->show_keys)
Jeff King7acdd6f2012-10-23 15:51:50 -0400266 strbuf_addstr(buf, key_);
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200267 if (!opts->omit_values) {
268 if (opts->show_keys)
269 strbuf_addch(buf, opts->key_delim);
Jeff King00b347d2012-10-23 16:52:44 -0400270
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200271 if (opts->type == TYPE_INT)
Jeff Kingf2259872015-08-20 10:47:34 -0400272 strbuf_addf(buf, "%"PRId64,
Glen Choo8868b1e2023-06-28 19:26:27 +0000273 git_config_int64(key_, value_ ? value_ : "", kvi));
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200274 else if (opts->type == TYPE_BOOL)
Jeff Kingf2259872015-08-20 10:47:34 -0400275 strbuf_addstr(buf, git_config_bool(key_, value_) ?
276 "true" : "false");
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200277 else if (opts->type == TYPE_BOOL_OR_INT) {
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200278 int is_bool, v;
Glen Choo8868b1e2023-06-28 19:26:27 +0000279 v = git_config_bool_or_int(key_, value_, kvi,
280 &is_bool);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200281 if (is_bool)
Jeff Kingf2259872015-08-20 10:47:34 -0400282 strbuf_addstr(buf, v ? "true" : "false");
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200283 else
Jeff Kingf2259872015-08-20 10:47:34 -0400284 strbuf_addf(buf, "%d", v);
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200285 } else if (opts->type == TYPE_BOOL_OR_STR) {
Lin Sundbd8c092020-05-07 07:31:14 +0800286 int v = git_parse_maybe_bool(value_);
287 if (v < 0)
288 strbuf_addstr(buf, value_);
289 else
290 strbuf_addstr(buf, v ? "true" : "false");
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200291 } else if (opts->type == TYPE_PATH) {
Patrick Steinhardt6073b3b2024-05-27 13:46:15 +0200292 char *v;
Jeff Kingf2259872015-08-20 10:47:34 -0400293 if (git_config_pathname(&v, key_, value_) < 0)
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200294 return -1;
Jeff Kingf2259872015-08-20 10:47:34 -0400295 strbuf_addstr(buf, v);
296 free((char *)v);
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200297 } else if (opts->type == TYPE_EXPIRY_DATE) {
Haaris Mehmood5f967422017-11-18 02:27:27 +0000298 timestamp_t t;
299 if (git_config_expiry_date(&t, key_, value_) < 0)
300 return -1;
301 strbuf_addf(buf, "%"PRItime, t);
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200302 } else if (opts->type == TYPE_COLOR) {
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700303 char v[COLOR_MAXLEN];
304 if (git_config_color(v, key_, value_) < 0)
305 return -1;
306 strbuf_addstr(buf, v);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200307 } else if (value_) {
Jeff Kingf2259872015-08-20 10:47:34 -0400308 strbuf_addstr(buf, value_);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200309 } else {
Jeff Kingf2259872015-08-20 10:47:34 -0400310 /* Just show the key name; back out delimiter */
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200311 if (opts->show_keys)
Jeff Kingf2259872015-08-20 10:47:34 -0400312 strbuf_setlen(buf, buf->len - 1);
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200313 }
SZEDER Gáborebca2d42015-08-20 16:14:22 +0200314 }
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200315 strbuf_addch(buf, opts->term);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100316 return 0;
317}
318
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200319#define GET_VALUE_ALL (1 << 0)
320#define GET_VALUE_KEY_REGEXP (1 << 1)
321
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200322struct collect_config_data {
323 const struct config_display_options *display_opts;
324 struct strbuf_list *values;
Patrick Steinhardtbfe45f82024-05-15 08:42:44 +0200325 const char *value_pattern;
Patrick Steinhardt040b1412024-05-15 08:42:58 +0200326 const char *key;
Patrick Steinhardt4ff8feb2024-05-15 08:42:49 +0200327 regex_t *regexp;
Patrick Steinhardtfdfaaa12024-05-15 08:42:53 +0200328 regex_t *key_regexp;
Patrick Steinhardt65d197c2024-05-15 08:42:39 +0200329 int do_not_match;
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200330 unsigned get_value_flags;
Patrick Steinhardtab8bac82024-05-15 08:43:02 +0200331 unsigned flags;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200332};
333
Glen Chooa4e7e312023-06-28 19:26:22 +0000334static int collect_config(const char *key_, const char *value_,
Glen Choo26b66932023-06-28 19:26:25 +0000335 const struct config_context *ctx, void *cb)
Junio C Hamanod9b91692013-07-29 14:23:16 -0700336{
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200337 struct collect_config_data *data = cb;
338 struct strbuf_list *values = data->values;
Glen Choo26b66932023-06-28 19:26:25 +0000339 const struct key_value_info *kvi = ctx->kvi;
Junio C Hamanod9b91692013-07-29 14:23:16 -0700340
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200341 if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
342 strcmp(key_, data->key))
Junio C Hamanod9b91692013-07-29 14:23:16 -0700343 return 0;
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200344 if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
345 regexec(data->key_regexp, key_, 0, NULL, 0))
Junio C Hamanod9b91692013-07-29 14:23:16 -0700346 return 0;
Patrick Steinhardtab8bac82024-05-15 08:43:02 +0200347 if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) &&
348 strcmp(data->value_pattern, (value_?value_:"")))
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000349 return 0;
Patrick Steinhardt4ff8feb2024-05-15 08:42:49 +0200350 if (data->regexp &&
351 (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0)))
Junio C Hamanod9b91692013-07-29 14:23:16 -0700352 return 0;
353
354 ALLOC_GROW(values->items, values->nr + 1, values->alloc);
Jeff King9f1429d2015-08-20 10:46:04 -0400355 strbuf_init(&values->items[values->nr], 0);
Junio C Hamanod9b91692013-07-29 14:23:16 -0700356
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200357 return format_config(data->display_opts, &values->items[values->nr++],
358 key_, value_, kvi);
Junio C Hamanod9b91692013-07-29 14:23:16 -0700359}
360
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200361static int get_value(const struct config_location_options *opts,
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200362 const struct config_display_options *display_opts,
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200363 const char *key_, const char *regex_,
364 unsigned get_value_flags, unsigned flags)
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100365{
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700366 int ret = CONFIG_GENERIC_ERROR;
Ramsay Jones5ba1a8a2012-10-28 21:05:25 +0000367 struct strbuf_list values = {NULL};
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200368 struct collect_config_data data = {
369 .display_opts = display_opts,
370 .values = &values,
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200371 .get_value_flags = get_value_flags,
Patrick Steinhardtab8bac82024-05-15 08:43:02 +0200372 .flags = flags,
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200373 };
Patrick Steinhardt040b1412024-05-15 08:42:58 +0200374 char *key = NULL;
Jeff King7acdd6f2012-10-23 15:51:50 -0400375 int i;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100376
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200377 if (get_value_flags & GET_VALUE_KEY_REGEXP) {
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100378 char *tl;
379
380 /*
381 * NEEDSWORK: this naive pattern lowercasing obviously does not
382 * work for more complex patterns like "^[^.]*Foo.*bar".
383 * Perhaps we should deprecate this altogether someday.
384 */
385
386 key = xstrdup(key_);
387 for (tl = key + strlen(key) - 1;
388 tl >= key && *tl != '.';
389 tl--)
390 *tl = tolower(*tl);
391 for (tl = key; *tl && *tl != '.'; tl++)
392 *tl = tolower(*tl);
393
Patrick Steinhardtfdfaaa12024-05-15 08:42:53 +0200394 data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
395 if (regcomp(data.key_regexp, key, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200396 error(_("invalid key pattern: %s"), key_);
Patrick Steinhardtfdfaaa12024-05-15 08:42:53 +0200397 FREE_AND_NULL(data.key_regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700398 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200399 goto free_strings;
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200400 }
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100401 } else {
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700402 if (git_config_parse_key(key_, &key, NULL)) {
403 ret = CONFIG_INVALID_KEY;
Libor Pechacekb09c53a2011-01-30 20:40:41 +0100404 goto free_strings;
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700405 }
Patrick Steinhardt040b1412024-05-15 08:42:58 +0200406
407 data.key = key;
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200408 }
409
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000410 if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
Patrick Steinhardtbfe45f82024-05-15 08:42:44 +0200411 data.value_pattern = regex_;
Derrick Stolee3f1bae12020-11-25 22:12:55 +0000412 else if (regex_) {
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100413 if (regex_[0] == '!') {
Patrick Steinhardt65d197c2024-05-15 08:42:39 +0200414 data.do_not_match = 1;
Johannes Schindelinf98d8632005-11-20 13:24:18 +0100415 regex_++;
416 }
417
Patrick Steinhardt4ff8feb2024-05-15 08:42:49 +0200418 data.regexp = (regex_t*)xmalloc(sizeof(regex_t));
419 if (regcomp(data.regexp, regex_, REG_EXTENDED)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200420 error(_("invalid pattern: %s"), regex_);
Patrick Steinhardt4ff8feb2024-05-15 08:42:49 +0200421 FREE_AND_NULL(data.regexp);
Junio C Hamano9409c7a2012-07-29 13:43:21 -0700422 ret = CONFIG_INVALID_PATTERN;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200423 goto free_strings;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100424 }
425 }
426
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200427 config_with_options(collect_config, &data,
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200428 &opts->source, the_repository,
429 &opts->options);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200430
Patrick Steinhardt4090a9c2024-05-15 08:42:30 +0200431 if (!values.nr && display_opts->default_value) {
Glen Choo26b66932023-06-28 19:26:25 +0000432 struct key_value_info kvi = KVI_INIT;
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700433 struct strbuf *item;
Glen Choo26b66932023-06-28 19:26:25 +0000434
435 kvi_from_param(&kvi);
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700436 ALLOC_GROW(values.items, values.nr + 1, values.alloc);
437 item = &values.items[values.nr++];
438 strbuf_init(item, 0);
Patrick Steinhardt4090a9c2024-05-15 08:42:30 +0200439 if (format_config(display_opts, item, key_,
440 display_opts->default_value, &kvi) < 0)
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700441 die(_("failed to format default config value: %s"),
Patrick Steinhardt4090a9c2024-05-15 08:42:30 +0200442 display_opts->default_value);
Taylor Blaueeaa24b2018-04-09 17:18:26 -0700443 }
444
Jeff King00b347d2012-10-23 16:52:44 -0400445 ret = !values.nr;
Jeff King7acdd6f2012-10-23 15:51:50 -0400446
447 for (i = 0; i < values.nr; i++) {
448 struct strbuf *buf = values.items + i;
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200449 if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1)
Jeff King00b347d2012-10-23 16:52:44 -0400450 fwrite(buf->buf, 1, buf->len, stdout);
Jeff King7acdd6f2012-10-23 15:51:50 -0400451 strbuf_release(buf);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100452 }
Jeff King7acdd6f2012-10-23 15:51:50 -0400453 free(values.items);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100454
Jeff King97ed50f2012-10-23 15:40:06 -0400455free_strings:
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100456 free(key);
Patrick Steinhardtfdfaaa12024-05-15 08:42:53 +0200457 if (data.key_regexp) {
458 regfree(data.key_regexp);
459 free(data.key_regexp);
Jeff King35998c82012-10-23 15:36:12 -0400460 }
Patrick Steinhardt4ff8feb2024-05-15 08:42:49 +0200461 if (data.regexp) {
462 regfree(data.regexp);
463 free(data.regexp);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100464 }
465
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200466 return ret;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100467}
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100468
Glen Choo8868b1e2023-06-28 19:26:27 +0000469static char *normalize_value(const char *key, const char *value,
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200470 int type, struct key_value_info *kvi)
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200471{
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200472 if (!value)
473 return NULL;
474
Taylor Blau0a8950b2018-04-09 15:46:54 -0700475 if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
Matthieu Moy13494842009-12-30 17:51:53 +0100476 /*
477 * We don't do normalization for TYPE_PATH here: If
478 * the path is like ~/foobar/, we prefer to store
479 * "~/foobar/" in the config file, and to expand the ~
480 * when retrieving the value.
Haaris Mehmood5f967422017-11-18 02:27:27 +0000481 * Also don't do normalization for expiry dates.
Matthieu Moy13494842009-12-30 17:51:53 +0100482 */
Jeff King3ec832c2015-09-24 17:07:05 -0400483 return xstrdup(value);
Taylor Blau0a8950b2018-04-09 15:46:54 -0700484 if (type == TYPE_INT)
Glen Choo8868b1e2023-06-28 19:26:27 +0000485 return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
Taylor Blau0a8950b2018-04-09 15:46:54 -0700486 if (type == TYPE_BOOL)
Jeff King3ec832c2015-09-24 17:07:05 -0400487 return xstrdup(git_config_bool(key, value) ? "true" : "false");
Taylor Blau0a8950b2018-04-09 15:46:54 -0700488 if (type == TYPE_BOOL_OR_INT) {
Jeff King3ec832c2015-09-24 17:07:05 -0400489 int is_bool, v;
Glen Choo8868b1e2023-06-28 19:26:27 +0000490 v = git_config_bool_or_int(key, value, kvi, &is_bool);
Jeff King3ec832c2015-09-24 17:07:05 -0400491 if (!is_bool)
492 return xstrfmt("%d", v);
493 else
494 return xstrdup(v ? "true" : "false");
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200495 }
Lin Sundbd8c092020-05-07 07:31:14 +0800496 if (type == TYPE_BOOL_OR_STR) {
497 int v = git_parse_maybe_bool(value);
498 if (v < 0)
499 return xstrdup(value);
500 else
501 return xstrdup(v ? "true" : "false");
502 }
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700503 if (type == TYPE_COLOR) {
504 char v[COLOR_MAXLEN];
505 if (git_config_color(v, key, value))
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200506 die(_("cannot parse color '%s'"), value);
Taylor Blau63e2a0f2018-04-09 17:18:31 -0700507
508 /*
509 * The contents of `v` now contain an ANSI escape
510 * sequence, not suitable for including within a
511 * configuration file. Treat the above as a
512 * "sanity-check", and return the given value, which we
513 * know is representable as valid color code.
514 */
515 return xstrdup(value);
516 }
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200517
Junio C Hamano50f08db2018-05-30 14:04:07 +0900518 BUG("cannot normalize type %d", type);
Frank Lichtenhelddb1696b2007-06-25 16:00:24 +0200519}
520
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200521struct get_color_config_data {
522 int get_color_found;
523 const char *get_color_slot;
524 char parsed_color[COLOR_MAXLEN];
525};
Junio C Hamano9ce03522007-11-27 22:41:05 -0800526
Jeff King783a86c2022-08-19 06:08:44 -0400527static int git_get_color_config(const char *var, const char *value,
Glen Chooa4e7e312023-06-28 19:26:22 +0000528 const struct config_context *ctx UNUSED,
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200529 void *cb)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800530{
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200531 struct get_color_config_data *data = cb;
532
533 if (!strcmp(var, data->get_color_slot)) {
Junio C Hamanof7699822008-02-11 10:48:12 -0800534 if (!value)
535 config_error_nonbool(var);
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200536 if (color_parse(value, data->parsed_color) < 0)
Jeff Kingf6c5a292014-10-07 15:33:09 -0400537 return -1;
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200538 data->get_color_found = 1;
Junio C Hamano9ce03522007-11-27 22:41:05 -0800539 }
540 return 0;
541}
542
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200543static void get_color(const struct config_location_options *opts,
544 const char *var, const char *def_color)
Junio C Hamano9ce03522007-11-27 22:41:05 -0800545{
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200546 struct get_color_config_data data = {
547 .get_color_slot = var,
548 .parsed_color[0] = '\0',
549 };
550
551 config_with_options(git_get_color_config, &data,
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200552 &opts->source, the_repository,
553 &opts->options);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800554
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200555 if (!data.get_color_found && def_color) {
556 if (color_parse(def_color, data.parsed_color) < 0)
Jeff Kingf6c5a292014-10-07 15:33:09 -0400557 die(_("unable to parse default color value"));
558 }
Junio C Hamano9ce03522007-11-27 22:41:05 -0800559
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200560 fputs(data.parsed_color, stdout);
Junio C Hamano9ce03522007-11-27 22:41:05 -0800561}
562
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200563struct get_colorbool_config_data {
564 int get_colorbool_found;
565 int get_diff_color_found;
566 int get_color_ui_found;
567 const char *get_colorbool_slot;
568};
569
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100570static int git_get_colorbool_config(const char *var, const char *value,
Glen Chooa4e7e312023-06-28 19:26:22 +0000571 const struct config_context *ctx UNUSED,
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200572 void *cb)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800573{
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200574 struct get_colorbool_config_data *data = cb;
575
576 if (!strcmp(var, data->get_colorbool_slot))
577 data->get_colorbool_found = git_config_colorbool(var, value);
Jeff Kinge269eb72011-08-17 22:03:48 -0700578 else if (!strcmp(var, "diff.color"))
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200579 data->get_diff_color_found = git_config_colorbool(var, value);
Jeff Kinge269eb72011-08-17 22:03:48 -0700580 else if (!strcmp(var, "color.ui"))
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200581 data->get_color_ui_found = git_config_colorbool(var, value);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800582 return 0;
583}
584
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200585static int get_colorbool(const struct config_location_options *opts,
586 const char *var, int print)
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800587{
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200588 struct get_colorbool_config_data data = {
589 .get_colorbool_slot = var,
590 .get_colorbool_found = -1,
591 .get_diff_color_found = -1,
592 .get_color_ui_found = -1,
593 };
594
595 config_with_options(git_get_colorbool_config, &data,
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200596 &opts->source, the_repository,
597 &opts->options);
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800598
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200599 if (data.get_colorbool_found < 0) {
600 if (!strcmp(data.get_colorbool_slot, "color.diff"))
601 data.get_colorbool_found = data.get_diff_color_found;
602 if (data.get_colorbool_found < 0)
603 data.get_colorbool_found = data.get_color_ui_found;
Junio C Hamano69243c22007-12-05 22:12:07 -0800604 }
605
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200606 if (data.get_colorbool_found < 0)
Matthieu Moyb8612b42013-05-15 19:00:55 +0200607 /* default value if none found in config */
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200608 data.get_colorbool_found = GIT_COLOR_AUTO;
Matthieu Moyb8612b42013-05-15 19:00:55 +0200609
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200610 data.get_colorbool_found = want_color(data.get_colorbool_found);
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700611
Felipe Contreras0e854a22009-02-21 02:48:57 +0200612 if (print) {
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200613 printf("%s\n", data.get_colorbool_found ? "true" : "false");
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800614 return 0;
Felipe Contreras0e854a22009-02-21 02:48:57 +0200615 } else
Patrick Steinhardt9c625342024-05-15 08:43:12 +0200616 return data.get_colorbool_found ? 0 : 1;
Junio C Hamano0f6f5a42007-12-05 17:26:11 -0800617}
618
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200619static void check_write(const struct git_config_source *source)
Heiko Voigt1bc88812013-07-12 00:46:47 +0200620{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200621 if (!source->file && !startup_info->have_repository)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200622 die(_("not in a git directory"));
Johannes Schindelin638fa622016-02-24 13:48:11 +0100623
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200624 if (source->use_stdin)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200625 die(_("writing to stdin is not supported"));
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200626
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200627 if (source->blob)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200628 die(_("writing config blobs is not supported"));
Heiko Voigt1bc88812013-07-12 00:46:47 +0200629}
630
Junio C Hamanod4770962013-07-31 11:14:59 -0700631struct urlmatch_current_candidate_value {
632 char value_is_null;
633 struct strbuf value;
Glen Choo26b66932023-06-28 19:26:25 +0000634 struct key_value_info kvi;
Junio C Hamanod4770962013-07-31 11:14:59 -0700635};
636
Glen Chooa4e7e312023-06-28 19:26:22 +0000637static int urlmatch_collect_fn(const char *var, const char *value,
Glen Choo26b66932023-06-28 19:26:25 +0000638 const struct config_context *ctx,
Glen Chooa4e7e312023-06-28 19:26:22 +0000639 void *cb)
Junio C Hamanod4770962013-07-31 11:14:59 -0700640{
641 struct string_list *values = cb;
642 struct string_list_item *item = string_list_insert(values, var);
643 struct urlmatch_current_candidate_value *matched = item->util;
Glen Choo26b66932023-06-28 19:26:25 +0000644 const struct key_value_info *kvi = ctx->kvi;
Junio C Hamanod4770962013-07-31 11:14:59 -0700645
646 if (!matched) {
647 matched = xmalloc(sizeof(*matched));
648 strbuf_init(&matched->value, 0);
649 item->util = matched;
650 } else {
651 strbuf_reset(&matched->value);
652 }
Glen Choo26b66932023-06-28 19:26:25 +0000653 matched->kvi = *kvi;
Junio C Hamanod4770962013-07-31 11:14:59 -0700654
655 if (value) {
656 strbuf_addstr(&matched->value, value);
657 matched->value_is_null = 0;
658 } else {
659 matched->value_is_null = 1;
660 }
661 return 0;
662}
663
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200664static int get_urlmatch(const struct config_location_options *opts,
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200665 const struct config_display_options *_display_opts,
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200666 const char *var, const char *url)
Junio C Hamanod4770962013-07-31 11:14:59 -0700667{
John Keeping27b30be2016-02-28 11:54:35 +0000668 int ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700669 char *section_tail;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200670 struct config_display_options display_opts = *_display_opts;
Junio C Hamanod4770962013-07-31 11:14:59 -0700671 struct string_list_item *item;
Ævar Arnfjörð Bjarmason73ee4492021-10-01 12:27:33 +0200672 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
Junio C Hamanod4770962013-07-31 11:14:59 -0700673 struct string_list values = STRING_LIST_INIT_DUP;
674
675 config.collect_fn = urlmatch_collect_fn;
676 config.cascade_fn = NULL;
677 config.cb = &values;
678
679 if (!url_normalize(url, &config.url))
Junio C Hamano6667a6a2013-08-08 21:41:44 -0700680 die("%s", config.url.err);
Junio C Hamanod4770962013-07-31 11:14:59 -0700681
Jeff King88d5a6f2014-05-22 05:44:09 -0400682 config.section = xstrdup_tolower(var);
Junio C Hamanod4770962013-07-31 11:14:59 -0700683 section_tail = strchr(config.section, '.');
684 if (section_tail) {
685 *section_tail = '\0';
686 config.key = section_tail + 1;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200687 display_opts.show_keys = 0;
Junio C Hamanod4770962013-07-31 11:14:59 -0700688 } else {
689 config.key = NULL;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200690 display_opts.show_keys = 1;
Junio C Hamanod4770962013-07-31 11:14:59 -0700691 }
692
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700693 config_with_options(urlmatch_config_entry, &config,
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200694 &opts->source, the_repository,
695 &opts->options);
Junio C Hamanod4770962013-07-31 11:14:59 -0700696
John Keeping27b30be2016-02-28 11:54:35 +0000697 ret = !values.nr;
698
Junio C Hamanod4770962013-07-31 11:14:59 -0700699 for_each_string_list_item(item, &values) {
700 struct urlmatch_current_candidate_value *matched = item->util;
Junio C Hamanod4770962013-07-31 11:14:59 -0700701 struct strbuf buf = STRBUF_INIT;
702
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200703 format_config(&display_opts, &buf, item->string,
Glen Choo26b66932023-06-28 19:26:25 +0000704 matched->value_is_null ? NULL : matched->value.buf,
705 &matched->kvi);
Junio C Hamanod4770962013-07-31 11:14:59 -0700706 fwrite(buf.buf, 1, buf.len, stdout);
Junio C Hamanod4770962013-07-31 11:14:59 -0700707 strbuf_release(&buf);
708
709 strbuf_release(&matched->value);
710 }
Ævar Arnfjörð Bjarmasona41e8e72022-03-04 19:32:07 +0100711 urlmatch_config_release(&config);
Junio C Hamanod4770962013-07-31 11:14:59 -0700712 string_list_clear(&values, 1);
713 free(config.url.url);
714
715 free((void *)config.section);
John Keeping27b30be2016-02-28 11:54:35 +0000716 return ret;
Junio C Hamanod4770962013-07-31 11:14:59 -0700717}
718
Matthieu Moy98305342014-07-25 21:11:34 +0200719static char *default_user_config(void)
720{
721 struct strbuf buf = STRBUF_INIT;
722 strbuf_addf(&buf,
723 _("# This is Git's per-user configuration file.\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300724 "[user]\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200725 "# Please adapt and uncomment the following lines:\n"
Ossi Herrala7e110522015-04-17 17:50:10 +0300726 "# name = %s\n"
Matthieu Moy98305342014-07-25 21:11:34 +0200727 "# email = %s\n"),
728 ident_default_name(),
729 ident_default_email());
730 return strbuf_detach(&buf, NULL);
731}
732
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200733static void location_options_init(struct config_location_options *opts,
734 const char *prefix)
Patrick Steinhardt424a29c2024-05-06 10:56:00 +0200735{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200736 if (!opts->source.file)
737 opts->source.file = opts->file_to_free =
738 xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
739
740 if (opts->use_global_config + opts->use_system_config +
741 opts->use_local_config + opts->use_worktree_config +
742 !!opts->source.file + !!opts->source.blob > 1) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200743 error(_("only one config file at a time"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +0200744 exit(129);
Felipe Contreras67052c92009-02-21 02:49:26 +0200745 }
746
Patrick Steinhardt9dda6b72024-05-06 10:56:14 +0200747 if (!startup_info->have_repository) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200748 if (opts->use_local_config)
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300749 die(_("--local can only be used inside a git repository"));
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200750 if (opts->source.blob)
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300751 die(_("--blob can only be used inside a git repository"));
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200752 if (opts->use_worktree_config)
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300753 die(_("--worktree can only be used inside a git repository"));
Matheus Tavares378fe5f2020-09-09 10:16:08 -0300754 }
Jeff King17b8a2d2018-05-18 15:27:04 -0700755
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200756 if (opts->source.file &&
757 !strcmp(opts->source.file, "-")) {
758 opts->source.file = NULL;
759 opts->source.use_stdin = 1;
760 opts->source.scope = CONFIG_SCOPE_COMMAND;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +0200761 }
762
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200763 if (opts->use_global_config) {
764 opts->source.file = opts->file_to_free = git_global_config();
765 if (!opts->source.file)
Kristoffer Haugsbakk1cb3b922024-01-28 19:31:40 +0100766 /*
767 * It is unknown if HOME/.gitconfig exists, so
768 * we do not know if we should write to XDG
769 * location; error out even if XDG_CONFIG_HOME
770 * is set and points at a sane location.
771 */
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +0200772 die(_("$HOME not set"));
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200773 opts->source.scope = CONFIG_SCOPE_GLOBAL;
774 } else if (opts->use_system_config) {
775 opts->source.file = opts->file_to_free = git_system_config();
776 opts->source.scope = CONFIG_SCOPE_SYSTEM;
777 } else if (opts->use_local_config) {
778 opts->source.file = opts->file_to_free = git_pathdup("config");
779 opts->source.scope = CONFIG_SCOPE_LOCAL;
780 } else if (opts->use_worktree_config) {
Eric Sunshine03f24652020-06-19 19:35:44 -0400781 struct worktree **worktrees = get_worktrees();
Victoria Dye3867f6d2023-05-26 01:33:00 +0000782 if (the_repository->repository_format_worktree_config)
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200783 opts->source.file = opts->file_to_free =
784 git_pathdup("config.worktree");
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200785 else if (worktrees[0] && worktrees[1])
786 die(_("--worktree cannot be used with multiple "
787 "working trees unless the config\n"
788 "extension worktreeConfig is enabled. "
789 "Please read \"CONFIGURATION FILE\"\n"
790 "section in \"git help worktree\" for details"));
791 else
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200792 opts->source.file = opts->file_to_free =
793 git_pathdup("config");
794 opts->source.scope = CONFIG_SCOPE_LOCAL;
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +0200795 free_worktrees(worktrees);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200796 } else if (opts->source.file) {
797 if (!is_absolute_path(opts->source.file) && prefix)
798 opts->source.file = opts->file_to_free =
799 prefix_filename(prefix, opts->source.file);
800 opts->source.scope = CONFIG_SCOPE_COMMAND;
801 } else if (opts->source.blob) {
802 opts->source.scope = CONFIG_SCOPE_COMMAND;
Petr Baudis7162dff2006-02-12 04:14:48 +0100803 }
804
Patrick Steinhardt8c869812024-05-15 08:42:35 +0200805 if (opts->respect_includes_opt == -1)
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200806 opts->options.respect_includes = !opts->source.file;
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +0700807 else
Patrick Steinhardt8c869812024-05-15 08:42:35 +0200808 opts->options.respect_includes = opts->respect_includes_opt;
Patrick Steinhardt9dda6b72024-05-06 10:56:14 +0200809 if (startup_info->have_repository) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200810 opts->options.commondir = get_git_common_dir();
811 opts->options.git_dir = get_git_dir();
Brandon Williamsdc8441f2017-06-14 11:07:39 -0700812 }
Patrick Steinhardt9dda6b72024-05-06 10:56:14 +0200813}
814
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200815static void location_options_release(struct config_location_options *opts)
816{
817 free(opts->file_to_free);
818}
819
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200820static void display_options_init(struct config_display_options *opts)
821{
822 if (opts->end_nul) {
823 opts->term = '\0';
824 opts->delim = '\n';
825 opts->key_delim = '\n';
Patrick Steinhardtfee37962024-05-06 10:56:19 +0200826 }
827}
828
Patrick Steinhardt14970502024-05-06 10:56:24 +0200829static int cmd_config_list(int argc, const char **argv, const char *prefix)
830{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200831 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200832 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
Patrick Steinhardt14970502024-05-06 10:56:24 +0200833 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200834 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200835 CONFIG_DISPLAY_OPTIONS(display_opts),
Patrick Steinhardt14970502024-05-06 10:56:24 +0200836 OPT_GROUP(N_("Other")),
Patrick Steinhardt8c869812024-05-15 08:42:35 +0200837 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
838 N_("respect include directives on lookup")),
Patrick Steinhardt14970502024-05-06 10:56:24 +0200839 OPT_END(),
840 };
841
842 argc = parse_options(argc, argv, prefix, opts, builtin_config_list_usage, 0);
843 check_argc(argc, 0, 0);
844
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200845 location_options_init(&location_opts, prefix);
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200846 display_options_init(&display_opts);
Patrick Steinhardt14970502024-05-06 10:56:24 +0200847
848 setup_auto_pager("config", 1);
849
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200850 if (config_with_options(show_all_config, &display_opts,
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200851 &location_opts.source, the_repository,
852 &location_opts.options) < 0) {
853 if (location_opts.source.file)
Patrick Steinhardt14970502024-05-06 10:56:24 +0200854 die_errno(_("unable to read config file '%s'"),
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200855 location_opts.source.file);
Patrick Steinhardt14970502024-05-06 10:56:24 +0200856 else
857 die(_("error processing config file(s)"));
858 }
859
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200860 location_options_release(&location_opts);
Patrick Steinhardt14970502024-05-06 10:56:24 +0200861 return 0;
862}
863
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200864static int cmd_config_get(int argc, const char **argv, const char *prefix)
865{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200866 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200867 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200868 const char *value_pattern = NULL, *url = NULL;
869 int flags = 0;
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200870 unsigned get_value_flags = 0;
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200871 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200872 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200873 OPT_GROUP(N_("Filter options")),
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200874 OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL),
875 OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP),
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200876 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
877 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
878 OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")),
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200879 CONFIG_DISPLAY_OPTIONS(display_opts),
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200880 OPT_GROUP(N_("Other")),
Patrick Steinhardt8c869812024-05-15 08:42:35 +0200881 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
882 N_("respect include directives on lookup")),
Patrick Steinhardt4090a9c2024-05-15 08:42:30 +0200883 OPT_STRING(0, "default", &display_opts.default_value,
884 N_("value"), N_("use default value when missing entry")),
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200885 OPT_END(),
886 };
Patrick Steinhardt999425c2024-05-15 08:42:11 +0200887 int ret;
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200888
889 argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage,
890 PARSE_OPT_STOP_AT_NON_OPTION);
891 check_argc(argc, 1, 1);
892
893 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
894 die(_("--fixed-value only applies with 'value-pattern'"));
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200895 if (display_opts.default_value &&
896 ((get_value_flags & GET_VALUE_ALL) || url))
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200897 die(_("--default= cannot be used with --all or --url="));
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200898 if (url && ((get_value_flags & GET_VALUE_ALL) ||
899 (get_value_flags & GET_VALUE_KEY_REGEXP) ||
900 value_pattern))
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200901 die(_("--url= cannot be used with --all, --regexp or --value"));
902
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200903 location_options_init(&location_opts, prefix);
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200904 display_options_init(&display_opts);
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200905
906 setup_auto_pager("config", 1);
907
908 if (url)
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +0200909 ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
Patrick Steinhardt999425c2024-05-15 08:42:11 +0200910 else
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +0200911 ret = get_value(&location_opts, &display_opts, argv[0], value_pattern,
912 get_value_flags, flags);
Patrick Steinhardt999425c2024-05-15 08:42:11 +0200913
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200914 location_options_release(&location_opts);
Patrick Steinhardt999425c2024-05-15 08:42:11 +0200915 return ret;
Patrick Steinhardt4e513892024-05-06 10:56:29 +0200916}
917
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200918static int cmd_config_set(int argc, const char **argv, const char *prefix)
919{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200920 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200921 const char *value_pattern = NULL, *comment_arg = NULL;
922 char *comment = NULL;
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200923 int flags = 0, append = 0, type = 0;
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200924 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200925 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200926 CONFIG_TYPE_OPTIONS(type),
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200927 OPT_GROUP(N_("Filter")),
928 OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
929 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
930 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
931 OPT_GROUP(N_("Other")),
932 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
933 OPT_BOOL(0, "append", &append, N_("add a new line without altering any existing values")),
934 OPT_END(),
935 };
936 struct key_value_info default_kvi = KVI_INIT;
937 char *value;
938 int ret;
939
940 argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage,
941 PARSE_OPT_STOP_AT_NON_OPTION);
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200942 check_argc(argc, 2, 2);
943
944 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
945 die(_("--fixed-value only applies with --value=<pattern>"));
946 if (append && value_pattern)
947 die(_("--append cannot be used with --value=<pattern>"));
948 if (append)
949 value_pattern = CONFIG_REGEX_NONE;
950
951 comment = git_config_prepare_comment_string(comment_arg);
952
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200953 location_options_init(&location_opts, prefix);
954 check_write(&location_opts.source);
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200955
Patrick Steinhardt94c46932024-05-15 08:42:25 +0200956 value = normalize_value(argv[0], argv[1], type, &default_kvi);
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200957
958 if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200959 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200960 argv[0], value, value_pattern,
961 comment, flags);
962 } else {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200963 ret = git_config_set_in_file_gently(location_opts.source.file,
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200964 argv[0], comment, value);
965 if (ret == CONFIG_NOTHING_SET)
966 error(_("cannot overwrite multiple values with a single value\n"
967 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
968 }
969
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200970 location_options_release(&location_opts);
Patrick Steinhardt00bbdde2024-05-06 10:56:33 +0200971 free(comment);
972 free(value);
973 return ret;
974}
975
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +0200976static int cmd_config_unset(int argc, const char **argv, const char *prefix)
977{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200978 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +0200979 const char *value_pattern = NULL;
980 int flags = 0;
981 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200982 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +0200983 OPT_GROUP(N_("Filter")),
984 OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
985 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
986 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
987 OPT_END(),
988 };
Patrick Steinhardt999425c2024-05-15 08:42:11 +0200989 int ret;
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +0200990
991 argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage,
992 PARSE_OPT_STOP_AT_NON_OPTION);
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +0200993 check_argc(argc, 1, 1);
994
995 if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
996 die(_("--fixed-value only applies with 'value-pattern'"));
997
Patrick Steinhardtddb103c2024-05-15 08:42:16 +0200998 location_options_init(&location_opts, prefix);
999 check_write(&location_opts.source);
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +02001000
1001 if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern)
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001002 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001003 argv[0], NULL, value_pattern,
1004 NULL, flags);
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +02001005 else
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001006 ret = git_config_set_in_file_gently(location_opts.source.file, argv[0],
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001007 NULL, NULL);
1008
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001009 location_options_release(&location_opts);
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001010 return ret;
Patrick Steinhardt95ea69c2024-05-06 10:56:38 +02001011}
1012
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001013static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
1014{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001015 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001016 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001017 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001018 OPT_END(),
1019 };
1020 int ret;
1021
1022 argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage,
1023 PARSE_OPT_STOP_AT_NON_OPTION);
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001024 check_argc(argc, 2, 2);
1025
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001026 location_options_init(&location_opts, prefix);
1027 check_write(&location_opts.source);
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001028
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001029 ret = git_config_rename_section_in_file(location_opts.source.file,
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001030 argv[0], argv[1]);
1031 if (ret < 0)
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001032 goto out;
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001033 else if (!ret)
1034 die(_("no such section: %s"), argv[0]);
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001035 ret = 0;
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001036
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001037out:
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001038 location_options_release(&location_opts);
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001039 return ret;
Patrick Steinhardt3418e962024-05-06 10:56:43 +02001040}
1041
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001042static int cmd_config_remove_section(int argc, const char **argv, const char *prefix)
1043{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001044 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001045 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001046 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001047 OPT_END(),
1048 };
1049 int ret;
1050
1051 argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage,
1052 PARSE_OPT_STOP_AT_NON_OPTION);
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001053 check_argc(argc, 1, 1);
1054
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001055 location_options_init(&location_opts, prefix);
1056 check_write(&location_opts.source);
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001057
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001058 ret = git_config_rename_section_in_file(location_opts.source.file,
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001059 argv[0], NULL);
1060 if (ret < 0)
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001061 goto out;
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001062 else if (!ret)
1063 die(_("no such section: %s"), argv[0]);
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001064 ret = 0;
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001065
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001066out:
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001067 location_options_release(&location_opts);
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001068 return ret;
Patrick Steinhardt15dad202024-05-06 10:56:47 +02001069}
1070
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001071static int show_editor(struct config_location_options *opts)
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001072{
1073 char *config_file;
1074
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001075 if (!opts->source.file && !startup_info->have_repository)
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001076 die(_("not in a git directory"));
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001077 if (opts->source.use_stdin)
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001078 die(_("editing stdin is not supported"));
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001079 if (opts->source.blob)
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001080 die(_("editing blobs is not supported"));
1081 git_config(git_default_config, NULL);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001082 config_file = opts->source.file ?
1083 xstrdup(opts->source.file) :
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001084 git_pathdup("config");
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001085 if (opts->use_global_config) {
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001086 int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
1087 if (fd >= 0) {
1088 char *content = default_user_config();
1089 write_str_in_full(fd, content);
1090 free(content);
1091 close(fd);
1092 }
1093 else if (errno != EEXIST)
1094 die_errno(_("cannot create configuration file %s"), config_file);
1095 }
1096 launch_editor(config_file, NULL, NULL);
1097 free(config_file);
1098
1099 return 0;
1100}
1101
1102static int cmd_config_edit(int argc, const char **argv, const char *prefix)
1103{
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001104 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001105 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001106 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001107 OPT_END(),
1108 };
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001109 int ret;
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001110
1111 argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0);
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001112 check_argc(argc, 0, 0);
1113
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001114 location_options_init(&location_opts, prefix);
1115 check_write(&location_opts.source);
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001116
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001117 ret = show_editor(&location_opts);
1118 location_options_release(&location_opts);
1119 return ret;
Patrick Steinhardt3cbace52024-05-06 10:56:52 +02001120}
1121
Patrick Steinhardt0336d002024-05-15 08:41:43 +02001122static int cmd_config_actions(int argc, const char **argv, const char *prefix)
Patrick Steinhardt9dda6b72024-05-06 10:56:14 +02001123{
Patrick Steinhardt9cab5e82024-05-15 08:41:57 +02001124 enum {
1125 ACTION_GET = (1<<0),
1126 ACTION_GET_ALL = (1<<1),
1127 ACTION_GET_REGEXP = (1<<2),
1128 ACTION_REPLACE_ALL = (1<<3),
1129 ACTION_ADD = (1<<4),
1130 ACTION_UNSET = (1<<5),
1131 ACTION_UNSET_ALL = (1<<6),
1132 ACTION_RENAME_SECTION = (1<<7),
1133 ACTION_REMOVE_SECTION = (1<<8),
1134 ACTION_LIST = (1<<9),
1135 ACTION_EDIT = (1<<10),
1136 ACTION_SET = (1<<11),
1137 ACTION_SET_ALL = (1<<12),
1138 ACTION_GET_COLOR = (1<<13),
1139 ACTION_GET_COLORBOOL = (1<<14),
1140 ACTION_GET_URLMATCH = (1<<15),
1141 };
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001142 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001143 struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
Patrick Steinhardt7d5387e2024-05-15 08:41:52 +02001144 const char *comment_arg = NULL;
1145 int actions = 0;
Patrick Steinhardtab8bac82024-05-15 08:43:02 +02001146 unsigned flags = 0;
Patrick Steinhardt7d5387e2024-05-15 08:41:52 +02001147 struct option opts[] = {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001148 CONFIG_LOCATION_OPTIONS(location_opts),
Patrick Steinhardt7d5387e2024-05-15 08:41:52 +02001149 OPT_GROUP(N_("Action")),
1150 OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET),
1151 OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL),
1152 OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP),
1153 OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
1154 OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL),
1155 OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
1156 OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET),
1157 OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL),
1158 OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
1159 OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
1160 OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST),
1161 OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
1162 OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
1163 OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001164 CONFIG_DISPLAY_OPTIONS(display_opts),
Patrick Steinhardt7d5387e2024-05-15 08:41:52 +02001165 OPT_GROUP(N_("Other")),
Patrick Steinhardt4090a9c2024-05-15 08:42:30 +02001166 OPT_STRING(0, "default", &display_opts.default_value,
1167 N_("value"), N_("with --get, use default value when missing entry")),
Patrick Steinhardt7d5387e2024-05-15 08:41:52 +02001168 OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
Patrick Steinhardtab8bac82024-05-15 08:43:02 +02001169 OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
Patrick Steinhardt8c869812024-05-15 08:42:35 +02001170 OPT_BOOL(0, "includes", &location_opts.respect_includes_opt,
1171 N_("respect include directives on lookup")),
Patrick Steinhardt7d5387e2024-05-15 08:41:52 +02001172 OPT_END(),
1173 };
Patrick Steinhardt9dda6b72024-05-06 10:56:14 +02001174 char *value = NULL, *comment = NULL;
Patrick Steinhardt9dda6b72024-05-06 10:56:14 +02001175 int ret = 0;
1176 struct key_value_info default_kvi = KVI_INIT;
1177
Patrick Steinhardt7d5387e2024-05-15 08:41:52 +02001178 argc = parse_options(argc, argv, prefix, opts,
Patrick Steinhardt9dda6b72024-05-06 10:56:14 +02001179 builtin_config_usage,
1180 PARSE_OPT_STOP_AT_NON_OPTION);
1181
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001182 location_options_init(&location_opts, prefix);
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001183 display_options_init(&display_opts);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001184
Patrick Steinhardt94c46932024-05-15 08:42:25 +02001185 if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001186 error(_("--get-color and variable type are incoherent"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +02001187 exit(129);
Felipe Contrerasc2387352009-02-21 02:49:29 +02001188 }
1189
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001190 if (actions == 0)
1191 switch (argc) {
1192 case 1: actions = ACTION_GET; break;
1193 case 2: actions = ACTION_SET; break;
1194 case 3: actions = ACTION_SET_ALL; break;
1195 default:
Patrick Steinhardta577d2f2024-05-15 08:41:38 +02001196 error(_("no action specified"));
1197 exit(129);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001198 }
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001199 if (display_opts.omit_values &&
SZEDER Gábor578625f2015-08-10 11:46:06 +02001200 !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001201 error(_("--name-only is only applicable to --list or --get-regexp"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +02001202 exit(129);
SZEDER Gábor578625f2015-08-10 11:46:06 +02001203 }
Lars Schneider70bd8792016-02-19 10:16:02 +01001204
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001205 if (display_opts.show_origin && !(actions &
Lars Schneider70bd8792016-02-19 10:16:02 +01001206 (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001207 error(_("--show-origin is only applicable to --get, --get-all, "
1208 "--get-regexp, and --list"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +02001209 exit(129);
Lars Schneider70bd8792016-02-19 10:16:02 +01001210 }
1211
Patrick Steinhardt4090a9c2024-05-15 08:42:30 +02001212 if (display_opts.default_value && !(actions & ACTION_GET)) {
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001213 error(_("--default is only applicable to --get"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +02001214 exit(129);
Taylor Blaueeaa24b2018-04-09 17:18:26 -07001215 }
1216
Patrick Steinhardta78b4622024-05-06 10:55:56 +02001217 if (comment_arg &&
Junio C Hamanofbad3342024-03-15 12:43:58 -07001218 !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) {
1219 error(_("--comment is only applicable to add/set/replace operations"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +02001220 exit(129);
Ralph Seichter42d5c032024-03-12 21:47:00 +00001221 }
1222
Derrick Stoleefda43942020-11-25 22:12:53 +00001223 /* check usage of --fixed-value */
Patrick Steinhardtab8bac82024-05-15 08:43:02 +02001224 if (flags & CONFIG_FLAGS_FIXED_VALUE) {
Derrick Stoleefda43942020-11-25 22:12:53 +00001225 int allowed_usage = 0;
1226
1227 switch (actions) {
1228 /* git config --get <name> <value-pattern> */
1229 case ACTION_GET:
1230 /* git config --get-all <name> <value-pattern> */
1231 case ACTION_GET_ALL:
1232 /* git config --get-regexp <name-pattern> <value-pattern> */
1233 case ACTION_GET_REGEXP:
1234 /* git config --unset <name> <value-pattern> */
1235 case ACTION_UNSET:
1236 /* git config --unset-all <name> <value-pattern> */
1237 case ACTION_UNSET_ALL:
1238 allowed_usage = argc > 1 && !!argv[1];
1239 break;
1240
1241 /* git config <name> <value> <value-pattern> */
1242 case ACTION_SET_ALL:
1243 /* git config --replace-all <name> <value> <value-pattern> */
1244 case ACTION_REPLACE_ALL:
1245 allowed_usage = argc > 2 && !!argv[2];
1246 break;
1247
1248 /* other options don't allow --fixed-value */
1249 }
1250
1251 if (!allowed_usage) {
1252 error(_("--fixed-value only applies with 'value-pattern'"));
Patrick Steinhardta577d2f2024-05-15 08:41:38 +02001253 exit(129);
Derrick Stoleefda43942020-11-25 22:12:53 +00001254 }
1255 }
1256
Patrick Steinhardta78b4622024-05-06 10:55:56 +02001257 comment = git_config_prepare_comment_string(comment_arg);
Junio C Hamanofbad3342024-03-15 12:43:58 -07001258
Patrick Steinhardt9cab5e82024-05-15 08:41:57 +02001259 /*
1260 * The following actions may produce more than one line of output and
1261 * should therefore be paged.
1262 */
1263 if (actions & (ACTION_LIST | ACTION_GET_ALL | ACTION_GET_REGEXP | ACTION_GET_URLMATCH))
Martin Ågrenc0e9f5b2018-02-21 19:51:44 +01001264 setup_auto_pager("config", 1);
Martin Ågren32888b82018-02-21 19:51:43 +01001265
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001266 if (actions == ACTION_LIST) {
Felipe Contreras225a9ca2009-02-21 02:49:28 +02001267 check_argc(argc, 0, 0);
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001268 if (config_with_options(show_all_config, &display_opts,
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001269 &location_opts.source, the_repository,
1270 &location_opts.options) < 0) {
1271 if (location_opts.source.file)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001272 die_errno(_("unable to read config file '%s'"),
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001273 location_opts.source.file);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001274 else
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001275 die(_("error processing config file(s)"));
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001276 }
1277 }
1278 else if (actions == ACTION_EDIT) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001279 ret = show_editor(&location_opts);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001280 }
1281 else if (actions == ACTION_SET) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001282 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001283 check_argc(argc, 2, 2);
Patrick Steinhardt94c46932024-05-15 08:42:25 +02001284 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001285 ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value);
Michael J Gruber5a2df362011-05-17 17:38:53 +02001286 if (ret == CONFIG_NOTHING_SET)
Vasco Almeidaccf63802016-09-15 14:59:00 +00001287 error(_("cannot overwrite multiple values with a single value\n"
1288 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001289 }
1290 else if (actions == ACTION_SET_ALL) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001291 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001292 check_argc(argc, 2, 3);
Patrick Steinhardt94c46932024-05-15 08:42:25 +02001293 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001294 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001295 argv[0], value, argv[2],
Ralph Seichter42d5c032024-03-12 21:47:00 +00001296 comment, flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001297 }
1298 else if (actions == ACTION_ADD) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001299 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001300 check_argc(argc, 2, 2);
Patrick Steinhardt94c46932024-05-15 08:42:25 +02001301 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001302 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001303 argv[0], value,
1304 CONFIG_REGEX_NONE,
Ralph Seichter42d5c032024-03-12 21:47:00 +00001305 comment, flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001306 }
1307 else if (actions == ACTION_REPLACE_ALL) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001308 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001309 check_argc(argc, 2, 3);
Patrick Steinhardt94c46932024-05-15 08:42:25 +02001310 value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001311 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001312 argv[0], value, argv[2],
Ralph Seichter42d5c032024-03-12 21:47:00 +00001313 comment, flags | CONFIG_FLAGS_MULTI_REPLACE);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001314 }
1315 else if (actions == ACTION_GET) {
1316 check_argc(argc, 1, 2);
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +02001317 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1318 0, flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001319 }
1320 else if (actions == ACTION_GET_ALL) {
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001321 check_argc(argc, 1, 2);
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +02001322 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1323 GET_VALUE_ALL, flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001324 }
1325 else if (actions == ACTION_GET_REGEXP) {
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001326 display_opts.show_keys = 1;
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001327 check_argc(argc, 1, 2);
Patrick Steinhardt35a7cfd2024-05-15 08:43:07 +02001328 ret = get_value(&location_opts, &display_opts, argv[0], argv[1],
1329 GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001330 }
Junio C Hamanod4770962013-07-31 11:14:59 -07001331 else if (actions == ACTION_GET_URLMATCH) {
1332 check_argc(argc, 2, 2);
Patrick Steinhardtc0c1e262024-05-15 08:42:21 +02001333 ret = get_urlmatch(&location_opts, &display_opts, argv[0], argv[1]);
Junio C Hamanod4770962013-07-31 11:14:59 -07001334 }
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001335 else if (actions == ACTION_UNSET) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001336 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001337 check_argc(argc, 1, 2);
1338 if (argc == 2)
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001339 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001340 argv[0], NULL, argv[1],
1341 NULL, flags);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001342 else
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001343 ret = git_config_set_in_file_gently(location_opts.source.file,
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001344 argv[0], NULL, NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001345 }
1346 else if (actions == ACTION_UNSET_ALL) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001347 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001348 check_argc(argc, 1, 2);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001349 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001350 argv[0], NULL, argv[1],
1351 NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001352 }
1353 else if (actions == ACTION_RENAME_SECTION) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001354 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001355 check_argc(argc, 2, 2);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001356 ret = git_config_rename_section_in_file(location_opts.source.file,
Jeff King270a3442012-02-16 03:07:32 -05001357 argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001358 if (ret < 0)
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001359 goto out;
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001360 else if (!ret)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001361 die(_("no such section: %s"), argv[0]);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001362 else
1363 ret = 0;
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001364 }
1365 else if (actions == ACTION_REMOVE_SECTION) {
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001366 check_write(&location_opts.source);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001367 check_argc(argc, 1, 1);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001368 ret = git_config_rename_section_in_file(location_opts.source.file,
Jeff King270a3442012-02-16 03:07:32 -05001369 argv[0], NULL);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001370 if (ret < 0)
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001371 goto out;
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001372 else if (!ret)
Nguyễn Thái Ngọc Duy1d28ff42018-07-21 09:49:22 +02001373 die(_("no such section: %s"), argv[0]);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001374 else
1375 ret = 0;
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001376 }
1377 else if (actions == ACTION_GET_COLOR) {
Jeff Kingd0e08d62014-11-20 10:15:51 -05001378 check_argc(argc, 1, 2);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001379 get_color(&location_opts, argv[0], argv[1]);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001380 }
1381 else if (actions == ACTION_GET_COLORBOOL) {
Jeff Kingd0e08d62014-11-20 10:15:51 -05001382 check_argc(argc, 1, 2);
1383 if (argc == 2)
1384 color_stdout_is_tty = git_config_bool("command line", argv[1]);
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001385 ret = get_colorbool(&location_opts, argv[0], argc == 2);
Felipe Contrerasd64ec162009-02-21 02:49:25 +02001386 }
1387
Patrick Steinhardt999425c2024-05-15 08:42:11 +02001388out:
Patrick Steinhardtddb103c2024-05-15 08:42:16 +02001389 location_options_release(&location_opts);
Patrick Steinhardta78b4622024-05-06 10:55:56 +02001390 free(comment);
Ævar Arnfjörð Bjarmasonac95f5d2022-11-08 19:17:51 +01001391 free(value);
1392 return ret;
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +01001393}
Patrick Steinhardt0336d002024-05-15 08:41:43 +02001394
1395int cmd_config(int argc, const char **argv, const char *prefix)
1396{
Patrick Steinhardt8b908f92024-05-15 08:41:47 +02001397 parse_opt_subcommand_fn *subcommand = NULL;
1398 struct option subcommand_opts[] = {
1399 OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
1400 OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
1401 OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
1402 OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
1403 OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
1404 OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section),
1405 OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit),
1406 OPT_END(),
1407 };
1408
Patrick Steinhardt0336d002024-05-15 08:41:43 +02001409 /*
1410 * This is somewhat hacky: we first parse the command line while
1411 * keeping all args intact in order to determine whether a subcommand
1412 * has been specified. If so, we re-parse it a second time, but this
1413 * time we drop KEEP_ARGV0. This is so that we don't munge the command
1414 * line in case no subcommand was given, which would otherwise confuse
1415 * us when parsing the legacy-style modes that don't use subcommands.
1416 */
Patrick Steinhardt8b908f92024-05-15 08:41:47 +02001417 argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
Patrick Steinhardt0336d002024-05-15 08:41:43 +02001418 PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT);
1419 if (subcommand) {
Patrick Steinhardt8b908f92024-05-15 08:41:47 +02001420 argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
Patrick Steinhardt0336d002024-05-15 08:41:43 +02001421 PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
1422 return subcommand(argc, argv, prefix);
1423 }
1424
1425 return cmd_config_actions(argc, argv, prefix);
1426}