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