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