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