]>
Commit | Line | Data |
---|---|---|
10bea152 JS |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | * Copyright (C) Johannes Schindelin, 2005 | |
6 | * | |
7 | */ | |
17712991 | 8 | #include "cache.h" |
88c7b4c3 | 9 | #include "date.h" |
e730b81d | 10 | #include "branch.h" |
b2141fc1 | 11 | #include "config.h" |
d8d77153 | 12 | #include "environment.h" |
3b256228 | 13 | #include "repository.h" |
697cc8ef | 14 | #include "lockfile.h" |
d807c4a0 | 15 | #include "exec-cmd.h" |
572e4f6a | 16 | #include "strbuf.h" |
2b64fc89 | 17 | #include "quote.h" |
3c8687a7 TA |
18 | #include "hashmap.h" |
19 | #include "string-list.h" | |
cbd53a21 | 20 | #include "object-store.h" |
599446dc | 21 | #include "utf8.h" |
3efd0bed | 22 | #include "dir.h" |
6d2f9acc | 23 | #include "color.h" |
07b2c0ea | 24 | #include "refs.h" |
fe187339 | 25 | #include "worktree.h" |
17712991 | 26 | |
4d8dd149 HV |
27 | struct config_source { |
28 | struct config_source *prev; | |
29 | union { | |
30 | FILE *file; | |
1bc88819 HV |
31 | struct config_buf { |
32 | const char *buf; | |
33 | size_t len; | |
34 | size_t pos; | |
35 | } buf; | |
4d8dd149 | 36 | } u; |
1b8132d9 | 37 | enum config_origin_type origin_type; |
924aaf3e | 38 | const char *name; |
d14d4244 | 39 | const char *path; |
66f97228 | 40 | enum config_error_action default_error_action; |
924aaf3e RJ |
41 | int linenr; |
42 | int eof; | |
348482de | 43 | size_t total_len; |
924aaf3e | 44 | struct strbuf value; |
0971e992 | 45 | struct strbuf var; |
2d84f13d | 46 | unsigned subsection_case_sensitive : 1; |
924aaf3e | 47 | |
49d6cfa5 JK |
48 | int (*do_fgetc)(struct config_source *c); |
49 | int (*do_ungetc)(int c, struct config_source *conf); | |
50 | long (*do_ftell)(struct config_source *c); | |
4d8dd149 HV |
51 | }; |
52 | ||
0d44a2da JK |
53 | /* |
54 | * These variables record the "current" config source, which | |
55 | * can be accessed by parsing callbacks. | |
56 | * | |
57 | * The "cf" variable will be non-NULL only when we are actually parsing a real | |
58 | * config source (file, blob, cmdline, etc). | |
59 | * | |
60 | * The "current_config_kvi" variable will be non-NULL only when we are feeding | |
61 | * cached config from a configset into a callback. | |
62 | * | |
63 | * They should generally never be non-NULL at the same time. If they are both | |
64 | * NULL, then we aren't parsing anything (and depending on the function looking | |
65 | * at the variables, it's either a bug for it to be called in the first place, | |
66 | * or it's a function which can be reused for non-config purposes, and should | |
67 | * fall back to some sane behavior). | |
68 | */ | |
4d8dd149 | 69 | static struct config_source *cf; |
0d44a2da | 70 | static struct key_value_info *current_config_kvi; |
924aaf3e | 71 | |
9acc5911 JK |
72 | /* |
73 | * Similar to the variables above, this gives access to the "scope" of the | |
74 | * current value (repo, global, etc). For cached values, it can be found via | |
75 | * the current_config_kvi as above. During parsing, the current value can be | |
76 | * found in this variable. It's not part of "cf" because it transcends a single | |
77 | * file (i.e., a file included from .git/config is still in "repo" scope). | |
78 | */ | |
79 | static enum config_scope current_parsing_scope; | |
924aaf3e | 80 | |
8de7eeb5 | 81 | static int pack_compression_seen; |
960ccca6 DH |
82 | static int zlib_compression_seen; |
83 | ||
5b3c6507 GC |
84 | /* |
85 | * Config that comes from trusted scopes, namely: | |
86 | * - CONFIG_SCOPE_SYSTEM (e.g. /etc/gitconfig) | |
87 | * - CONFIG_SCOPE_GLOBAL (e.g. $HOME/.gitconfig, $XDG_CONFIG_HOME/git) | |
88 | * - CONFIG_SCOPE_COMMAND (e.g. "-c" option, environment variables) | |
89 | * | |
90 | * This is declared here for code cleanliness, but unlike the other | |
91 | * static variables, this does not hold config parser state. | |
92 | */ | |
93 | static struct config_set protected_config; | |
94 | ||
4d8dd149 HV |
95 | static int config_file_fgetc(struct config_source *conf) |
96 | { | |
260d408e | 97 | return getc_unlocked(conf->u.file); |
4d8dd149 HV |
98 | } |
99 | ||
100 | static int config_file_ungetc(int c, struct config_source *conf) | |
101 | { | |
102 | return ungetc(c, conf->u.file); | |
103 | } | |
104 | ||
105 | static long config_file_ftell(struct config_source *conf) | |
106 | { | |
107 | return ftell(conf->u.file); | |
108 | } | |
109 | ||
1bc88819 HV |
110 | |
111 | static int config_buf_fgetc(struct config_source *conf) | |
112 | { | |
113 | if (conf->u.buf.pos < conf->u.buf.len) | |
114 | return conf->u.buf.buf[conf->u.buf.pos++]; | |
115 | ||
116 | return EOF; | |
117 | } | |
118 | ||
119 | static int config_buf_ungetc(int c, struct config_source *conf) | |
120 | { | |
1d0655c1 JK |
121 | if (conf->u.buf.pos > 0) { |
122 | conf->u.buf.pos--; | |
123 | if (conf->u.buf.buf[conf->u.buf.pos] != c) | |
033abf97 | 124 | BUG("config_buf can only ungetc the same character"); |
1d0655c1 JK |
125 | return c; |
126 | } | |
1bc88819 HV |
127 | |
128 | return EOF; | |
129 | } | |
130 | ||
131 | static long config_buf_ftell(struct config_source *conf) | |
132 | { | |
133 | return conf->u.buf.pos; | |
134 | } | |
135 | ||
ed69e11b JT |
136 | struct config_include_data { |
137 | int depth; | |
138 | config_fn_t fn; | |
139 | void *data; | |
140 | const struct config_options *opts; | |
399b1984 JT |
141 | struct git_config_source *config_source; |
142 | ||
143 | /* | |
144 | * All remote URLs discovered when reading all config files. | |
145 | */ | |
146 | struct string_list *remote_urls; | |
ed69e11b JT |
147 | }; |
148 | #define CONFIG_INCLUDE_INIT { 0 } | |
149 | ||
150 | static int git_config_include(const char *var, const char *value, void *data); | |
151 | ||
9b25a0b5 | 152 | #define MAX_INCLUDE_DEPTH 10 |
a769bfc7 | 153 | static const char include_depth_advice[] = N_( |
9b25a0b5 JK |
154 | "exceeded maximum include depth (%d) while including\n" |
155 | " %s\n" | |
156 | "from\n" | |
157 | " %s\n" | |
27c929ed | 158 | "This might be due to circular includes."); |
9b25a0b5 JK |
159 | static int handle_path_include(const char *path, struct config_include_data *inc) |
160 | { | |
161 | int ret = 0; | |
162 | struct strbuf buf = STRBUF_INIT; | |
67beb600 | 163 | char *expanded; |
4c0a89fc | 164 | |
67beb600 JK |
165 | if (!path) |
166 | return config_error_nonbool("include.path"); | |
167 | ||
a03b097d | 168 | expanded = interpolate_path(path, 0); |
4c0a89fc | 169 | if (!expanded) |
a769bfc7 | 170 | return error(_("could not expand include path '%s'"), path); |
4c0a89fc | 171 | path = expanded; |
9b25a0b5 JK |
172 | |
173 | /* | |
174 | * Use an absolute path as-is, but interpret relative paths | |
175 | * based on the including config file. | |
176 | */ | |
177 | if (!is_absolute_path(path)) { | |
178 | char *slash; | |
179 | ||
25ad7221 ÆAB |
180 | if (!cf || !cf->path) { |
181 | ret = error(_("relative config includes must come from files")); | |
182 | goto cleanup; | |
183 | } | |
9b25a0b5 | 184 | |
d14d4244 | 185 | slash = find_last_dir_sep(cf->path); |
9b25a0b5 | 186 | if (slash) |
d14d4244 | 187 | strbuf_add(&buf, cf->path, slash - cf->path + 1); |
9b25a0b5 JK |
188 | strbuf_addstr(&buf, path); |
189 | path = buf.buf; | |
190 | } | |
191 | ||
4698c8fe | 192 | if (!access_or_die(path, R_OK, 0)) { |
9b25a0b5 | 193 | if (++inc->depth > MAX_INCLUDE_DEPTH) |
a769bfc7 | 194 | die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path, |
3258258f JK |
195 | !cf ? "<unknown>" : |
196 | cf->name ? cf->name : | |
197 | "the command line"); | |
9b25a0b5 JK |
198 | ret = git_config_from_file(git_config_include, path, inc); |
199 | inc->depth--; | |
200 | } | |
25ad7221 | 201 | cleanup: |
9b25a0b5 | 202 | strbuf_release(&buf); |
4c0a89fc | 203 | free(expanded); |
9b25a0b5 JK |
204 | return ret; |
205 | } | |
206 | ||
07b2c0ea DL |
207 | static void add_trailing_starstar_for_dir(struct strbuf *pat) |
208 | { | |
209 | if (pat->len && is_dir_sep(pat->buf[pat->len - 1])) | |
210 | strbuf_addstr(pat, "**"); | |
211 | } | |
212 | ||
3efd0bed NTND |
213 | static int prepare_include_condition_pattern(struct strbuf *pat) |
214 | { | |
215 | struct strbuf path = STRBUF_INIT; | |
216 | char *expanded; | |
217 | int prefix = 0; | |
218 | ||
a03b097d | 219 | expanded = interpolate_path(pat->buf, 1); |
3efd0bed NTND |
220 | if (expanded) { |
221 | strbuf_reset(pat); | |
222 | strbuf_addstr(pat, expanded); | |
223 | free(expanded); | |
224 | } | |
225 | ||
226 | if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) { | |
227 | const char *slash; | |
228 | ||
229 | if (!cf || !cf->path) | |
230 | return error(_("relative config include " | |
231 | "conditionals must come from files")); | |
232 | ||
86f95157 | 233 | strbuf_realpath(&path, cf->path, 1); |
3efd0bed NTND |
234 | slash = find_last_dir_sep(path.buf); |
235 | if (!slash) | |
033abf97 | 236 | BUG("how is this possible?"); |
3efd0bed NTND |
237 | strbuf_splice(pat, 0, 1, path.buf, slash - path.buf); |
238 | prefix = slash - path.buf + 1 /* slash */; | |
239 | } else if (!is_absolute_path(pat->buf)) | |
a91cc7fa | 240 | strbuf_insertstr(pat, 0, "**/"); |
3efd0bed | 241 | |
07b2c0ea | 242 | add_trailing_starstar_for_dir(pat); |
3efd0bed NTND |
243 | |
244 | strbuf_release(&path); | |
245 | return prefix; | |
246 | } | |
247 | ||
2185fde5 NTND |
248 | static int include_by_gitdir(const struct config_options *opts, |
249 | const char *cond, size_t cond_len, int icase) | |
3efd0bed NTND |
250 | { |
251 | struct strbuf text = STRBUF_INIT; | |
252 | struct strbuf pattern = STRBUF_INIT; | |
253 | int ret = 0, prefix; | |
2185fde5 | 254 | const char *git_dir; |
0624c63c | 255 | int already_tried_absolute = 0; |
3efd0bed | 256 | |
2185fde5 NTND |
257 | if (opts->git_dir) |
258 | git_dir = opts->git_dir; | |
2185fde5 NTND |
259 | else |
260 | goto done; | |
261 | ||
c9672ba4 | 262 | strbuf_realpath(&text, git_dir, 1); |
3efd0bed NTND |
263 | strbuf_add(&pattern, cond, cond_len); |
264 | prefix = prepare_include_condition_pattern(&pattern); | |
265 | ||
0624c63c | 266 | again: |
3efd0bed NTND |
267 | if (prefix < 0) |
268 | goto done; | |
269 | ||
270 | if (prefix > 0) { | |
271 | /* | |
272 | * perform literal matching on the prefix part so that | |
273 | * any wildcard character in it can't create side effects. | |
274 | */ | |
275 | if (text.len < prefix) | |
276 | goto done; | |
277 | if (!icase && strncmp(pattern.buf, text.buf, prefix)) | |
278 | goto done; | |
279 | if (icase && strncasecmp(pattern.buf, text.buf, prefix)) | |
280 | goto done; | |
281 | } | |
282 | ||
283 | ret = !wildmatch(pattern.buf + prefix, text.buf + prefix, | |
19e7fdaa | 284 | WM_PATHNAME | (icase ? WM_CASEFOLD : 0)); |
3efd0bed | 285 | |
0624c63c ÆAB |
286 | if (!ret && !already_tried_absolute) { |
287 | /* | |
288 | * We've tried e.g. matching gitdir:~/work, but if | |
289 | * ~/work is a symlink to /mnt/storage/work | |
290 | * strbuf_realpath() will expand it, so the rule won't | |
291 | * match. Let's match against a | |
292 | * strbuf_add_absolute_path() version of the path, | |
293 | * which'll do the right thing | |
294 | */ | |
295 | strbuf_reset(&text); | |
296 | strbuf_add_absolute_path(&text, git_dir); | |
297 | already_tried_absolute = 1; | |
298 | goto again; | |
299 | } | |
3efd0bed NTND |
300 | done: |
301 | strbuf_release(&pattern); | |
302 | strbuf_release(&text); | |
303 | return ret; | |
304 | } | |
305 | ||
07b2c0ea DL |
306 | static int include_by_branch(const char *cond, size_t cond_len) |
307 | { | |
308 | int flags; | |
309 | int ret; | |
310 | struct strbuf pattern = STRBUF_INIT; | |
22932d91 | 311 | const char *refname = !the_repository->gitdir ? |
85fe0e80 | 312 | NULL : resolve_ref_unsafe("HEAD", 0, NULL, &flags); |
07b2c0ea DL |
313 | const char *shortname; |
314 | ||
315 | if (!refname || !(flags & REF_ISSYMREF) || | |
316 | !skip_prefix(refname, "refs/heads/", &shortname)) | |
317 | return 0; | |
318 | ||
319 | strbuf_add(&pattern, cond, cond_len); | |
320 | add_trailing_starstar_for_dir(&pattern); | |
321 | ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME); | |
322 | strbuf_release(&pattern); | |
323 | return ret; | |
324 | } | |
325 | ||
399b1984 JT |
326 | static int add_remote_url(const char *var, const char *value, void *data) |
327 | { | |
328 | struct string_list *remote_urls = data; | |
329 | const char *remote_name; | |
330 | size_t remote_name_len; | |
331 | const char *key; | |
332 | ||
333 | if (!parse_config_key(var, "remote", &remote_name, &remote_name_len, | |
334 | &key) && | |
335 | remote_name && | |
336 | !strcmp(key, "url")) | |
337 | string_list_append(remote_urls, value); | |
338 | return 0; | |
339 | } | |
340 | ||
341 | static void populate_remote_urls(struct config_include_data *inc) | |
342 | { | |
343 | struct config_options opts; | |
344 | ||
345 | struct config_source *store_cf = cf; | |
346 | struct key_value_info *store_kvi = current_config_kvi; | |
347 | enum config_scope store_scope = current_parsing_scope; | |
348 | ||
349 | opts = *inc->opts; | |
350 | opts.unconditional_remote_url = 1; | |
351 | ||
352 | cf = NULL; | |
353 | current_config_kvi = NULL; | |
354 | current_parsing_scope = 0; | |
355 | ||
356 | inc->remote_urls = xmalloc(sizeof(*inc->remote_urls)); | |
357 | string_list_init_dup(inc->remote_urls); | |
358 | config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts); | |
359 | ||
360 | cf = store_cf; | |
361 | current_config_kvi = store_kvi; | |
362 | current_parsing_scope = store_scope; | |
363 | } | |
364 | ||
365 | static int forbid_remote_url(const char *var, const char *value, void *data) | |
366 | { | |
367 | const char *remote_name; | |
368 | size_t remote_name_len; | |
369 | const char *key; | |
370 | ||
371 | if (!parse_config_key(var, "remote", &remote_name, &remote_name_len, | |
372 | &key) && | |
373 | remote_name && | |
374 | !strcmp(key, "url")) | |
375 | die(_("remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url")); | |
376 | return 0; | |
377 | } | |
378 | ||
379 | static int at_least_one_url_matches_glob(const char *glob, int glob_len, | |
380 | struct string_list *remote_urls) | |
381 | { | |
382 | struct strbuf pattern = STRBUF_INIT; | |
383 | struct string_list_item *url_item; | |
384 | int found = 0; | |
385 | ||
386 | strbuf_add(&pattern, glob, glob_len); | |
387 | for_each_string_list_item(url_item, remote_urls) { | |
388 | if (!wildmatch(pattern.buf, url_item->string, WM_PATHNAME)) { | |
389 | found = 1; | |
390 | break; | |
391 | } | |
392 | } | |
393 | strbuf_release(&pattern); | |
394 | return found; | |
395 | } | |
396 | ||
397 | static int include_by_remote_url(struct config_include_data *inc, | |
398 | const char *cond, size_t cond_len) | |
399 | { | |
400 | if (inc->opts->unconditional_remote_url) | |
401 | return 1; | |
402 | if (!inc->remote_urls) | |
403 | populate_remote_urls(inc); | |
404 | return at_least_one_url_matches_glob(cond, cond_len, | |
405 | inc->remote_urls); | |
406 | } | |
407 | ||
408 | static int include_condition_is_true(struct config_include_data *inc, | |
2185fde5 | 409 | const char *cond, size_t cond_len) |
3efd0bed | 410 | { |
399b1984 | 411 | const struct config_options *opts = inc->opts; |
3efd0bed NTND |
412 | |
413 | if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len)) | |
2185fde5 | 414 | return include_by_gitdir(opts, cond, cond_len, 0); |
3efd0bed | 415 | else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len)) |
2185fde5 | 416 | return include_by_gitdir(opts, cond, cond_len, 1); |
07b2c0ea DL |
417 | else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len)) |
418 | return include_by_branch(cond, cond_len); | |
399b1984 JT |
419 | else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond, |
420 | &cond_len)) | |
421 | return include_by_remote_url(inc, cond, cond_len); | |
3efd0bed NTND |
422 | |
423 | /* unknown conditionals are always false */ | |
424 | return 0; | |
425 | } | |
426 | ||
ed69e11b | 427 | static int git_config_include(const char *var, const char *value, void *data) |
9b25a0b5 JK |
428 | { |
429 | struct config_include_data *inc = data; | |
3efd0bed | 430 | const char *cond, *key; |
f5914f4b | 431 | size_t cond_len; |
9b25a0b5 JK |
432 | int ret; |
433 | ||
434 | /* | |
435 | * Pass along all values, including "include" directives; this makes it | |
436 | * possible to query information on the includes themselves. | |
437 | */ | |
438 | ret = inc->fn(var, value, inc->data); | |
439 | if (ret < 0) | |
440 | return ret; | |
441 | ||
37007c3a | 442 | if (!strcmp(var, "include.path")) |
9b25a0b5 | 443 | ret = handle_path_include(value, inc); |
3efd0bed NTND |
444 | |
445 | if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) && | |
399b1984 JT |
446 | cond && include_condition_is_true(inc, cond, cond_len) && |
447 | !strcmp(key, "path")) { | |
448 | config_fn_t old_fn = inc->fn; | |
449 | ||
450 | if (inc->opts->unconditional_remote_url) | |
451 | inc->fn = forbid_remote_url; | |
3efd0bed | 452 | ret = handle_path_include(value, inc); |
399b1984 JT |
453 | inc->fn = old_fn; |
454 | } | |
3efd0bed | 455 | |
9b25a0b5 JK |
456 | return ret; |
457 | } | |
458 | ||
1ff21c05 | 459 | static void git_config_push_split_parameter(const char *key, const char *value) |
2b64fc89 JK |
460 | { |
461 | struct strbuf env = STRBUF_INIT; | |
462 | const char *old = getenv(CONFIG_DATA_ENVIRONMENT); | |
d1f88498 | 463 | if (old && *old) { |
2b64fc89 JK |
464 | strbuf_addstr(&env, old); |
465 | strbuf_addch(&env, ' '); | |
466 | } | |
1ff21c05 PS |
467 | sq_quote_buf(&env, key); |
468 | strbuf_addch(&env, '='); | |
469 | if (value) | |
470 | sq_quote_buf(&env, value); | |
2b64fc89 JK |
471 | setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1); |
472 | strbuf_release(&env); | |
473 | } | |
474 | ||
1ff21c05 PS |
475 | void git_config_push_parameter(const char *text) |
476 | { | |
477 | const char *value; | |
478 | ||
479 | /* | |
480 | * When we see: | |
481 | * | |
482 | * section.subsection=with=equals.key=value | |
483 | * | |
484 | * we cannot tell if it means: | |
485 | * | |
486 | * [section "subsection=with=equals"] | |
487 | * key = value | |
488 | * | |
489 | * or: | |
490 | * | |
491 | * [section] | |
492 | * subsection = with=equals.key=value | |
493 | * | |
494 | * We parse left-to-right for the first "=", meaning we'll prefer to | |
495 | * keep the value intact over the subsection. This is historical, but | |
496 | * also sensible since values are more likely to contain odd or | |
497 | * untrusted input than a section name. | |
498 | * | |
499 | * A missing equals is explicitly allowed (as a bool-only entry). | |
500 | */ | |
501 | value = strchr(text, '='); | |
502 | if (value) { | |
503 | char *key = xmemdupz(text, value - text); | |
504 | git_config_push_split_parameter(key, value + 1); | |
505 | free(key); | |
506 | } else { | |
507 | git_config_push_split_parameter(text, NULL); | |
508 | } | |
509 | } | |
510 | ||
ce81b1da PS |
511 | void git_config_push_env(const char *spec) |
512 | { | |
1ff21c05 | 513 | char *key; |
ce81b1da PS |
514 | const char *env_name; |
515 | const char *env_value; | |
516 | ||
517 | env_name = strrchr(spec, '='); | |
518 | if (!env_name) | |
519 | die(_("invalid config format: %s"), spec); | |
1ff21c05 | 520 | key = xmemdupz(spec, env_name - spec); |
ce81b1da PS |
521 | env_name++; |
522 | if (!*env_name) | |
523 | die(_("missing environment variable name for configuration '%.*s'"), | |
524 | (int)(env_name - spec - 1), spec); | |
525 | ||
526 | env_value = getenv(env_name); | |
527 | if (!env_value) | |
528 | die(_("missing environment variable '%s' for configuration '%.*s'"), | |
529 | env_name, (int)(env_name - spec - 1), spec); | |
530 | ||
1ff21c05 PS |
531 | git_config_push_split_parameter(key, env_value); |
532 | free(key); | |
ce81b1da PS |
533 | } |
534 | ||
ee98df3f JH |
535 | static inline int iskeychar(int c) |
536 | { | |
537 | return isalnum(c) || c == '-'; | |
538 | } | |
539 | ||
540 | /* | |
541 | * Auxiliary function to sanity-check and split the key into the section | |
542 | * identifier and variable name. | |
543 | * | |
544 | * Returns 0 on success, -1 when there is an invalid character in the key and | |
545 | * -2 if there is no section name in the key. | |
546 | * | |
547 | * store_key - pointer to char* which will hold a copy of the key with | |
548 | * lowercase section and variable name | |
f011a965 | 549 | * baselen - pointer to size_t which will hold the length of the |
ee98df3f JH |
550 | * section + subsection part, can be NULL |
551 | */ | |
73c5f670 | 552 | int git_config_parse_key(const char *key, char **store_key, size_t *baselen_) |
ee98df3f | 553 | { |
f011a965 JK |
554 | size_t i, baselen; |
555 | int dot; | |
ee98df3f JH |
556 | const char *last_dot = strrchr(key, '.'); |
557 | ||
558 | /* | |
559 | * Since "key" actually contains the section name and the real | |
560 | * key name separated by a dot, we have to know where the dot is. | |
561 | */ | |
562 | ||
563 | if (last_dot == NULL || last_dot == key) { | |
73c5f670 | 564 | error(_("key does not contain a section: %s"), key); |
ee98df3f JH |
565 | return -CONFIG_NO_SECTION_OR_NAME; |
566 | } | |
567 | ||
568 | if (!last_dot[1]) { | |
73c5f670 | 569 | error(_("key does not contain variable name: %s"), key); |
ee98df3f JH |
570 | return -CONFIG_NO_SECTION_OR_NAME; |
571 | } | |
572 | ||
573 | baselen = last_dot - key; | |
574 | if (baselen_) | |
575 | *baselen_ = baselen; | |
576 | ||
577 | /* | |
578 | * Validate the key and while at it, lower case it for matching. | |
579 | */ | |
73c5f670 | 580 | *store_key = xmallocz(strlen(key)); |
ee98df3f JH |
581 | |
582 | dot = 0; | |
583 | for (i = 0; key[i]; i++) { | |
584 | unsigned char c = key[i]; | |
585 | if (c == '.') | |
586 | dot = 1; | |
587 | /* Leave the extended basename untouched.. */ | |
588 | if (!dot || i > baselen) { | |
589 | if (!iskeychar(c) || | |
590 | (i == baselen + 1 && !isalpha(c))) { | |
73c5f670 | 591 | error(_("invalid key: %s"), key); |
ee98df3f JH |
592 | goto out_free_ret_1; |
593 | } | |
594 | c = tolower(c); | |
595 | } else if (c == '\n') { | |
73c5f670 | 596 | error(_("invalid key (newline): %s"), key); |
ee98df3f JH |
597 | goto out_free_ret_1; |
598 | } | |
73c5f670 | 599 | (*store_key)[i] = c; |
ee98df3f JH |
600 | } |
601 | ||
602 | return 0; | |
603 | ||
604 | out_free_ret_1: | |
73c5f670 | 605 | FREE_AND_NULL(*store_key); |
ee98df3f JH |
606 | return -CONFIG_INVALID_KEY; |
607 | } | |
608 | ||
b342ae61 PS |
609 | static int config_parse_pair(const char *key, const char *value, |
610 | config_fn_t fn, void *data) | |
611 | { | |
612 | char *canonical_name; | |
613 | int ret; | |
614 | ||
615 | if (!strlen(key)) | |
616 | return error(_("empty config key")); | |
617 | if (git_config_parse_key(key, &canonical_name, NULL)) | |
618 | return -1; | |
619 | ||
620 | ret = (fn(canonical_name, value, data) < 0) ? -1 : 0; | |
621 | free(canonical_name); | |
622 | return ret; | |
623 | } | |
624 | ||
2496844b JK |
625 | int git_config_parse_parameter(const char *text, |
626 | config_fn_t fn, void *data) | |
8b1fa778 | 627 | { |
a789ca70 | 628 | const char *value; |
572e4f6a | 629 | struct strbuf **pair; |
1274a155 | 630 | int ret; |
a789ca70 | 631 | |
f77bccae | 632 | pair = strbuf_split_str(text, '=', 2); |
c5d6350b | 633 | if (!pair[0]) |
a769bfc7 | 634 | return error(_("bogus config parameter: %s"), text); |
a789ca70 JH |
635 | |
636 | if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') { | |
572e4f6a | 637 | strbuf_setlen(pair[0], pair[0]->len - 1); |
a789ca70 JH |
638 | value = pair[1] ? pair[1]->buf : ""; |
639 | } else { | |
640 | value = NULL; | |
641 | } | |
642 | ||
572e4f6a AR |
643 | strbuf_trim(pair[0]); |
644 | if (!pair[0]->len) { | |
645 | strbuf_list_free(pair); | |
a769bfc7 | 646 | return error(_("bogus config parameter: %s"), text); |
8b1fa778 | 647 | } |
1274a155 | 648 | |
b342ae61 | 649 | ret = config_parse_pair(pair[0]->buf, value, fn, data); |
572e4f6a | 650 | strbuf_list_free(pair); |
1274a155 | 651 | return ret; |
8b1fa778 AR |
652 | } |
653 | ||
f9dbb64f JK |
654 | static int parse_config_env_list(char *env, config_fn_t fn, void *data) |
655 | { | |
656 | char *cur = env; | |
657 | while (cur && *cur) { | |
658 | const char *key = sq_dequote_step(cur, &cur); | |
659 | if (!key) | |
660 | return error(_("bogus format in %s"), | |
661 | CONFIG_DATA_ENVIRONMENT); | |
662 | ||
663 | if (!cur || isspace(*cur)) { | |
664 | /* old-style 'key=value' */ | |
665 | if (git_config_parse_parameter(key, fn, data) < 0) | |
666 | return -1; | |
667 | } | |
668 | else if (*cur == '=') { | |
669 | /* new-style 'key'='value' */ | |
670 | const char *value; | |
671 | ||
672 | cur++; | |
673 | if (*cur == '\'') { | |
674 | /* quoted value */ | |
675 | value = sq_dequote_step(cur, &cur); | |
676 | if (!value || (cur && !isspace(*cur))) { | |
677 | return error(_("bogus format in %s"), | |
678 | CONFIG_DATA_ENVIRONMENT); | |
679 | } | |
680 | } else if (!*cur || isspace(*cur)) { | |
681 | /* implicit bool: 'key'= */ | |
682 | value = NULL; | |
683 | } else { | |
684 | return error(_("bogus format in %s"), | |
685 | CONFIG_DATA_ENVIRONMENT); | |
686 | } | |
687 | ||
688 | if (config_parse_pair(key, value, fn, data) < 0) | |
689 | return -1; | |
690 | } | |
691 | else { | |
692 | /* unknown format */ | |
693 | return error(_("bogus format in %s"), | |
694 | CONFIG_DATA_ENVIRONMENT); | |
695 | } | |
696 | ||
697 | if (cur) { | |
698 | while (isspace(*cur)) | |
699 | cur++; | |
700 | } | |
701 | } | |
702 | return 0; | |
703 | } | |
704 | ||
06eb708f JK |
705 | int git_config_from_parameters(config_fn_t fn, void *data) |
706 | { | |
d8d77153 PS |
707 | const char *env; |
708 | struct strbuf envvar = STRBUF_INIT; | |
709 | struct strvec to_free = STRVEC_INIT; | |
a77d6db6 | 710 | int ret = 0; |
d8d77153 | 711 | char *envw = NULL; |
3258258f | 712 | struct config_source source; |
2b64fc89 | 713 | |
3258258f JK |
714 | memset(&source, 0, sizeof(source)); |
715 | source.prev = cf; | |
1b8132d9 | 716 | source.origin_type = CONFIG_ORIGIN_CMDLINE; |
3258258f JK |
717 | cf = &source; |
718 | ||
d8d77153 PS |
719 | env = getenv(CONFIG_COUNT_ENVIRONMENT); |
720 | if (env) { | |
721 | unsigned long count; | |
722 | char *endp; | |
723 | int i; | |
2b64fc89 | 724 | |
d8d77153 PS |
725 | count = strtoul(env, &endp, 10); |
726 | if (*endp) { | |
727 | ret = error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT); | |
728 | goto out; | |
729 | } | |
730 | if (count > INT_MAX) { | |
731 | ret = error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT); | |
732 | goto out; | |
733 | } | |
734 | ||
735 | for (i = 0; i < count; i++) { | |
736 | const char *key, *value; | |
737 | ||
738 | strbuf_addf(&envvar, "GIT_CONFIG_KEY_%d", i); | |
739 | key = getenv_safe(&to_free, envvar.buf); | |
740 | if (!key) { | |
741 | ret = error(_("missing config key %s"), envvar.buf); | |
742 | goto out; | |
743 | } | |
744 | strbuf_reset(&envvar); | |
745 | ||
746 | strbuf_addf(&envvar, "GIT_CONFIG_VALUE_%d", i); | |
747 | value = getenv_safe(&to_free, envvar.buf); | |
748 | if (!value) { | |
749 | ret = error(_("missing config value %s"), envvar.buf); | |
750 | goto out; | |
751 | } | |
752 | strbuf_reset(&envvar); | |
753 | ||
754 | if (config_parse_pair(key, value, fn, data) < 0) { | |
755 | ret = -1; | |
756 | goto out; | |
757 | } | |
758 | } | |
2b64fc89 JK |
759 | } |
760 | ||
d8d77153 PS |
761 | env = getenv(CONFIG_DATA_ENVIRONMENT); |
762 | if (env) { | |
763 | /* sq_dequote will write over it */ | |
764 | envw = xstrdup(env); | |
765 | if (parse_config_env_list(envw, fn, data) < 0) { | |
a77d6db6 JK |
766 | ret = -1; |
767 | goto out; | |
2b64fc89 JK |
768 | } |
769 | } | |
770 | ||
a77d6db6 | 771 | out: |
d8d77153 PS |
772 | strbuf_release(&envvar); |
773 | strvec_clear(&to_free); | |
2b64fc89 | 774 | free(envw); |
3258258f | 775 | cf = source.prev; |
a77d6db6 | 776 | return ret; |
2b64fc89 JK |
777 | } |
778 | ||
17712991 LT |
779 | static int get_next_char(void) |
780 | { | |
49d6cfa5 | 781 | int c = cf->do_fgetc(cf); |
17712991 | 782 | |
dbb9a812 HV |
783 | if (c == '\r') { |
784 | /* DOS like systems */ | |
49d6cfa5 | 785 | c = cf->do_fgetc(cf); |
dbb9a812 | 786 | if (c != '\n') { |
5e0be134 JK |
787 | if (c != EOF) |
788 | cf->do_ungetc(c, cf); | |
dbb9a812 | 789 | c = '\r'; |
17712991 LT |
790 | } |
791 | } | |
348482de JK |
792 | |
793 | if (c != EOF && ++cf->total_len > INT_MAX) { | |
794 | /* | |
795 | * This is an absurdly long config file; refuse to parse | |
796 | * further in order to protect downstream code from integer | |
797 | * overflows. Note that we can't return an error specifically, | |
798 | * but we can mark EOF and put trash in the return value, | |
799 | * which will trigger a parse error. | |
800 | */ | |
801 | cf->eof = 1; | |
802 | return 0; | |
803 | } | |
804 | ||
dbb9a812 HV |
805 | if (c == '\n') |
806 | cf->linenr++; | |
807 | if (c == EOF) { | |
808 | cf->eof = 1; | |
b3b3f60b | 809 | cf->linenr++; |
dbb9a812 HV |
810 | c = '\n'; |
811 | } | |
17712991 LT |
812 | return c; |
813 | } | |
814 | ||
815 | static char *parse_value(void) | |
816 | { | |
e96c19c5 | 817 | int quote = 0, comment = 0, space = 0; |
17712991 | 818 | |
924aaf3e | 819 | strbuf_reset(&cf->value); |
17712991 LT |
820 | for (;;) { |
821 | int c = get_next_char(); | |
17712991 | 822 | if (c == '\n') { |
4b340593 MS |
823 | if (quote) { |
824 | cf->linenr--; | |
17712991 | 825 | return NULL; |
4b340593 | 826 | } |
924aaf3e | 827 | return cf->value.buf; |
17712991 LT |
828 | } |
829 | if (comment) | |
830 | continue; | |
831 | if (isspace(c) && !quote) { | |
924aaf3e | 832 | if (cf->value.len) |
ebdaae37 | 833 | space++; |
17712991 LT |
834 | continue; |
835 | } | |
7ebdba61 JS |
836 | if (!quote) { |
837 | if (c == ';' || c == '#') { | |
838 | comment = 1; | |
839 | continue; | |
840 | } | |
841 | } | |
ebdaae37 | 842 | for (; space; space--) |
924aaf3e | 843 | strbuf_addch(&cf->value, ' '); |
17712991 LT |
844 | if (c == '\\') { |
845 | c = get_next_char(); | |
846 | switch (c) { | |
847 | case '\n': | |
848 | continue; | |
849 | case 't': | |
850 | c = '\t'; | |
851 | break; | |
852 | case 'b': | |
853 | c = '\b'; | |
854 | break; | |
855 | case 'n': | |
856 | c = '\n'; | |
857 | break; | |
5cbb401d LT |
858 | /* Some characters escape as themselves */ |
859 | case '\\': case '"': | |
860 | break; | |
861 | /* Reject unknown escape sequences */ | |
862 | default: | |
863 | return NULL; | |
17712991 | 864 | } |
924aaf3e | 865 | strbuf_addch(&cf->value, c); |
17712991 LT |
866 | continue; |
867 | } | |
868 | if (c == '"') { | |
869 | quote = 1-quote; | |
870 | continue; | |
871 | } | |
924aaf3e | 872 | strbuf_addch(&cf->value, c); |
17712991 LT |
873 | } |
874 | } | |
875 | ||
0971e992 | 876 | static int get_value(config_fn_t fn, void *data, struct strbuf *name) |
17712991 LT |
877 | { |
878 | int c; | |
879 | char *value; | |
b3b3f60b | 880 | int ret; |
17712991 LT |
881 | |
882 | /* Get the full name */ | |
883 | for (;;) { | |
884 | c = get_next_char(); | |
924aaf3e | 885 | if (cf->eof) |
17712991 | 886 | break; |
38c5afa8 | 887 | if (!iskeychar(c)) |
17712991 | 888 | break; |
0971e992 | 889 | strbuf_addch(name, tolower(c)); |
17712991 | 890 | } |
0971e992 | 891 | |
17712991 LT |
892 | while (c == ' ' || c == '\t') |
893 | c = get_next_char(); | |
894 | ||
895 | value = NULL; | |
896 | if (c != '\n') { | |
897 | if (c != '=') | |
898 | return -1; | |
899 | value = parse_value(); | |
900 | if (!value) | |
901 | return -1; | |
902 | } | |
b3b3f60b MM |
903 | /* |
904 | * We already consumed the \n, but we need linenr to point to | |
905 | * the line we just parsed during the call to fn to get | |
906 | * accurate line number in error messages. | |
907 | */ | |
908 | cf->linenr--; | |
909 | ret = fn(name->buf, value, data); | |
e2e14251 JS |
910 | if (ret >= 0) |
911 | cf->linenr++; | |
b3b3f60b | 912 | return ret; |
17712991 LT |
913 | } |
914 | ||
0971e992 | 915 | static int get_extended_base_var(struct strbuf *name, int c) |
d14f7764 | 916 | { |
2d84f13d | 917 | cf->subsection_case_sensitive = 0; |
d14f7764 LT |
918 | do { |
919 | if (c == '\n') | |
4b340593 | 920 | goto error_incomplete_line; |
d14f7764 LT |
921 | c = get_next_char(); |
922 | } while (isspace(c)); | |
923 | ||
924 | /* We require the format to be '[base "extension"]' */ | |
925 | if (c != '"') | |
926 | return -1; | |
0971e992 | 927 | strbuf_addch(name, '.'); |
d14f7764 LT |
928 | |
929 | for (;;) { | |
930 | int c = get_next_char(); | |
931 | if (c == '\n') | |
4b340593 | 932 | goto error_incomplete_line; |
d14f7764 LT |
933 | if (c == '"') |
934 | break; | |
935 | if (c == '\\') { | |
936 | c = get_next_char(); | |
937 | if (c == '\n') | |
4b340593 | 938 | goto error_incomplete_line; |
d14f7764 | 939 | } |
0971e992 | 940 | strbuf_addch(name, c); |
d14f7764 LT |
941 | } |
942 | ||
943 | /* Final ']' */ | |
944 | if (get_next_char() != ']') | |
945 | return -1; | |
0971e992 | 946 | return 0; |
4b340593 MS |
947 | error_incomplete_line: |
948 | cf->linenr--; | |
949 | return -1; | |
d14f7764 LT |
950 | } |
951 | ||
0971e992 | 952 | static int get_base_var(struct strbuf *name) |
17712991 | 953 | { |
2d84f13d | 954 | cf->subsection_case_sensitive = 1; |
17712991 LT |
955 | for (;;) { |
956 | int c = get_next_char(); | |
924aaf3e | 957 | if (cf->eof) |
17712991 LT |
958 | return -1; |
959 | if (c == ']') | |
0971e992 | 960 | return 0; |
d14f7764 | 961 | if (isspace(c)) |
0971e992 | 962 | return get_extended_base_var(name, c); |
38c5afa8 | 963 | if (!iskeychar(c) && c != '.') |
17712991 | 964 | return -1; |
0971e992 | 965 | strbuf_addch(name, tolower(c)); |
17712991 LT |
966 | } |
967 | } | |
968 | ||
8032cc44 JS |
969 | struct parse_event_data { |
970 | enum config_event_t previous_type; | |
971 | size_t previous_offset; | |
972 | const struct config_options *opts; | |
973 | }; | |
974 | ||
975 | static int do_event(enum config_event_t type, struct parse_event_data *data) | |
976 | { | |
977 | size_t offset; | |
978 | ||
979 | if (!data->opts || !data->opts->event_fn) | |
980 | return 0; | |
981 | ||
982 | if (type == CONFIG_EVENT_WHITESPACE && | |
983 | data->previous_type == type) | |
984 | return 0; | |
985 | ||
986 | offset = cf->do_ftell(cf); | |
987 | /* | |
988 | * At EOF, the parser always "inserts" an extra '\n', therefore | |
989 | * the end offset of the event is the current file position, otherwise | |
990 | * we will already have advanced to the next event. | |
991 | */ | |
992 | if (type != CONFIG_EVENT_EOF) | |
993 | offset--; | |
994 | ||
995 | if (data->previous_type != CONFIG_EVENT_EOF && | |
996 | data->opts->event_fn(data->previous_type, data->previous_offset, | |
997 | offset, data->opts->event_fn_data) < 0) | |
998 | return -1; | |
999 | ||
1000 | data->previous_type = type; | |
1001 | data->previous_offset = offset; | |
1002 | ||
1003 | return 0; | |
1004 | } | |
1005 | ||
1006 | static int git_parse_source(config_fn_t fn, void *data, | |
1007 | const struct config_options *opts) | |
17712991 LT |
1008 | { |
1009 | int comment = 0; | |
6a9c235e | 1010 | size_t baselen = 0; |
0971e992 | 1011 | struct strbuf *var = &cf->var; |
1b8132d9 VA |
1012 | int error_return = 0; |
1013 | char *error_msg = NULL; | |
17712991 | 1014 | |
de056402 | 1015 | /* U+FEFF Byte Order Mark in UTF8 */ |
599446dc | 1016 | const char *bomptr = utf8_bom; |
de056402 | 1017 | |
8032cc44 JS |
1018 | /* For the parser event callback */ |
1019 | struct parse_event_data event_data = { | |
1020 | CONFIG_EVENT_EOF, 0, opts | |
1021 | }; | |
1022 | ||
17712991 | 1023 | for (;;) { |
8032cc44 JS |
1024 | int c; |
1025 | ||
1026 | c = get_next_char(); | |
de056402 PB |
1027 | if (bomptr && *bomptr) { |
1028 | /* We are at the file beginning; skip UTF8-encoded BOM | |
1029 | * if present. Sane editors won't put this in on their | |
1030 | * own, but e.g. Windows Notepad will do it happily. */ | |
599446dc | 1031 | if (c == (*bomptr & 0377)) { |
de056402 PB |
1032 | bomptr++; |
1033 | continue; | |
1034 | } else { | |
1035 | /* Do not tolerate partial BOM. */ | |
1036 | if (bomptr != utf8_bom) | |
1037 | break; | |
1038 | /* No BOM at file beginning. Cool. */ | |
1039 | bomptr = NULL; | |
1040 | } | |
1041 | } | |
17712991 | 1042 | if (c == '\n') { |
8032cc44 JS |
1043 | if (cf->eof) { |
1044 | if (do_event(CONFIG_EVENT_EOF, &event_data) < 0) | |
1045 | return -1; | |
17712991 | 1046 | return 0; |
8032cc44 JS |
1047 | } |
1048 | if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0) | |
1049 | return -1; | |
17712991 LT |
1050 | comment = 0; |
1051 | continue; | |
1052 | } | |
8032cc44 | 1053 | if (comment) |
17712991 | 1054 | continue; |
8032cc44 JS |
1055 | if (isspace(c)) { |
1056 | if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0) | |
1057 | return -1; | |
17712991 | 1058 | continue; |
8032cc44 | 1059 | } |
17712991 | 1060 | if (c == '#' || c == ';') { |
8032cc44 JS |
1061 | if (do_event(CONFIG_EVENT_COMMENT, &event_data) < 0) |
1062 | return -1; | |
17712991 LT |
1063 | comment = 1; |
1064 | continue; | |
1065 | } | |
1066 | if (c == '[') { | |
8032cc44 JS |
1067 | if (do_event(CONFIG_EVENT_SECTION, &event_data) < 0) |
1068 | return -1; | |
1069 | ||
0971e992 BW |
1070 | /* Reset prior to determining a new stem */ |
1071 | strbuf_reset(var); | |
1072 | if (get_base_var(var) < 0 || var->len < 1) | |
17712991 | 1073 | break; |
0971e992 BW |
1074 | strbuf_addch(var, '.'); |
1075 | baselen = var->len; | |
17712991 LT |
1076 | continue; |
1077 | } | |
1078 | if (!isalpha(c)) | |
1079 | break; | |
8032cc44 JS |
1080 | |
1081 | if (do_event(CONFIG_EVENT_ENTRY, &event_data) < 0) | |
1082 | return -1; | |
1083 | ||
0971e992 BW |
1084 | /* |
1085 | * Truncate the var name back to the section header | |
1086 | * stem prior to grabbing the suffix part of the name | |
1087 | * and the value. | |
1088 | */ | |
1089 | strbuf_setlen(var, baselen); | |
1090 | strbuf_addch(var, tolower(c)); | |
1091 | if (get_value(fn, data, var) < 0) | |
17712991 LT |
1092 | break; |
1093 | } | |
1b8132d9 | 1094 | |
8032cc44 JS |
1095 | if (do_event(CONFIG_EVENT_ERROR, &event_data) < 0) |
1096 | return -1; | |
1097 | ||
1b8132d9 VA |
1098 | switch (cf->origin_type) { |
1099 | case CONFIG_ORIGIN_BLOB: | |
1100 | error_msg = xstrfmt(_("bad config line %d in blob %s"), | |
1101 | cf->linenr, cf->name); | |
1102 | break; | |
1103 | case CONFIG_ORIGIN_FILE: | |
1104 | error_msg = xstrfmt(_("bad config line %d in file %s"), | |
1105 | cf->linenr, cf->name); | |
1106 | break; | |
1107 | case CONFIG_ORIGIN_STDIN: | |
1108 | error_msg = xstrfmt(_("bad config line %d in standard input"), | |
1109 | cf->linenr); | |
1110 | break; | |
1111 | case CONFIG_ORIGIN_SUBMODULE_BLOB: | |
1112 | error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"), | |
1113 | cf->linenr, cf->name); | |
1114 | break; | |
1115 | case CONFIG_ORIGIN_CMDLINE: | |
1116 | error_msg = xstrfmt(_("bad config line %d in command line %s"), | |
1117 | cf->linenr, cf->name); | |
1118 | break; | |
1119 | default: | |
1120 | error_msg = xstrfmt(_("bad config line %d in %s"), | |
1121 | cf->linenr, cf->name); | |
1122 | } | |
1123 | ||
66f97228 JK |
1124 | switch (opts && opts->error_action ? |
1125 | opts->error_action : | |
1126 | cf->default_error_action) { | |
1127 | case CONFIG_ERROR_DIE: | |
1b8132d9 | 1128 | die("%s", error_msg); |
66f97228 JK |
1129 | break; |
1130 | case CONFIG_ERROR_ERROR: | |
1b8132d9 | 1131 | error_return = error("%s", error_msg); |
66f97228 | 1132 | break; |
63583203 JK |
1133 | case CONFIG_ERROR_SILENT: |
1134 | error_return = -1; | |
1135 | break; | |
66f97228 JK |
1136 | case CONFIG_ERROR_UNSET: |
1137 | BUG("config error action unset"); | |
1138 | } | |
1b8132d9 VA |
1139 | |
1140 | free(error_msg); | |
1141 | return error_return; | |
17712991 LT |
1142 | } |
1143 | ||
39c575c9 | 1144 | static uintmax_t get_unit_factor(const char *end) |
0b87b6e0 BD |
1145 | { |
1146 | if (!*end) | |
1147 | return 1; | |
39c575c9 RS |
1148 | else if (!strcasecmp(end, "k")) |
1149 | return 1024; | |
1150 | else if (!strcasecmp(end, "m")) | |
1151 | return 1024 * 1024; | |
1152 | else if (!strcasecmp(end, "g")) | |
1153 | return 1024 * 1024 * 1024; | |
c8deb5a1 | 1154 | return 0; |
0b87b6e0 BD |
1155 | } |
1156 | ||
7192777d | 1157 | static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) |
0b87b6e0 BD |
1158 | { |
1159 | if (value && *value) { | |
1160 | char *end; | |
ebaa1bd4 NA |
1161 | intmax_t val; |
1162 | uintmax_t uval; | |
664178e8 | 1163 | uintmax_t factor; |
ebaa1bd4 NA |
1164 | |
1165 | errno = 0; | |
1166 | val = strtoimax(value, &end, 0); | |
1167 | if (errno == ERANGE) | |
1168 | return 0; | |
39c575c9 RS |
1169 | factor = get_unit_factor(end); |
1170 | if (!factor) { | |
33fdd77e | 1171 | errno = EINVAL; |
c8deb5a1 | 1172 | return 0; |
33fdd77e | 1173 | } |
9dae4fe7 | 1174 | uval = val < 0 ? -val : val; |
3fb72c7b RS |
1175 | if (unsigned_mult_overflows(factor, uval) || |
1176 | factor * uval > max) { | |
33fdd77e | 1177 | errno = ERANGE; |
ebaa1bd4 | 1178 | return 0; |
33fdd77e | 1179 | } |
ebaa1bd4 NA |
1180 | val *= factor; |
1181 | *ret = val; | |
0b87b6e0 BD |
1182 | return 1; |
1183 | } | |
33fdd77e | 1184 | errno = EINVAL; |
0b87b6e0 BD |
1185 | return 0; |
1186 | } | |
1187 | ||
0b4dc661 | 1188 | static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max) |
17712991 LT |
1189 | { |
1190 | if (value && *value) { | |
1191 | char *end; | |
ebaa1bd4 | 1192 | uintmax_t val; |
664178e8 | 1193 | uintmax_t factor; |
ebaa1bd4 NA |
1194 | |
1195 | errno = 0; | |
1196 | val = strtoumax(value, &end, 0); | |
1197 | if (errno == ERANGE) | |
1198 | return 0; | |
39c575c9 RS |
1199 | factor = get_unit_factor(end); |
1200 | if (!factor) { | |
33fdd77e | 1201 | errno = EINVAL; |
c8deb5a1 | 1202 | return 0; |
33fdd77e | 1203 | } |
3fb72c7b RS |
1204 | if (unsigned_mult_overflows(factor, val) || |
1205 | factor * val > max) { | |
33fdd77e | 1206 | errno = ERANGE; |
ebaa1bd4 | 1207 | return 0; |
33fdd77e | 1208 | } |
3fb72c7b | 1209 | val *= factor; |
c8deb5a1 | 1210 | *ret = val; |
0b87b6e0 | 1211 | return 1; |
17712991 | 1212 | } |
33fdd77e | 1213 | errno = EINVAL; |
0b87b6e0 BD |
1214 | return 0; |
1215 | } | |
1216 | ||
42d194e9 | 1217 | static int git_parse_int(const char *value, int *ret) |
c1867cea | 1218 | { |
7192777d | 1219 | intmax_t tmp; |
42d194e9 | 1220 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int))) |
7192777d JK |
1221 | return 0; |
1222 | *ret = tmp; | |
1223 | return 1; | |
1224 | } | |
1225 | ||
00160242 JK |
1226 | static int git_parse_int64(const char *value, int64_t *ret) |
1227 | { | |
1228 | intmax_t tmp; | |
1229 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t))) | |
1230 | return 0; | |
1231 | *ret = tmp; | |
1232 | return 1; | |
1233 | } | |
1234 | ||
7192777d JK |
1235 | int git_parse_ulong(const char *value, unsigned long *ret) |
1236 | { | |
1237 | uintmax_t tmp; | |
1238 | if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long))) | |
1239 | return 0; | |
1240 | *ret = tmp; | |
1241 | return 1; | |
1242 | } | |
1243 | ||
c79edf73 | 1244 | int git_parse_ssize_t(const char *value, ssize_t *ret) |
37ee680d DT |
1245 | { |
1246 | intmax_t tmp; | |
1247 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t))) | |
1248 | return 0; | |
1249 | *ret = tmp; | |
1250 | return 1; | |
1251 | } | |
1252 | ||
06bdc23b | 1253 | NORETURN |
2f666581 | 1254 | static void die_bad_number(const char *name, const char *value) |
c1867cea | 1255 | { |
2e43cd4c ÆAB |
1256 | const char *error_type = (errno == ERANGE) ? |
1257 | N_("out of range") : N_("invalid unit"); | |
1258 | const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s"); | |
078fe305 | 1259 | |
2f666581 JK |
1260 | if (!value) |
1261 | value = ""; | |
1262 | ||
1b8132d9 | 1263 | if (!(cf && cf->name)) |
2e43cd4c | 1264 | die(_(bad_numeric), value, name, _(error_type)); |
1b8132d9 VA |
1265 | |
1266 | switch (cf->origin_type) { | |
1267 | case CONFIG_ORIGIN_BLOB: | |
078fe305 | 1268 | die(_("bad numeric config value '%s' for '%s' in blob %s: %s"), |
2e43cd4c | 1269 | value, name, cf->name, _(error_type)); |
1b8132d9 | 1270 | case CONFIG_ORIGIN_FILE: |
078fe305 | 1271 | die(_("bad numeric config value '%s' for '%s' in file %s: %s"), |
2e43cd4c | 1272 | value, name, cf->name, _(error_type)); |
1b8132d9 | 1273 | case CONFIG_ORIGIN_STDIN: |
078fe305 | 1274 | die(_("bad numeric config value '%s' for '%s' in standard input: %s"), |
2e43cd4c | 1275 | value, name, _(error_type)); |
1b8132d9 | 1276 | case CONFIG_ORIGIN_SUBMODULE_BLOB: |
078fe305 | 1277 | die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"), |
2e43cd4c | 1278 | value, name, cf->name, _(error_type)); |
1b8132d9 | 1279 | case CONFIG_ORIGIN_CMDLINE: |
078fe305 | 1280 | die(_("bad numeric config value '%s' for '%s' in command line %s: %s"), |
2e43cd4c | 1281 | value, name, cf->name, _(error_type)); |
1b8132d9 | 1282 | default: |
078fe305 | 1283 | die(_("bad numeric config value '%s' for '%s' in %s: %s"), |
2e43cd4c | 1284 | value, name, cf->name, _(error_type)); |
1b8132d9 | 1285 | } |
c1867cea JK |
1286 | } |
1287 | ||
0b87b6e0 BD |
1288 | int git_config_int(const char *name, const char *value) |
1289 | { | |
42d194e9 JK |
1290 | int ret; |
1291 | if (!git_parse_int(value, &ret)) | |
2f666581 | 1292 | die_bad_number(name, value); |
0b87b6e0 BD |
1293 | return ret; |
1294 | } | |
1295 | ||
00160242 JK |
1296 | int64_t git_config_int64(const char *name, const char *value) |
1297 | { | |
1298 | int64_t ret; | |
1299 | if (!git_parse_int64(value, &ret)) | |
1300 | die_bad_number(name, value); | |
0b87b6e0 BD |
1301 | return ret; |
1302 | } | |
1303 | ||
1304 | unsigned long git_config_ulong(const char *name, const char *value) | |
1305 | { | |
1306 | unsigned long ret; | |
1307 | if (!git_parse_ulong(value, &ret)) | |
2f666581 | 1308 | die_bad_number(name, value); |
0b87b6e0 | 1309 | return ret; |
17712991 LT |
1310 | } |
1311 | ||
37ee680d DT |
1312 | ssize_t git_config_ssize_t(const char *name, const char *value) |
1313 | { | |
1314 | ssize_t ret; | |
1315 | if (!git_parse_ssize_t(value, &ret)) | |
1316 | die_bad_number(name, value); | |
1317 | return ret; | |
1318 | } | |
1319 | ||
9be04d64 | 1320 | static int git_parse_maybe_bool_text(const char *value) |
17712991 LT |
1321 | { |
1322 | if (!value) | |
1323 | return 1; | |
1324 | if (!*value) | |
1325 | return 0; | |
8420ccd8 JH |
1326 | if (!strcasecmp(value, "true") |
1327 | || !strcasecmp(value, "yes") | |
1328 | || !strcasecmp(value, "on")) | |
17712991 | 1329 | return 1; |
8420ccd8 JH |
1330 | if (!strcasecmp(value, "false") |
1331 | || !strcasecmp(value, "no") | |
1332 | || !strcasecmp(value, "off")) | |
17712991 | 1333 | return 0; |
8420ccd8 JH |
1334 | return -1; |
1335 | } | |
1336 | ||
844a8ad4 NS |
1337 | static const struct fsync_component_name { |
1338 | const char *name; | |
1339 | enum fsync_component component_bits; | |
1340 | } fsync_component_names[] = { | |
1341 | { "loose-object", FSYNC_COMPONENT_LOOSE_OBJECT }, | |
1342 | { "pack", FSYNC_COMPONENT_PACK }, | |
1343 | { "pack-metadata", FSYNC_COMPONENT_PACK_METADATA }, | |
1344 | { "commit-graph", FSYNC_COMPONENT_COMMIT_GRAPH }, | |
ba95e96d | 1345 | { "index", FSYNC_COMPONENT_INDEX }, |
b9f5d035 | 1346 | { "objects", FSYNC_COMPONENTS_OBJECTS }, |
bc22d845 | 1347 | { "reference", FSYNC_COMPONENT_REFERENCE }, |
b9f5d035 NS |
1348 | { "derived-metadata", FSYNC_COMPONENTS_DERIVED_METADATA }, |
1349 | { "committed", FSYNC_COMPONENTS_COMMITTED }, | |
1350 | { "added", FSYNC_COMPONENTS_ADDED }, | |
1351 | { "all", FSYNC_COMPONENTS_ALL }, | |
844a8ad4 NS |
1352 | }; |
1353 | ||
1354 | static enum fsync_component parse_fsync_components(const char *var, const char *string) | |
1355 | { | |
8a94d833 | 1356 | enum fsync_component current = FSYNC_COMPONENTS_PLATFORM_DEFAULT; |
844a8ad4 NS |
1357 | enum fsync_component positive = 0, negative = 0; |
1358 | ||
1359 | while (string) { | |
1360 | int i; | |
1361 | size_t len; | |
1362 | const char *ep; | |
1363 | int negated = 0; | |
1364 | int found = 0; | |
1365 | ||
1366 | string = string + strspn(string, ", \t\n\r"); | |
1367 | ep = strchrnul(string, ','); | |
1368 | len = ep - string; | |
1369 | if (!strcmp(string, "none")) { | |
1370 | current = FSYNC_COMPONENT_NONE; | |
1371 | goto next_name; | |
1372 | } | |
1373 | ||
1374 | if (*string == '-') { | |
1375 | negated = 1; | |
1376 | string++; | |
1377 | len--; | |
1378 | if (!len) | |
1379 | warning(_("invalid value for variable %s"), var); | |
1380 | } | |
1381 | ||
1382 | if (!len) | |
1383 | break; | |
1384 | ||
1385 | for (i = 0; i < ARRAY_SIZE(fsync_component_names); ++i) { | |
1386 | const struct fsync_component_name *n = &fsync_component_names[i]; | |
1387 | ||
1388 | if (strncmp(n->name, string, len)) | |
1389 | continue; | |
1390 | ||
1391 | found = 1; | |
1392 | if (negated) | |
1393 | negative |= n->component_bits; | |
1394 | else | |
1395 | positive |= n->component_bits; | |
1396 | } | |
1397 | ||
1398 | if (!found) { | |
1399 | char *component = xstrndup(string, len); | |
1400 | warning(_("ignoring unknown core.fsync component '%s'"), component); | |
1401 | free(component); | |
1402 | } | |
1403 | ||
1404 | next_name: | |
1405 | string = ep; | |
1406 | } | |
1407 | ||
1408 | return (current & ~negative) | positive; | |
1409 | } | |
1410 | ||
9be04d64 | 1411 | int git_parse_maybe_bool(const char *value) |
b2be2f6a | 1412 | { |
9be04d64 | 1413 | int v = git_parse_maybe_bool_text(value); |
b2be2f6a JK |
1414 | if (0 <= v) |
1415 | return v; | |
42d194e9 | 1416 | if (git_parse_int(value, &v)) |
db6195ef | 1417 | return !!v; |
b2be2f6a JK |
1418 | return -1; |
1419 | } | |
1420 | ||
8420ccd8 JH |
1421 | int git_config_bool_or_int(const char *name, const char *value, int *is_bool) |
1422 | { | |
9be04d64 | 1423 | int v = git_parse_maybe_bool_text(value); |
8420ccd8 JH |
1424 | if (0 <= v) { |
1425 | *is_bool = 1; | |
1426 | return v; | |
1427 | } | |
a53f2ec6 | 1428 | *is_bool = 0; |
c35b0b58 | 1429 | return git_config_int(name, value); |
17712991 LT |
1430 | } |
1431 | ||
a53f2ec6 JH |
1432 | int git_config_bool(const char *name, const char *value) |
1433 | { | |
f276e2a4 AK |
1434 | int v = git_parse_maybe_bool(value); |
1435 | if (v < 0) | |
39e12650 | 1436 | die(_("bad boolean config value '%s' for '%s'"), value, name); |
f276e2a4 | 1437 | return v; |
a53f2ec6 JH |
1438 | } |
1439 | ||
ea5105a5 CC |
1440 | int git_config_string(const char **dest, const char *var, const char *value) |
1441 | { | |
1442 | if (!value) | |
1443 | return config_error_nonbool(var); | |
1444 | *dest = xstrdup(value); | |
1445 | return 0; | |
1446 | } | |
1447 | ||
395de250 MM |
1448 | int git_config_pathname(const char **dest, const char *var, const char *value) |
1449 | { | |
1450 | if (!value) | |
1451 | return config_error_nonbool(var); | |
a03b097d | 1452 | *dest = interpolate_path(value, 0); |
395de250 | 1453 | if (!*dest) |
8262aaa2 | 1454 | die(_("failed to expand user dir in: '%s'"), value); |
395de250 MM |
1455 | return 0; |
1456 | } | |
1457 | ||
5f967424 HM |
1458 | int git_config_expiry_date(timestamp_t *timestamp, const char *var, const char *value) |
1459 | { | |
1460 | if (!value) | |
1461 | return config_error_nonbool(var); | |
1462 | if (parse_expiry_date(value, timestamp)) | |
1463 | return error(_("'%s' for '%s' is not a valid timestamp"), | |
1464 | value, var); | |
1465 | return 0; | |
1466 | } | |
1467 | ||
6d2f9acc TB |
1468 | int git_config_color(char *dest, const char *var, const char *value) |
1469 | { | |
1470 | if (!value) | |
1471 | return config_error_nonbool(var); | |
1472 | if (color_parse(value, dest) < 0) | |
1473 | return -1; | |
1474 | return 0; | |
1475 | } | |
1476 | ||
70fc5793 | 1477 | static int git_default_core_config(const char *var, const char *value, void *cb) |
17712991 LT |
1478 | { |
1479 | /* This needs a better name */ | |
1480 | if (!strcmp(var, "core.filemode")) { | |
1481 | trust_executable_bit = git_config_bool(var, value); | |
1482 | return 0; | |
1483 | } | |
1ce4790b AR |
1484 | if (!strcmp(var, "core.trustctime")) { |
1485 | trust_ctime = git_config_bool(var, value); | |
1486 | return 0; | |
1487 | } | |
c1b5d738 | 1488 | if (!strcmp(var, "core.checkstat")) { |
c08e4d5b RR |
1489 | if (!strcasecmp(value, "default")) |
1490 | check_stat = 1; | |
1491 | else if (!strcasecmp(value, "minimal")) | |
1492 | check_stat = 0; | |
1493 | } | |
17712991 | 1494 | |
9378c161 JH |
1495 | if (!strcmp(var, "core.quotepath")) { |
1496 | quote_path_fully = git_config_bool(var, value); | |
1497 | return 0; | |
1498 | } | |
1499 | ||
78a8d641 JS |
1500 | if (!strcmp(var, "core.symlinks")) { |
1501 | has_symlinks = git_config_bool(var, value); | |
1502 | return 0; | |
1503 | } | |
1504 | ||
0a9b88b7 LT |
1505 | if (!strcmp(var, "core.ignorecase")) { |
1506 | ignore_case = git_config_bool(var, value); | |
1507 | return 0; | |
1508 | } | |
1509 | ||
64589a03 JH |
1510 | if (!strcmp(var, "core.attributesfile")) |
1511 | return git_config_pathname(&git_attributes_file, var, value); | |
1512 | ||
867ad08a ÆAB |
1513 | if (!strcmp(var, "core.hookspath")) |
1514 | return git_config_pathname(&git_hooks_path, var, value); | |
1515 | ||
7d1864ce JH |
1516 | if (!strcmp(var, "core.bare")) { |
1517 | is_bare_repository_cfg = git_config_bool(var, value); | |
1518 | return 0; | |
1519 | } | |
1520 | ||
5f73076c JH |
1521 | if (!strcmp(var, "core.ignorestat")) { |
1522 | assume_unchanged = git_config_bool(var, value); | |
1523 | return 0; | |
1524 | } | |
1525 | ||
e388c738 JH |
1526 | if (!strcmp(var, "core.prefersymlinkrefs")) { |
1527 | prefer_symlink_refs = git_config_bool(var, value); | |
f8348be3 JS |
1528 | return 0; |
1529 | } | |
1530 | ||
6de08ae6 | 1531 | if (!strcmp(var, "core.logallrefupdates")) { |
341fb286 CW |
1532 | if (value && !strcasecmp(value, "always")) |
1533 | log_all_ref_updates = LOG_REFS_ALWAYS; | |
1534 | else if (git_config_bool(var, value)) | |
1535 | log_all_ref_updates = LOG_REFS_NORMAL; | |
1536 | else | |
1537 | log_all_ref_updates = LOG_REFS_NONE; | |
6de08ae6 SP |
1538 | return 0; |
1539 | } | |
1540 | ||
2f8acdb3 JH |
1541 | if (!strcmp(var, "core.warnambiguousrefs")) { |
1542 | warn_ambiguous_refs = git_config_bool(var, value); | |
1543 | return 0; | |
1544 | } | |
1545 | ||
a71f09fe | 1546 | if (!strcmp(var, "core.abbrev")) { |
48d5014d JH |
1547 | if (!value) |
1548 | return config_error_nonbool(var); | |
1549 | if (!strcasecmp(value, "auto")) | |
1550 | default_abbrev = -1; | |
a9ecaa06 EW |
1551 | else if (!git_parse_maybe_bool_text(value)) |
1552 | default_abbrev = the_hash_algo->hexsz; | |
48d5014d JH |
1553 | else { |
1554 | int abbrev = git_config_int(var, value); | |
fe9fec45 | 1555 | if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz) |
a769bfc7 | 1556 | return error(_("abbrev length out of range: %d"), abbrev); |
48d5014d JH |
1557 | default_abbrev = abbrev; |
1558 | } | |
dce96489 LT |
1559 | return 0; |
1560 | } | |
1561 | ||
5b33cb1f JK |
1562 | if (!strcmp(var, "core.disambiguate")) |
1563 | return set_disambiguate_hint_config(var, value); | |
1564 | ||
960ccca6 | 1565 | if (!strcmp(var, "core.loosecompression")) { |
12f6c308 JBH |
1566 | int level = git_config_int(var, value); |
1567 | if (level == -1) | |
1568 | level = Z_DEFAULT_COMPRESSION; | |
1569 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
8262aaa2 | 1570 | die(_("bad zlib compression level %d"), level); |
12f6c308 | 1571 | zlib_compression_level = level; |
960ccca6 DH |
1572 | zlib_compression_seen = 1; |
1573 | return 0; | |
1574 | } | |
1575 | ||
1576 | if (!strcmp(var, "core.compression")) { | |
1577 | int level = git_config_int(var, value); | |
1578 | if (level == -1) | |
1579 | level = Z_DEFAULT_COMPRESSION; | |
1580 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
8262aaa2 | 1581 | die(_("bad zlib compression level %d"), level); |
960ccca6 DH |
1582 | if (!zlib_compression_seen) |
1583 | zlib_compression_level = level; | |
8de7eeb5 JH |
1584 | if (!pack_compression_seen) |
1585 | pack_compression_level = level; | |
12f6c308 JBH |
1586 | return 0; |
1587 | } | |
1588 | ||
60bb8b14 | 1589 | if (!strcmp(var, "core.packedgitwindowsize")) { |
5faaf246 | 1590 | int pgsz_x2 = getpagesize() * 2; |
ebaa1bd4 | 1591 | packed_git_window_size = git_config_ulong(var, value); |
5faaf246 JH |
1592 | |
1593 | /* This value must be multiple of (pagesize * 2) */ | |
1594 | packed_git_window_size /= pgsz_x2; | |
1595 | if (packed_git_window_size < 1) | |
1596 | packed_git_window_size = 1; | |
1597 | packed_git_window_size *= pgsz_x2; | |
60bb8b14 SP |
1598 | return 0; |
1599 | } | |
1600 | ||
15366280 | 1601 | if (!strcmp(var, "core.bigfilethreshold")) { |
ebaa1bd4 | 1602 | big_file_threshold = git_config_ulong(var, value); |
15366280 JH |
1603 | return 0; |
1604 | } | |
1605 | ||
77ccc5bb | 1606 | if (!strcmp(var, "core.packedgitlimit")) { |
ebaa1bd4 | 1607 | packed_git_limit = git_config_ulong(var, value); |
77ccc5bb SP |
1608 | return 0; |
1609 | } | |
1610 | ||
18bdec11 | 1611 | if (!strcmp(var, "core.deltabasecachelimit")) { |
ebaa1bd4 | 1612 | delta_base_cache_limit = git_config_ulong(var, value); |
18bdec11 SP |
1613 | return 0; |
1614 | } | |
1615 | ||
6c510bee | 1616 | if (!strcmp(var, "core.autocrlf")) { |
d7f46334 | 1617 | if (value && !strcasecmp(value, "input")) { |
fd6cce9e | 1618 | auto_crlf = AUTO_CRLF_INPUT; |
d7f46334 LT |
1619 | return 0; |
1620 | } | |
6c510bee LT |
1621 | auto_crlf = git_config_bool(var, value); |
1622 | return 0; | |
1623 | } | |
1624 | ||
21e5ad50 | 1625 | if (!strcmp(var, "core.safecrlf")) { |
8462ff43 | 1626 | int eol_rndtrp_die; |
21e5ad50 | 1627 | if (value && !strcasecmp(value, "warn")) { |
8462ff43 | 1628 | global_conv_flags_eol = CONV_EOL_RNDTRP_WARN; |
21e5ad50 SP |
1629 | return 0; |
1630 | } | |
8462ff43 TB |
1631 | eol_rndtrp_die = git_config_bool(var, value); |
1632 | global_conv_flags_eol = eol_rndtrp_die ? | |
6cb09125 | 1633 | CONV_EOL_RNDTRP_DIE : 0; |
21e5ad50 SP |
1634 | return 0; |
1635 | } | |
1636 | ||
942e7747 EB |
1637 | if (!strcmp(var, "core.eol")) { |
1638 | if (value && !strcasecmp(value, "lf")) | |
ec70f52f | 1639 | core_eol = EOL_LF; |
942e7747 | 1640 | else if (value && !strcasecmp(value, "crlf")) |
ec70f52f | 1641 | core_eol = EOL_CRLF; |
942e7747 | 1642 | else if (value && !strcasecmp(value, "native")) |
ec70f52f | 1643 | core_eol = EOL_NATIVE; |
942e7747 | 1644 | else |
ec70f52f | 1645 | core_eol = EOL_UNSET; |
942e7747 EB |
1646 | return 0; |
1647 | } | |
1648 | ||
e92d6225 LS |
1649 | if (!strcmp(var, "core.checkroundtripencoding")) { |
1650 | check_roundtrip_encoding = xstrdup(value); | |
1651 | return 0; | |
1652 | } | |
1653 | ||
a97a7468 JS |
1654 | if (!strcmp(var, "core.notesref")) { |
1655 | notes_ref_name = xstrdup(value); | |
1656 | return 0; | |
1657 | } | |
1658 | ||
806e2ad7 LT |
1659 | if (!strcmp(var, "core.editor")) |
1660 | return git_config_string(&editor_program, var, value); | |
1661 | ||
eff80a9f | 1662 | if (!strcmp(var, "core.commentchar")) { |
649409b7 JK |
1663 | if (!value) |
1664 | return config_error_nonbool(var); | |
ad524f83 | 1665 | else if (!strcasecmp(value, "auto")) |
84c9dc2c | 1666 | auto_comment_line_char = 1; |
ad524f83 | 1667 | else if (value[0] && !value[1]) { |
649409b7 | 1668 | comment_line_char = value[0]; |
84c9dc2c | 1669 | auto_comment_line_char = 0; |
50b54fd7 | 1670 | } else |
a769bfc7 | 1671 | return error(_("core.commentChar should only be one character")); |
50b54fd7 | 1672 | return 0; |
eff80a9f JH |
1673 | } |
1674 | ||
d3e7da89 AK |
1675 | if (!strcmp(var, "core.askpass")) |
1676 | return git_config_string(&askpass_program, var, value); | |
1677 | ||
806e2ad7 | 1678 | if (!strcmp(var, "core.excludesfile")) |
395de250 | 1679 | return git_config_pathname(&excludes_file, var, value); |
806e2ad7 LT |
1680 | |
1681 | if (!strcmp(var, "core.whitespace")) { | |
1682 | if (!value) | |
1683 | return config_error_nonbool(var); | |
1684 | whitespace_rule_cfg = parse_whitespace_rule(value); | |
1685 | return 0; | |
1686 | } | |
1687 | ||
844a8ad4 NS |
1688 | if (!strcmp(var, "core.fsync")) { |
1689 | if (!value) | |
1690 | return config_error_nonbool(var); | |
1691 | fsync_components = parse_fsync_components(var, value); | |
1692 | return 0; | |
1693 | } | |
1694 | ||
abf38abe NS |
1695 | if (!strcmp(var, "core.fsyncmethod")) { |
1696 | if (!value) | |
1697 | return config_error_nonbool(var); | |
1698 | if (!strcmp(value, "fsync")) | |
1699 | fsync_method = FSYNC_METHOD_FSYNC; | |
1700 | else if (!strcmp(value, "writeout-only")) | |
1701 | fsync_method = FSYNC_METHOD_WRITEOUT_ONLY; | |
c0f4752e NS |
1702 | else if (!strcmp(value, "batch")) |
1703 | fsync_method = FSYNC_METHOD_BATCH; | |
abf38abe NS |
1704 | else |
1705 | warning(_("ignoring unknown core.fsyncMethod value '%s'"), value); | |
1706 | ||
1707 | } | |
1708 | ||
aafe9fba | 1709 | if (!strcmp(var, "core.fsyncobjectfiles")) { |
844a8ad4 | 1710 | if (fsync_object_files < 0) |
f12f3b98 | 1711 | warning(_("core.fsyncObjectFiles is deprecated; use core.fsync instead")); |
aafe9fba LT |
1712 | fsync_object_files = git_config_bool(var, value); |
1713 | return 0; | |
1714 | } | |
1715 | ||
671c9b7e LT |
1716 | if (!strcmp(var, "core.preloadindex")) { |
1717 | core_preload_index = git_config_bool(var, value); | |
1718 | return 0; | |
1719 | } | |
1720 | ||
348df166 JS |
1721 | if (!strcmp(var, "core.createobject")) { |
1722 | if (!strcmp(value, "rename")) | |
1723 | object_creation_mode = OBJECT_CREATION_USES_RENAMES; | |
1724 | else if (!strcmp(value, "link")) | |
1725 | object_creation_mode = OBJECT_CREATION_USES_HARDLINKS; | |
1726 | else | |
8262aaa2 | 1727 | die(_("invalid mode for object creation: %s"), value); |
be66a6c4 JS |
1728 | return 0; |
1729 | } | |
1730 | ||
08aefc9e NTND |
1731 | if (!strcmp(var, "core.sparsecheckout")) { |
1732 | core_apply_sparse_checkout = git_config_bool(var, value); | |
1733 | return 0; | |
1734 | } | |
1735 | ||
879321eb DS |
1736 | if (!strcmp(var, "core.sparsecheckoutcone")) { |
1737 | core_sparse_checkout_cone = git_config_bool(var, value); | |
1738 | return 0; | |
1739 | } | |
1740 | ||
76759c7d TB |
1741 | if (!strcmp(var, "core.precomposeunicode")) { |
1742 | precomposed_unicode = git_config_bool(var, value); | |
1743 | return 0; | |
1744 | } | |
1745 | ||
a42643aa JK |
1746 | if (!strcmp(var, "core.protecthfs")) { |
1747 | protect_hfs = git_config_bool(var, value); | |
1748 | return 0; | |
1749 | } | |
1750 | ||
2b4c6efc JS |
1751 | if (!strcmp(var, "core.protectntfs")) { |
1752 | protect_ntfs = git_config_bool(var, value); | |
1753 | return 0; | |
76759c7d TB |
1754 | } |
1755 | ||
da4398d6 JK |
1756 | if (!strcmp(var, "core.usereplacerefs")) { |
1757 | read_replace_refs = git_config_bool(var, value); | |
1758 | return 0; | |
1759 | } | |
1760 | ||
806e2ad7 | 1761 | /* Add other config variables here and to Documentation/config.txt. */ |
70fc5793 | 1762 | return platform_core_config(var, value, cb); |
806e2ad7 LT |
1763 | } |
1764 | ||
ecc7c884 EN |
1765 | static int git_default_sparse_config(const char *var, const char *value) |
1766 | { | |
1767 | if (!strcmp(var, "sparse.expectfilesoutsideofpatterns")) { | |
1768 | sparse_expect_files_outside_of_patterns = git_config_bool(var, value); | |
1769 | return 0; | |
1770 | } | |
1771 | ||
1772 | /* Add other config variables here and to Documentation/config/sparse.txt. */ | |
1773 | return 0; | |
1774 | } | |
1775 | ||
1141f492 | 1776 | static int git_default_i18n_config(const char *var, const char *value) |
d1364529 | 1777 | { |
ea5105a5 CC |
1778 | if (!strcmp(var, "i18n.commitencoding")) |
1779 | return git_config_string(&git_commit_encoding, var, value); | |
d2c11a38 | 1780 | |
ea5105a5 CC |
1781 | if (!strcmp(var, "i18n.logoutputencoding")) |
1782 | return git_config_string(&git_log_output_encoding, var, value); | |
d2c11a38 | 1783 | |
1141f492 LT |
1784 | /* Add other config variables here and to Documentation/config.txt. */ |
1785 | return 0; | |
1786 | } | |
039bc64e | 1787 | |
1141f492 LT |
1788 | static int git_default_branch_config(const char *var, const char *value) |
1789 | { | |
9ed36cfa | 1790 | if (!strcmp(var, "branch.autosetupmerge")) { |
44f14a9d | 1791 | if (value && !strcmp(value, "always")) { |
9ed36cfa JS |
1792 | git_branch_track = BRANCH_TRACK_ALWAYS; |
1793 | return 0; | |
d3115660 JS |
1794 | } else if (value && !strcmp(value, "inherit")) { |
1795 | git_branch_track = BRANCH_TRACK_INHERIT; | |
1796 | return 0; | |
bdaf1dfa TK |
1797 | } else if (value && !strcmp(value, "simple")) { |
1798 | git_branch_track = BRANCH_TRACK_SIMPLE; | |
1799 | return 0; | |
9ed36cfa JS |
1800 | } |
1801 | git_branch_track = git_config_bool(var, value); | |
1802 | return 0; | |
1803 | } | |
c998ae9b DS |
1804 | if (!strcmp(var, "branch.autosetuprebase")) { |
1805 | if (!value) | |
1806 | return config_error_nonbool(var); | |
1807 | else if (!strcmp(value, "never")) | |
1808 | autorebase = AUTOREBASE_NEVER; | |
1809 | else if (!strcmp(value, "local")) | |
1810 | autorebase = AUTOREBASE_LOCAL; | |
1811 | else if (!strcmp(value, "remote")) | |
1812 | autorebase = AUTOREBASE_REMOTE; | |
1813 | else if (!strcmp(value, "always")) | |
1814 | autorebase = AUTOREBASE_ALWAYS; | |
1815 | else | |
a769bfc7 | 1816 | return error(_("malformed value for %s"), var); |
c998ae9b DS |
1817 | return 0; |
1818 | } | |
a9cc857a | 1819 | |
1ab661dd | 1820 | /* Add other config variables here and to Documentation/config.txt. */ |
17712991 LT |
1821 | return 0; |
1822 | } | |
1823 | ||
52153747 FAG |
1824 | static int git_default_push_config(const char *var, const char *value) |
1825 | { | |
1826 | if (!strcmp(var, "push.default")) { | |
1827 | if (!value) | |
1828 | return config_error_nonbool(var); | |
1829 | else if (!strcmp(value, "nothing")) | |
1830 | push_default = PUSH_DEFAULT_NOTHING; | |
1831 | else if (!strcmp(value, "matching")) | |
1832 | push_default = PUSH_DEFAULT_MATCHING; | |
b55e6775 MM |
1833 | else if (!strcmp(value, "simple")) |
1834 | push_default = PUSH_DEFAULT_SIMPLE; | |
53c40311 JH |
1835 | else if (!strcmp(value, "upstream")) |
1836 | push_default = PUSH_DEFAULT_UPSTREAM; | |
1837 | else if (!strcmp(value, "tracking")) /* deprecated */ | |
1838 | push_default = PUSH_DEFAULT_UPSTREAM; | |
52153747 FAG |
1839 | else if (!strcmp(value, "current")) |
1840 | push_default = PUSH_DEFAULT_CURRENT; | |
1841 | else { | |
a769bfc7 NTND |
1842 | error(_("malformed value for %s: %s"), var, value); |
1843 | return error(_("must be one of nothing, matching, simple, " | |
1844 | "upstream or current")); | |
52153747 FAG |
1845 | } |
1846 | return 0; | |
1847 | } | |
1848 | ||
1849 | /* Add other config variables here and to Documentation/config.txt. */ | |
1850 | return 0; | |
1851 | } | |
1852 | ||
d551a488 MSO |
1853 | static int git_default_mailmap_config(const char *var, const char *value) |
1854 | { | |
1855 | if (!strcmp(var, "mailmap.file")) | |
9352fd57 | 1856 | return git_config_pathname(&git_mailmap_file, var, value); |
08610900 JK |
1857 | if (!strcmp(var, "mailmap.blob")) |
1858 | return git_config_string(&git_mailmap_blob, var, value); | |
d551a488 MSO |
1859 | |
1860 | /* Add other config variables here and to Documentation/config.txt. */ | |
1861 | return 0; | |
1862 | } | |
1863 | ||
409670f3 | 1864 | int git_default_config(const char *var, const char *value, void *cb) |
1141f492 | 1865 | { |
59556548 | 1866 | if (starts_with(var, "core.")) |
70fc5793 | 1867 | return git_default_core_config(var, value, cb); |
1141f492 | 1868 | |
39ab4d09 WH |
1869 | if (starts_with(var, "user.") || |
1870 | starts_with(var, "author.") || | |
1871 | starts_with(var, "committer.")) | |
409670f3 | 1872 | return git_ident_config(var, value, cb); |
1141f492 | 1873 | |
59556548 | 1874 | if (starts_with(var, "i18n.")) |
1141f492 LT |
1875 | return git_default_i18n_config(var, value); |
1876 | ||
59556548 | 1877 | if (starts_with(var, "branch.")) |
1141f492 LT |
1878 | return git_default_branch_config(var, value); |
1879 | ||
59556548 | 1880 | if (starts_with(var, "push.")) |
52153747 FAG |
1881 | return git_default_push_config(var, value); |
1882 | ||
59556548 | 1883 | if (starts_with(var, "mailmap.")) |
d551a488 MSO |
1884 | return git_default_mailmap_config(var, value); |
1885 | ||
960786e7 | 1886 | if (starts_with(var, "advice.") || starts_with(var, "color.advice")) |
75194438 JK |
1887 | return git_default_advice_config(var, value); |
1888 | ||
1141f492 LT |
1889 | if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { |
1890 | pager_use_color = git_config_bool(var,value); | |
1891 | return 0; | |
1892 | } | |
1893 | ||
568508e7 JH |
1894 | if (!strcmp(var, "pack.packsizelimit")) { |
1895 | pack_size_limit_cfg = git_config_ulong(var, value); | |
1896 | return 0; | |
1897 | } | |
8de7eeb5 JH |
1898 | |
1899 | if (!strcmp(var, "pack.compression")) { | |
1900 | int level = git_config_int(var, value); | |
1901 | if (level == -1) | |
1902 | level = Z_DEFAULT_COMPRESSION; | |
1903 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
1904 | die(_("bad pack compression level %d"), level); | |
1905 | pack_compression_level = level; | |
1906 | pack_compression_seen = 1; | |
1907 | return 0; | |
1908 | } | |
1909 | ||
ecc7c884 EN |
1910 | if (starts_with(var, "sparse.")) |
1911 | return git_default_sparse_config(var, value); | |
1912 | ||
1141f492 LT |
1913 | /* Add other config variables here and to Documentation/config.txt. */ |
1914 | return 0; | |
1915 | } | |
1916 | ||
ca4b5de2 | 1917 | /* |
b2dc0945 | 1918 | * All source specific fields in the union, die_on_error, name and the callbacks |
4d8dd149 | 1919 | * fgetc, ungetc, ftell of top need to be initialized before calling |
ca4b5de2 HV |
1920 | * this function. |
1921 | */ | |
8032cc44 JS |
1922 | static int do_config_from(struct config_source *top, config_fn_t fn, void *data, |
1923 | const struct config_options *opts) | |
ca4b5de2 HV |
1924 | { |
1925 | int ret; | |
1926 | ||
1927 | /* push config-file parsing state stack */ | |
1928 | top->prev = cf; | |
1929 | top->linenr = 1; | |
1930 | top->eof = 0; | |
348482de | 1931 | top->total_len = 0; |
ca4b5de2 HV |
1932 | strbuf_init(&top->value, 1024); |
1933 | strbuf_init(&top->var, 1024); | |
1934 | cf = top; | |
1935 | ||
8032cc44 | 1936 | ret = git_parse_source(fn, data, opts); |
ca4b5de2 HV |
1937 | |
1938 | /* pop config-file parsing state stack */ | |
1939 | strbuf_release(&top->value); | |
1940 | strbuf_release(&top->var); | |
1941 | cf = top->prev; | |
1942 | ||
1943 | return ret; | |
1944 | } | |
1945 | ||
3caec73b | 1946 | static int do_config_from_file(config_fn_t fn, |
1b8132d9 VA |
1947 | const enum config_origin_type origin_type, |
1948 | const char *name, const char *path, FILE *f, | |
8032cc44 | 1949 | void *data, const struct config_options *opts) |
17712991 | 1950 | { |
3caec73b | 1951 | struct config_source top; |
05e293c1 | 1952 | int ret; |
17712991 | 1953 | |
3caec73b | 1954 | top.u.file = f; |
473166b9 | 1955 | top.origin_type = origin_type; |
3caec73b KS |
1956 | top.name = name; |
1957 | top.path = path; | |
66f97228 | 1958 | top.default_error_action = CONFIG_ERROR_DIE; |
3caec73b KS |
1959 | top.do_fgetc = config_file_fgetc; |
1960 | top.do_ungetc = config_file_ungetc; | |
1961 | top.do_ftell = config_file_ftell; | |
924aaf3e | 1962 | |
05e293c1 | 1963 | flockfile(f); |
4f4d0b42 | 1964 | ret = do_config_from(&top, fn, data, opts); |
05e293c1 JK |
1965 | funlockfile(f); |
1966 | return ret; | |
3caec73b | 1967 | } |
924aaf3e | 1968 | |
3caec73b KS |
1969 | static int git_config_from_stdin(config_fn_t fn, void *data) |
1970 | { | |
8032cc44 JS |
1971 | return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin, |
1972 | data, NULL); | |
3caec73b KS |
1973 | } |
1974 | ||
8032cc44 JS |
1975 | int git_config_from_file_with_options(config_fn_t fn, const char *filename, |
1976 | void *data, | |
1977 | const struct config_options *opts) | |
3caec73b KS |
1978 | { |
1979 | int ret = -1; | |
1980 | FILE *f; | |
924aaf3e | 1981 | |
776f1848 GC |
1982 | if (!filename) |
1983 | BUG("filename cannot be NULL"); | |
e9d983f1 | 1984 | f = fopen_or_warn(filename, "r"); |
3caec73b | 1985 | if (f) { |
8032cc44 JS |
1986 | ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, |
1987 | filename, f, data, opts); | |
17712991 LT |
1988 | fclose(f); |
1989 | } | |
1990 | return ret; | |
1991 | } | |
10bea152 | 1992 | |
8032cc44 JS |
1993 | int git_config_from_file(config_fn_t fn, const char *filename, void *data) |
1994 | { | |
1995 | return git_config_from_file_with_options(fn, filename, data, NULL); | |
1996 | } | |
1997 | ||
4574f1aa JK |
1998 | int git_config_from_mem(config_fn_t fn, |
1999 | const enum config_origin_type origin_type, | |
2000 | const char *name, const char *buf, size_t len, | |
2001 | void *data, const struct config_options *opts) | |
1bc88819 HV |
2002 | { |
2003 | struct config_source top; | |
2004 | ||
2005 | top.u.buf.buf = buf; | |
2006 | top.u.buf.len = len; | |
2007 | top.u.buf.pos = 0; | |
473166b9 | 2008 | top.origin_type = origin_type; |
1bc88819 | 2009 | top.name = name; |
d14d4244 | 2010 | top.path = NULL; |
66f97228 | 2011 | top.default_error_action = CONFIG_ERROR_ERROR; |
49d6cfa5 JK |
2012 | top.do_fgetc = config_buf_fgetc; |
2013 | top.do_ungetc = config_buf_ungetc; | |
2014 | top.do_ftell = config_buf_ftell; | |
1bc88819 | 2015 | |
4574f1aa | 2016 | return do_config_from(&top, fn, data, opts); |
1bc88819 HV |
2017 | } |
2018 | ||
cd73de47 | 2019 | int git_config_from_blob_oid(config_fn_t fn, |
9ebf689a | 2020 | const char *name, |
e3e8bf04 | 2021 | struct repository *repo, |
cd73de47 | 2022 | const struct object_id *oid, |
9ebf689a | 2023 | void *data) |
1bc88819 HV |
2024 | { |
2025 | enum object_type type; | |
2026 | char *buf; | |
2027 | unsigned long size; | |
2028 | int ret; | |
2029 | ||
e3e8bf04 | 2030 | buf = repo_read_object_file(repo, oid, &type, &size); |
1bc88819 | 2031 | if (!buf) |
a769bfc7 | 2032 | return error(_("unable to load config blob object '%s'"), name); |
1bc88819 HV |
2033 | if (type != OBJ_BLOB) { |
2034 | free(buf); | |
a769bfc7 | 2035 | return error(_("reference '%s' does not point to a blob"), name); |
1bc88819 HV |
2036 | } |
2037 | ||
4574f1aa JK |
2038 | ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size, |
2039 | data, NULL); | |
1bc88819 HV |
2040 | free(buf); |
2041 | ||
2042 | return ret; | |
2043 | } | |
2044 | ||
2045 | static int git_config_from_blob_ref(config_fn_t fn, | |
e3e8bf04 | 2046 | struct repository *repo, |
1bc88819 HV |
2047 | const char *name, |
2048 | void *data) | |
2049 | { | |
cd73de47 | 2050 | struct object_id oid; |
1bc88819 | 2051 | |
e3e8bf04 | 2052 | if (repo_get_oid(repo, name, &oid) < 0) |
a769bfc7 | 2053 | return error(_("unable to resolve config blob '%s'"), name); |
e3e8bf04 | 2054 | return git_config_from_blob_oid(fn, name, repo, &oid, data); |
1bc88819 HV |
2055 | } |
2056 | ||
c62a999c | 2057 | char *git_system_config(void) |
506b17b1 | 2058 | { |
4179b489 | 2059 | char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM")); |
e3553076 JS |
2060 | if (!system_config) |
2061 | system_config = system_path(ETC_GITCONFIG); | |
2062 | normalize_path_copy(system_config, system_config); | |
2063 | return system_config; | |
506b17b1 JS |
2064 | } |
2065 | ||
4179b489 | 2066 | void git_global_config(char **user_out, char **xdg_out) |
1e06eb9b | 2067 | { |
4179b489 PS |
2068 | char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL")); |
2069 | char *xdg_config = NULL; | |
2070 | ||
2071 | if (!user_config) { | |
a03b097d | 2072 | user_config = interpolate_path("~/.gitconfig", 0); |
4179b489 PS |
2073 | xdg_config = xdg_config_home("config"); |
2074 | } | |
2075 | ||
2076 | *user_out = user_config; | |
2077 | *xdg_out = xdg_config; | |
506b17b1 JS |
2078 | } |
2079 | ||
23b0c478 SP |
2080 | /* |
2081 | * Parse environment variable 'k' as a boolean (in various | |
2082 | * possible spellings); if missing, use the default value 'def'. | |
2083 | */ | |
0ef37164 | 2084 | int git_env_bool(const char *k, int def) |
ab88c363 JK |
2085 | { |
2086 | const char *v = getenv(k); | |
2087 | return v ? git_config_bool(k, v) : def; | |
2088 | } | |
2089 | ||
23b0c478 SP |
2090 | /* |
2091 | * Parse environment variable 'k' as ulong with possibly a unit | |
2092 | * suffix; if missing, use the default value 'val'. | |
2093 | */ | |
2094 | unsigned long git_env_ulong(const char *k, unsigned long val) | |
2095 | { | |
2096 | const char *v = getenv(k); | |
2097 | if (v && !git_parse_ulong(v, &val)) | |
a769bfc7 | 2098 | die(_("failed to parse %s"), k); |
23b0c478 SP |
2099 | return val; |
2100 | } | |
2101 | ||
ab88c363 JK |
2102 | int git_config_system(void) |
2103 | { | |
2104 | return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); | |
2105 | } | |
2106 | ||
e145a0bc NTND |
2107 | static int do_git_config_sequence(const struct config_options *opts, |
2108 | config_fn_t fn, void *data) | |
4f629539 | 2109 | { |
c72ee44b | 2110 | int ret = 0; |
c62a999c | 2111 | char *system_config = git_system_config(); |
1e06eb9b PS |
2112 | char *xdg_config = NULL; |
2113 | char *user_config = NULL; | |
e145a0bc | 2114 | char *repo_config; |
5c105a84 | 2115 | enum config_scope prev_parsing_scope = current_parsing_scope; |
e145a0bc | 2116 | |
a577fb5f BW |
2117 | if (opts->commondir) |
2118 | repo_config = mkpathdup("%s/config", opts->commondir); | |
44004872 JS |
2119 | else if (opts->git_dir) |
2120 | BUG("git_dir without commondir"); | |
e145a0bc NTND |
2121 | else |
2122 | repo_config = NULL; | |
5f1a63e0 | 2123 | |
9acc5911 | 2124 | current_parsing_scope = CONFIG_SCOPE_SYSTEM; |
c62a999c PS |
2125 | if (git_config_system() && system_config && |
2126 | !access_or_die(system_config, R_OK, | |
2127 | opts->system_gently ? ACCESS_EACCES_OK : 0)) | |
2128 | ret += git_config_from_file(fn, system_config, data); | |
5f1a63e0 | 2129 | |
9acc5911 | 2130 | current_parsing_scope = CONFIG_SCOPE_GLOBAL; |
1e06eb9b PS |
2131 | git_global_config(&user_config, &xdg_config); |
2132 | ||
c72ee44b | 2133 | if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) |
21cf3227 | 2134 | ret += git_config_from_file(fn, xdg_config, data); |
21cf3227 | 2135 | |
c72ee44b | 2136 | if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) |
21cf3227 | 2137 | ret += git_config_from_file(fn, user_config, data); |
5f1a63e0 | 2138 | |
6dc905d9 | 2139 | current_parsing_scope = CONFIG_SCOPE_LOCAL; |
800a7f99 JH |
2140 | if (!opts->ignore_repo && repo_config && |
2141 | !access_or_die(repo_config, R_OK, 0)) | |
aa387407 | 2142 | ret += git_config_from_file(fn, repo_config, data); |
8b1fa778 | 2143 | |
6dc905d9 | 2144 | current_parsing_scope = CONFIG_SCOPE_WORKTREE; |
800a7f99 | 2145 | if (!opts->ignore_worktree && repository_format_worktree_config) { |
58b284a2 NTND |
2146 | char *path = git_pathdup("config.worktree"); |
2147 | if (!access_or_die(path, R_OK, 0)) | |
2148 | ret += git_config_from_file(fn, path, data); | |
2149 | free(path); | |
2150 | } | |
2151 | ||
6766e41b | 2152 | current_parsing_scope = CONFIG_SCOPE_COMMAND; |
800a7f99 | 2153 | if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0) |
8262aaa2 | 2154 | die(_("unable to parse command-line config")); |
8b1fa778 | 2155 | |
5c105a84 | 2156 | current_parsing_scope = prev_parsing_scope; |
c62a999c | 2157 | free(system_config); |
21cf3227 HKNN |
2158 | free(xdg_config); |
2159 | free(user_config); | |
80181868 | 2160 | free(repo_config); |
c72ee44b | 2161 | return ret; |
4f629539 JH |
2162 | } |
2163 | ||
dc8441fd BW |
2164 | int config_with_options(config_fn_t fn, void *data, |
2165 | struct git_config_source *config_source, | |
2166 | const struct config_options *opts) | |
dbdf5854 | 2167 | { |
9b25a0b5 | 2168 | struct config_include_data inc = CONFIG_INCLUDE_INIT; |
399b1984 | 2169 | int ret; |
9b25a0b5 | 2170 | |
c48f4b37 | 2171 | if (opts->respect_includes) { |
9b25a0b5 JK |
2172 | inc.fn = fn; |
2173 | inc.data = data; | |
c48f4b37 | 2174 | inc.opts = opts; |
399b1984 | 2175 | inc.config_source = config_source; |
9b25a0b5 JK |
2176 | fn = git_config_include; |
2177 | data = &inc; | |
2178 | } | |
dbdf5854 | 2179 | |
e37efa40 MR |
2180 | if (config_source) |
2181 | current_parsing_scope = config_source->scope; | |
2182 | ||
c9b5e2a5 JK |
2183 | /* |
2184 | * If we have a specific filename, use it. Otherwise, follow the | |
2185 | * regular lookup sequence. | |
2186 | */ | |
e3e8bf04 | 2187 | if (config_source && config_source->use_stdin) { |
399b1984 | 2188 | ret = git_config_from_stdin(fn, data); |
e3e8bf04 | 2189 | } else if (config_source && config_source->file) { |
399b1984 | 2190 | ret = git_config_from_file(fn, config_source->file, data); |
e3e8bf04 JT |
2191 | } else if (config_source && config_source->blob) { |
2192 | struct repository *repo = config_source->repo ? | |
2193 | config_source->repo : the_repository; | |
399b1984 | 2194 | ret = git_config_from_blob_ref(fn, repo, config_source->blob, |
e3e8bf04 | 2195 | data); |
399b1984 JT |
2196 | } else { |
2197 | ret = do_git_config_sequence(opts, fn, data); | |
e3e8bf04 | 2198 | } |
c9b5e2a5 | 2199 | |
399b1984 JT |
2200 | if (inc.remote_urls) { |
2201 | string_list_clear(inc.remote_urls, 0); | |
2202 | FREE_AND_NULL(inc.remote_urls); | |
2203 | } | |
2204 | return ret; | |
dbdf5854 NTND |
2205 | } |
2206 | ||
155ef25f | 2207 | static void configset_iter(struct config_set *cs, config_fn_t fn, void *data) |
c9b5e2a5 | 2208 | { |
155ef25f TA |
2209 | int i, value_index; |
2210 | struct string_list *values; | |
2211 | struct config_set_element *entry; | |
2212 | struct configset_list *list = &cs->list; | |
155ef25f TA |
2213 | |
2214 | for (i = 0; i < list->nr; i++) { | |
2215 | entry = list->items[i].e; | |
2216 | value_index = list->items[i].value_index; | |
2217 | values = &entry->value_list; | |
0d44a2da JK |
2218 | |
2219 | current_config_kvi = values->items[value_index].util; | |
2220 | ||
2221 | if (fn(entry->key, values->items[value_index].string, data) < 0) | |
2222 | git_die_config_linenr(entry->key, | |
2223 | current_config_kvi->filename, | |
2224 | current_config_kvi->linenr); | |
2225 | ||
2226 | current_config_kvi = NULL; | |
155ef25f TA |
2227 | } |
2228 | } | |
2229 | ||
0654aa57 JS |
2230 | void read_early_config(config_fn_t cb, void *data) |
2231 | { | |
c48f4b37 | 2232 | struct config_options opts = {0}; |
d3fb71b3 BW |
2233 | struct strbuf commondir = STRBUF_INIT; |
2234 | struct strbuf gitdir = STRBUF_INIT; | |
1a27409a | 2235 | |
c48f4b37 | 2236 | opts.respect_includes = 1; |
0654aa57 | 2237 | |
a577fb5f BW |
2238 | if (have_git_dir()) { |
2239 | opts.commondir = get_git_common_dir(); | |
2185fde5 | 2240 | opts.git_dir = get_git_dir(); |
0654aa57 | 2241 | /* |
1a27409a JS |
2242 | * When setup_git_directory() was not yet asked to discover the |
2243 | * GIT_DIR, we ask discover_git_directory() to figure out whether there | |
2244 | * is any repository config we should use (but unlike | |
2245 | * setup_git_directory_gently(), no global state is changed, most | |
2246 | * notably, the current working directory is still the same after the | |
2247 | * call). | |
0654aa57 | 2248 | */ |
a577fb5f BW |
2249 | } else if (!discover_git_directory(&commondir, &gitdir)) { |
2250 | opts.commondir = commondir.buf; | |
d3fb71b3 | 2251 | opts.git_dir = gitdir.buf; |
a577fb5f | 2252 | } |
2185fde5 | 2253 | |
dc8441fd | 2254 | config_with_options(cb, data, NULL, &opts); |
0654aa57 | 2255 | |
d3fb71b3 BW |
2256 | strbuf_release(&commondir); |
2257 | strbuf_release(&gitdir); | |
0654aa57 JS |
2258 | } |
2259 | ||
800a7f99 JH |
2260 | /* |
2261 | * Read config but only enumerate system and global settings. | |
2262 | * Omit any repo-local, worktree-local, or command-line settings. | |
2263 | */ | |
2264 | void read_very_early_config(config_fn_t cb, void *data) | |
2265 | { | |
2266 | struct config_options opts = { 0 }; | |
2267 | ||
2268 | opts.respect_includes = 1; | |
2269 | opts.ignore_repo = 1; | |
2270 | opts.ignore_worktree = 1; | |
2271 | opts.ignore_cmdline = 1; | |
f672deec | 2272 | opts.system_gently = 1; |
800a7f99 JH |
2273 | |
2274 | config_with_options(cb, data, NULL, &opts); | |
2275 | } | |
2276 | ||
3c8687a7 TA |
2277 | static struct config_set_element *configset_find_element(struct config_set *cs, const char *key) |
2278 | { | |
2279 | struct config_set_element k; | |
2280 | struct config_set_element *found_entry; | |
2281 | char *normalized_key; | |
3c8687a7 TA |
2282 | /* |
2283 | * `key` may come from the user, so normalize it before using it | |
2284 | * for querying entries from the hashmap. | |
2285 | */ | |
270cd9ea | 2286 | if (git_config_parse_key(key, &normalized_key, NULL)) |
3c8687a7 TA |
2287 | return NULL; |
2288 | ||
d22245a2 | 2289 | hashmap_entry_init(&k.ent, strhash(normalized_key)); |
3c8687a7 | 2290 | k.key = normalized_key; |
404ab78e | 2291 | found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL); |
3c8687a7 TA |
2292 | free(normalized_key); |
2293 | return found_entry; | |
2294 | } | |
2295 | ||
2296 | static int configset_add_value(struct config_set *cs, const char *key, const char *value) | |
2297 | { | |
2298 | struct config_set_element *e; | |
3df8fd62 | 2299 | struct string_list_item *si; |
155ef25f | 2300 | struct configset_list_item *l_item; |
3df8fd62 TA |
2301 | struct key_value_info *kv_info = xmalloc(sizeof(*kv_info)); |
2302 | ||
3c8687a7 TA |
2303 | e = configset_find_element(cs, key); |
2304 | /* | |
2305 | * Since the keys are being fed by git_config*() callback mechanism, they | |
2306 | * are already normalized. So simply add them without any further munging. | |
2307 | */ | |
2308 | if (!e) { | |
2309 | e = xmalloc(sizeof(*e)); | |
d22245a2 | 2310 | hashmap_entry_init(&e->ent, strhash(key)); |
3c8687a7 | 2311 | e->key = xstrdup(key); |
bc40dfb1 | 2312 | string_list_init_dup(&e->value_list); |
b94e5c1d | 2313 | hashmap_add(&cs->config_hash, &e->ent); |
3c8687a7 | 2314 | } |
8c53f071 | 2315 | si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value)); |
155ef25f TA |
2316 | |
2317 | ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc); | |
2318 | l_item = &cs->list.items[cs->list.nr++]; | |
2319 | l_item->e = e; | |
2320 | l_item->value_index = e->value_list.nr - 1; | |
2321 | ||
3258258f | 2322 | if (!cf) |
033abf97 | 2323 | BUG("configset_add_value has no source"); |
3258258f | 2324 | if (cf->name) { |
3df8fd62 TA |
2325 | kv_info->filename = strintern(cf->name); |
2326 | kv_info->linenr = cf->linenr; | |
1b8132d9 | 2327 | kv_info->origin_type = cf->origin_type; |
3df8fd62 TA |
2328 | } else { |
2329 | /* for values read from `git_config_from_parameters()` */ | |
2330 | kv_info->filename = NULL; | |
2331 | kv_info->linenr = -1; | |
1b8132d9 | 2332 | kv_info->origin_type = CONFIG_ORIGIN_CMDLINE; |
3df8fd62 | 2333 | } |
9acc5911 | 2334 | kv_info->scope = current_parsing_scope; |
3df8fd62 | 2335 | si->util = kv_info; |
3c8687a7 TA |
2336 | |
2337 | return 0; | |
2338 | } | |
2339 | ||
7663cdc8 | 2340 | static int config_set_element_cmp(const void *unused_cmp_data, |
939af16e EW |
2341 | const struct hashmap_entry *eptr, |
2342 | const struct hashmap_entry *entry_or_key, | |
7663cdc8 | 2343 | const void *unused_keydata) |
3c8687a7 | 2344 | { |
939af16e EW |
2345 | const struct config_set_element *e1, *e2; |
2346 | ||
2347 | e1 = container_of(eptr, const struct config_set_element, ent); | |
2348 | e2 = container_of(entry_or_key, const struct config_set_element, ent); | |
77bdc097 | 2349 | |
3c8687a7 TA |
2350 | return strcmp(e1->key, e2->key); |
2351 | } | |
2352 | ||
2353 | void git_configset_init(struct config_set *cs) | |
2354 | { | |
77bdc097 | 2355 | hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0); |
3c8687a7 | 2356 | cs->hash_initialized = 1; |
155ef25f TA |
2357 | cs->list.nr = 0; |
2358 | cs->list.alloc = 0; | |
2359 | cs->list.items = NULL; | |
3c8687a7 TA |
2360 | } |
2361 | ||
2362 | void git_configset_clear(struct config_set *cs) | |
2363 | { | |
2364 | struct config_set_element *entry; | |
2365 | struct hashmap_iter iter; | |
2366 | if (!cs->hash_initialized) | |
2367 | return; | |
2368 | ||
87571c3f | 2369 | hashmap_for_each_entry(&cs->config_hash, &iter, entry, |
87571c3f | 2370 | ent /* member name */) { |
3c8687a7 | 2371 | free(entry->key); |
3df8fd62 | 2372 | string_list_clear(&entry->value_list, 1); |
3c8687a7 | 2373 | } |
6da1a258 | 2374 | hashmap_clear_and_free(&cs->config_hash, struct config_set_element, ent); |
3c8687a7 | 2375 | cs->hash_initialized = 0; |
155ef25f TA |
2376 | free(cs->list.items); |
2377 | cs->list.nr = 0; | |
2378 | cs->list.alloc = 0; | |
2379 | cs->list.items = NULL; | |
3c8687a7 TA |
2380 | } |
2381 | ||
2382 | static int config_set_callback(const char *key, const char *value, void *cb) | |
2383 | { | |
2384 | struct config_set *cs = cb; | |
2385 | configset_add_value(cs, key, value); | |
2386 | return 0; | |
2387 | } | |
2388 | ||
2389 | int git_configset_add_file(struct config_set *cs, const char *filename) | |
2390 | { | |
2391 | return git_config_from_file(config_set_callback, filename, cs); | |
2392 | } | |
2393 | ||
5b3c6507 GC |
2394 | int git_configset_add_parameters(struct config_set *cs) |
2395 | { | |
2396 | return git_config_from_parameters(config_set_callback, cs); | |
2397 | } | |
2398 | ||
3c8687a7 TA |
2399 | int git_configset_get_value(struct config_set *cs, const char *key, const char **value) |
2400 | { | |
2401 | const struct string_list *values = NULL; | |
2402 | /* | |
2403 | * Follows "last one wins" semantic, i.e., if there are multiple matches for the | |
2404 | * queried key in the files of the configset, the value returned will be the last | |
2405 | * value in the value list for that key. | |
2406 | */ | |
2407 | values = git_configset_get_value_multi(cs, key); | |
2408 | ||
2409 | if (!values) | |
2410 | return 1; | |
2411 | assert(values->nr > 0); | |
2412 | *value = values->items[values->nr - 1].string; | |
2413 | return 0; | |
2414 | } | |
2415 | ||
2416 | const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key) | |
2417 | { | |
2418 | struct config_set_element *e = configset_find_element(cs, key); | |
2419 | return e ? &e->value_list : NULL; | |
2420 | } | |
2421 | ||
9a53219f | 2422 | int git_configset_get_string(struct config_set *cs, const char *key, char **dest) |
3c8687a7 TA |
2423 | { |
2424 | const char *value; | |
2425 | if (!git_configset_get_value(cs, key, &value)) | |
9a53219f | 2426 | return git_config_string((const char **)dest, key, value); |
3c8687a7 TA |
2427 | else |
2428 | return 1; | |
2429 | } | |
2430 | ||
3ce11382 DS |
2431 | static int git_configset_get_string_tmp(struct config_set *cs, const char *key, |
2432 | const char **dest) | |
3c8687a7 | 2433 | { |
f1de981e JK |
2434 | const char *value; |
2435 | if (!git_configset_get_value(cs, key, &value)) { | |
2436 | if (!value) | |
2437 | return config_error_nonbool(key); | |
2438 | *dest = value; | |
2439 | return 0; | |
2440 | } else { | |
2441 | return 1; | |
2442 | } | |
3c8687a7 TA |
2443 | } |
2444 | ||
2445 | int git_configset_get_int(struct config_set *cs, const char *key, int *dest) | |
2446 | { | |
2447 | const char *value; | |
2448 | if (!git_configset_get_value(cs, key, &value)) { | |
2449 | *dest = git_config_int(key, value); | |
2450 | return 0; | |
2451 | } else | |
2452 | return 1; | |
2453 | } | |
2454 | ||
2455 | int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest) | |
2456 | { | |
2457 | const char *value; | |
2458 | if (!git_configset_get_value(cs, key, &value)) { | |
2459 | *dest = git_config_ulong(key, value); | |
2460 | return 0; | |
2461 | } else | |
2462 | return 1; | |
2463 | } | |
2464 | ||
2465 | int git_configset_get_bool(struct config_set *cs, const char *key, int *dest) | |
2466 | { | |
2467 | const char *value; | |
2468 | if (!git_configset_get_value(cs, key, &value)) { | |
2469 | *dest = git_config_bool(key, value); | |
2470 | return 0; | |
2471 | } else | |
2472 | return 1; | |
2473 | } | |
2474 | ||
2475 | int git_configset_get_bool_or_int(struct config_set *cs, const char *key, | |
2476 | int *is_bool, int *dest) | |
2477 | { | |
2478 | const char *value; | |
2479 | if (!git_configset_get_value(cs, key, &value)) { | |
2480 | *dest = git_config_bool_or_int(key, value, is_bool); | |
2481 | return 0; | |
2482 | } else | |
2483 | return 1; | |
2484 | } | |
2485 | ||
2486 | int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest) | |
2487 | { | |
2488 | const char *value; | |
2489 | if (!git_configset_get_value(cs, key, &value)) { | |
89576613 | 2490 | *dest = git_parse_maybe_bool(value); |
3c8687a7 TA |
2491 | if (*dest == -1) |
2492 | return -1; | |
2493 | return 0; | |
2494 | } else | |
2495 | return 1; | |
2496 | } | |
2497 | ||
2498 | int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest) | |
2499 | { | |
2500 | const char *value; | |
2501 | if (!git_configset_get_value(cs, key, &value)) | |
2502 | return git_config_pathname(dest, key, value); | |
2503 | else | |
2504 | return 1; | |
2505 | } | |
2506 | ||
3b256228 BW |
2507 | /* Functions use to read configuration from a repository */ |
2508 | static void repo_read_config(struct repository *repo) | |
3c8687a7 | 2509 | { |
1703751f | 2510 | struct config_options opts = { 0 }; |
3b256228 BW |
2511 | |
2512 | opts.respect_includes = 1; | |
2513 | opts.commondir = repo->commondir; | |
2514 | opts.git_dir = repo->gitdir; | |
2515 | ||
2516 | if (!repo->config) | |
ca56dadb | 2517 | CALLOC_ARRAY(repo->config, 1); |
3b256228 BW |
2518 | else |
2519 | git_configset_clear(repo->config); | |
2520 | ||
2521 | git_configset_init(repo->config); | |
2522 | ||
2523 | if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0) | |
2524 | /* | |
2525 | * config_with_options() normally returns only | |
2526 | * zero, as most errors are fatal, and | |
2527 | * non-fatal potential errors are guarded by "if" | |
2528 | * statements that are entered only when no error is | |
2529 | * possible. | |
2530 | * | |
2531 | * If we ever encounter a non-fatal error, it means | |
2532 | * something went really wrong and we should stop | |
2533 | * immediately. | |
2534 | */ | |
2535 | die(_("unknown error occurred while reading the configuration files")); | |
2536 | } | |
2537 | ||
2538 | static void git_config_check_init(struct repository *repo) | |
2539 | { | |
2540 | if (repo->config && repo->config->hash_initialized) | |
3c8687a7 | 2541 | return; |
3b256228 | 2542 | repo_read_config(repo); |
3c8687a7 TA |
2543 | } |
2544 | ||
3b256228 | 2545 | static void repo_config_clear(struct repository *repo) |
3c8687a7 | 2546 | { |
3b256228 | 2547 | if (!repo->config || !repo->config->hash_initialized) |
3c8687a7 | 2548 | return; |
3b256228 | 2549 | git_configset_clear(repo->config); |
3c8687a7 TA |
2550 | } |
2551 | ||
3b256228 | 2552 | void repo_config(struct repository *repo, config_fn_t fn, void *data) |
3c8687a7 | 2553 | { |
3b256228 BW |
2554 | git_config_check_init(repo); |
2555 | configset_iter(repo->config, fn, data); | |
3c8687a7 TA |
2556 | } |
2557 | ||
3b256228 BW |
2558 | int repo_config_get_value(struct repository *repo, |
2559 | const char *key, const char **value) | |
3c8687a7 | 2560 | { |
3b256228 BW |
2561 | git_config_check_init(repo); |
2562 | return git_configset_get_value(repo->config, key, value); | |
3c8687a7 TA |
2563 | } |
2564 | ||
3b256228 BW |
2565 | const struct string_list *repo_config_get_value_multi(struct repository *repo, |
2566 | const char *key) | |
2567 | { | |
2568 | git_config_check_init(repo); | |
2569 | return git_configset_get_value_multi(repo->config, key); | |
2570 | } | |
2571 | ||
9a53219f JK |
2572 | int repo_config_get_string(struct repository *repo, |
2573 | const char *key, char **dest) | |
3b256228 BW |
2574 | { |
2575 | int ret; | |
2576 | git_config_check_init(repo); | |
9a53219f | 2577 | ret = git_configset_get_string(repo->config, key, dest); |
3b256228 BW |
2578 | if (ret < 0) |
2579 | git_die_config(key, NULL); | |
2580 | return ret; | |
2581 | } | |
2582 | ||
f1de981e JK |
2583 | int repo_config_get_string_tmp(struct repository *repo, |
2584 | const char *key, const char **dest) | |
3b256228 | 2585 | { |
f1de981e | 2586 | int ret; |
3b256228 | 2587 | git_config_check_init(repo); |
f1de981e JK |
2588 | ret = git_configset_get_string_tmp(repo->config, key, dest); |
2589 | if (ret < 0) | |
2590 | git_die_config(key, NULL); | |
2591 | return ret; | |
3b256228 BW |
2592 | } |
2593 | ||
2594 | int repo_config_get_int(struct repository *repo, | |
2595 | const char *key, int *dest) | |
2596 | { | |
2597 | git_config_check_init(repo); | |
2598 | return git_configset_get_int(repo->config, key, dest); | |
2599 | } | |
2600 | ||
2601 | int repo_config_get_ulong(struct repository *repo, | |
2602 | const char *key, unsigned long *dest) | |
2603 | { | |
2604 | git_config_check_init(repo); | |
2605 | return git_configset_get_ulong(repo->config, key, dest); | |
2606 | } | |
2607 | ||
2608 | int repo_config_get_bool(struct repository *repo, | |
2609 | const char *key, int *dest) | |
2610 | { | |
2611 | git_config_check_init(repo); | |
2612 | return git_configset_get_bool(repo->config, key, dest); | |
2613 | } | |
2614 | ||
2615 | int repo_config_get_bool_or_int(struct repository *repo, | |
2616 | const char *key, int *is_bool, int *dest) | |
2617 | { | |
2618 | git_config_check_init(repo); | |
2619 | return git_configset_get_bool_or_int(repo->config, key, is_bool, dest); | |
2620 | } | |
2621 | ||
2622 | int repo_config_get_maybe_bool(struct repository *repo, | |
2623 | const char *key, int *dest) | |
2624 | { | |
2625 | git_config_check_init(repo); | |
2626 | return git_configset_get_maybe_bool(repo->config, key, dest); | |
2627 | } | |
2628 | ||
2629 | int repo_config_get_pathname(struct repository *repo, | |
2630 | const char *key, const char **dest) | |
3c8687a7 | 2631 | { |
5a80e97c | 2632 | int ret; |
3b256228 BW |
2633 | git_config_check_init(repo); |
2634 | ret = git_configset_get_pathname(repo->config, key, dest); | |
5a80e97c TA |
2635 | if (ret < 0) |
2636 | git_die_config(key, NULL); | |
2637 | return ret; | |
3c8687a7 TA |
2638 | } |
2639 | ||
5b3c6507 GC |
2640 | /* Read values into protected_config. */ |
2641 | static void read_protected_config(void) | |
2642 | { | |
2643 | char *xdg_config = NULL, *user_config = NULL, *system_config = NULL; | |
2644 | ||
2645 | git_configset_init(&protected_config); | |
2646 | ||
2647 | system_config = git_system_config(); | |
2648 | git_global_config(&user_config, &xdg_config); | |
2649 | ||
776f1848 GC |
2650 | if (system_config) |
2651 | git_configset_add_file(&protected_config, system_config); | |
2652 | if (xdg_config) | |
2653 | git_configset_add_file(&protected_config, xdg_config); | |
2654 | if (user_config) | |
2655 | git_configset_add_file(&protected_config, user_config); | |
5b3c6507 GC |
2656 | git_configset_add_parameters(&protected_config); |
2657 | ||
2658 | free(system_config); | |
2659 | free(xdg_config); | |
2660 | free(user_config); | |
2661 | } | |
2662 | ||
2663 | void git_protected_config(config_fn_t fn, void *data) | |
2664 | { | |
2665 | if (!protected_config.hash_initialized) | |
2666 | read_protected_config(); | |
2667 | configset_iter(&protected_config, fn, data); | |
2668 | } | |
2669 | ||
3b256228 BW |
2670 | /* Functions used historically to read configuration from 'the_repository' */ |
2671 | void git_config(config_fn_t fn, void *data) | |
2672 | { | |
2673 | repo_config(the_repository, fn, data); | |
2674 | } | |
2675 | ||
2676 | void git_config_clear(void) | |
2677 | { | |
2678 | repo_config_clear(the_repository); | |
2679 | } | |
2680 | ||
2681 | int git_config_get_value(const char *key, const char **value) | |
2682 | { | |
2683 | return repo_config_get_value(the_repository, key, value); | |
2684 | } | |
2685 | ||
2686 | const struct string_list *git_config_get_value_multi(const char *key) | |
2687 | { | |
2688 | return repo_config_get_value_multi(the_repository, key); | |
2689 | } | |
2690 | ||
3c8687a7 | 2691 | int git_config_get_string(const char *key, char **dest) |
3b256228 | 2692 | { |
3b256228 | 2693 | return repo_config_get_string(the_repository, key, dest); |
3b256228 BW |
2694 | } |
2695 | ||
f1de981e | 2696 | int git_config_get_string_tmp(const char *key, const char **dest) |
3c8687a7 | 2697 | { |
f1de981e | 2698 | return repo_config_get_string_tmp(the_repository, key, dest); |
3c8687a7 TA |
2699 | } |
2700 | ||
2701 | int git_config_get_int(const char *key, int *dest) | |
2702 | { | |
3b256228 | 2703 | return repo_config_get_int(the_repository, key, dest); |
3c8687a7 TA |
2704 | } |
2705 | ||
2706 | int git_config_get_ulong(const char *key, unsigned long *dest) | |
2707 | { | |
3b256228 | 2708 | return repo_config_get_ulong(the_repository, key, dest); |
3c8687a7 TA |
2709 | } |
2710 | ||
2711 | int git_config_get_bool(const char *key, int *dest) | |
2712 | { | |
3b256228 | 2713 | return repo_config_get_bool(the_repository, key, dest); |
3c8687a7 TA |
2714 | } |
2715 | ||
2716 | int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest) | |
2717 | { | |
3b256228 | 2718 | return repo_config_get_bool_or_int(the_repository, key, is_bool, dest); |
3c8687a7 TA |
2719 | } |
2720 | ||
2721 | int git_config_get_maybe_bool(const char *key, int *dest) | |
2722 | { | |
3b256228 | 2723 | return repo_config_get_maybe_bool(the_repository, key, dest); |
3c8687a7 TA |
2724 | } |
2725 | ||
2726 | int git_config_get_pathname(const char *key, const char **dest) | |
2727 | { | |
3b256228 | 2728 | return repo_config_get_pathname(the_repository, key, dest); |
5a80e97c TA |
2729 | } |
2730 | ||
77d67977 CC |
2731 | int git_config_get_expiry(const char *key, const char **output) |
2732 | { | |
9a53219f | 2733 | int ret = git_config_get_string(key, (char **)output); |
77d67977 CC |
2734 | if (ret) |
2735 | return ret; | |
2736 | if (strcmp(*output, "now")) { | |
dddbad72 | 2737 | timestamp_t now = approxidate("now"); |
77d67977 CC |
2738 | if (approxidate(*output) >= now) |
2739 | git_die_config(key, _("Invalid %s: '%s'"), key, *output); | |
2740 | } | |
2741 | return ret; | |
2742 | } | |
2743 | ||
6e96cb52 JH |
2744 | int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestamp_t now) |
2745 | { | |
1c890016 | 2746 | const char *expiry_string; |
6e96cb52 JH |
2747 | intmax_t days; |
2748 | timestamp_t when; | |
2749 | ||
1c890016 | 2750 | if (git_config_get_string_tmp(key, &expiry_string)) |
6e96cb52 JH |
2751 | return 1; /* no such thing */ |
2752 | ||
2753 | if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) { | |
2754 | const int scale = 86400; | |
2755 | *expiry = now - days * scale; | |
2756 | return 0; | |
2757 | } | |
2758 | ||
2759 | if (!parse_expiry_date(expiry_string, &when)) { | |
2760 | *expiry = when; | |
2761 | return 0; | |
2762 | } | |
2763 | return -1; /* thing exists but cannot be parsed */ | |
2764 | } | |
2765 | ||
1f44b09b CC |
2766 | int git_config_get_split_index(void) |
2767 | { | |
2768 | int val; | |
2769 | ||
2770 | if (!git_config_get_maybe_bool("core.splitindex", &val)) | |
2771 | return val; | |
2772 | ||
2773 | return -1; /* default value */ | |
2774 | } | |
2775 | ||
72dcb7b3 CC |
2776 | int git_config_get_max_percent_split_change(void) |
2777 | { | |
2778 | int val = -1; | |
2779 | ||
2780 | if (!git_config_get_int("splitindex.maxpercentchange", &val)) { | |
2781 | if (0 <= val && val <= 100) | |
2782 | return val; | |
2783 | ||
2784 | return error(_("splitIndex.maxPercentChange value '%d' " | |
2785 | "should be between 0 and 100"), val); | |
2786 | } | |
2787 | ||
2788 | return -1; /* default value */ | |
2789 | } | |
2790 | ||
2a9dedef | 2791 | int git_config_get_index_threads(int *dest) |
c780b9cf | 2792 | { |
2a9dedef | 2793 | int is_bool, val; |
c780b9cf BP |
2794 | |
2795 | val = git_env_ulong("GIT_TEST_INDEX_THREADS", 0); | |
2a9dedef JN |
2796 | if (val) { |
2797 | *dest = val; | |
2798 | return 0; | |
2799 | } | |
c780b9cf BP |
2800 | |
2801 | if (!git_config_get_bool_or_int("index.threads", &is_bool, &val)) { | |
2802 | if (is_bool) | |
2a9dedef | 2803 | *dest = val ? 0 : 1; |
c780b9cf | 2804 | else |
2a9dedef JN |
2805 | *dest = val; |
2806 | return 0; | |
c780b9cf BP |
2807 | } |
2808 | ||
2a9dedef | 2809 | return 1; |
c780b9cf BP |
2810 | } |
2811 | ||
5a80e97c TA |
2812 | NORETURN |
2813 | void git_die_config_linenr(const char *key, const char *filename, int linenr) | |
2814 | { | |
2815 | if (!filename) | |
2816 | die(_("unable to parse '%s' from command-line config"), key); | |
2817 | else | |
2818 | die(_("bad config variable '%s' in file '%s' at line %d"), | |
2819 | key, filename, linenr); | |
2820 | } | |
2821 | ||
2822 | NORETURN __attribute__((format(printf, 2, 3))) | |
2823 | void git_die_config(const char *key, const char *err, ...) | |
2824 | { | |
2825 | const struct string_list *values; | |
2826 | struct key_value_info *kv_info; | |
f5c39c32 | 2827 | report_fn error_fn = get_error_routine(); |
5a80e97c TA |
2828 | |
2829 | if (err) { | |
2830 | va_list params; | |
2831 | va_start(params, err); | |
f5c39c32 | 2832 | error_fn(err, params); |
5a80e97c TA |
2833 | va_end(params); |
2834 | } | |
2835 | values = git_config_get_value_multi(key); | |
2836 | kv_info = values->items[values->nr - 1].util; | |
2837 | git_die_config_linenr(key, kv_info->filename, kv_info->linenr); | |
3c8687a7 TA |
2838 | } |
2839 | ||
10bea152 JS |
2840 | /* |
2841 | * Find all the stuff for git_config_set() below. | |
2842 | */ | |
4ddba79d | 2843 | |
fee8572c | 2844 | struct config_store_data { |
f011a965 | 2845 | size_t baselen; |
4b25d091 | 2846 | char *key; |
f98d863d | 2847 | int do_not_match; |
c90702a1 | 2848 | const char *fixed_value; |
247e2f82 | 2849 | regex_t *value_pattern; |
4ddba79d | 2850 | int multi_replace; |
6ae996f2 JS |
2851 | struct { |
2852 | size_t begin, end; | |
2853 | enum config_event_t type; | |
22aedfcc | 2854 | int is_keys_section; |
6ae996f2 JS |
2855 | } *parsed; |
2856 | unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc; | |
5221c315 | 2857 | unsigned int key_seen:1, section_seen:1, is_keys_section:1; |
fee8572c | 2858 | }; |
10bea152 | 2859 | |
2a00e594 MÅ |
2860 | static void config_store_data_clear(struct config_store_data *store) |
2861 | { | |
e7347cb9 | 2862 | free(store->key); |
247e2f82 DS |
2863 | if (store->value_pattern != NULL && |
2864 | store->value_pattern != CONFIG_REGEX_NONE) { | |
2865 | regfree(store->value_pattern); | |
2866 | free(store->value_pattern); | |
3b82542d | 2867 | } |
2a00e594 MÅ |
2868 | free(store->parsed); |
2869 | free(store->seen); | |
2870 | memset(store, 0, sizeof(*store)); | |
2871 | } | |
2872 | ||
fee8572c JS |
2873 | static int matches(const char *key, const char *value, |
2874 | const struct config_store_data *store) | |
f98d863d | 2875 | { |
fee8572c | 2876 | if (strcmp(key, store->key)) |
c1063be2 | 2877 | return 0; /* not ours */ |
c90702a1 DS |
2878 | if (store->fixed_value) |
2879 | return !strcmp(store->fixed_value, value); | |
247e2f82 | 2880 | if (!store->value_pattern) |
c1063be2 | 2881 | return 1; /* always matches */ |
247e2f82 | 2882 | if (store->value_pattern == CONFIG_REGEX_NONE) |
c1063be2 JK |
2883 | return 0; /* never matches */ |
2884 | ||
fee8572c | 2885 | return store->do_not_match ^ |
247e2f82 | 2886 | (value && !regexec(store->value_pattern, value, 0, NULL, 0)); |
f98d863d JS |
2887 | } |
2888 | ||
6ae996f2 JS |
2889 | static int store_aux_event(enum config_event_t type, |
2890 | size_t begin, size_t end, void *data) | |
2891 | { | |
2892 | struct config_store_data *store = data; | |
2893 | ||
2894 | ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc); | |
2895 | store->parsed[store->parsed_nr].begin = begin; | |
2896 | store->parsed[store->parsed_nr].end = end; | |
2897 | store->parsed[store->parsed_nr].type = type; | |
6ae996f2 JS |
2898 | |
2899 | if (type == CONFIG_EVENT_SECTION) { | |
2d84f13d SB |
2900 | int (*cmpfn)(const char *, const char *, size_t); |
2901 | ||
6ae996f2 | 2902 | if (cf->var.len < 2 || cf->var.buf[cf->var.len - 1] != '.') |
a769bfc7 | 2903 | return error(_("invalid section name '%s'"), cf->var.buf); |
6ae996f2 | 2904 | |
2d84f13d SB |
2905 | if (cf->subsection_case_sensitive) |
2906 | cmpfn = strncasecmp; | |
2907 | else | |
2908 | cmpfn = strncmp; | |
2909 | ||
6ae996f2 | 2910 | /* Is this the section we were looking for? */ |
22aedfcc JS |
2911 | store->is_keys_section = |
2912 | store->parsed[store->parsed_nr].is_keys_section = | |
2913 | cf->var.len - 1 == store->baselen && | |
2d84f13d | 2914 | !cmpfn(cf->var.buf, store->key, store->baselen); |
c71d8bb3 JS |
2915 | if (store->is_keys_section) { |
2916 | store->section_seen = 1; | |
2917 | ALLOC_GROW(store->seen, store->seen_nr + 1, | |
2918 | store->seen_alloc); | |
2919 | store->seen[store->seen_nr] = store->parsed_nr; | |
2920 | } | |
6ae996f2 JS |
2921 | } |
2922 | ||
22aedfcc JS |
2923 | store->parsed_nr++; |
2924 | ||
6ae996f2 | 2925 | return 0; |
f98d863d JS |
2926 | } |
2927 | ||
4b25d091 | 2928 | static int store_aux(const char *key, const char *value, void *cb) |
10bea152 | 2929 | { |
fee8572c | 2930 | struct config_store_data *store = cb; |
ae9ee41d | 2931 | |
5221c315 | 2932 | if (store->key_seen) { |
fee8572c | 2933 | if (matches(key, value, store)) { |
668b9ade | 2934 | if (store->seen_nr == 1 && store->multi_replace == 0) { |
8262aaa2 | 2935 | warning(_("%s has multiple values"), key); |
10bea152 | 2936 | } |
4ddba79d | 2937 | |
668b9ade JS |
2938 | ALLOC_GROW(store->seen, store->seen_nr + 1, |
2939 | store->seen_alloc); | |
83786fa4 | 2940 | |
6ae996f2 | 2941 | store->seen[store->seen_nr] = store->parsed_nr; |
668b9ade | 2942 | store->seen_nr++; |
10bea152 | 2943 | } |
5221c315 | 2944 | } else if (store->is_keys_section) { |
ae9ee41d | 2945 | /* |
6ae996f2 JS |
2946 | * Do not increment matches yet: this may not be a match, but we |
2947 | * are in the desired section. | |
ae9ee41d | 2948 | */ |
6ae996f2 JS |
2949 | ALLOC_GROW(store->seen, store->seen_nr + 1, store->seen_alloc); |
2950 | store->seen[store->seen_nr] = store->parsed_nr; | |
5221c315 | 2951 | store->section_seen = 1; |
ae9ee41d | 2952 | |
6ae996f2 JS |
2953 | if (matches(key, value, store)) { |
2954 | store->seen_nr++; | |
2955 | store->key_seen = 1; | |
bdf0ef08 | 2956 | } |
10bea152 | 2957 | } |
5221c315 | 2958 | |
10bea152 JS |
2959 | return 0; |
2960 | } | |
2961 | ||
64c0d71c | 2962 | static int write_error(const char *filename) |
480c9e52 | 2963 | { |
a769bfc7 | 2964 | error(_("failed to write new configuration file %s"), filename); |
480c9e52 AW |
2965 | |
2966 | /* Same error code as "failed to rename". */ | |
2967 | return 4; | |
2968 | } | |
2969 | ||
fee8572c JS |
2970 | static struct strbuf store_create_section(const char *key, |
2971 | const struct config_store_data *store) | |
10bea152 | 2972 | { |
cb891a59 | 2973 | const char *dot; |
f011a965 | 2974 | size_t i; |
f285a2d7 | 2975 | struct strbuf sb = STRBUF_INIT; |
d14f7764 | 2976 | |
fee8572c | 2977 | dot = memchr(key, '.', store->baselen); |
d14f7764 | 2978 | if (dot) { |
cb891a59 | 2979 | strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key); |
fee8572c | 2980 | for (i = dot - key + 1; i < store->baselen; i++) { |
e5c349ba | 2981 | if (key[i] == '"' || key[i] == '\\') |
cb891a59 KH |
2982 | strbuf_addch(&sb, '\\'); |
2983 | strbuf_addch(&sb, key[i]); | |
d14f7764 | 2984 | } |
cb891a59 KH |
2985 | strbuf_addstr(&sb, "\"]\n"); |
2986 | } else { | |
f011a965 JK |
2987 | strbuf_addch(&sb, '['); |
2988 | strbuf_add(&sb, key, store->baselen); | |
2989 | strbuf_addstr(&sb, "]\n"); | |
d14f7764 LT |
2990 | } |
2991 | ||
5463caab SD |
2992 | return sb; |
2993 | } | |
2994 | ||
fee8572c JS |
2995 | static ssize_t write_section(int fd, const char *key, |
2996 | const struct config_store_data *store) | |
5463caab | 2997 | { |
fee8572c | 2998 | struct strbuf sb = store_create_section(key, store); |
3b48045c | 2999 | ssize_t ret; |
5463caab | 3000 | |
782c030e | 3001 | ret = write_in_full(fd, sb.buf, sb.len); |
cb891a59 | 3002 | strbuf_release(&sb); |
480c9e52 | 3003 | |
d9bd4cbb | 3004 | return ret; |
10bea152 JS |
3005 | } |
3006 | ||
fee8572c JS |
3007 | static ssize_t write_pair(int fd, const char *key, const char *value, |
3008 | const struct config_store_data *store) | |
10bea152 | 3009 | { |
d9bd4cbb JK |
3010 | int i; |
3011 | ssize_t ret; | |
cb891a59 | 3012 | const char *quote = ""; |
f285a2d7 | 3013 | struct strbuf sb = STRBUF_INIT; |
cdd4fb15 | 3014 | |
6281f394 JM |
3015 | /* |
3016 | * Check to see if the value needs to be surrounded with a dq pair. | |
3017 | * Note that problematic characters are always backslash-quoted; this | |
3018 | * check is about not losing leading or trailing SP and strings that | |
3019 | * follow beginning-of-comment characters (i.e. ';' and '#') by the | |
3020 | * configuration parser. | |
3021 | */ | |
cdd4fb15 | 3022 | if (value[0] == ' ') |
cb891a59 | 3023 | quote = "\""; |
cdd4fb15 BG |
3024 | for (i = 0; value[i]; i++) |
3025 | if (value[i] == ';' || value[i] == '#') | |
cb891a59 KH |
3026 | quote = "\""; |
3027 | if (i && value[i - 1] == ' ') | |
3028 | quote = "\""; | |
3029 | ||
6c7e6963 | 3030 | strbuf_addf(&sb, "\t%s = %s", key + store->baselen + 1, quote); |
10bea152 | 3031 | |
10bea152 JS |
3032 | for (i = 0; value[i]; i++) |
3033 | switch (value[i]) { | |
480c9e52 | 3034 | case '\n': |
cb891a59 | 3035 | strbuf_addstr(&sb, "\\n"); |
480c9e52 AW |
3036 | break; |
3037 | case '\t': | |
cb891a59 | 3038 | strbuf_addstr(&sb, "\\t"); |
480c9e52 AW |
3039 | break; |
3040 | case '"': | |
3041 | case '\\': | |
cb891a59 | 3042 | strbuf_addch(&sb, '\\'); |
1cf01a34 | 3043 | /* fallthrough */ |
480c9e52 | 3044 | default: |
cb891a59 | 3045 | strbuf_addch(&sb, value[i]); |
480c9e52 AW |
3046 | break; |
3047 | } | |
cb891a59 KH |
3048 | strbuf_addf(&sb, "%s\n", quote); |
3049 | ||
d9bd4cbb | 3050 | ret = write_in_full(fd, sb.buf, sb.len); |
cb891a59 KH |
3051 | strbuf_release(&sb); |
3052 | ||
d9bd4cbb | 3053 | return ret; |
10bea152 JS |
3054 | } |
3055 | ||
22aedfcc JS |
3056 | /* |
3057 | * If we are about to unset the last key(s) in a section, and if there are | |
3058 | * no comments surrounding (or included in) the section, we will want to | |
3059 | * extend begin/end to remove the entire section. | |
3060 | * | |
3061 | * Note: the parameter `seen_ptr` points to the index into the store.seen | |
3062 | * array. * This index may be incremented if a section has more than one | |
3063 | * entry (which all are to be removed). | |
3064 | */ | |
3065 | static void maybe_remove_section(struct config_store_data *store, | |
22aedfcc JS |
3066 | size_t *begin_offset, size_t *end_offset, |
3067 | int *seen_ptr) | |
4ddba79d | 3068 | { |
22aedfcc JS |
3069 | size_t begin; |
3070 | int i, seen, section_seen = 0; | |
4ddba79d | 3071 | |
22aedfcc JS |
3072 | /* |
3073 | * First, ensure that this is the first key, and that there are no | |
3074 | * comments before the entry nor before the section header. | |
3075 | */ | |
3076 | seen = *seen_ptr; | |
3077 | for (i = store->seen[seen]; i > 0; i--) { | |
3078 | enum config_event_t type = store->parsed[i - 1].type; | |
3079 | ||
3080 | if (type == CONFIG_EVENT_COMMENT) | |
3081 | /* There is a comment before this entry or section */ | |
3082 | return; | |
3083 | if (type == CONFIG_EVENT_ENTRY) { | |
3084 | if (!section_seen) | |
3085 | /* This is not the section's first entry. */ | |
3086 | return; | |
3087 | /* We encountered no comment before the section. */ | |
3088 | break; | |
3089 | } | |
3090 | if (type == CONFIG_EVENT_SECTION) { | |
3091 | if (!store->parsed[i - 1].is_keys_section) | |
3092 | break; | |
3093 | section_seen = 1; | |
4ddba79d | 3094 | } |
7a31cc0f | 3095 | } |
22aedfcc JS |
3096 | begin = store->parsed[i].begin; |
3097 | ||
3098 | /* | |
46a237f4 | 3099 | * Next, make sure that we are removing the last key(s) in the section, |
22aedfcc JS |
3100 | * and that there are no comments that are possibly about the current |
3101 | * section. | |
3102 | */ | |
3103 | for (i = store->seen[seen] + 1; i < store->parsed_nr; i++) { | |
3104 | enum config_event_t type = store->parsed[i].type; | |
4ddba79d | 3105 | |
22aedfcc JS |
3106 | if (type == CONFIG_EVENT_COMMENT) |
3107 | return; | |
3108 | if (type == CONFIG_EVENT_SECTION) { | |
3109 | if (store->parsed[i].is_keys_section) | |
3110 | continue; | |
3111 | break; | |
3112 | } | |
3113 | if (type == CONFIG_EVENT_ENTRY) { | |
3114 | if (++seen < store->seen_nr && | |
3115 | i == store->seen[seen]) | |
3116 | /* We want to remove this entry, too */ | |
3117 | continue; | |
3118 | /* There is another entry in this section. */ | |
3119 | return; | |
3120 | } | |
3121 | } | |
3122 | ||
3123 | /* | |
3124 | * We are really removing the last entry/entries from this section, and | |
3125 | * there are no enclosed or surrounding comments. Remove the entire, | |
3126 | * now-empty section. | |
3127 | */ | |
3128 | *seen_ptr = seen; | |
3129 | *begin_offset = begin; | |
3130 | if (i < store->parsed_nr) | |
3131 | *end_offset = store->parsed[i].begin; | |
3132 | else | |
3133 | *end_offset = store->parsed[store->parsed_nr - 1].end; | |
4ddba79d JS |
3134 | } |
3135 | ||
30598ad0 PS |
3136 | int git_config_set_in_file_gently(const char *config_filename, |
3137 | const char *key, const char *value) | |
5ec31182 | 3138 | { |
30598ad0 | 3139 | return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0); |
5ec31182 RR |
3140 | } |
3141 | ||
3d180648 PS |
3142 | void git_config_set_in_file(const char *config_filename, |
3143 | const char *key, const char *value) | |
10bea152 | 3144 | { |
3d180648 | 3145 | git_config_set_multivar_in_file(config_filename, key, value, NULL, 0); |
b4c8aba6 PS |
3146 | } |
3147 | ||
30598ad0 | 3148 | int git_config_set_gently(const char *key, const char *value) |
10bea152 | 3149 | { |
30598ad0 | 3150 | return git_config_set_multivar_gently(key, value, NULL, 0); |
10bea152 JS |
3151 | } |
3152 | ||
fe187339 DS |
3153 | int repo_config_set_worktree_gently(struct repository *r, |
3154 | const char *key, const char *value) | |
3155 | { | |
3156 | /* Only use worktree-specific config if it is is already enabled. */ | |
3157 | if (repository_format_worktree_config) { | |
3158 | char *file = repo_git_path(r, "config.worktree"); | |
3159 | int ret = git_config_set_multivar_in_file_gently( | |
3160 | file, key, value, NULL, 0); | |
3161 | free(file); | |
3162 | return ret; | |
3163 | } | |
3164 | return repo_config_set_multivar_gently(r, key, value, NULL, 0); | |
3165 | } | |
3166 | ||
3d180648 | 3167 | void git_config_set(const char *key, const char *value) |
b4c8aba6 | 3168 | { |
3d180648 | 3169 | git_config_set_multivar(key, value, NULL, 0); |
ee4512ed JH |
3170 | |
3171 | trace2_cmd_set_config(key, value); | |
10bea152 JS |
3172 | } |
3173 | ||
3174 | /* | |
3175 | * If value==NULL, unset in (remove from) config, | |
247e2f82 DS |
3176 | * if value_pattern!=NULL, disregard key/value pairs where value does not match. |
3177 | * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values | |
c1063be2 | 3178 | * (only add a new one) |
504ee129 DS |
3179 | * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching |
3180 | * key/values are removed before a single new pair is written. If the | |
3181 | * flag is not present, then replace only the first match. | |
10bea152 JS |
3182 | * |
3183 | * Returns 0 on success. | |
3184 | * | |
3185 | * This function does this: | |
3186 | * | |
3187 | * - it locks the config file by creating ".git/config.lock" | |
3188 | * | |
3189 | * - it then parses the config using store_aux() as validator to find | |
3190 | * the position on the key/value pair to replace. If it is to be unset, | |
3191 | * it must be found exactly once. | |
3192 | * | |
3193 | * - the config file is mmap()ed and the part before the match (if any) is | |
3194 | * written to the lock file, then the changed part and the rest. | |
3195 | * | |
3196 | * - the config file is removed and the lock file rename()d to it. | |
3197 | * | |
3198 | */ | |
30598ad0 PS |
3199 | int git_config_set_multivar_in_file_gently(const char *config_filename, |
3200 | const char *key, const char *value, | |
247e2f82 | 3201 | const char *value_pattern, |
504ee129 | 3202 | unsigned flags) |
10bea152 | 3203 | { |
54d160ec | 3204 | int fd = -1, in_fd = -1; |
dafc88b1 | 3205 | int ret; |
bfffb48c | 3206 | struct lock_file lock = LOCK_INIT; |
0a5f5759 | 3207 | char *filename_buf = NULL; |
3a1b3126 JK |
3208 | char *contents = NULL; |
3209 | size_t contents_sz; | |
fee8572c JS |
3210 | struct config_store_data store; |
3211 | ||
3212 | memset(&store, 0, sizeof(store)); | |
4ddba79d | 3213 | |
b09c53a3 LP |
3214 | /* parse-key returns negative; flip the sign to feed exit(3) */ |
3215 | ret = 0 - git_config_parse_key(key, &store.key, &store.baselen); | |
3216 | if (ret) | |
dafc88b1 | 3217 | goto out_free; |
b17e659d | 3218 | |
504ee129 | 3219 | store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0; |
10bea152 | 3220 | |
0a5f5759 JK |
3221 | if (!config_filename) |
3222 | config_filename = filename_buf = git_pathdup("config"); | |
10bea152 JS |
3223 | |
3224 | /* | |
6cbf973c | 3225 | * The lock serves a purpose in addition to locking: the new |
10bea152 JS |
3226 | * contents of .git/config will be written into it. |
3227 | */ | |
bfffb48c | 3228 | fd = hold_lock_file_for_update(&lock, config_filename, 0); |
6cbf973c | 3229 | if (fd < 0) { |
a769bfc7 | 3230 | error_errno(_("could not lock config file %s"), config_filename); |
7a397419 | 3231 | ret = CONFIG_NO_LOCK; |
dafc88b1 | 3232 | goto out_free; |
10bea152 JS |
3233 | } |
3234 | ||
3235 | /* | |
3236 | * If .git/config does not exist yet, write a minimal version. | |
3237 | */ | |
88fb958b AR |
3238 | in_fd = open(config_filename, O_RDONLY); |
3239 | if ( in_fd < 0 ) { | |
88fb958b | 3240 | if ( ENOENT != errno ) { |
a769bfc7 | 3241 | error_errno(_("opening %s"), config_filename); |
7a397419 | 3242 | ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */ |
dafc88b1 | 3243 | goto out_free; |
88fb958b | 3244 | } |
10bea152 | 3245 | /* if nothing to unset, error out */ |
afe8a907 | 3246 | if (!value) { |
7a397419 | 3247 | ret = CONFIG_NOTHING_SET; |
dafc88b1 | 3248 | goto out_free; |
10bea152 JS |
3249 | } |
3250 | ||
e7347cb9 MÅ |
3251 | free(store.key); |
3252 | store.key = xstrdup(key); | |
fee8572c JS |
3253 | if (write_section(fd, key, &store) < 0 || |
3254 | write_pair(fd, key, value, &store) < 0) | |
93c1e079 JH |
3255 | goto write_err_out; |
3256 | } else { | |
88fb958b | 3257 | struct stat st; |
3a1b3126 | 3258 | size_t copy_begin, copy_end; |
dc49cd76 | 3259 | int i, new_line = 0; |
6ae996f2 | 3260 | struct config_options opts; |
10bea152 | 3261 | |
afe8a907 | 3262 | if (!value_pattern) |
247e2f82 DS |
3263 | store.value_pattern = NULL; |
3264 | else if (value_pattern == CONFIG_REGEX_NONE) | |
3265 | store.value_pattern = CONFIG_REGEX_NONE; | |
c90702a1 DS |
3266 | else if (flags & CONFIG_FLAGS_FIXED_VALUE) |
3267 | store.fixed_value = value_pattern; | |
10bea152 | 3268 | else { |
247e2f82 | 3269 | if (value_pattern[0] == '!') { |
f98d863d | 3270 | store.do_not_match = 1; |
247e2f82 | 3271 | value_pattern++; |
f98d863d JS |
3272 | } else |
3273 | store.do_not_match = 0; | |
3274 | ||
247e2f82 DS |
3275 | store.value_pattern = (regex_t*)xmalloc(sizeof(regex_t)); |
3276 | if (regcomp(store.value_pattern, value_pattern, | |
10bea152 | 3277 | REG_EXTENDED)) { |
247e2f82 DS |
3278 | error(_("invalid pattern: %s"), value_pattern); |
3279 | FREE_AND_NULL(store.value_pattern); | |
7a397419 | 3280 | ret = CONFIG_INVALID_PATTERN; |
dafc88b1 | 3281 | goto out_free; |
10bea152 JS |
3282 | } |
3283 | } | |
3284 | ||
6ae996f2 JS |
3285 | ALLOC_GROW(store.parsed, 1, store.parsed_alloc); |
3286 | store.parsed[0].end = 0; | |
3287 | ||
3288 | memset(&opts, 0, sizeof(opts)); | |
3289 | opts.event_fn = store_aux_event; | |
3290 | opts.event_fn_data = &store; | |
10bea152 JS |
3291 | |
3292 | /* | |
6ae996f2 JS |
3293 | * After this, store.parsed will contain offsets of all the |
3294 | * parsed elements, and store.seen will contain a list of | |
3295 | * matches, as indices into store.parsed. | |
3296 | * | |
10bea152 JS |
3297 | * As a side effect, we make sure to transform only a valid |
3298 | * existing config file. | |
3299 | */ | |
6ae996f2 JS |
3300 | if (git_config_from_file_with_options(store_aux, |
3301 | config_filename, | |
3302 | &store, &opts)) { | |
a769bfc7 | 3303 | error(_("invalid config file %s"), config_filename); |
7a397419 | 3304 | ret = CONFIG_INVALID_FILE; |
dafc88b1 | 3305 | goto out_free; |
10bea152 JS |
3306 | } |
3307 | ||
4ddba79d | 3308 | /* if nothing to unset, or too many matches, error out */ |
668b9ade | 3309 | if ((store.seen_nr == 0 && value == NULL) || |
504ee129 | 3310 | (store.seen_nr > 1 && !store.multi_replace)) { |
7a397419 | 3311 | ret = CONFIG_NOTHING_SET; |
dafc88b1 | 3312 | goto out_free; |
10bea152 JS |
3313 | } |
3314 | ||
29647d79 NTND |
3315 | if (fstat(in_fd, &st) == -1) { |
3316 | error_errno(_("fstat on %s failed"), config_filename); | |
3317 | ret = CONFIG_INVALID_FILE; | |
3318 | goto out_free; | |
3319 | } | |
3320 | ||
dc49cd76 | 3321 | contents_sz = xsize_t(st.st_size); |
1570856b JK |
3322 | contents = xmmap_gently(NULL, contents_sz, PROT_READ, |
3323 | MAP_PRIVATE, in_fd, 0); | |
3324 | if (contents == MAP_FAILED) { | |
0e8771f1 JK |
3325 | if (errno == ENODEV && S_ISDIR(st.st_mode)) |
3326 | errno = EISDIR; | |
dc059294 EW |
3327 | error_errno(_("unable to mmap '%s'%s"), |
3328 | config_filename, mmap_os_err()); | |
1570856b JK |
3329 | ret = CONFIG_INVALID_FILE; |
3330 | contents = NULL; | |
3331 | goto out_free; | |
3332 | } | |
10bea152 | 3333 | close(in_fd); |
54d160ec | 3334 | in_fd = -1; |
10bea152 | 3335 | |
bfffb48c | 3336 | if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) { |
a769bfc7 | 3337 | error_errno(_("chmod on %s failed"), get_lock_file_path(&lock)); |
daa22c6f EW |
3338 | ret = CONFIG_NO_WRITE; |
3339 | goto out_free; | |
3340 | } | |
3341 | ||
6ae996f2 JS |
3342 | if (store.seen_nr == 0) { |
3343 | if (!store.seen_alloc) { | |
3344 | /* Did not see key nor section */ | |
3345 | ALLOC_GROW(store.seen, 1, store.seen_alloc); | |
3346 | store.seen[0] = store.parsed_nr | |
3347 | - !!store.parsed_nr; | |
3348 | } | |
668b9ade | 3349 | store.seen_nr = 1; |
6ae996f2 | 3350 | } |
4ddba79d | 3351 | |
668b9ade | 3352 | for (i = 0, copy_begin = 0; i < store.seen_nr; i++) { |
6ae996f2 JS |
3353 | size_t replace_end; |
3354 | int j = store.seen[i]; | |
3355 | ||
46fc89ce | 3356 | new_line = 0; |
6ae996f2 | 3357 | if (!store.key_seen) { |
c71d8bb3 JS |
3358 | copy_end = store.parsed[j].end; |
3359 | /* include '\n' when copying section header */ | |
3360 | if (copy_end > 0 && copy_end < contents_sz && | |
3361 | contents[copy_end - 1] != '\n' && | |
3362 | contents[copy_end] == '\n') | |
3363 | copy_end++; | |
3364 | replace_end = copy_end; | |
6ae996f2 JS |
3365 | } else { |
3366 | replace_end = store.parsed[j].end; | |
3367 | copy_end = store.parsed[j].begin; | |
22aedfcc | 3368 | if (!value) |
a263ea84 | 3369 | maybe_remove_section(&store, |
22aedfcc JS |
3370 | ©_end, |
3371 | &replace_end, &i); | |
6ae996f2 JS |
3372 | /* |
3373 | * Swallow preceding white-space on the same | |
3374 | * line. | |
3375 | */ | |
3376 | while (copy_end > 0 ) { | |
3377 | char c = contents[copy_end - 1]; | |
3378 | ||
3379 | if (isspace(c) && c != '\n') | |
3380 | copy_end--; | |
3381 | else | |
3382 | break; | |
3383 | } | |
3384 | } | |
4ddba79d | 3385 | |
02e5ba4a JK |
3386 | if (copy_end > 0 && contents[copy_end-1] != '\n') |
3387 | new_line = 1; | |
3388 | ||
4ddba79d JS |
3389 | /* write the first part of the config */ |
3390 | if (copy_end > copy_begin) { | |
93c1e079 | 3391 | if (write_in_full(fd, contents + copy_begin, |
efacf609 | 3392 | copy_end - copy_begin) < 0) |
93c1e079 JH |
3393 | goto write_err_out; |
3394 | if (new_line && | |
06f46f23 | 3395 | write_str_in_full(fd, "\n") < 0) |
93c1e079 | 3396 | goto write_err_out; |
4ddba79d | 3397 | } |
6ae996f2 | 3398 | copy_begin = replace_end; |
10bea152 JS |
3399 | } |
3400 | ||
10bea152 | 3401 | /* write the pair (value == NULL means unset) */ |
afe8a907 | 3402 | if (value) { |
5221c315 | 3403 | if (!store.section_seen) { |
fee8572c | 3404 | if (write_section(fd, key, &store) < 0) |
93c1e079 | 3405 | goto write_err_out; |
480c9e52 | 3406 | } |
fee8572c | 3407 | if (write_pair(fd, key, value, &store) < 0) |
93c1e079 | 3408 | goto write_err_out; |
10bea152 JS |
3409 | } |
3410 | ||
3411 | /* write the rest of the config */ | |
dc49cd76 | 3412 | if (copy_begin < contents_sz) |
93c1e079 | 3413 | if (write_in_full(fd, contents + copy_begin, |
efacf609 | 3414 | contents_sz - copy_begin) < 0) |
93c1e079 | 3415 | goto write_err_out; |
7a64592c KB |
3416 | |
3417 | munmap(contents, contents_sz); | |
3418 | contents = NULL; | |
10bea152 JS |
3419 | } |
3420 | ||
bfffb48c | 3421 | if (commit_lock_file(&lock) < 0) { |
a769bfc7 | 3422 | error_errno(_("could not write config file %s"), config_filename); |
7a397419 | 3423 | ret = CONFIG_NO_WRITE; |
dafc88b1 | 3424 | goto out_free; |
10bea152 JS |
3425 | } |
3426 | ||
dafc88b1 SH |
3427 | ret = 0; |
3428 | ||
3c8687a7 TA |
3429 | /* Invalidate the config cache */ |
3430 | git_config_clear(); | |
3431 | ||
dafc88b1 | 3432 | out_free: |
bfffb48c | 3433 | rollback_lock_file(&lock); |
0a5f5759 | 3434 | free(filename_buf); |
3a1b3126 JK |
3435 | if (contents) |
3436 | munmap(contents, contents_sz); | |
54d160ec SS |
3437 | if (in_fd >= 0) |
3438 | close(in_fd); | |
2a00e594 | 3439 | config_store_data_clear(&store); |
dafc88b1 | 3440 | return ret; |
93c1e079 JH |
3441 | |
3442 | write_err_out: | |
bfffb48c | 3443 | ret = write_error(get_lock_file_path(&lock)); |
93c1e079 JH |
3444 | goto out_free; |
3445 | ||
10bea152 JS |
3446 | } |
3447 | ||
3d180648 PS |
3448 | void git_config_set_multivar_in_file(const char *config_filename, |
3449 | const char *key, const char *value, | |
247e2f82 | 3450 | const char *value_pattern, unsigned flags) |
b4c8aba6 | 3451 | { |
1cae428e | 3452 | if (!git_config_set_multivar_in_file_gently(config_filename, key, value, |
247e2f82 | 3453 | value_pattern, flags)) |
1cae428e JK |
3454 | return; |
3455 | if (value) | |
8c3ca351 | 3456 | die(_("could not set '%s' to '%s'"), key, value); |
1cae428e JK |
3457 | else |
3458 | die(_("could not unset '%s'"), key); | |
b4c8aba6 PS |
3459 | } |
3460 | ||
30598ad0 | 3461 | int git_config_set_multivar_gently(const char *key, const char *value, |
247e2f82 | 3462 | const char *value_pattern, unsigned flags) |
5ec31182 | 3463 | { |
fe187339 DS |
3464 | return repo_config_set_multivar_gently(the_repository, key, value, |
3465 | value_pattern, flags); | |
3466 | } | |
3467 | ||
3468 | int repo_config_set_multivar_gently(struct repository *r, const char *key, | |
3469 | const char *value, | |
3470 | const char *value_pattern, unsigned flags) | |
3471 | { | |
3472 | char *file = repo_git_path(r, "config"); | |
3473 | int res = git_config_set_multivar_in_file_gently(file, | |
3474 | key, value, | |
3475 | value_pattern, | |
3476 | flags); | |
3477 | free(file); | |
3478 | return res; | |
5ec31182 RR |
3479 | } |
3480 | ||
3d180648 | 3481 | void git_config_set_multivar(const char *key, const char *value, |
247e2f82 | 3482 | const char *value_pattern, unsigned flags) |
5ec31182 | 3483 | { |
fe187339 DS |
3484 | git_config_set_multivar_in_file(git_path("config"), |
3485 | key, value, value_pattern, | |
504ee129 | 3486 | flags); |
5ec31182 RR |
3487 | } |
3488 | ||
118f8b24 PB |
3489 | static int section_name_match (const char *buf, const char *name) |
3490 | { | |
3491 | int i = 0, j = 0, dot = 0; | |
a4c0d463 AV |
3492 | if (buf[i] != '[') |
3493 | return 0; | |
3494 | for (i = 1; buf[i] && buf[i] != ']'; i++) { | |
118f8b24 PB |
3495 | if (!dot && isspace(buf[i])) { |
3496 | dot = 1; | |
3497 | if (name[j++] != '.') | |
3498 | break; | |
3499 | for (i++; isspace(buf[i]); i++) | |
3500 | ; /* do nothing */ | |
3501 | if (buf[i] != '"') | |
3502 | break; | |
3503 | continue; | |
3504 | } | |
3505 | if (buf[i] == '\\' && dot) | |
3506 | i++; | |
3507 | else if (buf[i] == '"' && dot) { | |
3508 | for (i++; isspace(buf[i]); i++) | |
3509 | ; /* do_nothing */ | |
3510 | break; | |
3511 | } | |
3512 | if (buf[i] != name[j++]) | |
3513 | break; | |
3514 | } | |
a4c0d463 AV |
3515 | if (buf[i] == ']' && name[j] == 0) { |
3516 | /* | |
3517 | * We match, now just find the right length offset by | |
3518 | * gobbling up any whitespace after it, as well | |
3519 | */ | |
3520 | i++; | |
3521 | for (; buf[i] && isspace(buf[i]); i++) | |
3522 | ; /* do nothing */ | |
3523 | return i; | |
3524 | } | |
3525 | return 0; | |
118f8b24 PB |
3526 | } |
3527 | ||
94a35b1a JK |
3528 | static int section_name_is_ok(const char *name) |
3529 | { | |
3530 | /* Empty section names are bogus. */ | |
3531 | if (!*name) | |
3532 | return 0; | |
3533 | ||
3534 | /* | |
3535 | * Before a dot, we must be alphanumeric or dash. After the first dot, | |
3536 | * anything goes, so we can stop checking. | |
3537 | */ | |
3538 | for (; *name && *name != '.'; name++) | |
3539 | if (*name != '-' && !isalnum(*name)) | |
3540 | return 0; | |
3541 | return 1; | |
3542 | } | |
3543 | ||
118f8b24 | 3544 | /* if new_name == NULL, the section is removed instead */ |
52d59cc6 | 3545 | static int git_config_copy_or_rename_section_in_file(const char *config_filename, |
fee8572c JS |
3546 | const char *old_name, |
3547 | const char *new_name, int copy) | |
0667fcfb | 3548 | { |
118f8b24 | 3549 | int ret = 0, remove = 0; |
42bd39b5 | 3550 | char *filename_buf = NULL; |
837e34eb | 3551 | struct lock_file lock = LOCK_INIT; |
0667fcfb JS |
3552 | int out_fd; |
3553 | char buf[1024]; | |
4db7dbdb | 3554 | FILE *config_file = NULL; |
daa22c6f | 3555 | struct stat st; |
52d59cc6 | 3556 | struct strbuf copystr = STRBUF_INIT; |
fee8572c JS |
3557 | struct config_store_data store; |
3558 | ||
3559 | memset(&store, 0, sizeof(store)); | |
0667fcfb | 3560 | |
94a35b1a | 3561 | if (new_name && !section_name_is_ok(new_name)) { |
a769bfc7 | 3562 | ret = error(_("invalid section name: %s"), new_name); |
c06fa62d | 3563 | goto out_no_rollback; |
94a35b1a JK |
3564 | } |
3565 | ||
42bd39b5 JK |
3566 | if (!config_filename) |
3567 | config_filename = filename_buf = git_pathdup("config"); | |
3568 | ||
837e34eb | 3569 | out_fd = hold_lock_file_for_update(&lock, config_filename, 0); |
fc1905bb | 3570 | if (out_fd < 0) { |
a769bfc7 | 3571 | ret = error(_("could not lock config file %s"), config_filename); |
fc1905bb JH |
3572 | goto out; |
3573 | } | |
0667fcfb | 3574 | |
fc1905bb | 3575 | if (!(config_file = fopen(config_filename, "rb"))) { |
11dc1fcb NTND |
3576 | ret = warn_on_fopen_errors(config_filename); |
3577 | if (ret) | |
3578 | goto out; | |
01ebb9dc | 3579 | /* no config file means nothing to rename, no error */ |
6e45b43f | 3580 | goto commit_and_out; |
fc1905bb | 3581 | } |
0667fcfb | 3582 | |
29647d79 NTND |
3583 | if (fstat(fileno(config_file), &st) == -1) { |
3584 | ret = error_errno(_("fstat on %s failed"), config_filename); | |
3585 | goto out; | |
3586 | } | |
daa22c6f | 3587 | |
837e34eb | 3588 | if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) { |
a769bfc7 | 3589 | ret = error_errno(_("chmod on %s failed"), |
837e34eb | 3590 | get_lock_file_path(&lock)); |
daa22c6f EW |
3591 | goto out; |
3592 | } | |
3593 | ||
0667fcfb | 3594 | while (fgets(buf, sizeof(buf), config_file)) { |
aec0bba1 | 3595 | unsigned i; |
480c9e52 | 3596 | int length; |
52d59cc6 | 3597 | int is_section = 0; |
9a5abfc7 | 3598 | char *output = buf; |
0667fcfb JS |
3599 | for (i = 0; buf[i] && isspace(buf[i]); i++) |
3600 | ; /* do nothing */ | |
3601 | if (buf[i] == '[') { | |
3602 | /* it's a section */ | |
52d59cc6 SD |
3603 | int offset; |
3604 | is_section = 1; | |
3605 | ||
3606 | /* | |
3607 | * When encountering a new section under -c we | |
3608 | * need to flush out any section we're already | |
3609 | * coping and begin anew. There might be | |
3610 | * multiple [branch "$name"] sections. | |
3611 | */ | |
3612 | if (copystr.len > 0) { | |
c5e3bc6e | 3613 | if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) { |
0b646bca | 3614 | ret = write_error(get_lock_file_path(&lock)); |
52d59cc6 SD |
3615 | goto out; |
3616 | } | |
3617 | strbuf_reset(©str); | |
3618 | } | |
3619 | ||
3620 | offset = section_name_match(&buf[i], old_name); | |
a4c0d463 | 3621 | if (offset > 0) { |
118f8b24 | 3622 | ret++; |
afe8a907 | 3623 | if (!new_name) { |
118f8b24 | 3624 | remove = 1; |
0667fcfb JS |
3625 | continue; |
3626 | } | |
0667fcfb | 3627 | store.baselen = strlen(new_name); |
52d59cc6 | 3628 | if (!copy) { |
fee8572c | 3629 | if (write_section(out_fd, new_name, &store) < 0) { |
0b646bca | 3630 | ret = write_error(get_lock_file_path(&lock)); |
52d59cc6 SD |
3631 | goto out; |
3632 | } | |
9a5abfc7 | 3633 | /* |
52d59cc6 SD |
3634 | * We wrote out the new section, with |
3635 | * a newline, now skip the old | |
3636 | * section's length | |
9a5abfc7 | 3637 | */ |
52d59cc6 SD |
3638 | output += offset + i; |
3639 | if (strlen(output) > 0) { | |
3640 | /* | |
3641 | * More content means there's | |
3642 | * a declaration to put on the | |
3643 | * next line; indent with a | |
3644 | * tab | |
3645 | */ | |
3646 | output -= 1; | |
3647 | output[0] = '\t'; | |
3648 | } | |
3649 | } else { | |
fee8572c | 3650 | copystr = store_create_section(new_name, &store); |
9a5abfc7 | 3651 | } |
0667fcfb | 3652 | } |
118f8b24 | 3653 | remove = 0; |
0667fcfb | 3654 | } |
118f8b24 PB |
3655 | if (remove) |
3656 | continue; | |
9a5abfc7 | 3657 | length = strlen(output); |
52d59cc6 SD |
3658 | |
3659 | if (!is_section && copystr.len > 0) { | |
3660 | strbuf_add(©str, output, length); | |
3661 | } | |
3662 | ||
06f46f23 | 3663 | if (write_in_full(out_fd, output, length) < 0) { |
837e34eb | 3664 | ret = write_error(get_lock_file_path(&lock)); |
480c9e52 AW |
3665 | goto out; |
3666 | } | |
0667fcfb | 3667 | } |
52d59cc6 SD |
3668 | |
3669 | /* | |
3670 | * Copy a trailing section at the end of the config, won't be | |
3671 | * flushed by the usual "flush because we have a new section | |
3672 | * logic in the loop above. | |
3673 | */ | |
3674 | if (copystr.len > 0) { | |
c5e3bc6e | 3675 | if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) { |
0b646bca | 3676 | ret = write_error(get_lock_file_path(&lock)); |
52d59cc6 SD |
3677 | goto out; |
3678 | } | |
3679 | strbuf_reset(©str); | |
3680 | } | |
3681 | ||
fc1905bb | 3682 | fclose(config_file); |
4db7dbdb | 3683 | config_file = NULL; |
6e45b43f | 3684 | commit_and_out: |
837e34eb | 3685 | if (commit_lock_file(&lock) < 0) |
a769bfc7 | 3686 | ret = error_errno(_("could not write config file %s"), |
f0658ec9 | 3687 | config_filename); |
8b590075 | 3688 | out: |
4db7dbdb JS |
3689 | if (config_file) |
3690 | fclose(config_file); | |
837e34eb | 3691 | rollback_lock_file(&lock); |
c06fa62d | 3692 | out_no_rollback: |
42bd39b5 | 3693 | free(filename_buf); |
2a00e594 | 3694 | config_store_data_clear(&store); |
0667fcfb JS |
3695 | return ret; |
3696 | } | |
40ea4ed9 | 3697 | |
52d59cc6 SD |
3698 | int git_config_rename_section_in_file(const char *config_filename, |
3699 | const char *old_name, const char *new_name) | |
3700 | { | |
3701 | return git_config_copy_or_rename_section_in_file(config_filename, | |
3702 | old_name, new_name, 0); | |
3703 | } | |
3704 | ||
42bd39b5 JK |
3705 | int git_config_rename_section(const char *old_name, const char *new_name) |
3706 | { | |
4a7bb5ba | 3707 | return git_config_rename_section_in_file(NULL, old_name, new_name); |
42bd39b5 JK |
3708 | } |
3709 | ||
52d59cc6 SD |
3710 | int git_config_copy_section_in_file(const char *config_filename, |
3711 | const char *old_name, const char *new_name) | |
3712 | { | |
3713 | return git_config_copy_or_rename_section_in_file(config_filename, | |
3714 | old_name, new_name, 1); | |
3715 | } | |
3716 | ||
3717 | int git_config_copy_section(const char *old_name, const char *new_name) | |
3718 | { | |
3719 | return git_config_copy_section_in_file(NULL, old_name, new_name); | |
3720 | } | |
3721 | ||
40ea4ed9 JH |
3722 | /* |
3723 | * Call this to report error for your variable that should not | |
3724 | * get a boolean value (i.e. "[my] var" means "true"). | |
3725 | */ | |
a469a101 | 3726 | #undef config_error_nonbool |
40ea4ed9 JH |
3727 | int config_error_nonbool(const char *var) |
3728 | { | |
a769bfc7 | 3729 | return error(_("missing value for '%s'"), var); |
40ea4ed9 | 3730 | } |
1b86bbb0 JK |
3731 | |
3732 | int parse_config_key(const char *var, | |
3733 | const char *section, | |
f5914f4b | 3734 | const char **subsection, size_t *subsection_len, |
1b86bbb0 JK |
3735 | const char **key) |
3736 | { | |
1b86bbb0 JK |
3737 | const char *dot; |
3738 | ||
3739 | /* Does it start with "section." ? */ | |
e3394fdc | 3740 | if (!skip_prefix(var, section, &var) || *var != '.') |
1b86bbb0 JK |
3741 | return -1; |
3742 | ||
3743 | /* | |
3744 | * Find the key; we don't know yet if we have a subsection, but we must | |
3745 | * parse backwards from the end, since the subsection may have dots in | |
3746 | * it, too. | |
3747 | */ | |
3748 | dot = strrchr(var, '.'); | |
3749 | *key = dot + 1; | |
3750 | ||
3751 | /* Did we have a subsection at all? */ | |
e3394fdc | 3752 | if (dot == var) { |
48f8d9f7 JK |
3753 | if (subsection) { |
3754 | *subsection = NULL; | |
3755 | *subsection_len = 0; | |
3756 | } | |
1b86bbb0 JK |
3757 | } |
3758 | else { | |
48f8d9f7 JK |
3759 | if (!subsection) |
3760 | return -1; | |
e3394fdc | 3761 | *subsection = var + 1; |
1b86bbb0 JK |
3762 | *subsection_len = dot - *subsection; |
3763 | } | |
3764 | ||
3765 | return 0; | |
3766 | } | |
473166b9 LS |
3767 | |
3768 | const char *current_config_origin_type(void) | |
3769 | { | |
1b8132d9 | 3770 | int type; |
0d44a2da JK |
3771 | if (current_config_kvi) |
3772 | type = current_config_kvi->origin_type; | |
3773 | else if(cf) | |
3774 | type = cf->origin_type; | |
3775 | else | |
033abf97 | 3776 | BUG("current_config_origin_type called outside config callback"); |
1b8132d9 VA |
3777 | |
3778 | switch (type) { | |
3779 | case CONFIG_ORIGIN_BLOB: | |
3780 | return "blob"; | |
3781 | case CONFIG_ORIGIN_FILE: | |
3782 | return "file"; | |
3783 | case CONFIG_ORIGIN_STDIN: | |
3784 | return "standard input"; | |
3785 | case CONFIG_ORIGIN_SUBMODULE_BLOB: | |
3786 | return "submodule-blob"; | |
3787 | case CONFIG_ORIGIN_CMDLINE: | |
3788 | return "command line"; | |
3789 | default: | |
033abf97 | 3790 | BUG("unknown config origin type"); |
1b8132d9 | 3791 | } |
473166b9 LS |
3792 | } |
3793 | ||
a5cb4204 MR |
3794 | const char *config_scope_name(enum config_scope scope) |
3795 | { | |
3796 | switch (scope) { | |
3797 | case CONFIG_SCOPE_SYSTEM: | |
3798 | return "system"; | |
3799 | case CONFIG_SCOPE_GLOBAL: | |
3800 | return "global"; | |
6dc905d9 MR |
3801 | case CONFIG_SCOPE_LOCAL: |
3802 | return "local"; | |
3803 | case CONFIG_SCOPE_WORKTREE: | |
3804 | return "worktree"; | |
6766e41b MR |
3805 | case CONFIG_SCOPE_COMMAND: |
3806 | return "command"; | |
9a83d088 MR |
3807 | case CONFIG_SCOPE_SUBMODULE: |
3808 | return "submodule"; | |
a5cb4204 MR |
3809 | default: |
3810 | return "unknown"; | |
3811 | } | |
3812 | } | |
3813 | ||
473166b9 LS |
3814 | const char *current_config_name(void) |
3815 | { | |
0d44a2da JK |
3816 | const char *name; |
3817 | if (current_config_kvi) | |
3818 | name = current_config_kvi->filename; | |
3819 | else if (cf) | |
3820 | name = cf->name; | |
3821 | else | |
033abf97 | 3822 | BUG("current_config_name called outside config callback"); |
0d44a2da | 3823 | return name ? name : ""; |
473166b9 | 3824 | } |
9acc5911 JK |
3825 | |
3826 | enum config_scope current_config_scope(void) | |
3827 | { | |
3828 | if (current_config_kvi) | |
3829 | return current_config_kvi->scope; | |
3830 | else | |
3831 | return current_parsing_scope; | |
473166b9 | 3832 | } |
a73b3680 | 3833 | |
f2a2327a BW |
3834 | int current_config_line(void) |
3835 | { | |
3836 | if (current_config_kvi) | |
3837 | return current_config_kvi->linenr; | |
3838 | else | |
3839 | return cf->linenr; | |
3840 | } | |
3841 | ||
a73b3680 NTND |
3842 | int lookup_config(const char **mapping, int nr_mapping, const char *var) |
3843 | { | |
3844 | int i; | |
3845 | ||
3846 | for (i = 0; i < nr_mapping; i++) { | |
3847 | const char *name = mapping[i]; | |
3848 | ||
3849 | if (name && !strcasecmp(var, name)) | |
3850 | return i; | |
3851 | } | |
3852 | return -1; | |
3853 | } |