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