]>
Commit | Line | Data |
---|---|---|
1 | #define USE_THE_REPOSITORY_VARIABLE | |
2 | #include "builtin.h" | |
3 | #include "abspath.h" | |
4 | #include "config.h" | |
5 | #include "color.h" | |
6 | #include "editor.h" | |
7 | #include "environment.h" | |
8 | #include "gettext.h" | |
9 | #include "ident.h" | |
10 | #include "parse-options.h" | |
11 | #include "urlmatch.h" | |
12 | #include "path.h" | |
13 | #include "quote.h" | |
14 | #include "setup.h" | |
15 | #include "strbuf.h" | |
16 | #include "worktree.h" | |
17 | ||
18 | static const char *const builtin_config_usage[] = { | |
19 | N_("git config list [<file-option>] [<display-option>] [--includes]"), | |
20 | N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp] [--value=<value>] [--fixed-value] [--default=<default>] <name>"), | |
21 | N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"), | |
22 | N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name>"), | |
23 | N_("git config rename-section [<file-option>] <old-name> <new-name>"), | |
24 | N_("git config remove-section [<file-option>] <name>"), | |
25 | N_("git config edit [<file-option>]"), | |
26 | N_("git config [<file-option>] --get-colorbool <name> [<stdout-is-tty>]"), | |
27 | NULL | |
28 | }; | |
29 | ||
30 | static const char *const builtin_config_list_usage[] = { | |
31 | N_("git config list [<file-option>] [<display-option>] [--includes]"), | |
32 | NULL | |
33 | }; | |
34 | ||
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 | ||
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 | ||
45 | static const char *const builtin_config_unset_usage[] = { | |
46 | N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name>"), | |
47 | NULL | |
48 | }; | |
49 | ||
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 | ||
55 | static const char *const builtin_config_remove_section_usage[] = { | |
56 | N_("git config remove-section [<file-option>] <name>"), | |
57 | NULL | |
58 | }; | |
59 | ||
60 | static const char *const builtin_config_edit_usage[] = { | |
61 | N_("git config edit [<file-option>]"), | |
62 | NULL | |
63 | }; | |
64 | ||
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; | |
82 | int respect_includes_opt; | |
83 | }; | |
84 | #define CONFIG_LOCATION_OPTIONS_INIT { \ | |
85 | .respect_includes_opt = -1, \ | |
86 | } | |
87 | ||
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 | ||
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)")), \ | |
104 | OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \ | |
105 | CONFIG_TYPE_OPTIONS(opts.type) | |
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; | |
113 | int type; | |
114 | char *default_value; | |
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 | ||
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 | |
131 | #define TYPE_COLOR 6 | |
132 | #define TYPE_BOOL_OR_STR 7 | |
133 | ||
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 | } | |
144 | ||
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; | |
167 | else if (!strcmp(arg, "bool-or-str")) | |
168 | new_type = TYPE_BOOL_OR_STR; | |
169 | else if (!strcmp(arg, "path")) | |
170 | new_type = TYPE_PATH; | |
171 | else if (!strcmp(arg, "expiry-date")) | |
172 | new_type = TYPE_EXPIRY_DATE; | |
173 | else if (!strcmp(arg, "color")) | |
174 | new_type = TYPE_COLOR; | |
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 | */ | |
188 | error(_("only one type at a time")); | |
189 | exit(129); | |
190 | } | |
191 | *to_type = new_type; | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static void check_argc(int argc, int min, int max) | |
197 | { | |
198 | if (argc >= min && argc <= max) | |
199 | return; | |
200 | if (min == max) | |
201 | error(_("wrong number of arguments, should be %d"), min); | |
202 | else | |
203 | error(_("wrong number of arguments, should be from %d to %d"), | |
204 | min, max); | |
205 | exit(129); | |
206 | } | |
207 | ||
208 | static void show_config_origin(const struct config_display_options *opts, | |
209 | const struct key_value_info *kvi, | |
210 | struct strbuf *buf) | |
211 | { | |
212 | const char term = opts->end_nul ? '\0' : '\t'; | |
213 | ||
214 | strbuf_addstr(buf, config_origin_type_name(kvi->origin_type)); | |
215 | strbuf_addch(buf, ':'); | |
216 | if (opts->end_nul) | |
217 | strbuf_addstr(buf, kvi->filename ? kvi->filename : ""); | |
218 | else | |
219 | quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0); | |
220 | strbuf_addch(buf, term); | |
221 | } | |
222 | ||
223 | static void show_config_scope(const struct config_display_options *opts, | |
224 | const struct key_value_info *kvi, | |
225 | struct strbuf *buf) | |
226 | { | |
227 | const char term = opts->end_nul ? '\0' : '\t'; | |
228 | const char *scope = config_scope_name(kvi->scope); | |
229 | ||
230 | strbuf_addstr(buf, N_(scope)); | |
231 | strbuf_addch(buf, term); | |
232 | } | |
233 | ||
234 | static int show_all_config(const char *key_, const char *value_, | |
235 | const struct config_context *ctx, | |
236 | void *cb) | |
237 | { | |
238 | const struct config_display_options *opts = cb; | |
239 | const struct key_value_info *kvi = ctx->kvi; | |
240 | ||
241 | if (opts->show_origin || opts->show_scope) { | |
242 | struct strbuf buf = STRBUF_INIT; | |
243 | if (opts->show_scope) | |
244 | show_config_scope(opts, kvi, &buf); | |
245 | if (opts->show_origin) | |
246 | show_config_origin(opts, kvi, &buf); | |
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 | } | |
251 | if (!opts->omit_values && value_) | |
252 | printf("%s%c%s%c", key_, opts->delim, value_, opts->term); | |
253 | else | |
254 | printf("%s%c", key_, opts->term); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | struct strbuf_list { | |
259 | struct strbuf *items; | |
260 | int nr; | |
261 | int alloc; | |
262 | }; | |
263 | ||
264 | static int format_config(const struct config_display_options *opts, | |
265 | struct strbuf *buf, const char *key_, | |
266 | const char *value_, const struct key_value_info *kvi) | |
267 | { | |
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) | |
273 | strbuf_addstr(buf, key_); | |
274 | if (!opts->omit_values) { | |
275 | if (opts->show_keys) | |
276 | strbuf_addch(buf, opts->key_delim); | |
277 | ||
278 | if (opts->type == TYPE_INT) | |
279 | strbuf_addf(buf, "%"PRId64, | |
280 | git_config_int64(key_, value_ ? value_ : "", kvi)); | |
281 | else if (opts->type == TYPE_BOOL) | |
282 | strbuf_addstr(buf, git_config_bool(key_, value_) ? | |
283 | "true" : "false"); | |
284 | else if (opts->type == TYPE_BOOL_OR_INT) { | |
285 | int is_bool, v; | |
286 | v = git_config_bool_or_int(key_, value_, kvi, | |
287 | &is_bool); | |
288 | if (is_bool) | |
289 | strbuf_addstr(buf, v ? "true" : "false"); | |
290 | else | |
291 | strbuf_addf(buf, "%d", v); | |
292 | } else if (opts->type == TYPE_BOOL_OR_STR) { | |
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"); | |
298 | } else if (opts->type == TYPE_PATH) { | |
299 | char *v; | |
300 | if (git_config_pathname(&v, key_, value_) < 0) | |
301 | return -1; | |
302 | strbuf_addstr(buf, v); | |
303 | free((char *)v); | |
304 | } else if (opts->type == TYPE_EXPIRY_DATE) { | |
305 | timestamp_t t; | |
306 | if (git_config_expiry_date(&t, key_, value_) < 0) | |
307 | return -1; | |
308 | strbuf_addf(buf, "%"PRItime, t); | |
309 | } else if (opts->type == TYPE_COLOR) { | |
310 | char v[COLOR_MAXLEN]; | |
311 | if (git_config_color(v, key_, value_) < 0) | |
312 | return -1; | |
313 | strbuf_addstr(buf, v); | |
314 | } else if (value_) { | |
315 | strbuf_addstr(buf, value_); | |
316 | } else { | |
317 | /* Just show the key name; back out delimiter */ | |
318 | if (opts->show_keys) | |
319 | strbuf_setlen(buf, buf->len - 1); | |
320 | } | |
321 | } | |
322 | strbuf_addch(buf, opts->term); | |
323 | return 0; | |
324 | } | |
325 | ||
326 | #define GET_VALUE_ALL (1 << 0) | |
327 | #define GET_VALUE_KEY_REGEXP (1 << 1) | |
328 | ||
329 | struct collect_config_data { | |
330 | const struct config_display_options *display_opts; | |
331 | struct strbuf_list *values; | |
332 | const char *value_pattern; | |
333 | const char *key; | |
334 | regex_t *regexp; | |
335 | regex_t *key_regexp; | |
336 | int do_not_match; | |
337 | unsigned get_value_flags; | |
338 | unsigned flags; | |
339 | }; | |
340 | ||
341 | static int collect_config(const char *key_, const char *value_, | |
342 | const struct config_context *ctx, void *cb) | |
343 | { | |
344 | struct collect_config_data *data = cb; | |
345 | struct strbuf_list *values = data->values; | |
346 | const struct key_value_info *kvi = ctx->kvi; | |
347 | ||
348 | if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) && | |
349 | strcmp(key_, data->key)) | |
350 | return 0; | |
351 | if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) && | |
352 | regexec(data->key_regexp, key_, 0, NULL, 0)) | |
353 | return 0; | |
354 | if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) && | |
355 | strcmp(data->value_pattern, (value_?value_:""))) | |
356 | return 0; | |
357 | if (data->regexp && | |
358 | (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0))) | |
359 | return 0; | |
360 | ||
361 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); | |
362 | strbuf_init(&values->items[values->nr], 0); | |
363 | ||
364 | return format_config(data->display_opts, &values->items[values->nr++], | |
365 | key_, value_, kvi); | |
366 | } | |
367 | ||
368 | static int get_value(const struct config_location_options *opts, | |
369 | const struct config_display_options *display_opts, | |
370 | const char *key_, const char *regex_, | |
371 | unsigned get_value_flags, unsigned flags) | |
372 | { | |
373 | int ret = CONFIG_GENERIC_ERROR; | |
374 | struct strbuf_list values = {NULL}; | |
375 | struct collect_config_data data = { | |
376 | .display_opts = display_opts, | |
377 | .values = &values, | |
378 | .get_value_flags = get_value_flags, | |
379 | .flags = flags, | |
380 | }; | |
381 | char *key = NULL; | |
382 | int i; | |
383 | ||
384 | if (get_value_flags & GET_VALUE_KEY_REGEXP) { | |
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 | ||
401 | data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); | |
402 | if (regcomp(data.key_regexp, key, REG_EXTENDED)) { | |
403 | error(_("invalid key pattern: %s"), key_); | |
404 | FREE_AND_NULL(data.key_regexp); | |
405 | ret = CONFIG_INVALID_PATTERN; | |
406 | goto free_strings; | |
407 | } | |
408 | } else { | |
409 | if (git_config_parse_key(key_, &key, NULL)) { | |
410 | ret = CONFIG_INVALID_KEY; | |
411 | goto free_strings; | |
412 | } | |
413 | ||
414 | data.key = key; | |
415 | } | |
416 | ||
417 | if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE)) | |
418 | data.value_pattern = regex_; | |
419 | else if (regex_) { | |
420 | if (regex_[0] == '!') { | |
421 | data.do_not_match = 1; | |
422 | regex_++; | |
423 | } | |
424 | ||
425 | data.regexp = (regex_t*)xmalloc(sizeof(regex_t)); | |
426 | if (regcomp(data.regexp, regex_, REG_EXTENDED)) { | |
427 | error(_("invalid pattern: %s"), regex_); | |
428 | FREE_AND_NULL(data.regexp); | |
429 | ret = CONFIG_INVALID_PATTERN; | |
430 | goto free_strings; | |
431 | } | |
432 | } | |
433 | ||
434 | config_with_options(collect_config, &data, | |
435 | &opts->source, the_repository, | |
436 | &opts->options); | |
437 | ||
438 | if (!values.nr && display_opts->default_value) { | |
439 | struct key_value_info kvi = KVI_INIT; | |
440 | struct strbuf *item; | |
441 | ||
442 | kvi_from_param(&kvi); | |
443 | ALLOC_GROW(values.items, values.nr + 1, values.alloc); | |
444 | item = &values.items[values.nr++]; | |
445 | strbuf_init(item, 0); | |
446 | if (format_config(display_opts, item, key_, | |
447 | display_opts->default_value, &kvi) < 0) | |
448 | die(_("failed to format default config value: %s"), | |
449 | display_opts->default_value); | |
450 | } | |
451 | ||
452 | ret = !values.nr; | |
453 | ||
454 | for (i = 0; i < values.nr; i++) { | |
455 | struct strbuf *buf = values.items + i; | |
456 | if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1) | |
457 | fwrite(buf->buf, 1, buf->len, stdout); | |
458 | strbuf_release(buf); | |
459 | } | |
460 | free(values.items); | |
461 | ||
462 | free_strings: | |
463 | free(key); | |
464 | if (data.key_regexp) { | |
465 | regfree(data.key_regexp); | |
466 | free(data.key_regexp); | |
467 | } | |
468 | if (data.regexp) { | |
469 | regfree(data.regexp); | |
470 | free(data.regexp); | |
471 | } | |
472 | ||
473 | return ret; | |
474 | } | |
475 | ||
476 | static char *normalize_value(const char *key, const char *value, | |
477 | int type, struct key_value_info *kvi) | |
478 | { | |
479 | if (!value) | |
480 | return NULL; | |
481 | ||
482 | if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE) | |
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. | |
488 | * Also don't do normalization for expiry dates. | |
489 | */ | |
490 | return xstrdup(value); | |
491 | if (type == TYPE_INT) | |
492 | return xstrfmt("%"PRId64, git_config_int64(key, value, kvi)); | |
493 | if (type == TYPE_BOOL) | |
494 | return xstrdup(git_config_bool(key, value) ? "true" : "false"); | |
495 | if (type == TYPE_BOOL_OR_INT) { | |
496 | int is_bool, v; | |
497 | v = git_config_bool_or_int(key, value, kvi, &is_bool); | |
498 | if (!is_bool) | |
499 | return xstrfmt("%d", v); | |
500 | else | |
501 | return xstrdup(v ? "true" : "false"); | |
502 | } | |
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 | } | |
510 | if (type == TYPE_COLOR) { | |
511 | char v[COLOR_MAXLEN]; | |
512 | if (git_config_color(v, key, value)) | |
513 | die(_("cannot parse color '%s'"), value); | |
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 | } | |
524 | ||
525 | BUG("cannot normalize type %d", type); | |
526 | } | |
527 | ||
528 | struct get_color_config_data { | |
529 | int get_color_found; | |
530 | const char *get_color_slot; | |
531 | char parsed_color[COLOR_MAXLEN]; | |
532 | }; | |
533 | ||
534 | static int git_get_color_config(const char *var, const char *value, | |
535 | const struct config_context *ctx UNUSED, | |
536 | void *cb) | |
537 | { | |
538 | struct get_color_config_data *data = cb; | |
539 | ||
540 | if (!strcmp(var, data->get_color_slot)) { | |
541 | if (!value) | |
542 | config_error_nonbool(var); | |
543 | if (color_parse(value, data->parsed_color) < 0) | |
544 | return -1; | |
545 | data->get_color_found = 1; | |
546 | } | |
547 | return 0; | |
548 | } | |
549 | ||
550 | static void get_color(const struct config_location_options *opts, | |
551 | const char *var, const char *def_color) | |
552 | { | |
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, | |
559 | &opts->source, the_repository, | |
560 | &opts->options); | |
561 | ||
562 | if (!data.get_color_found && def_color) { | |
563 | if (color_parse(def_color, data.parsed_color) < 0) | |
564 | die(_("unable to parse default color value")); | |
565 | } | |
566 | ||
567 | fputs(data.parsed_color, stdout); | |
568 | } | |
569 | ||
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 | ||
577 | static int git_get_colorbool_config(const char *var, const char *value, | |
578 | const struct config_context *ctx UNUSED, | |
579 | void *cb) | |
580 | { | |
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); | |
585 | else if (!strcmp(var, "diff.color")) | |
586 | data->get_diff_color_found = git_config_colorbool(var, value); | |
587 | else if (!strcmp(var, "color.ui")) | |
588 | data->get_color_ui_found = git_config_colorbool(var, value); | |
589 | return 0; | |
590 | } | |
591 | ||
592 | static int get_colorbool(const struct config_location_options *opts, | |
593 | const char *var, int print) | |
594 | { | |
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, | |
603 | &opts->source, the_repository, | |
604 | &opts->options); | |
605 | ||
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; | |
611 | } | |
612 | ||
613 | if (data.get_colorbool_found < 0) | |
614 | /* default value if none found in config */ | |
615 | data.get_colorbool_found = GIT_COLOR_AUTO; | |
616 | ||
617 | data.get_colorbool_found = want_color(data.get_colorbool_found); | |
618 | ||
619 | if (print) { | |
620 | printf("%s\n", data.get_colorbool_found ? "true" : "false"); | |
621 | return 0; | |
622 | } else | |
623 | return data.get_colorbool_found ? 0 : 1; | |
624 | } | |
625 | ||
626 | static void check_write(const struct git_config_source *source) | |
627 | { | |
628 | if (!source->file && !startup_info->have_repository) | |
629 | die(_("not in a git directory")); | |
630 | ||
631 | if (source->use_stdin) | |
632 | die(_("writing to stdin is not supported")); | |
633 | ||
634 | if (source->blob) | |
635 | die(_("writing config blobs is not supported")); | |
636 | } | |
637 | ||
638 | struct urlmatch_current_candidate_value { | |
639 | char value_is_null; | |
640 | struct strbuf value; | |
641 | struct key_value_info kvi; | |
642 | }; | |
643 | ||
644 | static int urlmatch_collect_fn(const char *var, const char *value, | |
645 | const struct config_context *ctx, | |
646 | void *cb) | |
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; | |
651 | const struct key_value_info *kvi = ctx->kvi; | |
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 | } | |
660 | matched->kvi = *kvi; | |
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 | ||
671 | static int get_urlmatch(const struct config_location_options *opts, | |
672 | const struct config_display_options *_display_opts, | |
673 | const char *var, const char *url) | |
674 | { | |
675 | int ret; | |
676 | char *section_tail; | |
677 | struct config_display_options display_opts = *_display_opts; | |
678 | struct string_list_item *item; | |
679 | struct urlmatch_config config = URLMATCH_CONFIG_INIT; | |
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)) | |
687 | die("%s", config.url.err); | |
688 | ||
689 | config.section = xstrdup_tolower(var); | |
690 | section_tail = strchr(config.section, '.'); | |
691 | if (section_tail) { | |
692 | *section_tail = '\0'; | |
693 | config.key = section_tail + 1; | |
694 | display_opts.show_keys = 0; | |
695 | } else { | |
696 | config.key = NULL; | |
697 | display_opts.show_keys = 1; | |
698 | } | |
699 | ||
700 | config_with_options(urlmatch_config_entry, &config, | |
701 | &opts->source, the_repository, | |
702 | &opts->options); | |
703 | ||
704 | ret = !values.nr; | |
705 | ||
706 | for_each_string_list_item(item, &values) { | |
707 | struct urlmatch_current_candidate_value *matched = item->util; | |
708 | struct strbuf buf = STRBUF_INIT; | |
709 | ||
710 | format_config(&display_opts, &buf, item->string, | |
711 | matched->value_is_null ? NULL : matched->value.buf, | |
712 | &matched->kvi); | |
713 | fwrite(buf.buf, 1, buf.len, stdout); | |
714 | strbuf_release(&buf); | |
715 | ||
716 | strbuf_release(&matched->value); | |
717 | } | |
718 | urlmatch_config_release(&config); | |
719 | string_list_clear(&values, 1); | |
720 | free(config.url.url); | |
721 | ||
722 | free((void *)config.section); | |
723 | return ret; | |
724 | } | |
725 | ||
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" | |
731 | "[user]\n" | |
732 | "# Please adapt and uncomment the following lines:\n" | |
733 | "# name = %s\n" | |
734 | "# email = %s\n"), | |
735 | ident_default_name(), | |
736 | ident_default_email()); | |
737 | return strbuf_detach(&buf, NULL); | |
738 | } | |
739 | ||
740 | static void location_options_init(struct config_location_options *opts, | |
741 | const char *prefix) | |
742 | { | |
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) { | |
750 | error(_("only one config file at a time")); | |
751 | exit(129); | |
752 | } | |
753 | ||
754 | if (!startup_info->have_repository) { | |
755 | if (opts->use_local_config) | |
756 | die(_("--local can only be used inside a git repository")); | |
757 | if (opts->source.blob) | |
758 | die(_("--blob can only be used inside a git repository")); | |
759 | if (opts->use_worktree_config) | |
760 | die(_("--worktree can only be used inside a git repository")); | |
761 | } | |
762 | ||
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; | |
768 | } | |
769 | ||
770 | if (opts->use_global_config) { | |
771 | opts->source.file = opts->file_to_free = git_global_config(); | |
772 | if (!opts->source.file) | |
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 | */ | |
779 | die(_("$HOME not set")); | |
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) { | |
785 | opts->source.file = opts->file_to_free = repo_git_path(the_repository, "config"); | |
786 | opts->source.scope = CONFIG_SCOPE_LOCAL; | |
787 | } else if (opts->use_worktree_config) { | |
788 | struct worktree **worktrees = get_worktrees(); | |
789 | if (the_repository->repository_format_worktree_config) | |
790 | opts->source.file = opts->file_to_free = | |
791 | repo_git_path(the_repository, "config.worktree"); | |
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 | |
799 | opts->source.file = opts->file_to_free = | |
800 | repo_git_path(the_repository, "config"); | |
801 | opts->source.scope = CONFIG_SCOPE_LOCAL; | |
802 | free_worktrees(worktrees); | |
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; | |
810 | } | |
811 | ||
812 | if (opts->respect_includes_opt == -1) | |
813 | opts->options.respect_includes = !opts->source.file; | |
814 | else | |
815 | opts->options.respect_includes = opts->respect_includes_opt; | |
816 | if (startup_info->have_repository) { | |
817 | opts->options.commondir = repo_get_common_dir(the_repository); | |
818 | opts->options.git_dir = repo_get_git_dir(the_repository); | |
819 | } | |
820 | } | |
821 | ||
822 | static void location_options_release(struct config_location_options *opts) | |
823 | { | |
824 | free(opts->file_to_free); | |
825 | } | |
826 | ||
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'; | |
833 | } | |
834 | } | |
835 | ||
836 | static int cmd_config_list(int argc, const char **argv, const char *prefix, | |
837 | struct repository *repo UNUSED) | |
838 | { | |
839 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
840 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; | |
841 | struct option opts[] = { | |
842 | CONFIG_LOCATION_OPTIONS(location_opts), | |
843 | CONFIG_DISPLAY_OPTIONS(display_opts), | |
844 | OPT_GROUP(N_("Other")), | |
845 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, | |
846 | N_("respect include directives on lookup")), | |
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 | ||
853 | location_options_init(&location_opts, prefix); | |
854 | display_options_init(&display_opts); | |
855 | ||
856 | setup_auto_pager("config", 1); | |
857 | ||
858 | if (config_with_options(show_all_config, &display_opts, | |
859 | &location_opts.source, the_repository, | |
860 | &location_opts.options) < 0) { | |
861 | if (location_opts.source.file) | |
862 | die_errno(_("unable to read config file '%s'"), | |
863 | location_opts.source.file); | |
864 | else | |
865 | die(_("error processing config file(s)")); | |
866 | } | |
867 | ||
868 | location_options_release(&location_opts); | |
869 | return 0; | |
870 | } | |
871 | ||
872 | static int cmd_config_get(int argc, const char **argv, const char *prefix, | |
873 | struct repository *repo UNUSED) | |
874 | { | |
875 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
876 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; | |
877 | const char *value_pattern = NULL, *url = NULL; | |
878 | int flags = 0; | |
879 | unsigned get_value_flags = 0; | |
880 | struct option opts[] = { | |
881 | CONFIG_LOCATION_OPTIONS(location_opts), | |
882 | OPT_GROUP(N_("Filter options")), | |
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), | |
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")), | |
888 | CONFIG_DISPLAY_OPTIONS(display_opts), | |
889 | OPT_GROUP(N_("Other")), | |
890 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, | |
891 | N_("respect include directives on lookup")), | |
892 | OPT_STRING(0, "default", &display_opts.default_value, | |
893 | N_("value"), N_("use default value when missing entry")), | |
894 | OPT_END(), | |
895 | }; | |
896 | int ret; | |
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'")); | |
904 | if (display_opts.default_value && | |
905 | ((get_value_flags & GET_VALUE_ALL) || url)) | |
906 | die(_("--default= cannot be used with --all or --url=")); | |
907 | if (url && ((get_value_flags & GET_VALUE_ALL) || | |
908 | (get_value_flags & GET_VALUE_KEY_REGEXP) || | |
909 | value_pattern)) | |
910 | die(_("--url= cannot be used with --all, --regexp or --value")); | |
911 | ||
912 | location_options_init(&location_opts, prefix); | |
913 | display_options_init(&display_opts); | |
914 | ||
915 | setup_auto_pager("config", 1); | |
916 | ||
917 | if (url) | |
918 | ret = get_urlmatch(&location_opts, &display_opts, argv[0], url); | |
919 | else | |
920 | ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, | |
921 | get_value_flags, flags); | |
922 | ||
923 | location_options_release(&location_opts); | |
924 | return ret; | |
925 | } | |
926 | ||
927 | static int cmd_config_set(int argc, const char **argv, const char *prefix, | |
928 | struct repository *repo UNUSED) | |
929 | { | |
930 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
931 | const char *value_pattern = NULL, *comment_arg = NULL; | |
932 | char *comment = NULL; | |
933 | int flags = 0, append = 0, type = 0; | |
934 | struct option opts[] = { | |
935 | CONFIG_LOCATION_OPTIONS(location_opts), | |
936 | CONFIG_TYPE_OPTIONS(type), | |
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); | |
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 | ||
963 | location_options_init(&location_opts, prefix); | |
964 | check_write(&location_opts.source); | |
965 | ||
966 | value = normalize_value(argv[0], argv[1], type, &default_kvi); | |
967 | ||
968 | if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) { | |
969 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, | |
970 | argv[0], value, value_pattern, | |
971 | comment, flags); | |
972 | } else { | |
973 | ret = git_config_set_in_file_gently(location_opts.source.file, | |
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 | ||
980 | location_options_release(&location_opts); | |
981 | free(comment); | |
982 | free(value); | |
983 | return ret; | |
984 | } | |
985 | ||
986 | static int cmd_config_unset(int argc, const char **argv, const char *prefix, | |
987 | struct repository *repo UNUSED) | |
988 | { | |
989 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
990 | const char *value_pattern = NULL; | |
991 | int flags = 0; | |
992 | struct option opts[] = { | |
993 | CONFIG_LOCATION_OPTIONS(location_opts), | |
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 | }; | |
1000 | int ret; | |
1001 | ||
1002 | argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage, | |
1003 | PARSE_OPT_STOP_AT_NON_OPTION); | |
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 | ||
1009 | location_options_init(&location_opts, prefix); | |
1010 | check_write(&location_opts.source); | |
1011 | ||
1012 | if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) | |
1013 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, | |
1014 | argv[0], NULL, value_pattern, | |
1015 | NULL, flags); | |
1016 | else | |
1017 | ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], | |
1018 | NULL, NULL); | |
1019 | ||
1020 | location_options_release(&location_opts); | |
1021 | return ret; | |
1022 | } | |
1023 | ||
1024 | static int cmd_config_rename_section(int argc, const char **argv, const char *prefix, | |
1025 | struct repository *repo UNUSED) | |
1026 | { | |
1027 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
1028 | struct option opts[] = { | |
1029 | CONFIG_LOCATION_OPTIONS(location_opts), | |
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); | |
1036 | check_argc(argc, 2, 2); | |
1037 | ||
1038 | location_options_init(&location_opts, prefix); | |
1039 | check_write(&location_opts.source); | |
1040 | ||
1041 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, | |
1042 | argv[0], argv[1]); | |
1043 | if (ret < 0) | |
1044 | goto out; | |
1045 | else if (!ret) | |
1046 | die(_("no such section: %s"), argv[0]); | |
1047 | ret = 0; | |
1048 | ||
1049 | out: | |
1050 | location_options_release(&location_opts); | |
1051 | return ret; | |
1052 | } | |
1053 | ||
1054 | static int cmd_config_remove_section(int argc, const char **argv, const char *prefix, | |
1055 | struct repository *repo UNUSED) | |
1056 | { | |
1057 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
1058 | struct option opts[] = { | |
1059 | CONFIG_LOCATION_OPTIONS(location_opts), | |
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); | |
1066 | check_argc(argc, 1, 1); | |
1067 | ||
1068 | location_options_init(&location_opts, prefix); | |
1069 | check_write(&location_opts.source); | |
1070 | ||
1071 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, | |
1072 | argv[0], NULL); | |
1073 | if (ret < 0) | |
1074 | goto out; | |
1075 | else if (!ret) | |
1076 | die(_("no such section: %s"), argv[0]); | |
1077 | ret = 0; | |
1078 | ||
1079 | out: | |
1080 | location_options_release(&location_opts); | |
1081 | return ret; | |
1082 | } | |
1083 | ||
1084 | static int show_editor(struct config_location_options *opts) | |
1085 | { | |
1086 | char *config_file; | |
1087 | ||
1088 | if (!opts->source.file && !startup_info->have_repository) | |
1089 | die(_("not in a git directory")); | |
1090 | if (opts->source.use_stdin) | |
1091 | die(_("editing stdin is not supported")); | |
1092 | if (opts->source.blob) | |
1093 | die(_("editing blobs is not supported")); | |
1094 | git_config(git_default_config, NULL); | |
1095 | config_file = opts->source.file ? | |
1096 | xstrdup(opts->source.file) : | |
1097 | repo_git_path(the_repository, "config"); | |
1098 | if (opts->use_global_config) { | |
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 | ||
1115 | static int cmd_config_edit(int argc, const char **argv, const char *prefix, | |
1116 | struct repository *repo UNUSED) | |
1117 | { | |
1118 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
1119 | struct option opts[] = { | |
1120 | CONFIG_LOCATION_OPTIONS(location_opts), | |
1121 | OPT_END(), | |
1122 | }; | |
1123 | int ret; | |
1124 | ||
1125 | argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0); | |
1126 | check_argc(argc, 0, 0); | |
1127 | ||
1128 | location_options_init(&location_opts, prefix); | |
1129 | check_write(&location_opts.source); | |
1130 | ||
1131 | ret = show_editor(&location_opts); | |
1132 | location_options_release(&location_opts); | |
1133 | return ret; | |
1134 | } | |
1135 | ||
1136 | static int cmd_config_actions(int argc, const char **argv, const char *prefix) | |
1137 | { | |
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 | }; | |
1156 | struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; | |
1157 | struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; | |
1158 | const char *comment_arg = NULL; | |
1159 | int actions = 0; | |
1160 | unsigned flags = 0; | |
1161 | struct option opts[] = { | |
1162 | CONFIG_LOCATION_OPTIONS(location_opts), | |
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), | |
1178 | CONFIG_DISPLAY_OPTIONS(display_opts), | |
1179 | OPT_GROUP(N_("Other")), | |
1180 | OPT_STRING(0, "default", &display_opts.default_value, | |
1181 | N_("value"), N_("with --get, use default value when missing entry")), | |
1182 | OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), | |
1183 | OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), | |
1184 | OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, | |
1185 | N_("respect include directives on lookup")), | |
1186 | OPT_END(), | |
1187 | }; | |
1188 | char *value = NULL, *comment = NULL; | |
1189 | int ret = 0; | |
1190 | struct key_value_info default_kvi = KVI_INIT; | |
1191 | ||
1192 | argc = parse_options(argc, argv, prefix, opts, | |
1193 | builtin_config_usage, | |
1194 | PARSE_OPT_STOP_AT_NON_OPTION); | |
1195 | ||
1196 | location_options_init(&location_opts, prefix); | |
1197 | display_options_init(&display_opts); | |
1198 | ||
1199 | if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) { | |
1200 | error(_("--get-color and variable type are incoherent")); | |
1201 | exit(129); | |
1202 | } | |
1203 | ||
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: | |
1210 | error(_("no action specified")); | |
1211 | exit(129); | |
1212 | } | |
1213 | if (display_opts.omit_values && | |
1214 | !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) { | |
1215 | error(_("--name-only is only applicable to --list or --get-regexp")); | |
1216 | exit(129); | |
1217 | } | |
1218 | ||
1219 | if (display_opts.show_origin && !(actions & | |
1220 | (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) { | |
1221 | error(_("--show-origin is only applicable to --get, --get-all, " | |
1222 | "--get-regexp, and --list")); | |
1223 | exit(129); | |
1224 | } | |
1225 | ||
1226 | if (display_opts.default_value && !(actions & ACTION_GET)) { | |
1227 | error(_("--default is only applicable to --get")); | |
1228 | exit(129); | |
1229 | } | |
1230 | ||
1231 | if (comment_arg && | |
1232 | !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) { | |
1233 | error(_("--comment is only applicable to add/set/replace operations")); | |
1234 | exit(129); | |
1235 | } | |
1236 | ||
1237 | /* check usage of --fixed-value */ | |
1238 | if (flags & CONFIG_FLAGS_FIXED_VALUE) { | |
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'")); | |
1267 | exit(129); | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | comment = git_config_prepare_comment_string(comment_arg); | |
1272 | ||
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)) | |
1278 | setup_auto_pager("config", 1); | |
1279 | ||
1280 | if (actions == ACTION_LIST) { | |
1281 | check_argc(argc, 0, 0); | |
1282 | if (config_with_options(show_all_config, &display_opts, | |
1283 | &location_opts.source, the_repository, | |
1284 | &location_opts.options) < 0) { | |
1285 | if (location_opts.source.file) | |
1286 | die_errno(_("unable to read config file '%s'"), | |
1287 | location_opts.source.file); | |
1288 | else | |
1289 | die(_("error processing config file(s)")); | |
1290 | } | |
1291 | } | |
1292 | else if (actions == ACTION_EDIT) { | |
1293 | ret = show_editor(&location_opts); | |
1294 | } | |
1295 | else if (actions == ACTION_SET) { | |
1296 | check_write(&location_opts.source); | |
1297 | check_argc(argc, 2, 2); | |
1298 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); | |
1299 | ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value); | |
1300 | if (ret == CONFIG_NOTHING_SET) | |
1301 | error(_("cannot overwrite multiple values with a single value\n" | |
1302 | " Use a regexp, --add or --replace-all to change %s."), argv[0]); | |
1303 | } | |
1304 | else if (actions == ACTION_SET_ALL) { | |
1305 | check_write(&location_opts.source); | |
1306 | check_argc(argc, 2, 3); | |
1307 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); | |
1308 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, | |
1309 | argv[0], value, argv[2], | |
1310 | comment, flags); | |
1311 | } | |
1312 | else if (actions == ACTION_ADD) { | |
1313 | check_write(&location_opts.source); | |
1314 | check_argc(argc, 2, 2); | |
1315 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); | |
1316 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, | |
1317 | argv[0], value, | |
1318 | CONFIG_REGEX_NONE, | |
1319 | comment, flags); | |
1320 | } | |
1321 | else if (actions == ACTION_REPLACE_ALL) { | |
1322 | check_write(&location_opts.source); | |
1323 | check_argc(argc, 2, 3); | |
1324 | value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); | |
1325 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, | |
1326 | argv[0], value, argv[2], | |
1327 | comment, flags | CONFIG_FLAGS_MULTI_REPLACE); | |
1328 | } | |
1329 | else if (actions == ACTION_GET) { | |
1330 | check_argc(argc, 1, 2); | |
1331 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], | |
1332 | 0, flags); | |
1333 | } | |
1334 | else if (actions == ACTION_GET_ALL) { | |
1335 | check_argc(argc, 1, 2); | |
1336 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], | |
1337 | GET_VALUE_ALL, flags); | |
1338 | } | |
1339 | else if (actions == ACTION_GET_REGEXP) { | |
1340 | display_opts.show_keys = 1; | |
1341 | check_argc(argc, 1, 2); | |
1342 | ret = get_value(&location_opts, &display_opts, argv[0], argv[1], | |
1343 | GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags); | |
1344 | } | |
1345 | else if (actions == ACTION_GET_URLMATCH) { | |
1346 | check_argc(argc, 2, 2); | |
1347 | ret = get_urlmatch(&location_opts, &display_opts, argv[0], argv[1]); | |
1348 | } | |
1349 | else if (actions == ACTION_UNSET) { | |
1350 | check_write(&location_opts.source); | |
1351 | check_argc(argc, 1, 2); | |
1352 | if (argc == 2) | |
1353 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, | |
1354 | argv[0], NULL, argv[1], | |
1355 | NULL, flags); | |
1356 | else | |
1357 | ret = git_config_set_in_file_gently(location_opts.source.file, | |
1358 | argv[0], NULL, NULL); | |
1359 | } | |
1360 | else if (actions == ACTION_UNSET_ALL) { | |
1361 | check_write(&location_opts.source); | |
1362 | check_argc(argc, 1, 2); | |
1363 | ret = git_config_set_multivar_in_file_gently(location_opts.source.file, | |
1364 | argv[0], NULL, argv[1], | |
1365 | NULL, flags | CONFIG_FLAGS_MULTI_REPLACE); | |
1366 | } | |
1367 | else if (actions == ACTION_RENAME_SECTION) { | |
1368 | check_write(&location_opts.source); | |
1369 | check_argc(argc, 2, 2); | |
1370 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, | |
1371 | argv[0], argv[1]); | |
1372 | if (ret < 0) | |
1373 | goto out; | |
1374 | else if (!ret) | |
1375 | die(_("no such section: %s"), argv[0]); | |
1376 | else | |
1377 | ret = 0; | |
1378 | } | |
1379 | else if (actions == ACTION_REMOVE_SECTION) { | |
1380 | check_write(&location_opts.source); | |
1381 | check_argc(argc, 1, 1); | |
1382 | ret = repo_config_rename_section_in_file(the_repository, location_opts.source.file, | |
1383 | argv[0], NULL); | |
1384 | if (ret < 0) | |
1385 | goto out; | |
1386 | else if (!ret) | |
1387 | die(_("no such section: %s"), argv[0]); | |
1388 | else | |
1389 | ret = 0; | |
1390 | } | |
1391 | else if (actions == ACTION_GET_COLOR) { | |
1392 | check_argc(argc, 1, 2); | |
1393 | get_color(&location_opts, argv[0], argv[1]); | |
1394 | } | |
1395 | else if (actions == ACTION_GET_COLORBOOL) { | |
1396 | check_argc(argc, 1, 2); | |
1397 | if (argc == 2) | |
1398 | color_stdout_is_tty = git_config_bool("command line", argv[1]); | |
1399 | ret = get_colorbool(&location_opts, argv[0], argc == 2); | |
1400 | } | |
1401 | ||
1402 | out: | |
1403 | location_options_release(&location_opts); | |
1404 | free(comment); | |
1405 | free(value); | |
1406 | return ret; | |
1407 | } | |
1408 | ||
1409 | int cmd_config(int argc, | |
1410 | const char **argv, | |
1411 | const char *prefix, | |
1412 | struct repository *repo) | |
1413 | { | |
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 | ||
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 | */ | |
1434 | argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, | |
1435 | PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT); | |
1436 | if (subcommand) { | |
1437 | argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, | |
1438 | PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT); | |
1439 | return subcommand(argc, argv, prefix, repo); | |
1440 | } | |
1441 | ||
1442 | return cmd_config_actions(argc, argv, prefix); | |
1443 | } |