]>
Commit | Line | Data |
---|---|---|
03eae9af | 1 | #define USE_THE_REPOSITORY_VARIABLE |
e12c095a | 2 | #include "builtin.h" |
0b027f6c | 3 | #include "abspath.h" |
b2141fc1 | 4 | #include "config.h" |
9ce03522 | 5 | #include "color.h" |
4e120823 | 6 | #include "editor.h" |
32a8f510 | 7 | #include "environment.h" |
f394e093 | 8 | #include "gettext.h" |
b5fa6081 | 9 | #include "ident.h" |
d64ec16c | 10 | #include "parse-options.h" |
d4770964 | 11 | #include "urlmatch.h" |
d1cbe1e6 | 12 | #include "path.h" |
70bd879a | 13 | #include "quote.h" |
e38da487 | 14 | #include "setup.h" |
88e4e183 | 15 | #include "strbuf.h" |
58b284a2 | 16 | #include "worktree.h" |
1b1e59c5 | 17 | |
d64ec16c | 18 | static const char *const builtin_config_usage[] = { |
14970509 | 19 | N_("git config list [<file-option>] [<display-option>] [--includes]"), |
16094704 | 20 | N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp] [--value=<value>] [--fixed-value] [--default=<default>] <name>"), |
00bbdde1 | 21 | N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"), |
f36b8cba | 22 | N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name>"), |
3418e96f | 23 | N_("git config rename-section [<file-option>] <old-name> <new-name>"), |
15dad20c | 24 | N_("git config remove-section [<file-option>] <name>"), |
3cbace5e | 25 | N_("git config edit [<file-option>]"), |
7b91d310 | 26 | N_("git config [<file-option>] --get-colorbool <name> [<stdout-is-tty>]"), |
d64ec16c FC |
27 | NULL |
28 | }; | |
4ddba79d | 29 | |
14970509 PS |
30 | static const char *const builtin_config_list_usage[] = { |
31 | N_("git config list [<file-option>] [<display-option>] [--includes]"), | |
32 | NULL | |
33 | }; | |
34 | ||
4e513890 PS |
35 | static 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 | ||
00bbdde1 PS |
40 | static 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 | ||
95ea69c6 | 45 | static const char *const builtin_config_unset_usage[] = { |
f36b8cba | 46 | N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name>"), |
95ea69c6 PS |
47 | NULL |
48 | }; | |
49 | ||
3418e96f PS |
50 | static const char *const builtin_config_rename_section_usage[] = { |
51 | N_("git config rename-section [<file-option>] <old-name> <new-name>"), | |
52 | NULL | |
53 | }; | |
54 | ||
15dad20c PS |
55 | static const char *const builtin_config_remove_section_usage[] = { |
56 | N_("git config remove-section [<file-option>] <name>"), | |
57 | NULL | |
58 | }; | |
59 | ||
3cbace5e PS |
60 | static const char *const builtin_config_edit_usage[] = { |
61 | N_("git config edit [<file-option>]"), | |
62 | NULL | |
63 | }; | |
64 | ||
ddb103c2 PS |
65 | #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 | ||
74 | struct 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; | |
8c869812 | 82 | int respect_includes_opt; |
ddb103c2 | 83 | }; |
8c869812 PS |
84 | #define CONFIG_LOCATION_OPTIONS_INIT { \ |
85 | .respect_includes_opt = -1, \ | |
86 | } | |
ddb103c2 | 87 | |
94c46930 PS |
88 | #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 | ||
c0c1e263 PS |
98 | #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)")), \ | |
94c46930 PS |
104 | OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \ |
105 | CONFIG_TYPE_OPTIONS(opts.type) | |
c0c1e263 PS |
106 | |
107 | struct config_display_options { | |
108 | int end_nul; | |
109 | int omit_values; | |
110 | int show_origin; | |
111 | int show_scope; | |
112 | int show_keys; | |
94c46930 | 113 | int type; |
4090a9c9 | 114 | char *default_value; |
c0c1e263 PS |
115 | /* 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 | ||
0a8950be TB |
126 | #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 | |
63e2a0f8 | 131 | #define TYPE_COLOR 6 |
dbd8c09b | 132 | #define TYPE_BOOL_OR_STR 7 |
16c1e939 | 133 | |
d012ceb5 PS |
134 | #define OPT_CALLBACK_VALUE(s, l, v, h, i) { \ |
135 | .type = OPTION_CALLBACK, \ | |
136 | .short_name = (s), \ | |
137 | .long_name = (l), \ | |
138 | .value = (v), \ | |
139 | .help = (h), \ | |
140 | .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, \ | |
141 | .callback = option_parse_type, \ | |
142 | .defval = (i), \ | |
143 | } | |
fb0dc3ba | 144 | |
fb0dc3ba TB |
145 | static int option_parse_type(const struct option *opt, const char *arg, |
146 | int unset) | |
147 | { | |
148 | int new_type, *to_type; | |
149 | ||
150 | if (unset) { | |
151 | *((int *) opt->value) = 0; | |
152 | return 0; | |
153 | } | |
154 | ||
155 | /* | |
156 | * To support '--<type>' style flags, begin with new_type equal to | |
157 | * opt->defval. | |
158 | */ | |
159 | new_type = opt->defval; | |
160 | if (!new_type) { | |
161 | if (!strcmp(arg, "bool")) | |
162 | new_type = TYPE_BOOL; | |
163 | else if (!strcmp(arg, "int")) | |
164 | new_type = TYPE_INT; | |
165 | else if (!strcmp(arg, "bool-or-int")) | |
166 | new_type = TYPE_BOOL_OR_INT; | |
dbd8c09b LS |
167 | else if (!strcmp(arg, "bool-or-str")) |
168 | new_type = TYPE_BOOL_OR_STR; | |
fb0dc3ba TB |
169 | else if (!strcmp(arg, "path")) |
170 | new_type = TYPE_PATH; | |
171 | else if (!strcmp(arg, "expiry-date")) | |
172 | new_type = TYPE_EXPIRY_DATE; | |
63e2a0f8 TB |
173 | else if (!strcmp(arg, "color")) |
174 | new_type = TYPE_COLOR; | |
fb0dc3ba TB |
175 | else |
176 | die(_("unrecognized --type argument, %s"), arg); | |
177 | } | |
178 | ||
179 | to_type = opt->value; | |
180 | if (*to_type && *to_type != new_type) { | |
181 | /* | |
182 | * Complain when there is a new type not equal to the old type. | |
183 | * This allows for combinations like '--int --type=int' and | |
184 | * '--type=int --type=int', but disallows ones like '--type=bool | |
185 | * --int' and '--type=bool | |
186 | * --type=int'. | |
187 | */ | |
1d28ff4c | 188 | error(_("only one type at a time")); |
a577d2f1 | 189 | exit(129); |
fb0dc3ba TB |
190 | } |
191 | *to_type = new_type; | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
3b335762 NTND |
196 | static void check_argc(int argc, int min, int max) |
197 | { | |
d64ec16c FC |
198 | if (argc >= min && argc <= max) |
199 | return; | |
1a07e59c | 200 | if (min == max) |
1d28ff4c | 201 | error(_("wrong number of arguments, should be %d"), min); |
1a07e59c | 202 | else |
1d28ff4c | 203 | error(_("wrong number of arguments, should be from %d to %d"), |
1a07e59c | 204 | min, max); |
a577d2f1 | 205 | exit(129); |
d64ec16c FC |
206 | } |
207 | ||
c0c1e263 PS |
208 | static void show_config_origin(const struct config_display_options *opts, |
209 | const struct key_value_info *kvi, | |
26b66932 | 210 | struct strbuf *buf) |
70bd879a | 211 | { |
c0c1e263 | 212 | const char term = opts->end_nul ? '\0' : '\t'; |
70bd879a | 213 | |
26b66932 | 214 | strbuf_addstr(buf, config_origin_type_name(kvi->origin_type)); |
70bd879a | 215 | strbuf_addch(buf, ':'); |
c0c1e263 | 216 | if (opts->end_nul) |
26b66932 | 217 | strbuf_addstr(buf, kvi->filename ? kvi->filename : ""); |
70bd879a | 218 | else |
26b66932 | 219 | quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0); |
70bd879a LS |
220 | strbuf_addch(buf, term); |
221 | } | |
222 | ||
c0c1e263 PS |
223 | static void show_config_scope(const struct config_display_options *opts, |
224 | const struct key_value_info *kvi, | |
26b66932 | 225 | struct strbuf *buf) |
145d59f4 | 226 | { |
c0c1e263 | 227 | const char term = opts->end_nul ? '\0' : '\t'; |
26b66932 | 228 | const char *scope = config_scope_name(kvi->scope); |
145d59f4 MR |
229 | |
230 | strbuf_addstr(buf, N_(scope)); | |
231 | strbuf_addch(buf, term); | |
232 | } | |
233 | ||
783a86c1 | 234 | static int show_all_config(const char *key_, const char *value_, |
26b66932 | 235 | const struct config_context *ctx, |
c0c1e263 | 236 | void *cb) |
de791f15 | 237 | { |
c0c1e263 | 238 | const struct config_display_options *opts = cb; |
26b66932 GC |
239 | const struct key_value_info *kvi = ctx->kvi; |
240 | ||
c0c1e263 | 241 | if (opts->show_origin || opts->show_scope) { |
70bd879a | 242 | struct strbuf buf = STRBUF_INIT; |
c0c1e263 PS |
243 | if (opts->show_scope) |
244 | show_config_scope(opts, kvi, &buf); | |
245 | if (opts->show_origin) | |
246 | show_config_origin(opts, kvi, &buf); | |
70bd879a LS |
247 | /* Use fwrite as "buf" can contain \0's if "end_null" is set. */ |
248 | fwrite(buf.buf, 1, buf.len, stdout); | |
249 | strbuf_release(&buf); | |
250 | } | |
c0c1e263 PS |
251 | if (!opts->omit_values && value_) |
252 | printf("%s%c%s%c", key_, opts->delim, value_, opts->term); | |
de791f15 | 253 | else |
c0c1e263 | 254 | printf("%s%c", key_, opts->term); |
de791f15 PB |
255 | return 0; |
256 | } | |
257 | ||
7acdd6f0 JK |
258 | struct strbuf_list { |
259 | struct strbuf *items; | |
260 | int nr; | |
261 | int alloc; | |
262 | }; | |
263 | ||
c0c1e263 PS |
264 | static int format_config(const struct config_display_options *opts, |
265 | struct strbuf *buf, const char *key_, | |
26b66932 | 266 | const char *value_, const struct key_value_info *kvi) |
4ddba79d | 267 | { |
c0c1e263 PS |
268 | if (opts->show_scope) |
269 | show_config_scope(opts, kvi, buf); | |
270 | if (opts->show_origin) | |
271 | show_config_origin(opts, kvi, buf); | |
272 | if (opts->show_keys) | |
7acdd6f0 | 273 | strbuf_addstr(buf, key_); |
c0c1e263 PS |
274 | if (!opts->omit_values) { |
275 | if (opts->show_keys) | |
276 | strbuf_addch(buf, opts->key_delim); | |
ebca2d49 | 277 | |
94c46930 | 278 | if (opts->type == TYPE_INT) |
f2259877 | 279 | strbuf_addf(buf, "%"PRId64, |
8868b1eb | 280 | git_config_int64(key_, value_ ? value_ : "", kvi)); |
94c46930 | 281 | else if (opts->type == TYPE_BOOL) |
f2259877 JK |
282 | strbuf_addstr(buf, git_config_bool(key_, value_) ? |
283 | "true" : "false"); | |
94c46930 | 284 | else if (opts->type == TYPE_BOOL_OR_INT) { |
ebca2d49 | 285 | int is_bool, v; |
8868b1eb GC |
286 | v = git_config_bool_or_int(key_, value_, kvi, |
287 | &is_bool); | |
ebca2d49 | 288 | if (is_bool) |
f2259877 | 289 | strbuf_addstr(buf, v ? "true" : "false"); |
ebca2d49 | 290 | else |
f2259877 | 291 | strbuf_addf(buf, "%d", v); |
94c46930 | 292 | } else if (opts->type == TYPE_BOOL_OR_STR) { |
dbd8c09b LS |
293 | int v = git_parse_maybe_bool(value_); |
294 | if (v < 0) | |
295 | strbuf_addstr(buf, value_); | |
296 | else | |
297 | strbuf_addstr(buf, v ? "true" : "false"); | |
94c46930 | 298 | } else if (opts->type == TYPE_PATH) { |
6073b3b5 | 299 | char *v; |
f2259877 | 300 | if (git_config_pathname(&v, key_, value_) < 0) |
ebca2d49 | 301 | return -1; |
f2259877 JK |
302 | strbuf_addstr(buf, v); |
303 | free((char *)v); | |
94c46930 | 304 | } else if (opts->type == TYPE_EXPIRY_DATE) { |
5f967424 HM |
305 | timestamp_t t; |
306 | if (git_config_expiry_date(&t, key_, value_) < 0) | |
307 | return -1; | |
308 | strbuf_addf(buf, "%"PRItime, t); | |
94c46930 | 309 | } else if (opts->type == TYPE_COLOR) { |
63e2a0f8 TB |
310 | char v[COLOR_MAXLEN]; |
311 | if (git_config_color(v, key_, value_) < 0) | |
312 | return -1; | |
313 | strbuf_addstr(buf, v); | |
ebca2d49 | 314 | } else if (value_) { |
f2259877 | 315 | strbuf_addstr(buf, value_); |
ebca2d49 | 316 | } else { |
f2259877 | 317 | /* Just show the key name; back out delimiter */ |
c0c1e263 | 318 | if (opts->show_keys) |
f2259877 | 319 | strbuf_setlen(buf, buf->len - 1); |
ebca2d49 | 320 | } |
578625fa | 321 | } |
c0c1e263 | 322 | strbuf_addch(buf, opts->term); |
4ddba79d JS |
323 | return 0; |
324 | } | |
325 | ||
35a7cfda PS |
326 | #define GET_VALUE_ALL (1 << 0) |
327 | #define GET_VALUE_KEY_REGEXP (1 << 1) | |
328 | ||
c0c1e263 PS |
329 | struct collect_config_data { |
330 | const struct config_display_options *display_opts; | |
331 | struct strbuf_list *values; | |
bfe45f83 | 332 | const char *value_pattern; |
040b141d | 333 | const char *key; |
4ff8feb3 | 334 | regex_t *regexp; |
fdfaaa1b | 335 | regex_t *key_regexp; |
65d197cf | 336 | int do_not_match; |
35a7cfda | 337 | unsigned get_value_flags; |
ab8bac8b | 338 | unsigned flags; |
c0c1e263 PS |
339 | }; |
340 | ||
a4e7e317 | 341 | static int collect_config(const char *key_, const char *value_, |
26b66932 | 342 | const struct config_context *ctx, void *cb) |
d9b9169b | 343 | { |
c0c1e263 PS |
344 | struct collect_config_data *data = cb; |
345 | struct strbuf_list *values = data->values; | |
26b66932 | 346 | const struct key_value_info *kvi = ctx->kvi; |
d9b9169b | 347 | |
35a7cfda PS |
348 | if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) && |
349 | strcmp(key_, data->key)) | |
d9b9169b | 350 | return 0; |
35a7cfda PS |
351 | if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) && |
352 | regexec(data->key_regexp, key_, 0, NULL, 0)) | |
d9b9169b | 353 | return 0; |
ab8bac8b PS |
354 | if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) && |
355 | strcmp(data->value_pattern, (value_?value_:""))) | |
3f1bae1d | 356 | return 0; |
4ff8feb3 PS |
357 | if (data->regexp && |
358 | (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0))) | |
d9b9169b JH |
359 | return 0; |
360 | ||
361 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); | |
9f1429df | 362 | strbuf_init(&values->items[values->nr], 0); |
d9b9169b | 363 | |
c0c1e263 PS |
364 | return format_config(data->display_opts, &values->items[values->nr++], |
365 | key_, value_, kvi); | |
d9b9169b JH |
366 | } |
367 | ||
ddb103c2 | 368 | static int get_value(const struct config_location_options *opts, |
c0c1e263 | 369 | const struct config_display_options *display_opts, |
35a7cfda PS |
370 | const char *key_, const char *regex_, |
371 | unsigned get_value_flags, unsigned flags) | |
4ddba79d | 372 | { |
9409c7a5 | 373 | int ret = CONFIG_GENERIC_ERROR; |
5ba1a8a7 | 374 | struct strbuf_list values = {NULL}; |
c0c1e263 PS |
375 | struct collect_config_data data = { |
376 | .display_opts = display_opts, | |
377 | .values = &values, | |
35a7cfda | 378 | .get_value_flags = get_value_flags, |
ab8bac8b | 379 | .flags = flags, |
c0c1e263 | 380 | }; |
040b141d | 381 | char *key = NULL; |
7acdd6f0 | 382 | int i; |
4ddba79d | 383 | |
35a7cfda | 384 | if (get_value_flags & GET_VALUE_KEY_REGEXP) { |
b09c53a3 LP |
385 | char *tl; |
386 | ||
387 | /* | |
388 | * NEEDSWORK: this naive pattern lowercasing obviously does not | |
389 | * work for more complex patterns like "^[^.]*Foo.*bar". | |
390 | * Perhaps we should deprecate this altogether someday. | |
391 | */ | |
392 | ||
393 | key = xstrdup(key_); | |
394 | for (tl = key + strlen(key) - 1; | |
395 | tl >= key && *tl != '.'; | |
396 | tl--) | |
397 | *tl = tolower(*tl); | |
398 | for (tl = key; *tl && *tl != '.'; tl++) | |
399 | *tl = tolower(*tl); | |
400 | ||
fdfaaa1b PS |
401 | data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
402 | if (regcomp(data.key_regexp, key, REG_EXTENDED)) { | |
1d28ff4c | 403 | error(_("invalid key pattern: %s"), key_); |
fdfaaa1b | 404 | FREE_AND_NULL(data.key_regexp); |
9409c7a5 | 405 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 406 | goto free_strings; |
2fa9a0fb | 407 | } |
b09c53a3 | 408 | } else { |
9409c7a5 JH |
409 | if (git_config_parse_key(key_, &key, NULL)) { |
410 | ret = CONFIG_INVALID_KEY; | |
b09c53a3 | 411 | goto free_strings; |
9409c7a5 | 412 | } |
040b141d PS |
413 | |
414 | data.key = key; | |
2fa9a0fb JS |
415 | } |
416 | ||
3f1bae1d | 417 | if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE)) |
bfe45f83 | 418 | data.value_pattern = regex_; |
3f1bae1d | 419 | else if (regex_) { |
f98d863d | 420 | if (regex_[0] == '!') { |
65d197cf | 421 | data.do_not_match = 1; |
f98d863d JS |
422 | regex_++; |
423 | } | |
424 | ||
4ff8feb3 PS |
425 | data.regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
426 | if (regcomp(data.regexp, regex_, REG_EXTENDED)) { | |
1d28ff4c | 427 | error(_("invalid pattern: %s"), regex_); |
4ff8feb3 | 428 | FREE_AND_NULL(data.regexp); |
9409c7a5 | 429 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 430 | goto free_strings; |
4ddba79d JS |
431 | } |
432 | } | |
433 | ||
c0c1e263 | 434 | config_with_options(collect_config, &data, |
ddb103c2 PS |
435 | &opts->source, the_repository, |
436 | &opts->options); | |
5f1a63e0 | 437 | |
4090a9c9 | 438 | if (!values.nr && display_opts->default_value) { |
26b66932 | 439 | struct key_value_info kvi = KVI_INIT; |
eeaa24b9 | 440 | struct strbuf *item; |
26b66932 GC |
441 | |
442 | kvi_from_param(&kvi); | |
eeaa24b9 TB |
443 | ALLOC_GROW(values.items, values.nr + 1, values.alloc); |
444 | item = &values.items[values.nr++]; | |
445 | strbuf_init(item, 0); | |
4090a9c9 PS |
446 | if (format_config(display_opts, item, key_, |
447 | display_opts->default_value, &kvi) < 0) | |
eeaa24b9 | 448 | die(_("failed to format default config value: %s"), |
4090a9c9 | 449 | display_opts->default_value); |
eeaa24b9 TB |
450 | } |
451 | ||
00b347d3 | 452 | ret = !values.nr; |
9b25a0b5 | 453 | |
7acdd6f0 JK |
454 | for (i = 0; i < values.nr; i++) { |
455 | struct strbuf *buf = values.items + i; | |
35a7cfda | 456 | if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1) |
00b347d3 | 457 | fwrite(buf->buf, 1, buf->len, stdout); |
7acdd6f0 JK |
458 | strbuf_release(buf); |
459 | } | |
460 | free(values.items); | |
5f1a63e0 | 461 | |
97ed50f9 | 462 | free_strings: |
4ddba79d | 463 | free(key); |
fdfaaa1b PS |
464 | if (data.key_regexp) { |
465 | regfree(data.key_regexp); | |
466 | free(data.key_regexp); | |
35998c89 | 467 | } |
4ff8feb3 PS |
468 | if (data.regexp) { |
469 | regfree(data.regexp); | |
470 | free(data.regexp); | |
4ddba79d JS |
471 | } |
472 | ||
5f1a63e0 | 473 | return ret; |
4ddba79d | 474 | } |
1b1e59c5 | 475 | |
8868b1eb | 476 | static char *normalize_value(const char *key, const char *value, |
94c46930 | 477 | int type, struct key_value_info *kvi) |
db1696b8 | 478 | { |
db1696b8 FL |
479 | if (!value) |
480 | return NULL; | |
481 | ||
0a8950be | 482 | if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE) |
1349484e MM |
483 | /* |
484 | * We don't do normalization for TYPE_PATH here: If | |
485 | * the path is like ~/foobar/, we prefer to store | |
486 | * "~/foobar/" in the config file, and to expand the ~ | |
487 | * when retrieving the value. | |
5f967424 | 488 | * Also don't do normalization for expiry dates. |
1349484e | 489 | */ |
3ec832c4 | 490 | return xstrdup(value); |
0a8950be | 491 | if (type == TYPE_INT) |
8868b1eb | 492 | return xstrfmt("%"PRId64, git_config_int64(key, value, kvi)); |
0a8950be | 493 | if (type == TYPE_BOOL) |
3ec832c4 | 494 | return xstrdup(git_config_bool(key, value) ? "true" : "false"); |
0a8950be | 495 | if (type == TYPE_BOOL_OR_INT) { |
3ec832c4 | 496 | int is_bool, v; |
8868b1eb | 497 | v = git_config_bool_or_int(key, value, kvi, &is_bool); |
3ec832c4 JK |
498 | if (!is_bool) |
499 | return xstrfmt("%d", v); | |
500 | else | |
501 | return xstrdup(v ? "true" : "false"); | |
db1696b8 | 502 | } |
dbd8c09b LS |
503 | if (type == TYPE_BOOL_OR_STR) { |
504 | int v = git_parse_maybe_bool(value); | |
505 | if (v < 0) | |
506 | return xstrdup(value); | |
507 | else | |
508 | return xstrdup(v ? "true" : "false"); | |
509 | } | |
63e2a0f8 TB |
510 | if (type == TYPE_COLOR) { |
511 | char v[COLOR_MAXLEN]; | |
512 | if (git_config_color(v, key, value)) | |
1d28ff4c | 513 | die(_("cannot parse color '%s'"), value); |
63e2a0f8 TB |
514 | |
515 | /* | |
516 | * The contents of `v` now contain an ANSI escape | |
517 | * sequence, not suitable for including within a | |
518 | * configuration file. Treat the above as a | |
519 | * "sanity-check", and return the given value, which we | |
520 | * know is representable as valid color code. | |
521 | */ | |
522 | return xstrdup(value); | |
523 | } | |
db1696b8 | 524 | |
50f08db5 | 525 | BUG("cannot normalize type %d", type); |
db1696b8 FL |
526 | } |
527 | ||
9c625343 PS |
528 | struct get_color_config_data { |
529 | int get_color_found; | |
530 | const char *get_color_slot; | |
531 | char parsed_color[COLOR_MAXLEN]; | |
532 | }; | |
9ce03522 | 533 | |
783a86c1 | 534 | static int git_get_color_config(const char *var, const char *value, |
a4e7e317 | 535 | const struct config_context *ctx UNUSED, |
9c625343 | 536 | void *cb) |
9ce03522 | 537 | { |
9c625343 PS |
538 | struct get_color_config_data *data = cb; |
539 | ||
540 | if (!strcmp(var, data->get_color_slot)) { | |
f769982d JH |
541 | if (!value) |
542 | config_error_nonbool(var); | |
9c625343 | 543 | if (color_parse(value, data->parsed_color) < 0) |
f6c5a296 | 544 | return -1; |
9c625343 | 545 | data->get_color_found = 1; |
9ce03522 JH |
546 | } |
547 | return 0; | |
548 | } | |
549 | ||
ddb103c2 PS |
550 | static void get_color(const struct config_location_options *opts, |
551 | const char *var, const char *def_color) | |
9ce03522 | 552 | { |
9c625343 PS |
553 | struct get_color_config_data data = { |
554 | .get_color_slot = var, | |
555 | .parsed_color[0] = '\0', | |
556 | }; | |
557 | ||
558 | config_with_options(git_get_color_config, &data, | |
ddb103c2 PS |
559 | &opts->source, the_repository, |
560 | &opts->options); | |
9ce03522 | 561 | |
9c625343 PS |
562 | if (!data.get_color_found && def_color) { |
563 | if (color_parse(def_color, data.parsed_color) < 0) | |
f6c5a296 JK |
564 | die(_("unable to parse default color value")); |
565 | } | |
9ce03522 | 566 | |
9c625343 | 567 | fputs(data.parsed_color, stdout); |
9ce03522 JH |
568 | } |
569 | ||
9c625343 PS |
570 | struct get_colorbool_config_data { |
571 | int get_colorbool_found; | |
572 | int get_diff_color_found; | |
573 | int get_color_ui_found; | |
574 | const char *get_colorbool_slot; | |
575 | }; | |
576 | ||
ef90d6d4 | 577 | static int git_get_colorbool_config(const char *var, const char *value, |
a4e7e317 | 578 | const struct config_context *ctx UNUSED, |
9c625343 | 579 | void *cb) |
0f6f5a40 | 580 | { |
9c625343 PS |
581 | struct get_colorbool_config_data *data = cb; |
582 | ||
583 | if (!strcmp(var, data->get_colorbool_slot)) | |
584 | data->get_colorbool_found = git_config_colorbool(var, value); | |
e269eb79 | 585 | else if (!strcmp(var, "diff.color")) |
9c625343 | 586 | data->get_diff_color_found = git_config_colorbool(var, value); |
e269eb79 | 587 | else if (!strcmp(var, "color.ui")) |
9c625343 | 588 | data->get_color_ui_found = git_config_colorbool(var, value); |
0f6f5a40 JH |
589 | return 0; |
590 | } | |
591 | ||
ddb103c2 PS |
592 | static int get_colorbool(const struct config_location_options *opts, |
593 | const char *var, int print) | |
0f6f5a40 | 594 | { |
9c625343 PS |
595 | struct get_colorbool_config_data data = { |
596 | .get_colorbool_slot = var, | |
597 | .get_colorbool_found = -1, | |
598 | .get_diff_color_found = -1, | |
599 | .get_color_ui_found = -1, | |
600 | }; | |
601 | ||
602 | config_with_options(git_get_colorbool_config, &data, | |
ddb103c2 PS |
603 | &opts->source, the_repository, |
604 | &opts->options); | |
0f6f5a40 | 605 | |
9c625343 PS |
606 | if (data.get_colorbool_found < 0) { |
607 | if (!strcmp(data.get_colorbool_slot, "color.diff")) | |
608 | data.get_colorbool_found = data.get_diff_color_found; | |
609 | if (data.get_colorbool_found < 0) | |
610 | data.get_colorbool_found = data.get_color_ui_found; | |
69243c2b JH |
611 | } |
612 | ||
9c625343 | 613 | if (data.get_colorbool_found < 0) |
b8612b4d | 614 | /* default value if none found in config */ |
9c625343 | 615 | data.get_colorbool_found = GIT_COLOR_AUTO; |
b8612b4d | 616 | |
9c625343 | 617 | data.get_colorbool_found = want_color(data.get_colorbool_found); |
daa0c3d9 | 618 | |
0e854a28 | 619 | if (print) { |
9c625343 | 620 | printf("%s\n", data.get_colorbool_found ? "true" : "false"); |
0f6f5a40 | 621 | return 0; |
0e854a28 | 622 | } else |
9c625343 | 623 | return data.get_colorbool_found ? 0 : 1; |
0f6f5a40 JH |
624 | } |
625 | ||
ddb103c2 | 626 | static void check_write(const struct git_config_source *source) |
1bc88819 | 627 | { |
ddb103c2 | 628 | if (!source->file && !startup_info->have_repository) |
1d28ff4c | 629 | die(_("not in a git directory")); |
638fa623 | 630 | |
ddb103c2 | 631 | if (source->use_stdin) |
1d28ff4c | 632 | die(_("writing to stdin is not supported")); |
3caec73b | 633 | |
ddb103c2 | 634 | if (source->blob) |
1d28ff4c | 635 | die(_("writing config blobs is not supported")); |
1bc88819 HV |
636 | } |
637 | ||
d4770964 JH |
638 | struct urlmatch_current_candidate_value { |
639 | char value_is_null; | |
640 | struct strbuf value; | |
26b66932 | 641 | struct key_value_info kvi; |
d4770964 JH |
642 | }; |
643 | ||
a4e7e317 | 644 | static int urlmatch_collect_fn(const char *var, const char *value, |
26b66932 | 645 | const struct config_context *ctx, |
a4e7e317 | 646 | void *cb) |
d4770964 JH |
647 | { |
648 | struct string_list *values = cb; | |
649 | struct string_list_item *item = string_list_insert(values, var); | |
650 | struct urlmatch_current_candidate_value *matched = item->util; | |
26b66932 | 651 | const struct key_value_info *kvi = ctx->kvi; |
d4770964 JH |
652 | |
653 | if (!matched) { | |
654 | matched = xmalloc(sizeof(*matched)); | |
655 | strbuf_init(&matched->value, 0); | |
656 | item->util = matched; | |
657 | } else { | |
658 | strbuf_reset(&matched->value); | |
659 | } | |
26b66932 | 660 | matched->kvi = *kvi; |
d4770964 JH |
661 | |
662 | if (value) { | |
663 | strbuf_addstr(&matched->value, value); | |
664 | matched->value_is_null = 0; | |
665 | } else { | |
666 | matched->value_is_null = 1; | |
667 | } | |
668 | return 0; | |
669 | } | |
670 | ||
ddb103c2 | 671 | static int get_urlmatch(const struct config_location_options *opts, |
c0c1e263 | 672 | const struct config_display_options *_display_opts, |
ddb103c2 | 673 | const char *var, const char *url) |
d4770964 | 674 | { |
27b30be6 | 675 | int ret; |
d4770964 | 676 | char *section_tail; |
c0c1e263 | 677 | struct config_display_options display_opts = *_display_opts; |
d4770964 | 678 | struct string_list_item *item; |
73ee449b | 679 | struct urlmatch_config config = URLMATCH_CONFIG_INIT; |
d4770964 JH |
680 | struct string_list values = STRING_LIST_INIT_DUP; |
681 | ||
682 | config.collect_fn = urlmatch_collect_fn; | |
683 | config.cascade_fn = NULL; | |
684 | config.cb = &values; | |
685 | ||
686 | if (!url_normalize(url, &config.url)) | |
6667a6ac | 687 | die("%s", config.url.err); |
d4770964 | 688 | |
88d5a6f6 | 689 | config.section = xstrdup_tolower(var); |
d4770964 JH |
690 | section_tail = strchr(config.section, '.'); |
691 | if (section_tail) { | |
692 | *section_tail = '\0'; | |
693 | config.key = section_tail + 1; | |
c0c1e263 | 694 | display_opts.show_keys = 0; |
d4770964 JH |
695 | } else { |
696 | config.key = NULL; | |
c0c1e263 | 697 | display_opts.show_keys = 1; |
d4770964 JH |
698 | } |
699 | ||
dc8441fd | 700 | config_with_options(urlmatch_config_entry, &config, |
ddb103c2 PS |
701 | &opts->source, the_repository, |
702 | &opts->options); | |
d4770964 | 703 | |
27b30be6 JK |
704 | ret = !values.nr; |
705 | ||
d4770964 JH |
706 | for_each_string_list_item(item, &values) { |
707 | struct urlmatch_current_candidate_value *matched = item->util; | |
d4770964 JH |
708 | struct strbuf buf = STRBUF_INIT; |
709 | ||
c0c1e263 | 710 | format_config(&display_opts, &buf, item->string, |
26b66932 GC |
711 | matched->value_is_null ? NULL : matched->value.buf, |
712 | &matched->kvi); | |
d4770964 | 713 | fwrite(buf.buf, 1, buf.len, stdout); |
d4770964 JH |
714 | strbuf_release(&buf); |
715 | ||
716 | strbuf_release(&matched->value); | |
717 | } | |
a41e8e74 | 718 | urlmatch_config_release(&config); |
d4770964 JH |
719 | string_list_clear(&values, 1); |
720 | free(config.url.url); | |
721 | ||
722 | free((void *)config.section); | |
27b30be6 | 723 | return ret; |
d4770964 JH |
724 | } |
725 | ||
9830534e MM |
726 | static char *default_user_config(void) |
727 | { | |
728 | struct strbuf buf = STRBUF_INIT; | |
729 | strbuf_addf(&buf, | |
730 | _("# This is Git's per-user configuration file.\n" | |
7e110524 | 731 | "[user]\n" |
9830534e | 732 | "# Please adapt and uncomment the following lines:\n" |
7e110524 | 733 | "# name = %s\n" |
9830534e MM |
734 | "# email = %s\n"), |
735 | ident_default_name(), | |
736 | ident_default_email()); | |
737 | return strbuf_detach(&buf, NULL); | |
738 | } | |
739 | ||
ddb103c2 PS |
740 | static void location_options_init(struct config_location_options *opts, |
741 | const char *prefix) | |
424a29c3 | 742 | { |
ddb103c2 PS |
743 | if (!opts->source.file) |
744 | opts->source.file = opts->file_to_free = | |
745 | xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); | |
746 | ||
747 | if (opts->use_global_config + opts->use_system_config + | |
748 | opts->use_local_config + opts->use_worktree_config + | |
749 | !!opts->source.file + !!opts->source.blob > 1) { | |
1d28ff4c | 750 | error(_("only one config file at a time")); |
a577d2f1 | 751 | exit(129); |
67052c9d FC |
752 | } |
753 | ||
9dda6b72 | 754 | if (!startup_info->have_repository) { |
ddb103c2 | 755 | if (opts->use_local_config) |
378fe5fc | 756 | die(_("--local can only be used inside a git repository")); |
ddb103c2 | 757 | if (opts->source.blob) |
378fe5fc | 758 | die(_("--blob can only be used inside a git repository")); |
ddb103c2 | 759 | if (opts->use_worktree_config) |
378fe5fc | 760 | die(_("--worktree can only be used inside a git repository")); |
378fe5fc | 761 | } |
17b8a2d6 | 762 | |
ddb103c2 PS |
763 | if (opts->source.file && |
764 | !strcmp(opts->source.file, "-")) { | |
765 | opts->source.file = NULL; | |
766 | opts->source.use_stdin = 1; | |
767 | opts->source.scope = CONFIG_SCOPE_COMMAND; | |
3caec73b KS |
768 | } |
769 | ||
ddb103c2 PS |
770 | if (opts->use_global_config) { |
771 | opts->source.file = opts->file_to_free = git_global_config(); | |
772 | if (!opts->source.file) | |
1cb3b92f KH |
773 | /* |
774 | * It is unknown if HOME/.gitconfig exists, so | |
775 | * we do not know if we should write to XDG | |
776 | * location; error out even if XDG_CONFIG_HOME | |
777 | * is set and points at a sane location. | |
778 | */ | |
1d28ff4c | 779 | die(_("$HOME not set")); |
ddb103c2 PS |
780 | opts->source.scope = CONFIG_SCOPE_GLOBAL; |
781 | } else if (opts->use_system_config) { | |
782 | opts->source.file = opts->file_to_free = git_system_config(); | |
783 | opts->source.scope = CONFIG_SCOPE_SYSTEM; | |
784 | } else if (opts->use_local_config) { | |
bba59f58 | 785 | opts->source.file = opts->file_to_free = repo_git_path(the_repository, "config"); |
ddb103c2 PS |
786 | opts->source.scope = CONFIG_SCOPE_LOCAL; |
787 | } else if (opts->use_worktree_config) { | |
03f2465b | 788 | struct worktree **worktrees = get_worktrees(); |
3867f6d6 | 789 | if (the_repository->repository_format_worktree_config) |
ddb103c2 | 790 | opts->source.file = opts->file_to_free = |
bba59f58 | 791 | repo_git_path(the_repository, "config.worktree"); |
58b284a2 NTND |
792 | else if (worktrees[0] && worktrees[1]) |
793 | die(_("--worktree cannot be used with multiple " | |
794 | "working trees unless the config\n" | |
795 | "extension worktreeConfig is enabled. " | |
796 | "Please read \"CONFIGURATION FILE\"\n" | |
797 | "section in \"git help worktree\" for details")); | |
798 | else | |
ddb103c2 | 799 | opts->source.file = opts->file_to_free = |
bba59f58 | 800 | repo_git_path(the_repository, "config"); |
ddb103c2 | 801 | opts->source.scope = CONFIG_SCOPE_LOCAL; |
58b284a2 | 802 | free_worktrees(worktrees); |
ddb103c2 PS |
803 | } else if (opts->source.file) { |
804 | if (!is_absolute_path(opts->source.file) && prefix) | |
805 | opts->source.file = opts->file_to_free = | |
806 | prefix_filename(prefix, opts->source.file); | |
807 | opts->source.scope = CONFIG_SCOPE_COMMAND; | |
808 | } else if (opts->source.blob) { | |
809 | opts->source.scope = CONFIG_SCOPE_COMMAND; | |
d64ec16c FC |
810 | } |
811 | ||
8c869812 | 812 | if (opts->respect_includes_opt == -1) |
ddb103c2 | 813 | opts->options.respect_includes = !opts->source.file; |
c48f4b37 | 814 | else |
8c869812 | 815 | opts->options.respect_includes = opts->respect_includes_opt; |
9dda6b72 | 816 | if (startup_info->have_repository) { |
661624a4 | 817 | opts->options.commondir = repo_get_common_dir(the_repository); |
246deeac | 818 | opts->options.git_dir = repo_get_git_dir(the_repository); |
dc8441fd | 819 | } |
9dda6b72 PS |
820 | } |
821 | ||
ddb103c2 PS |
822 | static void location_options_release(struct config_location_options *opts) |
823 | { | |
824 | free(opts->file_to_free); | |
825 | } | |
826 | ||
c0c1e263 PS |
827 | static void display_options_init(struct config_display_options *opts) |
828 | { | |
829 | if (opts->end_nul) { | |
830 | opts->term = '\0'; | |
831 | opts->delim = '\n'; | |
832 | opts->key_delim = '\n'; | |
fee37966 PS |
833 | } |
834 | } | |
835 | ||
6f33d8e2 KN |
836 | static int cmd_config_list(int argc, const char **argv, const char *prefix, |
837 | struct repository *repo UNUSED) | |
14970509 | 838 | { |
ddb103c2 | 839 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
c0c1e263 | 840 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; |
14970509 | 841 | struct option opts[] = { |
ddb103c2 | 842 | CONFIG_LOCATION_OPTIONS(location_opts), |
c0c1e263 | 843 | CONFIG_DISPLAY_OPTIONS(display_opts), |
14970509 | 844 | OPT_GROUP(N_("Other")), |
8c869812 PS |
845 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, |
846 | N_("respect include directives on lookup")), | |
14970509 PS |
847 | OPT_END(), |
848 | }; | |
849 | ||
850 | argc = parse_options(argc, argv, prefix, opts, builtin_config_list_usage, 0); | |
851 | check_argc(argc, 0, 0); | |
852 | ||
ddb103c2 | 853 | location_options_init(&location_opts, prefix); |
c0c1e263 | 854 | display_options_init(&display_opts); |
14970509 PS |
855 | |
856 | setup_auto_pager("config", 1); | |
857 | ||
c0c1e263 | 858 | if (config_with_options(show_all_config, &display_opts, |
ddb103c2 PS |
859 | &location_opts.source, the_repository, |
860 | &location_opts.options) < 0) { | |
861 | if (location_opts.source.file) | |
14970509 | 862 | die_errno(_("unable to read config file '%s'"), |
ddb103c2 | 863 | location_opts.source.file); |
14970509 PS |
864 | else |
865 | die(_("error processing config file(s)")); | |
866 | } | |
867 | ||
ddb103c2 | 868 | location_options_release(&location_opts); |
14970509 PS |
869 | return 0; |
870 | } | |
871 | ||
6f33d8e2 KN |
872 | static int cmd_config_get(int argc, const char **argv, const char *prefix, |
873 | struct repository *repo UNUSED) | |
4e513890 | 874 | { |
ddb103c2 | 875 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
c0c1e263 | 876 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; |
4e513890 PS |
877 | const char *value_pattern = NULL, *url = NULL; |
878 | int flags = 0; | |
35a7cfda | 879 | unsigned get_value_flags = 0; |
4e513890 | 880 | struct option opts[] = { |
ddb103c2 | 881 | CONFIG_LOCATION_OPTIONS(location_opts), |
4e513890 | 882 | OPT_GROUP(N_("Filter options")), |
35a7cfda PS |
883 | OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL), |
884 | OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP), | |
4e513890 PS |
885 | OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), |
886 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), | |
887 | OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")), | |
c0c1e263 | 888 | CONFIG_DISPLAY_OPTIONS(display_opts), |
4e513890 | 889 | OPT_GROUP(N_("Other")), |
8c869812 PS |
890 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, |
891 | N_("respect include directives on lookup")), | |
4090a9c9 PS |
892 | OPT_STRING(0, "default", &display_opts.default_value, |
893 | N_("value"), N_("use default value when missing entry")), | |
4e513890 PS |
894 | OPT_END(), |
895 | }; | |
999425cb | 896 | int ret; |
4e513890 PS |
897 | |
898 | argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage, | |
899 | PARSE_OPT_STOP_AT_NON_OPTION); | |
900 | check_argc(argc, 1, 1); | |
901 | ||
902 | if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) | |
903 | die(_("--fixed-value only applies with 'value-pattern'")); | |
35a7cfda PS |
904 | if (display_opts.default_value && |
905 | ((get_value_flags & GET_VALUE_ALL) || url)) | |
4e513890 | 906 | die(_("--default= cannot be used with --all or --url=")); |
35a7cfda PS |
907 | if (url && ((get_value_flags & GET_VALUE_ALL) || |
908 | (get_value_flags & GET_VALUE_KEY_REGEXP) || | |
909 | value_pattern)) | |
4e513890 PS |
910 | die(_("--url= cannot be used with --all, --regexp or --value")); |
911 | ||
ddb103c2 | 912 | location_options_init(&location_opts, prefix); |
c0c1e263 | 913 | display_options_init(&display_opts); |
4e513890 PS |
914 | |
915 | setup_auto_pager("config", 1); | |
916 | ||
917 | if (url) | |
c0c1e263 | 918 | ret = get_urlmatch(&location_opts, &display_opts, argv[0], url); |
999425cb | 919 | else |
35a7cfda PS |
920 | ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, |
921 | get_value_flags, flags); | |
999425cb | 922 | |
ddb103c2 | 923 | location_options_release(&location_opts); |
999425cb | 924 | return ret; |
4e513890 PS |
925 | } |
926 | ||
6f33d8e2 KN |
927 | static int cmd_config_set(int argc, const char **argv, const char *prefix, |
928 | struct repository *repo UNUSED) | |
00bbdde1 | 929 | { |
ddb103c2 | 930 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
00bbdde1 PS |
931 | const char *value_pattern = NULL, *comment_arg = NULL; |
932 | char *comment = NULL; | |
94c46930 | 933 | int flags = 0, append = 0, type = 0; |
00bbdde1 | 934 | struct option opts[] = { |
ddb103c2 | 935 | CONFIG_LOCATION_OPTIONS(location_opts), |
94c46930 | 936 | CONFIG_TYPE_OPTIONS(type), |
00bbdde1 PS |
937 | OPT_GROUP(N_("Filter")), |
938 | OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE), | |
939 | OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), | |
940 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), | |
941 | OPT_GROUP(N_("Other")), | |
942 | OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), | |
943 | OPT_BOOL(0, "append", &append, N_("add a new line without altering any existing values")), | |
944 | OPT_END(), | |
945 | }; | |
946 | struct key_value_info default_kvi = KVI_INIT; | |
947 | char *value; | |
948 | int ret; | |
949 | ||
950 | argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage, | |
951 | PARSE_OPT_STOP_AT_NON_OPTION); | |
00bbdde1 PS |
952 | check_argc(argc, 2, 2); |
953 | ||
954 | if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) | |
955 | die(_("--fixed-value only applies with --value=<pattern>")); | |
956 | if (append && value_pattern) | |
957 | die(_("--append cannot be used with --value=<pattern>")); | |
958 | if (append) | |
959 | value_pattern = CONFIG_REGEX_NONE; | |
960 | ||
961 | comment = git_config_prepare_comment_string(comment_arg); | |
962 | ||
ddb103c2 PS |
963 | location_options_init(&location_opts, prefix); |
964 | check_write(&location_opts.source); | |
00bbdde1 | 965 | |
94c46930 | 966 | value = normalize_value(argv[0], argv[1], type, &default_kvi); |
00bbdde1 PS |
967 | |
968 | if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) { | |
ddb103c2 | 969 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, |
00bbdde1 PS |
970 | argv[0], value, value_pattern, |
971 | comment, flags); | |
972 | } else { | |
ddb103c2 | 973 | ret = git_config_set_in_file_gently(location_opts.source.file, |
00bbdde1 PS |
974 | argv[0], comment, value); |
975 | if (ret == CONFIG_NOTHING_SET) | |
976 | error(_("cannot overwrite multiple values with a single value\n" | |
977 | " Use a regexp, --add or --replace-all to change %s."), argv[0]); | |
978 | } | |
979 | ||
ddb103c2 | 980 | location_options_release(&location_opts); |
00bbdde1 PS |
981 | free(comment); |
982 | free(value); | |
983 | return ret; | |
984 | } | |
985 | ||
6f33d8e2 KN |
986 | static int cmd_config_unset(int argc, const char **argv, const char *prefix, |
987 | struct repository *repo UNUSED) | |
95ea69c6 | 988 | { |
ddb103c2 | 989 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
95ea69c6 PS |
990 | const char *value_pattern = NULL; |
991 | int flags = 0; | |
992 | struct option opts[] = { | |
ddb103c2 | 993 | CONFIG_LOCATION_OPTIONS(location_opts), |
95ea69c6 PS |
994 | OPT_GROUP(N_("Filter")), |
995 | OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE), | |
996 | OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), | |
997 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), | |
998 | OPT_END(), | |
999 | }; | |
999425cb | 1000 | int ret; |
95ea69c6 PS |
1001 | |
1002 | argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage, | |
1003 | PARSE_OPT_STOP_AT_NON_OPTION); | |
95ea69c6 PS |
1004 | check_argc(argc, 1, 1); |
1005 | ||
1006 | if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) | |
1007 | die(_("--fixed-value only applies with 'value-pattern'")); | |
1008 | ||
ddb103c2 PS |
1009 | location_options_init(&location_opts, prefix); |
1010 | check_write(&location_opts.source); | |
95ea69c6 PS |
1011 | |
1012 | if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) | |
ddb103c2 | 1013 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, |
999425cb PS |
1014 | argv[0], NULL, value_pattern, |
1015 | NULL, flags); | |
95ea69c6 | 1016 | else |
ddb103c2 | 1017 | ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], |
999425cb PS |
1018 | NULL, NULL); |
1019 | ||
ddb103c2 | 1020 | location_options_release(&location_opts); |
999425cb | 1021 | return ret; |
95ea69c6 PS |
1022 | } |
1023 | ||
6f33d8e2 KN |
1024 | static int cmd_config_rename_section(int argc, const char **argv, const char *prefix, |
1025 | struct repository *repo UNUSED) | |
3418e96f | 1026 | { |
ddb103c2 | 1027 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
3418e96f | 1028 | struct option opts[] = { |
ddb103c2 | 1029 | CONFIG_LOCATION_OPTIONS(location_opts), |
3418e96f PS |
1030 | OPT_END(), |
1031 | }; | |
1032 | int ret; | |
1033 | ||
1034 | argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage, | |
1035 | PARSE_OPT_STOP_AT_NON_OPTION); | |
3418e96f PS |
1036 | check_argc(argc, 2, 2); |
1037 | ||
ddb103c2 PS |
1038 | location_options_init(&location_opts, prefix); |
1039 | check_write(&location_opts.source); | |
3418e96f | 1040 | |
76fc9906 PS |
1041 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
1042 | argv[0], argv[1]); | |
3418e96f | 1043 | if (ret < 0) |
999425cb | 1044 | goto out; |
3418e96f PS |
1045 | else if (!ret) |
1046 | die(_("no such section: %s"), argv[0]); | |
999425cb | 1047 | ret = 0; |
3418e96f | 1048 | |
999425cb | 1049 | out: |
ddb103c2 | 1050 | location_options_release(&location_opts); |
999425cb | 1051 | return ret; |
3418e96f PS |
1052 | } |
1053 | ||
6f33d8e2 KN |
1054 | static int cmd_config_remove_section(int argc, const char **argv, const char *prefix, |
1055 | struct repository *repo UNUSED) | |
15dad20c | 1056 | { |
ddb103c2 | 1057 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
15dad20c | 1058 | struct option opts[] = { |
ddb103c2 | 1059 | CONFIG_LOCATION_OPTIONS(location_opts), |
15dad20c PS |
1060 | OPT_END(), |
1061 | }; | |
1062 | int ret; | |
1063 | ||
1064 | argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage, | |
1065 | PARSE_OPT_STOP_AT_NON_OPTION); | |
15dad20c PS |
1066 | check_argc(argc, 1, 1); |
1067 | ||
ddb103c2 PS |
1068 | location_options_init(&location_opts, prefix); |
1069 | check_write(&location_opts.source); | |
15dad20c | 1070 | |
76fc9906 PS |
1071 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
1072 | argv[0], NULL); | |
15dad20c | 1073 | if (ret < 0) |
999425cb | 1074 | goto out; |
15dad20c PS |
1075 | else if (!ret) |
1076 | die(_("no such section: %s"), argv[0]); | |
999425cb | 1077 | ret = 0; |
15dad20c | 1078 | |
999425cb | 1079 | out: |
ddb103c2 | 1080 | location_options_release(&location_opts); |
999425cb | 1081 | return ret; |
15dad20c PS |
1082 | } |
1083 | ||
ddb103c2 | 1084 | static int show_editor(struct config_location_options *opts) |
3cbace5e PS |
1085 | { |
1086 | char *config_file; | |
1087 | ||
ddb103c2 | 1088 | if (!opts->source.file && !startup_info->have_repository) |
3cbace5e | 1089 | die(_("not in a git directory")); |
ddb103c2 | 1090 | if (opts->source.use_stdin) |
3cbace5e | 1091 | die(_("editing stdin is not supported")); |
ddb103c2 | 1092 | if (opts->source.blob) |
3cbace5e PS |
1093 | die(_("editing blobs is not supported")); |
1094 | git_config(git_default_config, NULL); | |
ddb103c2 PS |
1095 | config_file = opts->source.file ? |
1096 | xstrdup(opts->source.file) : | |
bba59f58 | 1097 | repo_git_path(the_repository, "config"); |
ddb103c2 | 1098 | if (opts->use_global_config) { |
3cbace5e PS |
1099 | int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); |
1100 | if (fd >= 0) { | |
1101 | char *content = default_user_config(); | |
1102 | write_str_in_full(fd, content); | |
1103 | free(content); | |
1104 | close(fd); | |
1105 | } | |
1106 | else if (errno != EEXIST) | |
1107 | die_errno(_("cannot create configuration file %s"), config_file); | |
1108 | } | |
1109 | launch_editor(config_file, NULL, NULL); | |
1110 | free(config_file); | |
1111 | ||
1112 | return 0; | |
1113 | } | |
1114 | ||
6f33d8e2 KN |
1115 | static int cmd_config_edit(int argc, const char **argv, const char *prefix, |
1116 | struct repository *repo UNUSED) | |
3cbace5e | 1117 | { |
ddb103c2 | 1118 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
3cbace5e | 1119 | struct option opts[] = { |
ddb103c2 | 1120 | CONFIG_LOCATION_OPTIONS(location_opts), |
3cbace5e PS |
1121 | OPT_END(), |
1122 | }; | |
ddb103c2 | 1123 | int ret; |
3cbace5e PS |
1124 | |
1125 | argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0); | |
3cbace5e PS |
1126 | check_argc(argc, 0, 0); |
1127 | ||
ddb103c2 PS |
1128 | location_options_init(&location_opts, prefix); |
1129 | check_write(&location_opts.source); | |
3cbace5e | 1130 | |
ddb103c2 PS |
1131 | ret = show_editor(&location_opts); |
1132 | location_options_release(&location_opts); | |
1133 | return ret; | |
3cbace5e PS |
1134 | } |
1135 | ||
0336d005 | 1136 | static int cmd_config_actions(int argc, const char **argv, const char *prefix) |
9dda6b72 | 1137 | { |
9cab5e80 PS |
1138 | enum { |
1139 | ACTION_GET = (1<<0), | |
1140 | ACTION_GET_ALL = (1<<1), | |
1141 | ACTION_GET_REGEXP = (1<<2), | |
1142 | ACTION_REPLACE_ALL = (1<<3), | |
1143 | ACTION_ADD = (1<<4), | |
1144 | ACTION_UNSET = (1<<5), | |
1145 | ACTION_UNSET_ALL = (1<<6), | |
1146 | ACTION_RENAME_SECTION = (1<<7), | |
1147 | ACTION_REMOVE_SECTION = (1<<8), | |
1148 | ACTION_LIST = (1<<9), | |
1149 | ACTION_EDIT = (1<<10), | |
1150 | ACTION_SET = (1<<11), | |
1151 | ACTION_SET_ALL = (1<<12), | |
1152 | ACTION_GET_COLOR = (1<<13), | |
1153 | ACTION_GET_COLORBOOL = (1<<14), | |
1154 | ACTION_GET_URLMATCH = (1<<15), | |
1155 | }; | |
ddb103c2 | 1156 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; |
c0c1e263 | 1157 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; |
7d5387e2 PS |
1158 | const char *comment_arg = NULL; |
1159 | int actions = 0; | |
ab8bac8b | 1160 | unsigned flags = 0; |
7d5387e2 | 1161 | struct option opts[] = { |
ddb103c2 | 1162 | CONFIG_LOCATION_OPTIONS(location_opts), |
7d5387e2 PS |
1163 | OPT_GROUP(N_("Action")), |
1164 | OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET), | |
1165 | OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL), | |
1166 | OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP), | |
1167 | OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), | |
1168 | OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL), | |
1169 | OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), | |
1170 | OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET), | |
1171 | OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL), | |
1172 | OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), | |
1173 | OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), | |
1174 | OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST), | |
1175 | OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), | |
1176 | OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR), | |
1177 | OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL), | |
c0c1e263 | 1178 | CONFIG_DISPLAY_OPTIONS(display_opts), |
7d5387e2 | 1179 | OPT_GROUP(N_("Other")), |
4090a9c9 PS |
1180 | OPT_STRING(0, "default", &display_opts.default_value, |
1181 | N_("value"), N_("with --get, use default value when missing entry")), | |
7d5387e2 | 1182 | OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), |
ab8bac8b | 1183 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), |
8c869812 PS |
1184 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, |
1185 | N_("respect include directives on lookup")), | |
7d5387e2 PS |
1186 | OPT_END(), |
1187 | }; | |
9dda6b72 | 1188 | char *value = NULL, *comment = NULL; |
9dda6b72 PS |
1189 | int ret = 0; |
1190 | struct key_value_info default_kvi = KVI_INIT; | |
1191 | ||
7d5387e2 | 1192 | argc = parse_options(argc, argv, prefix, opts, |
9dda6b72 PS |
1193 | builtin_config_usage, |
1194 | PARSE_OPT_STOP_AT_NON_OPTION); | |
1195 | ||
ddb103c2 | 1196 | location_options_init(&location_opts, prefix); |
c0c1e263 | 1197 | display_options_init(&display_opts); |
d64ec16c | 1198 | |
94c46930 | 1199 | if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) { |
1d28ff4c | 1200 | error(_("--get-color and variable type are incoherent")); |
a577d2f1 | 1201 | exit(129); |
c2387358 FC |
1202 | } |
1203 | ||
d64ec16c FC |
1204 | if (actions == 0) |
1205 | switch (argc) { | |
1206 | case 1: actions = ACTION_GET; break; | |
1207 | case 2: actions = ACTION_SET; break; | |
1208 | case 3: actions = ACTION_SET_ALL; break; | |
1209 | default: | |
a577d2f1 PS |
1210 | error(_("no action specified")); |
1211 | exit(129); | |
db1696b8 | 1212 | } |
c0c1e263 | 1213 | if (display_opts.omit_values && |
578625fa | 1214 | !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) { |
1d28ff4c | 1215 | error(_("--name-only is only applicable to --list or --get-regexp")); |
a577d2f1 | 1216 | exit(129); |
578625fa | 1217 | } |
70bd879a | 1218 | |
c0c1e263 | 1219 | if (display_opts.show_origin && !(actions & |
70bd879a | 1220 | (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) { |
1d28ff4c NTND |
1221 | error(_("--show-origin is only applicable to --get, --get-all, " |
1222 | "--get-regexp, and --list")); | |
a577d2f1 | 1223 | exit(129); |
70bd879a LS |
1224 | } |
1225 | ||
4090a9c9 | 1226 | if (display_opts.default_value && !(actions & ACTION_GET)) { |
1d28ff4c | 1227 | error(_("--default is only applicable to --get")); |
a577d2f1 | 1228 | exit(129); |
eeaa24b9 TB |
1229 | } |
1230 | ||
a78b4629 | 1231 | if (comment_arg && |
fbad334d JH |
1232 | !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) { |
1233 | error(_("--comment is only applicable to add/set/replace operations")); | |
a577d2f1 | 1234 | exit(129); |
42d5c033 RS |
1235 | } |
1236 | ||
fda43942 | 1237 | /* check usage of --fixed-value */ |
ab8bac8b | 1238 | if (flags & CONFIG_FLAGS_FIXED_VALUE) { |
fda43942 DS |
1239 | int allowed_usage = 0; |
1240 | ||
1241 | switch (actions) { | |
1242 | /* git config --get <name> <value-pattern> */ | |
1243 | case ACTION_GET: | |
1244 | /* git config --get-all <name> <value-pattern> */ | |
1245 | case ACTION_GET_ALL: | |
1246 | /* git config --get-regexp <name-pattern> <value-pattern> */ | |
1247 | case ACTION_GET_REGEXP: | |
1248 | /* git config --unset <name> <value-pattern> */ | |
1249 | case ACTION_UNSET: | |
1250 | /* git config --unset-all <name> <value-pattern> */ | |
1251 | case ACTION_UNSET_ALL: | |
1252 | allowed_usage = argc > 1 && !!argv[1]; | |
1253 | break; | |
1254 | ||
1255 | /* git config <name> <value> <value-pattern> */ | |
1256 | case ACTION_SET_ALL: | |
1257 | /* git config --replace-all <name> <value> <value-pattern> */ | |
1258 | case ACTION_REPLACE_ALL: | |
1259 | allowed_usage = argc > 2 && !!argv[2]; | |
1260 | break; | |
1261 | ||
1262 | /* other options don't allow --fixed-value */ | |
1263 | } | |
1264 | ||
1265 | if (!allowed_usage) { | |
1266 | error(_("--fixed-value only applies with 'value-pattern'")); | |
a577d2f1 | 1267 | exit(129); |
fda43942 DS |
1268 | } |
1269 | } | |
1270 | ||
a78b4629 | 1271 | comment = git_config_prepare_comment_string(comment_arg); |
fbad334d | 1272 | |
9cab5e80 PS |
1273 | /* |
1274 | * The following actions may produce more than one line of output and | |
1275 | * should therefore be paged. | |
1276 | */ | |
1277 | if (actions & (ACTION_LIST | ACTION_GET_ALL | ACTION_GET_REGEXP | ACTION_GET_URLMATCH)) | |
c0e9f5be | 1278 | setup_auto_pager("config", 1); |
32888b8f | 1279 | |
d64ec16c | 1280 | if (actions == ACTION_LIST) { |
225a9caf | 1281 | check_argc(argc, 0, 0); |
c0c1e263 | 1282 | if (config_with_options(show_all_config, &display_opts, |
ddb103c2 PS |
1283 | &location_opts.source, the_repository, |
1284 | &location_opts.options) < 0) { | |
1285 | if (location_opts.source.file) | |
1d28ff4c | 1286 | die_errno(_("unable to read config file '%s'"), |
ddb103c2 | 1287 | location_opts.source.file); |
d64ec16c | 1288 | else |
1d28ff4c | 1289 | die(_("error processing config file(s)")); |
db1696b8 | 1290 | } |
1b1e59c5 | 1291 | } |
d64ec16c | 1292 | else if (actions == ACTION_EDIT) { |
ddb103c2 | 1293 | ret = show_editor(&location_opts); |
d64ec16c FC |
1294 | } |
1295 | else if (actions == ACTION_SET) { | |
ddb103c2 | 1296 | check_write(&location_opts.source); |
d64ec16c | 1297 | check_argc(argc, 2, 2); |
94c46930 | 1298 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
ddb103c2 | 1299 | ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value); |
5a2df368 | 1300 | if (ret == CONFIG_NOTHING_SET) |
ccf63801 VA |
1301 | error(_("cannot overwrite multiple values with a single value\n" |
1302 | " Use a regexp, --add or --replace-all to change %s."), argv[0]); | |
d64ec16c FC |
1303 | } |
1304 | else if (actions == ACTION_SET_ALL) { | |
ddb103c2 | 1305 | check_write(&location_opts.source); |
d64ec16c | 1306 | check_argc(argc, 2, 3); |
94c46930 | 1307 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
ddb103c2 | 1308 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, |
ac95f5d3 | 1309 | argv[0], value, argv[2], |
42d5c033 | 1310 | comment, flags); |
d64ec16c FC |
1311 | } |
1312 | else if (actions == ACTION_ADD) { | |
ddb103c2 | 1313 | check_write(&location_opts.source); |
d64ec16c | 1314 | check_argc(argc, 2, 2); |
94c46930 | 1315 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
ddb103c2 | 1316 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, |
ac95f5d3 ÆAB |
1317 | argv[0], value, |
1318 | CONFIG_REGEX_NONE, | |
42d5c033 | 1319 | comment, flags); |
d64ec16c FC |
1320 | } |
1321 | else if (actions == ACTION_REPLACE_ALL) { | |
ddb103c2 | 1322 | check_write(&location_opts.source); |
d64ec16c | 1323 | check_argc(argc, 2, 3); |
94c46930 | 1324 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); |
ddb103c2 | 1325 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, |
ac95f5d3 | 1326 | argv[0], value, argv[2], |
42d5c033 | 1327 | comment, flags | CONFIG_FLAGS_MULTI_REPLACE); |
d64ec16c FC |
1328 | } |
1329 | else if (actions == ACTION_GET) { | |
1330 | check_argc(argc, 1, 2); | |
35a7cfda PS |
1331 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], |
1332 | 0, flags); | |
d64ec16c FC |
1333 | } |
1334 | else if (actions == ACTION_GET_ALL) { | |
d64ec16c | 1335 | check_argc(argc, 1, 2); |
35a7cfda PS |
1336 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], |
1337 | GET_VALUE_ALL, flags); | |
d64ec16c FC |
1338 | } |
1339 | else if (actions == ACTION_GET_REGEXP) { | |
c0c1e263 | 1340 | display_opts.show_keys = 1; |
d64ec16c | 1341 | check_argc(argc, 1, 2); |
35a7cfda PS |
1342 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], |
1343 | GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags); | |
d64ec16c | 1344 | } |
d4770964 JH |
1345 | else if (actions == ACTION_GET_URLMATCH) { |
1346 | check_argc(argc, 2, 2); | |
c0c1e263 | 1347 | ret = get_urlmatch(&location_opts, &display_opts, argv[0], argv[1]); |
d4770964 | 1348 | } |
d64ec16c | 1349 | else if (actions == ACTION_UNSET) { |
ddb103c2 | 1350 | check_write(&location_opts.source); |
d64ec16c FC |
1351 | check_argc(argc, 1, 2); |
1352 | if (argc == 2) | |
ddb103c2 | 1353 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, |
999425cb PS |
1354 | argv[0], NULL, argv[1], |
1355 | NULL, flags); | |
d64ec16c | 1356 | else |
ddb103c2 | 1357 | ret = git_config_set_in_file_gently(location_opts.source.file, |
999425cb | 1358 | argv[0], NULL, NULL); |
d64ec16c FC |
1359 | } |
1360 | else if (actions == ACTION_UNSET_ALL) { | |
ddb103c2 | 1361 | check_write(&location_opts.source); |
d64ec16c | 1362 | check_argc(argc, 1, 2); |
ddb103c2 | 1363 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, |
999425cb PS |
1364 | argv[0], NULL, argv[1], |
1365 | NULL, flags | CONFIG_FLAGS_MULTI_REPLACE); | |
d64ec16c FC |
1366 | } |
1367 | else if (actions == ACTION_RENAME_SECTION) { | |
ddb103c2 | 1368 | check_write(&location_opts.source); |
d64ec16c | 1369 | check_argc(argc, 2, 2); |
76fc9906 PS |
1370 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
1371 | argv[0], argv[1]); | |
d64ec16c | 1372 | if (ret < 0) |
999425cb | 1373 | goto out; |
ac95f5d3 | 1374 | else if (!ret) |
1d28ff4c | 1375 | die(_("no such section: %s"), argv[0]); |
ac95f5d3 ÆAB |
1376 | else |
1377 | ret = 0; | |
d64ec16c FC |
1378 | } |
1379 | else if (actions == ACTION_REMOVE_SECTION) { | |
ddb103c2 | 1380 | check_write(&location_opts.source); |
d64ec16c | 1381 | check_argc(argc, 1, 1); |
76fc9906 PS |
1382 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, |
1383 | argv[0], NULL); | |
d64ec16c | 1384 | if (ret < 0) |
999425cb | 1385 | goto out; |
ac95f5d3 | 1386 | else if (!ret) |
1d28ff4c | 1387 | die(_("no such section: %s"), argv[0]); |
ac95f5d3 ÆAB |
1388 | else |
1389 | ret = 0; | |
d64ec16c FC |
1390 | } |
1391 | else if (actions == ACTION_GET_COLOR) { | |
d0e08d62 | 1392 | check_argc(argc, 1, 2); |
ddb103c2 | 1393 | get_color(&location_opts, argv[0], argv[1]); |
d64ec16c FC |
1394 | } |
1395 | else if (actions == ACTION_GET_COLORBOOL) { | |
d0e08d62 JK |
1396 | check_argc(argc, 1, 2); |
1397 | if (argc == 2) | |
1398 | color_stdout_is_tty = git_config_bool("command line", argv[1]); | |
ddb103c2 | 1399 | ret = get_colorbool(&location_opts, argv[0], argc == 2); |
d64ec16c FC |
1400 | } |
1401 | ||
999425cb | 1402 | out: |
ddb103c2 | 1403 | location_options_release(&location_opts); |
a78b4629 | 1404 | free(comment); |
ac95f5d3 ÆAB |
1405 | free(value); |
1406 | return ret; | |
1b1e59c5 | 1407 | } |
0336d005 | 1408 | |
9b1cb507 JC |
1409 | int cmd_config(int argc, |
1410 | const char **argv, | |
1411 | const char *prefix, | |
6f33d8e2 | 1412 | struct repository *repo) |
0336d005 | 1413 | { |
8b908f9d PS |
1414 | parse_opt_subcommand_fn *subcommand = NULL; |
1415 | struct option subcommand_opts[] = { | |
1416 | OPT_SUBCOMMAND("list", &subcommand, cmd_config_list), | |
1417 | OPT_SUBCOMMAND("get", &subcommand, cmd_config_get), | |
1418 | OPT_SUBCOMMAND("set", &subcommand, cmd_config_set), | |
1419 | OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset), | |
1420 | OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section), | |
1421 | OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section), | |
1422 | OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit), | |
1423 | OPT_END(), | |
1424 | }; | |
1425 | ||
0336d005 PS |
1426 | /* |
1427 | * This is somewhat hacky: we first parse the command line while | |
1428 | * keeping all args intact in order to determine whether a subcommand | |
1429 | * has been specified. If so, we re-parse it a second time, but this | |
1430 | * time we drop KEEP_ARGV0. This is so that we don't munge the command | |
1431 | * line in case no subcommand was given, which would otherwise confuse | |
1432 | * us when parsing the legacy-style modes that don't use subcommands. | |
1433 | */ | |
8b908f9d | 1434 | argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, |
0336d005 PS |
1435 | PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT); |
1436 | if (subcommand) { | |
8b908f9d | 1437 | argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, |
0336d005 | 1438 | PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT); |
6f33d8e2 | 1439 | return subcommand(argc, argv, prefix, repo); |
0336d005 PS |
1440 | } |
1441 | ||
1442 | return cmd_config_actions(argc, argv, prefix); | |
1443 | } |