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