]>
Commit | Line | Data |
---|---|---|
e12c095a | 1 | #include "builtin.h" |
1b1e59c5 | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
9ce03522 | 4 | #include "color.h" |
d64ec16c | 5 | #include "parse-options.h" |
d4770964 | 6 | #include "urlmatch.h" |
70bd879a | 7 | #include "quote.h" |
58b284a2 | 8 | #include "worktree.h" |
1b1e59c5 | 9 | |
d64ec16c | 10 | static const char *const builtin_config_usage[] = { |
9c9b4f2f | 11 | N_("git config [<options>]"), |
d64ec16c FC |
12 | NULL |
13 | }; | |
4ddba79d | 14 | |
96f1e58f DR |
15 | static char *key; |
16 | static regex_t *key_regexp; | |
17 | static regex_t *regexp; | |
18 | static int show_keys; | |
578625fa | 19 | static int omit_values; |
96f1e58f DR |
20 | static int use_key_regexp; |
21 | static int do_all; | |
22 | static int do_not_match; | |
2275d502 FL |
23 | static char delim = '='; |
24 | static char key_delim = ' '; | |
25 | static char term = '\n'; | |
4ddba79d | 26 | |
57210a67 | 27 | static int use_global_config, use_system_config, use_local_config; |
58b284a2 | 28 | static int use_worktree_config; |
c8985ce0 | 29 | static struct git_config_source given_config_source; |
0a8950be | 30 | static int actions, type; |
eeaa24b9 | 31 | static char *default_value; |
d64ec16c | 32 | static int end_null; |
c48f4b37 NTND |
33 | static int respect_includes_opt = -1; |
34 | static struct config_options config_options; | |
70bd879a | 35 | static int show_origin; |
d64ec16c FC |
36 | |
37 | #define ACTION_GET (1<<0) | |
38 | #define ACTION_GET_ALL (1<<1) | |
39 | #define ACTION_GET_REGEXP (1<<2) | |
40 | #define ACTION_REPLACE_ALL (1<<3) | |
41 | #define ACTION_ADD (1<<4) | |
42 | #define ACTION_UNSET (1<<5) | |
43 | #define ACTION_UNSET_ALL (1<<6) | |
44 | #define ACTION_RENAME_SECTION (1<<7) | |
45 | #define ACTION_REMOVE_SECTION (1<<8) | |
46 | #define ACTION_LIST (1<<9) | |
47 | #define ACTION_EDIT (1<<10) | |
48 | #define ACTION_SET (1<<11) | |
49 | #define ACTION_SET_ALL (1<<12) | |
50 | #define ACTION_GET_COLOR (1<<13) | |
51 | #define ACTION_GET_COLORBOOL (1<<14) | |
d4770964 | 52 | #define ACTION_GET_URLMATCH (1<<15) |
d64ec16c | 53 | |
32888b8f MÅ |
54 | /* |
55 | * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than | |
56 | * one line of output and which should therefore be paged. | |
57 | */ | |
58 | #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \ | |
59 | ACTION_GET_REGEXP | ACTION_GET_URLMATCH) | |
60 | ||
0a8950be TB |
61 | #define TYPE_BOOL 1 |
62 | #define TYPE_INT 2 | |
63 | #define TYPE_BOOL_OR_INT 3 | |
64 | #define TYPE_PATH 4 | |
65 | #define TYPE_EXPIRY_DATE 5 | |
63e2a0f8 | 66 | #define TYPE_COLOR 6 |
16c1e939 | 67 | |
fb0dc3ba TB |
68 | #define OPT_CALLBACK_VALUE(s, l, v, h, i) \ |
69 | { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \ | |
70 | PARSE_OPT_NONEG, option_parse_type, (i) } | |
71 | ||
6aaded55 | 72 | static NORETURN void usage_builtin_config(void); |
fb0dc3ba TB |
73 | |
74 | static int option_parse_type(const struct option *opt, const char *arg, | |
75 | int unset) | |
76 | { | |
77 | int new_type, *to_type; | |
78 | ||
79 | if (unset) { | |
80 | *((int *) opt->value) = 0; | |
81 | return 0; | |
82 | } | |
83 | ||
84 | /* | |
85 | * To support '--<type>' style flags, begin with new_type equal to | |
86 | * opt->defval. | |
87 | */ | |
88 | new_type = opt->defval; | |
89 | if (!new_type) { | |
90 | if (!strcmp(arg, "bool")) | |
91 | new_type = TYPE_BOOL; | |
92 | else if (!strcmp(arg, "int")) | |
93 | new_type = TYPE_INT; | |
94 | else if (!strcmp(arg, "bool-or-int")) | |
95 | new_type = TYPE_BOOL_OR_INT; | |
96 | else if (!strcmp(arg, "path")) | |
97 | new_type = TYPE_PATH; | |
98 | else if (!strcmp(arg, "expiry-date")) | |
99 | new_type = TYPE_EXPIRY_DATE; | |
63e2a0f8 TB |
100 | else if (!strcmp(arg, "color")) |
101 | new_type = TYPE_COLOR; | |
fb0dc3ba TB |
102 | else |
103 | die(_("unrecognized --type argument, %s"), arg); | |
104 | } | |
105 | ||
106 | to_type = opt->value; | |
107 | if (*to_type && *to_type != new_type) { | |
108 | /* | |
109 | * Complain when there is a new type not equal to the old type. | |
110 | * This allows for combinations like '--int --type=int' and | |
111 | * '--type=int --type=int', but disallows ones like '--type=bool | |
112 | * --int' and '--type=bool | |
113 | * --type=int'. | |
114 | */ | |
1d28ff4c | 115 | error(_("only one type at a time")); |
6aaded55 | 116 | usage_builtin_config(); |
fb0dc3ba TB |
117 | } |
118 | *to_type = new_type; | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
d64ec16c | 123 | static struct option builtin_config_options[] = { |
1bd31ce6 | 124 | OPT_GROUP(N_("Config file location")), |
21e047dc SB |
125 | OPT_BOOL(0, "global", &use_global_config, N_("use global config file")), |
126 | OPT_BOOL(0, "system", &use_system_config, N_("use system config file")), | |
127 | OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")), | |
58b284a2 | 128 | OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")), |
c8985ce0 KS |
129 | OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), |
130 | OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")), | |
1bd31ce6 NTND |
131 | OPT_GROUP(N_("Action")), |
132 | OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET), | |
133 | OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL), | |
134 | OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP), | |
d4770964 | 135 | OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), |
1bd31ce6 | 136 | OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL), |
f63cf8c9 NTND |
137 | OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), |
138 | OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET), | |
139 | OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL), | |
1bd31ce6 NTND |
140 | OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), |
141 | OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), | |
142 | OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), | |
f63cf8c9 | 143 | OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), |
d0e08d62 JK |
144 | OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR), |
145 | OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL), | |
1bd31ce6 | 146 | OPT_GROUP(N_("Type")), |
fb0dc3ba TB |
147 | OPT_CALLBACK('t', "type", &type, "", N_("value is given this type"), option_parse_type), |
148 | OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), | |
149 | OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), | |
150 | OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), | |
151 | OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), | |
152 | OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE), | |
1bd31ce6 | 153 | OPT_GROUP(N_("Other")), |
d5d09d47 | 154 | OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")), |
578625fa | 155 | OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")), |
c48f4b37 | 156 | OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")), |
70bd879a | 157 | OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")), |
eeaa24b9 | 158 | OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")), |
d64ec16c FC |
159 | OPT_END(), |
160 | }; | |
161 | ||
6aaded55 BB |
162 | static NORETURN void usage_builtin_config(void) |
163 | { | |
164 | usage_with_options(builtin_config_usage, builtin_config_options); | |
165 | } | |
166 | ||
3b335762 NTND |
167 | static void check_argc(int argc, int min, int max) |
168 | { | |
d64ec16c FC |
169 | if (argc >= min && argc <= max) |
170 | return; | |
1a07e59c | 171 | if (min == max) |
1d28ff4c | 172 | error(_("wrong number of arguments, should be %d"), min); |
1a07e59c | 173 | else |
1d28ff4c | 174 | error(_("wrong number of arguments, should be from %d to %d"), |
1a07e59c | 175 | min, max); |
6aaded55 | 176 | usage_builtin_config(); |
d64ec16c FC |
177 | } |
178 | ||
70bd879a LS |
179 | static void show_config_origin(struct strbuf *buf) |
180 | { | |
181 | const char term = end_null ? '\0' : '\t'; | |
182 | ||
183 | strbuf_addstr(buf, current_config_origin_type()); | |
184 | strbuf_addch(buf, ':'); | |
185 | if (end_null) | |
186 | strbuf_addstr(buf, current_config_name()); | |
187 | else | |
188 | quote_c_style(current_config_name(), buf, NULL, 0); | |
189 | strbuf_addch(buf, term); | |
190 | } | |
191 | ||
ef90d6d4 | 192 | static int show_all_config(const char *key_, const char *value_, void *cb) |
de791f15 | 193 | { |
70bd879a LS |
194 | if (show_origin) { |
195 | struct strbuf buf = STRBUF_INIT; | |
196 | show_config_origin(&buf); | |
197 | /* Use fwrite as "buf" can contain \0's if "end_null" is set. */ | |
198 | fwrite(buf.buf, 1, buf.len, stdout); | |
199 | strbuf_release(&buf); | |
200 | } | |
578625fa | 201 | if (!omit_values && value_) |
2275d502 | 202 | printf("%s%c%s%c", key_, delim, value_, term); |
de791f15 | 203 | else |
2275d502 | 204 | printf("%s%c", key_, term); |
de791f15 PB |
205 | return 0; |
206 | } | |
207 | ||
7acdd6f0 JK |
208 | struct strbuf_list { |
209 | struct strbuf *items; | |
210 | int nr; | |
211 | int alloc; | |
212 | }; | |
213 | ||
d9b9169b | 214 | static int format_config(struct strbuf *buf, const char *key_, const char *value_) |
4ddba79d | 215 | { |
70bd879a LS |
216 | if (show_origin) |
217 | show_config_origin(buf); | |
ebca2d49 | 218 | if (show_keys) |
7acdd6f0 | 219 | strbuf_addstr(buf, key_); |
ebca2d49 | 220 | if (!omit_values) { |
f2259877 JK |
221 | if (show_keys) |
222 | strbuf_addch(buf, key_delim); | |
ebca2d49 | 223 | |
0a8950be | 224 | if (type == TYPE_INT) |
f2259877 JK |
225 | strbuf_addf(buf, "%"PRId64, |
226 | git_config_int64(key_, value_ ? value_ : "")); | |
0a8950be | 227 | else if (type == TYPE_BOOL) |
f2259877 JK |
228 | strbuf_addstr(buf, git_config_bool(key_, value_) ? |
229 | "true" : "false"); | |
0a8950be | 230 | else if (type == TYPE_BOOL_OR_INT) { |
ebca2d49 SG |
231 | int is_bool, v; |
232 | v = git_config_bool_or_int(key_, value_, &is_bool); | |
233 | if (is_bool) | |
f2259877 | 234 | strbuf_addstr(buf, v ? "true" : "false"); |
ebca2d49 | 235 | else |
f2259877 | 236 | strbuf_addf(buf, "%d", v); |
0a8950be | 237 | } else if (type == TYPE_PATH) { |
f2259877 JK |
238 | const char *v; |
239 | if (git_config_pathname(&v, key_, value_) < 0) | |
ebca2d49 | 240 | return -1; |
f2259877 JK |
241 | strbuf_addstr(buf, v); |
242 | free((char *)v); | |
0a8950be | 243 | } else if (type == TYPE_EXPIRY_DATE) { |
5f967424 HM |
244 | timestamp_t t; |
245 | if (git_config_expiry_date(&t, key_, value_) < 0) | |
246 | return -1; | |
247 | strbuf_addf(buf, "%"PRItime, t); | |
63e2a0f8 TB |
248 | } else if (type == TYPE_COLOR) { |
249 | char v[COLOR_MAXLEN]; | |
250 | if (git_config_color(v, key_, value_) < 0) | |
251 | return -1; | |
252 | strbuf_addstr(buf, v); | |
ebca2d49 | 253 | } else if (value_) { |
f2259877 | 254 | strbuf_addstr(buf, value_); |
ebca2d49 | 255 | } else { |
f2259877 JK |
256 | /* Just show the key name; back out delimiter */ |
257 | if (show_keys) | |
258 | strbuf_setlen(buf, buf->len - 1); | |
ebca2d49 | 259 | } |
578625fa | 260 | } |
00b347d3 | 261 | strbuf_addch(buf, term); |
4ddba79d JS |
262 | return 0; |
263 | } | |
264 | ||
d9b9169b JH |
265 | static int collect_config(const char *key_, const char *value_, void *cb) |
266 | { | |
267 | struct strbuf_list *values = cb; | |
268 | ||
269 | if (!use_key_regexp && strcmp(key_, key)) | |
270 | return 0; | |
271 | if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0)) | |
272 | return 0; | |
273 | if (regexp != NULL && | |
274 | (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0))) | |
275 | return 0; | |
276 | ||
277 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); | |
9f1429df | 278 | strbuf_init(&values->items[values->nr], 0); |
d9b9169b JH |
279 | |
280 | return format_config(&values->items[values->nr++], key_, value_); | |
281 | } | |
282 | ||
4b951b7e | 283 | static int get_value(const char *key_, const char *regex_) |
4ddba79d | 284 | { |
9409c7a5 | 285 | int ret = CONFIG_GENERIC_ERROR; |
5ba1a8a7 | 286 | struct strbuf_list values = {NULL}; |
7acdd6f0 | 287 | int i; |
4ddba79d | 288 | |
2fa9a0fb | 289 | if (use_key_regexp) { |
b09c53a3 LP |
290 | char *tl; |
291 | ||
292 | /* | |
293 | * NEEDSWORK: this naive pattern lowercasing obviously does not | |
294 | * work for more complex patterns like "^[^.]*Foo.*bar". | |
295 | * Perhaps we should deprecate this altogether someday. | |
296 | */ | |
297 | ||
298 | key = xstrdup(key_); | |
299 | for (tl = key + strlen(key) - 1; | |
300 | tl >= key && *tl != '.'; | |
301 | tl--) | |
302 | *tl = tolower(*tl); | |
303 | for (tl = key; *tl && *tl != '.'; tl++) | |
304 | *tl = tolower(*tl); | |
305 | ||
2d7320d0 | 306 | key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
2fa9a0fb | 307 | if (regcomp(key_regexp, key, REG_EXTENDED)) { |
1d28ff4c | 308 | error(_("invalid key pattern: %s"), key_); |
6a83d902 | 309 | FREE_AND_NULL(key_regexp); |
9409c7a5 | 310 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 311 | goto free_strings; |
2fa9a0fb | 312 | } |
b09c53a3 | 313 | } else { |
9409c7a5 JH |
314 | if (git_config_parse_key(key_, &key, NULL)) { |
315 | ret = CONFIG_INVALID_KEY; | |
b09c53a3 | 316 | goto free_strings; |
9409c7a5 | 317 | } |
2fa9a0fb JS |
318 | } |
319 | ||
4ddba79d | 320 | if (regex_) { |
f98d863d JS |
321 | if (regex_[0] == '!') { |
322 | do_not_match = 1; | |
323 | regex_++; | |
324 | } | |
325 | ||
2d7320d0 | 326 | regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
0a152171 | 327 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
1d28ff4c | 328 | error(_("invalid pattern: %s"), regex_); |
6a83d902 | 329 | FREE_AND_NULL(regexp); |
9409c7a5 | 330 | ret = CONFIG_INVALID_PATTERN; |
5f1a63e0 | 331 | goto free_strings; |
4ddba79d JS |
332 | } |
333 | } | |
334 | ||
dc8441fd BW |
335 | config_with_options(collect_config, &values, |
336 | &given_config_source, &config_options); | |
5f1a63e0 | 337 | |
eeaa24b9 TB |
338 | if (!values.nr && default_value) { |
339 | struct strbuf *item; | |
340 | ALLOC_GROW(values.items, values.nr + 1, values.alloc); | |
341 | item = &values.items[values.nr++]; | |
342 | strbuf_init(item, 0); | |
343 | if (format_config(item, key_, default_value) < 0) | |
344 | die(_("failed to format default config value: %s"), | |
345 | default_value); | |
346 | } | |
347 | ||
00b347d3 | 348 | ret = !values.nr; |
9b25a0b5 | 349 | |
7acdd6f0 JK |
350 | for (i = 0; i < values.nr; i++) { |
351 | struct strbuf *buf = values.items + i; | |
00b347d3 JK |
352 | if (do_all || i == values.nr - 1) |
353 | fwrite(buf->buf, 1, buf->len, stdout); | |
7acdd6f0 JK |
354 | strbuf_release(buf); |
355 | } | |
356 | free(values.items); | |
5f1a63e0 | 357 | |
97ed50f9 | 358 | free_strings: |
4ddba79d | 359 | free(key); |
35998c89 JK |
360 | if (key_regexp) { |
361 | regfree(key_regexp); | |
362 | free(key_regexp); | |
363 | } | |
0a152171 AW |
364 | if (regexp) { |
365 | regfree(regexp); | |
366 | free(regexp); | |
4ddba79d JS |
367 | } |
368 | ||
5f1a63e0 | 369 | return ret; |
4ddba79d | 370 | } |
1b1e59c5 | 371 | |
186458b1 | 372 | static char *normalize_value(const char *key, const char *value) |
db1696b8 | 373 | { |
db1696b8 FL |
374 | if (!value) |
375 | return NULL; | |
376 | ||
0a8950be | 377 | if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE) |
1349484e MM |
378 | /* |
379 | * We don't do normalization for TYPE_PATH here: If | |
380 | * the path is like ~/foobar/, we prefer to store | |
381 | * "~/foobar/" in the config file, and to expand the ~ | |
382 | * when retrieving the value. | |
5f967424 | 383 | * Also don't do normalization for expiry dates. |
1349484e | 384 | */ |
3ec832c4 | 385 | return xstrdup(value); |
0a8950be | 386 | if (type == TYPE_INT) |
3ec832c4 | 387 | return xstrfmt("%"PRId64, git_config_int64(key, value)); |
0a8950be | 388 | if (type == TYPE_BOOL) |
3ec832c4 | 389 | return xstrdup(git_config_bool(key, value) ? "true" : "false"); |
0a8950be | 390 | if (type == TYPE_BOOL_OR_INT) { |
3ec832c4 JK |
391 | int is_bool, v; |
392 | v = git_config_bool_or_int(key, value, &is_bool); | |
393 | if (!is_bool) | |
394 | return xstrfmt("%d", v); | |
395 | else | |
396 | return xstrdup(v ? "true" : "false"); | |
db1696b8 | 397 | } |
63e2a0f8 TB |
398 | if (type == TYPE_COLOR) { |
399 | char v[COLOR_MAXLEN]; | |
400 | if (git_config_color(v, key, value)) | |
1d28ff4c | 401 | die(_("cannot parse color '%s'"), value); |
63e2a0f8 TB |
402 | |
403 | /* | |
404 | * The contents of `v` now contain an ANSI escape | |
405 | * sequence, not suitable for including within a | |
406 | * configuration file. Treat the above as a | |
407 | * "sanity-check", and return the given value, which we | |
408 | * know is representable as valid color code. | |
409 | */ | |
410 | return xstrdup(value); | |
411 | } | |
db1696b8 | 412 | |
50f08db5 | 413 | BUG("cannot normalize type %d", type); |
db1696b8 FL |
414 | } |
415 | ||
9ce03522 JH |
416 | static int get_color_found; |
417 | static const char *get_color_slot; | |
b408457f | 418 | static const char *get_colorbool_slot; |
9ce03522 JH |
419 | static char parsed_color[COLOR_MAXLEN]; |
420 | ||
ef90d6d4 | 421 | static int git_get_color_config(const char *var, const char *value, void *cb) |
9ce03522 JH |
422 | { |
423 | if (!strcmp(var, get_color_slot)) { | |
f769982d JH |
424 | if (!value) |
425 | config_error_nonbool(var); | |
f6c5a296 JK |
426 | if (color_parse(value, parsed_color) < 0) |
427 | return -1; | |
9ce03522 JH |
428 | get_color_found = 1; |
429 | } | |
430 | return 0; | |
431 | } | |
432 | ||
d0e08d62 | 433 | static void get_color(const char *var, const char *def_color) |
9ce03522 | 434 | { |
d0e08d62 | 435 | get_color_slot = var; |
9ce03522 JH |
436 | get_color_found = 0; |
437 | parsed_color[0] = '\0'; | |
dc8441fd BW |
438 | config_with_options(git_get_color_config, NULL, |
439 | &given_config_source, &config_options); | |
9ce03522 | 440 | |
f6c5a296 JK |
441 | if (!get_color_found && def_color) { |
442 | if (color_parse(def_color, parsed_color) < 0) | |
443 | die(_("unable to parse default color value")); | |
444 | } | |
9ce03522 JH |
445 | |
446 | fputs(parsed_color, stdout); | |
9ce03522 JH |
447 | } |
448 | ||
0f6f5a40 | 449 | static int get_colorbool_found; |
69243c2b | 450 | static int get_diff_color_found; |
c659f55b | 451 | static int get_color_ui_found; |
ef90d6d4 JS |
452 | static int git_get_colorbool_config(const char *var, const char *value, |
453 | void *cb) | |
0f6f5a40 | 454 | { |
e269eb79 JK |
455 | if (!strcmp(var, get_colorbool_slot)) |
456 | get_colorbool_found = git_config_colorbool(var, value); | |
457 | else if (!strcmp(var, "diff.color")) | |
458 | get_diff_color_found = git_config_colorbool(var, value); | |
459 | else if (!strcmp(var, "color.ui")) | |
c659f55b | 460 | get_color_ui_found = git_config_colorbool(var, value); |
0f6f5a40 JH |
461 | return 0; |
462 | } | |
463 | ||
d0e08d62 | 464 | static int get_colorbool(const char *var, int print) |
0f6f5a40 | 465 | { |
d0e08d62 | 466 | get_colorbool_slot = var; |
69243c2b JH |
467 | get_colorbool_found = -1; |
468 | get_diff_color_found = -1; | |
b8612b4d | 469 | get_color_ui_found = -1; |
dc8441fd BW |
470 | config_with_options(git_get_colorbool_config, NULL, |
471 | &given_config_source, &config_options); | |
0f6f5a40 | 472 | |
69243c2b | 473 | if (get_colorbool_found < 0) { |
b408457f | 474 | if (!strcmp(get_colorbool_slot, "color.diff")) |
69243c2b JH |
475 | get_colorbool_found = get_diff_color_found; |
476 | if (get_colorbool_found < 0) | |
c659f55b | 477 | get_colorbool_found = get_color_ui_found; |
69243c2b JH |
478 | } |
479 | ||
b8612b4d MM |
480 | if (get_colorbool_found < 0) |
481 | /* default value if none found in config */ | |
4c7f1819 | 482 | get_colorbool_found = GIT_COLOR_AUTO; |
b8612b4d | 483 | |
daa0c3d9 JK |
484 | get_colorbool_found = want_color(get_colorbool_found); |
485 | ||
0e854a28 | 486 | if (print) { |
0f6f5a40 JH |
487 | printf("%s\n", get_colorbool_found ? "true" : "false"); |
488 | return 0; | |
0e854a28 FC |
489 | } else |
490 | return get_colorbool_found ? 0 : 1; | |
0f6f5a40 JH |
491 | } |
492 | ||
6aea9f0f | 493 | static void check_write(void) |
1bc88819 | 494 | { |
638fa623 | 495 | if (!given_config_source.file && !startup_info->have_repository) |
1d28ff4c | 496 | die(_("not in a git directory")); |
638fa623 | 497 | |
3caec73b | 498 | if (given_config_source.use_stdin) |
1d28ff4c | 499 | die(_("writing to stdin is not supported")); |
3caec73b | 500 | |
c8985ce0 | 501 | if (given_config_source.blob) |
1d28ff4c | 502 | die(_("writing config blobs is not supported")); |
1bc88819 HV |
503 | } |
504 | ||
d4770964 JH |
505 | struct urlmatch_current_candidate_value { |
506 | char value_is_null; | |
507 | struct strbuf value; | |
508 | }; | |
509 | ||
510 | static int urlmatch_collect_fn(const char *var, const char *value, void *cb) | |
511 | { | |
512 | struct string_list *values = cb; | |
513 | struct string_list_item *item = string_list_insert(values, var); | |
514 | struct urlmatch_current_candidate_value *matched = item->util; | |
515 | ||
516 | if (!matched) { | |
517 | matched = xmalloc(sizeof(*matched)); | |
518 | strbuf_init(&matched->value, 0); | |
519 | item->util = matched; | |
520 | } else { | |
521 | strbuf_reset(&matched->value); | |
522 | } | |
523 | ||
524 | if (value) { | |
525 | strbuf_addstr(&matched->value, value); | |
526 | matched->value_is_null = 0; | |
527 | } else { | |
528 | matched->value_is_null = 1; | |
529 | } | |
530 | return 0; | |
531 | } | |
532 | ||
d4770964 JH |
533 | static int get_urlmatch(const char *var, const char *url) |
534 | { | |
27b30be6 | 535 | int ret; |
d4770964 JH |
536 | char *section_tail; |
537 | struct string_list_item *item; | |
538 | struct urlmatch_config config = { STRING_LIST_INIT_DUP }; | |
539 | struct string_list values = STRING_LIST_INIT_DUP; | |
540 | ||
541 | config.collect_fn = urlmatch_collect_fn; | |
542 | config.cascade_fn = NULL; | |
543 | config.cb = &values; | |
544 | ||
545 | if (!url_normalize(url, &config.url)) | |
6667a6ac | 546 | die("%s", config.url.err); |
d4770964 | 547 | |
88d5a6f6 | 548 | config.section = xstrdup_tolower(var); |
d4770964 JH |
549 | section_tail = strchr(config.section, '.'); |
550 | if (section_tail) { | |
551 | *section_tail = '\0'; | |
552 | config.key = section_tail + 1; | |
553 | show_keys = 0; | |
554 | } else { | |
555 | config.key = NULL; | |
556 | show_keys = 1; | |
557 | } | |
558 | ||
dc8441fd BW |
559 | config_with_options(urlmatch_config_entry, &config, |
560 | &given_config_source, &config_options); | |
d4770964 | 561 | |
27b30be6 JK |
562 | ret = !values.nr; |
563 | ||
d4770964 JH |
564 | for_each_string_list_item(item, &values) { |
565 | struct urlmatch_current_candidate_value *matched = item->util; | |
d4770964 JH |
566 | struct strbuf buf = STRBUF_INIT; |
567 | ||
a92330d2 | 568 | format_config(&buf, item->string, |
d4770964 JH |
569 | matched->value_is_null ? NULL : matched->value.buf); |
570 | fwrite(buf.buf, 1, buf.len, stdout); | |
d4770964 JH |
571 | strbuf_release(&buf); |
572 | ||
573 | strbuf_release(&matched->value); | |
574 | } | |
575 | string_list_clear(&config.vars, 1); | |
576 | string_list_clear(&values, 1); | |
577 | free(config.url.url); | |
578 | ||
579 | free((void *)config.section); | |
27b30be6 | 580 | return ret; |
d4770964 JH |
581 | } |
582 | ||
9830534e MM |
583 | static char *default_user_config(void) |
584 | { | |
585 | struct strbuf buf = STRBUF_INIT; | |
586 | strbuf_addf(&buf, | |
587 | _("# This is Git's per-user configuration file.\n" | |
7e110524 | 588 | "[user]\n" |
9830534e | 589 | "# Please adapt and uncomment the following lines:\n" |
7e110524 | 590 | "# name = %s\n" |
9830534e MM |
591 | "# email = %s\n"), |
592 | ident_default_name(), | |
593 | ident_default_email()); | |
594 | return strbuf_detach(&buf, NULL); | |
595 | } | |
596 | ||
3ba7e6e2 | 597 | int cmd_config(int argc, const char **argv, const char *prefix) |
1b1e59c5 | 598 | { |
3ba7e6e2 | 599 | int nongit = !startup_info->have_repository; |
4b951b7e | 600 | char *value; |
7162dff3 | 601 | |
423ff9be | 602 | given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); |
dc871831 | 603 | |
37782920 SB |
604 | argc = parse_options(argc, argv, prefix, builtin_config_options, |
605 | builtin_config_usage, | |
d64ec16c FC |
606 | PARSE_OPT_STOP_AT_NON_OPTION); |
607 | ||
1bc88819 | 608 | if (use_global_config + use_system_config + use_local_config + |
58b284a2 | 609 | use_worktree_config + |
c8985ce0 | 610 | !!given_config_source.file + !!given_config_source.blob > 1) { |
1d28ff4c | 611 | error(_("only one config file at a time")); |
6aaded55 | 612 | usage_builtin_config(); |
67052c9d FC |
613 | } |
614 | ||
25cd2919 JK |
615 | if (use_local_config && nongit) |
616 | die(_("--local can only be used inside a git repository")); | |
617 | ||
17b8a2d6 JK |
618 | if (given_config_source.blob && nongit) |
619 | die(_("--blob can only be used inside a git repository")); | |
620 | ||
3caec73b KS |
621 | if (given_config_source.file && |
622 | !strcmp(given_config_source.file, "-")) { | |
623 | given_config_source.file = NULL; | |
624 | given_config_source.use_stdin = 1; | |
625 | } | |
626 | ||
d64ec16c | 627 | if (use_global_config) { |
4aad2f16 | 628 | char *user_config = expand_user_path("~/.gitconfig", 0); |
509adc33 | 629 | char *xdg_config = xdg_config_home("config"); |
21cf3227 | 630 | |
e3ebc35b MM |
631 | if (!user_config) |
632 | /* | |
633 | * It is unknown if HOME/.gitconfig exists, so | |
634 | * we do not know if we should write to XDG | |
635 | * location; error out even if XDG_CONFIG_HOME | |
636 | * is set and points at a sane location. | |
637 | */ | |
1d28ff4c | 638 | die(_("$HOME not set")); |
e3ebc35b | 639 | |
4698c8fe | 640 | if (access_or_warn(user_config, R_OK, 0) && |
6c6b08d2 | 641 | xdg_config && !access_or_warn(xdg_config, R_OK, 0)) { |
c8985ce0 | 642 | given_config_source.file = xdg_config; |
6c6b08d2 JK |
643 | free(user_config); |
644 | } else { | |
c8985ce0 | 645 | given_config_source.file = user_config; |
6c6b08d2 JK |
646 | free(xdg_config); |
647 | } | |
d64ec16c FC |
648 | } |
649 | else if (use_system_config) | |
c8985ce0 | 650 | given_config_source.file = git_etc_gitconfig(); |
57210a67 | 651 | else if (use_local_config) |
c8985ce0 | 652 | given_config_source.file = git_pathdup("config"); |
58b284a2 NTND |
653 | else if (use_worktree_config) { |
654 | struct worktree **worktrees = get_worktrees(0); | |
655 | if (repository_format_worktree_config) | |
656 | given_config_source.file = git_pathdup("config.worktree"); | |
657 | else if (worktrees[0] && worktrees[1]) | |
658 | die(_("--worktree cannot be used with multiple " | |
659 | "working trees unless the config\n" | |
660 | "extension worktreeConfig is enabled. " | |
661 | "Please read \"CONFIGURATION FILE\"\n" | |
662 | "section in \"git help worktree\" for details")); | |
663 | else | |
664 | given_config_source.file = git_pathdup("config"); | |
665 | free_worktrees(worktrees); | |
666 | } else if (given_config_source.file) { | |
c8985ce0 KS |
667 | if (!is_absolute_path(given_config_source.file) && prefix) |
668 | given_config_source.file = | |
e4da43b1 | 669 | prefix_filename(prefix, given_config_source.file); |
d64ec16c FC |
670 | } |
671 | ||
c48f4b37 NTND |
672 | if (respect_includes_opt == -1) |
673 | config_options.respect_includes = !given_config_source.file; | |
674 | else | |
675 | config_options.respect_includes = respect_includes_opt; | |
dc8441fd BW |
676 | if (!nongit) { |
677 | config_options.commondir = get_git_common_dir(); | |
678 | config_options.git_dir = get_git_dir(); | |
679 | } | |
9b25a0b5 | 680 | |
d64ec16c FC |
681 | if (end_null) { |
682 | term = '\0'; | |
683 | delim = '\n'; | |
684 | key_delim = '\n'; | |
685 | } | |
686 | ||
0a8950be | 687 | if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) { |
1d28ff4c | 688 | error(_("--get-color and variable type are incoherent")); |
6aaded55 | 689 | usage_builtin_config(); |
c2387358 FC |
690 | } |
691 | ||
d64ec16c | 692 | if (HAS_MULTI_BITS(actions)) { |
1d28ff4c | 693 | error(_("only one action at a time")); |
6aaded55 | 694 | usage_builtin_config(); |
d64ec16c FC |
695 | } |
696 | if (actions == 0) | |
697 | switch (argc) { | |
698 | case 1: actions = ACTION_GET; break; | |
699 | case 2: actions = ACTION_SET; break; | |
700 | case 3: actions = ACTION_SET_ALL; break; | |
701 | default: | |
6aaded55 | 702 | usage_builtin_config(); |
db1696b8 | 703 | } |
578625fa SG |
704 | if (omit_values && |
705 | !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) { | |
1d28ff4c | 706 | error(_("--name-only is only applicable to --list or --get-regexp")); |
6aaded55 | 707 | usage_builtin_config(); |
578625fa | 708 | } |
70bd879a LS |
709 | |
710 | if (show_origin && !(actions & | |
711 | (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) { | |
1d28ff4c NTND |
712 | error(_("--show-origin is only applicable to --get, --get-all, " |
713 | "--get-regexp, and --list")); | |
6aaded55 | 714 | usage_builtin_config(); |
70bd879a LS |
715 | } |
716 | ||
eeaa24b9 | 717 | if (default_value && !(actions & ACTION_GET)) { |
1d28ff4c | 718 | error(_("--default is only applicable to --get")); |
6aaded55 | 719 | usage_builtin_config(); |
eeaa24b9 TB |
720 | } |
721 | ||
32888b8f | 722 | if (actions & PAGING_ACTIONS) |
c0e9f5be | 723 | setup_auto_pager("config", 1); |
32888b8f | 724 | |
d64ec16c | 725 | if (actions == ACTION_LIST) { |
225a9caf | 726 | check_argc(argc, 0, 0); |
dc8441fd BW |
727 | if (config_with_options(show_all_config, NULL, |
728 | &given_config_source, | |
729 | &config_options) < 0) { | |
c8985ce0 | 730 | if (given_config_source.file) |
1d28ff4c | 731 | die_errno(_("unable to read config file '%s'"), |
c8985ce0 | 732 | given_config_source.file); |
d64ec16c | 733 | else |
1d28ff4c | 734 | die(_("error processing config file(s)")); |
db1696b8 | 735 | } |
1b1e59c5 | 736 | } |
d64ec16c | 737 | else if (actions == ACTION_EDIT) { |
3696a7c2 MH |
738 | char *config_file; |
739 | ||
225a9caf | 740 | check_argc(argc, 0, 0); |
c8985ce0 | 741 | if (!given_config_source.file && nongit) |
1d28ff4c | 742 | die(_("not in a git directory")); |
3caec73b | 743 | if (given_config_source.use_stdin) |
1d28ff4c | 744 | die(_("editing stdin is not supported")); |
c8985ce0 | 745 | if (given_config_source.blob) |
1d28ff4c | 746 | die(_("editing blobs is not supported")); |
d64ec16c | 747 | git_config(git_default_config, NULL); |
d9c69644 JK |
748 | config_file = given_config_source.file ? |
749 | xstrdup(given_config_source.file) : | |
750 | git_pathdup("config"); | |
9830534e MM |
751 | if (use_global_config) { |
752 | int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
aabbd3f3 | 753 | if (fd >= 0) { |
9830534e MM |
754 | char *content = default_user_config(); |
755 | write_str_in_full(fd, content); | |
756 | free(content); | |
757 | close(fd); | |
758 | } | |
759 | else if (errno != EEXIST) | |
760 | die_errno(_("cannot create configuration file %s"), config_file); | |
761 | } | |
762 | launch_editor(config_file, NULL, NULL); | |
3696a7c2 | 763 | free(config_file); |
d64ec16c FC |
764 | } |
765 | else if (actions == ACTION_SET) { | |
5a2df368 | 766 | int ret; |
6aea9f0f | 767 | check_write(); |
d64ec16c FC |
768 | check_argc(argc, 2, 2); |
769 | value = normalize_value(argv[0], argv[1]); | |
0e5bba53 | 770 | UNLEAK(value); |
30598ad0 | 771 | ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value); |
5a2df368 | 772 | if (ret == CONFIG_NOTHING_SET) |
ccf63801 VA |
773 | error(_("cannot overwrite multiple values with a single value\n" |
774 | " Use a regexp, --add or --replace-all to change %s."), argv[0]); | |
5a2df368 | 775 | return ret; |
d64ec16c FC |
776 | } |
777 | else if (actions == ACTION_SET_ALL) { | |
6aea9f0f | 778 | check_write(); |
d64ec16c FC |
779 | check_argc(argc, 2, 3); |
780 | value = normalize_value(argv[0], argv[1]); | |
0e5bba53 | 781 | UNLEAK(value); |
30598ad0 PS |
782 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
783 | argv[0], value, argv[2], 0); | |
d64ec16c FC |
784 | } |
785 | else if (actions == ACTION_ADD) { | |
6aea9f0f | 786 | check_write(); |
d64ec16c FC |
787 | check_argc(argc, 2, 2); |
788 | value = normalize_value(argv[0], argv[1]); | |
0e5bba53 | 789 | UNLEAK(value); |
30598ad0 PS |
790 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
791 | argv[0], value, | |
792 | CONFIG_REGEX_NONE, 0); | |
d64ec16c FC |
793 | } |
794 | else if (actions == ACTION_REPLACE_ALL) { | |
6aea9f0f | 795 | check_write(); |
d64ec16c FC |
796 | check_argc(argc, 2, 3); |
797 | value = normalize_value(argv[0], argv[1]); | |
0e5bba53 | 798 | UNLEAK(value); |
30598ad0 PS |
799 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
800 | argv[0], value, argv[2], 1); | |
d64ec16c FC |
801 | } |
802 | else if (actions == ACTION_GET) { | |
803 | check_argc(argc, 1, 2); | |
804 | return get_value(argv[0], argv[1]); | |
805 | } | |
806 | else if (actions == ACTION_GET_ALL) { | |
807 | do_all = 1; | |
808 | check_argc(argc, 1, 2); | |
809 | return get_value(argv[0], argv[1]); | |
810 | } | |
811 | else if (actions == ACTION_GET_REGEXP) { | |
812 | show_keys = 1; | |
813 | use_key_regexp = 1; | |
814 | do_all = 1; | |
815 | check_argc(argc, 1, 2); | |
816 | return get_value(argv[0], argv[1]); | |
817 | } | |
d4770964 JH |
818 | else if (actions == ACTION_GET_URLMATCH) { |
819 | check_argc(argc, 2, 2); | |
820 | return get_urlmatch(argv[0], argv[1]); | |
821 | } | |
d64ec16c | 822 | else if (actions == ACTION_UNSET) { |
6aea9f0f | 823 | check_write(); |
d64ec16c FC |
824 | check_argc(argc, 1, 2); |
825 | if (argc == 2) | |
30598ad0 PS |
826 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
827 | argv[0], NULL, argv[1], 0); | |
d64ec16c | 828 | else |
30598ad0 PS |
829 | return git_config_set_in_file_gently(given_config_source.file, |
830 | argv[0], NULL); | |
d64ec16c FC |
831 | } |
832 | else if (actions == ACTION_UNSET_ALL) { | |
6aea9f0f | 833 | check_write(); |
d64ec16c | 834 | check_argc(argc, 1, 2); |
30598ad0 PS |
835 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
836 | argv[0], NULL, argv[1], 1); | |
d64ec16c FC |
837 | } |
838 | else if (actions == ACTION_RENAME_SECTION) { | |
839 | int ret; | |
6aea9f0f | 840 | check_write(); |
d64ec16c | 841 | check_argc(argc, 2, 2); |
c8985ce0 | 842 | ret = git_config_rename_section_in_file(given_config_source.file, |
270a3443 | 843 | argv[0], argv[1]); |
d64ec16c FC |
844 | if (ret < 0) |
845 | return ret; | |
846 | if (ret == 0) | |
1d28ff4c | 847 | die(_("no such section: %s"), argv[0]); |
d64ec16c FC |
848 | } |
849 | else if (actions == ACTION_REMOVE_SECTION) { | |
850 | int ret; | |
6aea9f0f | 851 | check_write(); |
d64ec16c | 852 | check_argc(argc, 1, 1); |
c8985ce0 | 853 | ret = git_config_rename_section_in_file(given_config_source.file, |
270a3443 | 854 | argv[0], NULL); |
d64ec16c FC |
855 | if (ret < 0) |
856 | return ret; | |
857 | if (ret == 0) | |
1d28ff4c | 858 | die(_("no such section: %s"), argv[0]); |
d64ec16c FC |
859 | } |
860 | else if (actions == ACTION_GET_COLOR) { | |
d0e08d62 JK |
861 | check_argc(argc, 1, 2); |
862 | get_color(argv[0], argv[1]); | |
d64ec16c FC |
863 | } |
864 | else if (actions == ACTION_GET_COLORBOOL) { | |
d0e08d62 JK |
865 | check_argc(argc, 1, 2); |
866 | if (argc == 2) | |
867 | color_stdout_is_tty = git_config_bool("command line", argv[1]); | |
868 | return get_colorbool(argv[0], argc == 2); | |
d64ec16c FC |
869 | } |
870 | ||
1b1e59c5 JS |
871 | return 0; |
872 | } |