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