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