blob: f9c400ad3062d9953142d6a2f9c4b357962c48c7 [file] [log] [blame]
Johannes Schindelin10bea152005-11-17 22:32:36 +01001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
6 *
7 */
Linus Torvalds17712992005-10-10 16:31:08 -07008#include "cache.h"
Elijah Newrene730b812018-08-15 10:54:07 -07009#include "branch.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070010#include "config.h"
Patrick Steinhardtd8d77152021-01-12 13:27:14 +010011#include "environment.h"
Brandon Williams3b256222017-06-22 11:43:42 -070012#include "repository.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020013#include "lockfile.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -070014#include "exec-cmd.h"
Alex Riesen572e4f62010-03-26 23:56:01 +010015#include "strbuf.h"
Jeff King2b64fc82010-08-23 15:16:00 -040016#include "quote.h"
Tanay Abhra3c8687a2014-07-28 03:10:38 -070017#include "hashmap.h"
18#include "string-list.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070019#include "object-store.h"
Junio C Hamano599446d2015-04-16 10:47:45 -070020#include "utf8.h"
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +070021#include "dir.h"
Taylor Blau6d2f9ac2018-04-09 17:18:28 -070022#include "color.h"
Denton Liu07b2c0e2019-06-05 17:21:59 -040023#include "refs.h"
Linus Torvalds17712992005-10-10 16:31:08 -070024
Heiko Voigt4d8dd142013-07-12 00:44:39 +020025struct config_source {
26 struct config_source *prev;
27 union {
28 FILE *file;
Heiko Voigt1bc88812013-07-12 00:46:47 +020029 struct config_buf {
30 const char *buf;
31 size_t len;
32 size_t pos;
33 } buf;
Heiko Voigt4d8dd142013-07-12 00:44:39 +020034 } u;
Vasco Almeida1b8132d2016-07-28 13:14:03 +000035 enum config_origin_type origin_type;
Ramsay Jones924aaf32011-06-16 21:24:51 +010036 const char *name;
Jeff Kingd14d4242014-02-19 00:58:52 +020037 const char *path;
Jeff King66f97222018-06-28 18:05:00 -040038 enum config_error_action default_error_action;
Ramsay Jones924aaf32011-06-16 21:24:51 +010039 int linenr;
40 int eof;
Jeff King348482d2020-04-10 15:50:07 -040041 size_t total_len;
Ramsay Jones924aaf32011-06-16 21:24:51 +010042 struct strbuf value;
Ben Walton0971e992012-09-30 20:44:36 +010043 struct strbuf var;
Stefan Beller2d84f132018-08-08 12:50:19 -070044 unsigned subsection_case_sensitive : 1;
Ramsay Jones924aaf32011-06-16 21:24:51 +010045
Jeff King49d6cfa2013-08-26 17:57:18 -040046 int (*do_fgetc)(struct config_source *c);
47 int (*do_ungetc)(int c, struct config_source *conf);
48 long (*do_ftell)(struct config_source *c);
Heiko Voigt4d8dd142013-07-12 00:44:39 +020049};
50
Jeff King0d44a2d2016-05-26 20:32:23 -040051/*
52 * These variables record the "current" config source, which
53 * can be accessed by parsing callbacks.
54 *
55 * The "cf" variable will be non-NULL only when we are actually parsing a real
56 * config source (file, blob, cmdline, etc).
57 *
58 * The "current_config_kvi" variable will be non-NULL only when we are feeding
59 * cached config from a configset into a callback.
60 *
61 * They should generally never be non-NULL at the same time. If they are both
62 * NULL, then we aren't parsing anything (and depending on the function looking
63 * at the variables, it's either a bug for it to be called in the first place,
64 * or it's a function which can be reused for non-config purposes, and should
65 * fall back to some sane behavior).
66 */
Heiko Voigt4d8dd142013-07-12 00:44:39 +020067static struct config_source *cf;
Jeff King0d44a2d2016-05-26 20:32:23 -040068static struct key_value_info *current_config_kvi;
Ramsay Jones924aaf32011-06-16 21:24:51 +010069
Jeff King9acc5912016-05-18 18:44:23 -040070/*
71 * Similar to the variables above, this gives access to the "scope" of the
72 * current value (repo, global, etc). For cached values, it can be found via
73 * the current_config_kvi as above. During parsing, the current value can be
74 * found in this variable. It's not part of "cf" because it transcends a single
75 * file (i.e., a file included from .git/config is still in "repo" scope).
76 */
77static enum config_scope current_parsing_scope;
Jeff King02e5ba42008-01-01 01:17:34 -050078
Junio C Hamano8de7eeb2016-11-15 17:42:40 -080079static int core_compression_seen;
80static int pack_compression_seen;
Dana How960ccca2007-05-09 13:56:50 -070081static int zlib_compression_seen;
82
Heiko Voigt4d8dd142013-07-12 00:44:39 +020083static int config_file_fgetc(struct config_source *conf)
84{
Jeff King260d4082015-04-16 04:51:18 -040085 return getc_unlocked(conf->u.file);
Heiko Voigt4d8dd142013-07-12 00:44:39 +020086}
87
88static int config_file_ungetc(int c, struct config_source *conf)
89{
90 return ungetc(c, conf->u.file);
91}
92
93static long config_file_ftell(struct config_source *conf)
94{
95 return ftell(conf->u.file);
96}
97
Heiko Voigt1bc88812013-07-12 00:46:47 +020098
99static int config_buf_fgetc(struct config_source *conf)
100{
101 if (conf->u.buf.pos < conf->u.buf.len)
102 return conf->u.buf.buf[conf->u.buf.pos++];
103
104 return EOF;
105}
106
107static int config_buf_ungetc(int c, struct config_source *conf)
108{
Jeff King1d0655c2015-02-05 16:00:24 -0500109 if (conf->u.buf.pos > 0) {
110 conf->u.buf.pos--;
111 if (conf->u.buf.buf[conf->u.buf.pos] != c)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200112 BUG("config_buf can only ungetc the same character");
Jeff King1d0655c2015-02-05 16:00:24 -0500113 return c;
114 }
Heiko Voigt1bc88812013-07-12 00:46:47 +0200115
116 return EOF;
117}
118
119static long config_buf_ftell(struct config_source *conf)
120{
121 return conf->u.buf.pos;
122}
123
Jeff King9b25a0b2012-02-06 04:54:04 -0500124#define MAX_INCLUDE_DEPTH 10
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200125static const char include_depth_advice[] = N_(
Jeff King9b25a0b2012-02-06 04:54:04 -0500126"exceeded maximum include depth (%d) while including\n"
127" %s\n"
128"from\n"
129" %s\n"
Jean-Noël Avila27c929e2018-08-23 23:00:56 +0200130"This might be due to circular includes.");
Jeff King9b25a0b2012-02-06 04:54:04 -0500131static int handle_path_include(const char *path, struct config_include_data *inc)
132{
133 int ret = 0;
134 struct strbuf buf = STRBUF_INIT;
Jeff King67beb602014-01-27 20:37:30 -0500135 char *expanded;
Jeff King4c0a89f2012-04-25 08:00:36 -0400136
Jeff King67beb602014-01-27 20:37:30 -0500137 if (!path)
138 return config_error_nonbool("include.path");
139
Nguyễn Thái Ngọc Duy4aad2f12017-04-05 17:24:38 +0700140 expanded = expand_user_path(path, 0);
Jeff King4c0a89f2012-04-25 08:00:36 -0400141 if (!expanded)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200142 return error(_("could not expand include path '%s'"), path);
Jeff King4c0a89f2012-04-25 08:00:36 -0400143 path = expanded;
Jeff King9b25a0b2012-02-06 04:54:04 -0500144
145 /*
146 * Use an absolute path as-is, but interpret relative paths
147 * based on the including config file.
148 */
149 if (!is_absolute_path(path)) {
150 char *slash;
151
Jeff Kingd14d4242014-02-19 00:58:52 +0200152 if (!cf || !cf->path)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200153 return error(_("relative config includes must come from files"));
Jeff King9b25a0b2012-02-06 04:54:04 -0500154
Jeff Kingd14d4242014-02-19 00:58:52 +0200155 slash = find_last_dir_sep(cf->path);
Jeff King9b25a0b2012-02-06 04:54:04 -0500156 if (slash)
Jeff Kingd14d4242014-02-19 00:58:52 +0200157 strbuf_add(&buf, cf->path, slash - cf->path + 1);
Jeff King9b25a0b2012-02-06 04:54:04 -0500158 strbuf_addstr(&buf, path);
159 path = buf.buf;
160 }
161
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700162 if (!access_or_die(path, R_OK, 0)) {
Jeff King9b25a0b2012-02-06 04:54:04 -0500163 if (++inc->depth > MAX_INCLUDE_DEPTH)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200164 die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path,
Jeff King32582582016-05-18 18:41:08 -0400165 !cf ? "<unknown>" :
166 cf->name ? cf->name :
167 "the command line");
Jeff King9b25a0b2012-02-06 04:54:04 -0500168 ret = git_config_from_file(git_config_include, path, inc);
169 inc->depth--;
170 }
171 strbuf_release(&buf);
Jeff King4c0a89f2012-04-25 08:00:36 -0400172 free(expanded);
Jeff King9b25a0b2012-02-06 04:54:04 -0500173 return ret;
174}
175
Denton Liu07b2c0e2019-06-05 17:21:59 -0400176static void add_trailing_starstar_for_dir(struct strbuf *pat)
177{
178 if (pat->len && is_dir_sep(pat->buf[pat->len - 1]))
179 strbuf_addstr(pat, "**");
180}
181
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700182static int prepare_include_condition_pattern(struct strbuf *pat)
183{
184 struct strbuf path = STRBUF_INIT;
185 char *expanded;
186 int prefix = 0;
187
Nguyễn Thái Ngọc Duy86f95152017-04-05 17:24:39 +0700188 expanded = expand_user_path(pat->buf, 1);
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700189 if (expanded) {
190 strbuf_reset(pat);
191 strbuf_addstr(pat, expanded);
192 free(expanded);
193 }
194
195 if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) {
196 const char *slash;
197
198 if (!cf || !cf->path)
199 return error(_("relative config include "
200 "conditionals must come from files"));
201
Nguyễn Thái Ngọc Duy86f95152017-04-05 17:24:39 +0700202 strbuf_realpath(&path, cf->path, 1);
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700203 slash = find_last_dir_sep(path.buf);
204 if (!slash)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200205 BUG("how is this possible?");
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700206 strbuf_splice(pat, 0, 1, path.buf, slash - path.buf);
207 prefix = slash - path.buf + 1 /* slash */;
208 } else if (!is_absolute_path(pat->buf))
René Scharfea91cc7f2020-02-09 14:44:23 +0100209 strbuf_insertstr(pat, 0, "**/");
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700210
Denton Liu07b2c0e2019-06-05 17:21:59 -0400211 add_trailing_starstar_for_dir(pat);
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700212
213 strbuf_release(&path);
214 return prefix;
215}
216
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700217static int include_by_gitdir(const struct config_options *opts,
218 const char *cond, size_t cond_len, int icase)
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700219{
220 struct strbuf text = STRBUF_INIT;
221 struct strbuf pattern = STRBUF_INIT;
222 int ret = 0, prefix;
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700223 const char *git_dir;
Ævar Arnfjörð Bjarmason0624c632017-05-16 08:28:46 +0000224 int already_tried_absolute = 0;
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700225
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700226 if (opts->git_dir)
227 git_dir = opts->git_dir;
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700228 else
229 goto done;
230
Junio C Hamanoc9672ba2017-04-26 15:39:05 +0900231 strbuf_realpath(&text, git_dir, 1);
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700232 strbuf_add(&pattern, cond, cond_len);
233 prefix = prepare_include_condition_pattern(&pattern);
234
Ævar Arnfjörð Bjarmason0624c632017-05-16 08:28:46 +0000235again:
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700236 if (prefix < 0)
237 goto done;
238
239 if (prefix > 0) {
240 /*
241 * perform literal matching on the prefix part so that
242 * any wildcard character in it can't create side effects.
243 */
244 if (text.len < prefix)
245 goto done;
246 if (!icase && strncmp(pattern.buf, text.buf, prefix))
247 goto done;
248 if (icase && strncasecmp(pattern.buf, text.buf, prefix))
249 goto done;
250 }
251
252 ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
Nguyễn Thái Ngọc Duy19e7fda2019-03-26 16:41:01 +0700253 WM_PATHNAME | (icase ? WM_CASEFOLD : 0));
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700254
Ævar Arnfjörð Bjarmason0624c632017-05-16 08:28:46 +0000255 if (!ret && !already_tried_absolute) {
256 /*
257 * We've tried e.g. matching gitdir:~/work, but if
258 * ~/work is a symlink to /mnt/storage/work
259 * strbuf_realpath() will expand it, so the rule won't
260 * match. Let's match against a
261 * strbuf_add_absolute_path() version of the path,
262 * which'll do the right thing
263 */
264 strbuf_reset(&text);
265 strbuf_add_absolute_path(&text, git_dir);
266 already_tried_absolute = 1;
267 goto again;
268 }
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700269done:
270 strbuf_release(&pattern);
271 strbuf_release(&text);
272 return ret;
273}
274
Denton Liu07b2c0e2019-06-05 17:21:59 -0400275static int include_by_branch(const char *cond, size_t cond_len)
276{
277 int flags;
278 int ret;
279 struct strbuf pattern = STRBUF_INIT;
Jeff King22932d92019-08-06 08:27:58 -0400280 const char *refname = !the_repository->gitdir ?
Johannes Schindelin85fe0e82019-07-31 13:06:42 -0700281 NULL : resolve_ref_unsafe("HEAD", 0, NULL, &flags);
Denton Liu07b2c0e2019-06-05 17:21:59 -0400282 const char *shortname;
283
284 if (!refname || !(flags & REF_ISSYMREF) ||
285 !skip_prefix(refname, "refs/heads/", &shortname))
286 return 0;
287
288 strbuf_add(&pattern, cond, cond_len);
289 add_trailing_starstar_for_dir(&pattern);
290 ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME);
291 strbuf_release(&pattern);
292 return ret;
293}
294
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700295static int include_condition_is_true(const struct config_options *opts,
296 const char *cond, size_t cond_len)
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700297{
298
299 if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700300 return include_by_gitdir(opts, cond, cond_len, 0);
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700301 else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700302 return include_by_gitdir(opts, cond, cond_len, 1);
Denton Liu07b2c0e2019-06-05 17:21:59 -0400303 else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
304 return include_by_branch(cond, cond_len);
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700305
306 /* unknown conditionals are always false */
307 return 0;
308}
309
Jeff King9b25a0b2012-02-06 04:54:04 -0500310int git_config_include(const char *var, const char *value, void *data)
311{
312 struct config_include_data *inc = data;
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700313 const char *cond, *key;
Jeff Kingf5914f42020-04-10 15:44:28 -0400314 size_t cond_len;
Jeff King9b25a0b2012-02-06 04:54:04 -0500315 int ret;
316
317 /*
318 * Pass along all values, including "include" directives; this makes it
319 * possible to query information on the includes themselves.
320 */
321 ret = inc->fn(var, value, inc->data);
322 if (ret < 0)
323 return ret;
324
René Scharfe37007c32014-08-30 18:07:05 +0200325 if (!strcmp(var, "include.path"))
Jeff King9b25a0b2012-02-06 04:54:04 -0500326 ret = handle_path_include(value, inc);
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700327
328 if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +0700329 (cond && include_condition_is_true(inc->opts, cond, cond_len)) &&
Nguyễn Thái Ngọc Duy3efd0be2017-03-01 18:26:31 +0700330 !strcmp(key, "path"))
331 ret = handle_path_include(value, inc);
332
Jeff King9b25a0b2012-02-06 04:54:04 -0500333 return ret;
334}
335
Patrick Steinhardt1ff21c02021-01-12 13:27:01 +0100336static void git_config_push_split_parameter(const char *key, const char *value)
Jeff King2b64fc82010-08-23 15:16:00 -0400337{
338 struct strbuf env = STRBUF_INIT;
339 const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
Jeff Kingd1f88492016-03-22 15:50:51 -0400340 if (old && *old) {
Jeff King2b64fc82010-08-23 15:16:00 -0400341 strbuf_addstr(&env, old);
342 strbuf_addch(&env, ' ');
343 }
Patrick Steinhardt1ff21c02021-01-12 13:27:01 +0100344 sq_quote_buf(&env, key);
345 strbuf_addch(&env, '=');
346 if (value)
347 sq_quote_buf(&env, value);
Jeff King2b64fc82010-08-23 15:16:00 -0400348 setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
349 strbuf_release(&env);
350}
351
Patrick Steinhardt1ff21c02021-01-12 13:27:01 +0100352void git_config_push_parameter(const char *text)
353{
354 const char *value;
355
356 /*
357 * When we see:
358 *
359 * section.subsection=with=equals.key=value
360 *
361 * we cannot tell if it means:
362 *
363 * [section "subsection=with=equals"]
364 * key = value
365 *
366 * or:
367 *
368 * [section]
369 * subsection = with=equals.key=value
370 *
371 * We parse left-to-right for the first "=", meaning we'll prefer to
372 * keep the value intact over the subsection. This is historical, but
373 * also sensible since values are more likely to contain odd or
374 * untrusted input than a section name.
375 *
376 * A missing equals is explicitly allowed (as a bool-only entry).
377 */
378 value = strchr(text, '=');
379 if (value) {
380 char *key = xmemdupz(text, value - text);
381 git_config_push_split_parameter(key, value + 1);
382 free(key);
383 } else {
384 git_config_push_split_parameter(text, NULL);
385 }
386}
387
Patrick Steinhardtce81b1d2021-01-12 13:26:45 +0100388void git_config_push_env(const char *spec)
389{
Patrick Steinhardt1ff21c02021-01-12 13:27:01 +0100390 char *key;
Patrick Steinhardtce81b1d2021-01-12 13:26:45 +0100391 const char *env_name;
392 const char *env_value;
393
394 env_name = strrchr(spec, '=');
395 if (!env_name)
396 die(_("invalid config format: %s"), spec);
Patrick Steinhardt1ff21c02021-01-12 13:27:01 +0100397 key = xmemdupz(spec, env_name - spec);
Patrick Steinhardtce81b1d2021-01-12 13:26:45 +0100398 env_name++;
399 if (!*env_name)
400 die(_("missing environment variable name for configuration '%.*s'"),
401 (int)(env_name - spec - 1), spec);
402
403 env_value = getenv(env_name);
404 if (!env_value)
405 die(_("missing environment variable '%s' for configuration '%.*s'"),
406 env_name, (int)(env_name - spec - 1), spec);
407
Patrick Steinhardt1ff21c02021-01-12 13:27:01 +0100408 git_config_push_split_parameter(key, env_value);
409 free(key);
Patrick Steinhardtce81b1d2021-01-12 13:26:45 +0100410}
411
Junio C Hamanoee98df32017-02-23 14:44:07 -0800412static inline int iskeychar(int c)
413{
414 return isalnum(c) || c == '-';
415}
416
417/*
418 * Auxiliary function to sanity-check and split the key into the section
419 * identifier and variable name.
420 *
421 * Returns 0 on success, -1 when there is an invalid character in the key and
422 * -2 if there is no section name in the key.
423 *
424 * store_key - pointer to char* which will hold a copy of the key with
425 * lowercase section and variable name
Jeff Kingf011a962020-04-10 15:46:07 -0400426 * baselen - pointer to size_t which will hold the length of the
Junio C Hamanoee98df32017-02-23 14:44:07 -0800427 * section + subsection part, can be NULL
428 */
Jeff Kingf011a962020-04-10 15:46:07 -0400429static int git_config_parse_key_1(const char *key, char **store_key, size_t *baselen_, int quiet)
Junio C Hamanoee98df32017-02-23 14:44:07 -0800430{
Jeff Kingf011a962020-04-10 15:46:07 -0400431 size_t i, baselen;
432 int dot;
Junio C Hamanoee98df32017-02-23 14:44:07 -0800433 const char *last_dot = strrchr(key, '.');
434
435 /*
436 * Since "key" actually contains the section name and the real
437 * key name separated by a dot, we have to know where the dot is.
438 */
439
440 if (last_dot == NULL || last_dot == key) {
441 if (!quiet)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200442 error(_("key does not contain a section: %s"), key);
Junio C Hamanoee98df32017-02-23 14:44:07 -0800443 return -CONFIG_NO_SECTION_OR_NAME;
444 }
445
446 if (!last_dot[1]) {
447 if (!quiet)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200448 error(_("key does not contain variable name: %s"), key);
Junio C Hamanoee98df32017-02-23 14:44:07 -0800449 return -CONFIG_NO_SECTION_OR_NAME;
450 }
451
452 baselen = last_dot - key;
453 if (baselen_)
454 *baselen_ = baselen;
455
456 /*
457 * Validate the key and while at it, lower case it for matching.
458 */
459 if (store_key)
460 *store_key = xmallocz(strlen(key));
461
462 dot = 0;
463 for (i = 0; key[i]; i++) {
464 unsigned char c = key[i];
465 if (c == '.')
466 dot = 1;
467 /* Leave the extended basename untouched.. */
468 if (!dot || i > baselen) {
469 if (!iskeychar(c) ||
470 (i == baselen + 1 && !isalpha(c))) {
471 if (!quiet)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200472 error(_("invalid key: %s"), key);
Junio C Hamanoee98df32017-02-23 14:44:07 -0800473 goto out_free_ret_1;
474 }
475 c = tolower(c);
476 } else if (c == '\n') {
477 if (!quiet)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200478 error(_("invalid key (newline): %s"), key);
Junio C Hamanoee98df32017-02-23 14:44:07 -0800479 goto out_free_ret_1;
480 }
481 if (store_key)
482 (*store_key)[i] = c;
483 }
484
485 return 0;
486
487out_free_ret_1:
488 if (store_key) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000489 FREE_AND_NULL(*store_key);
Junio C Hamanoee98df32017-02-23 14:44:07 -0800490 }
491 return -CONFIG_INVALID_KEY;
492}
493
Jeff Kingf011a962020-04-10 15:46:07 -0400494int git_config_parse_key(const char *key, char **store_key, size_t *baselen)
Junio C Hamanoee98df32017-02-23 14:44:07 -0800495{
496 return git_config_parse_key_1(key, store_key, baselen, 0);
497}
498
499int git_config_key_is_valid(const char *key)
500{
501 return !git_config_parse_key_1(key, NULL, NULL, 1);
502}
503
Patrick Steinhardtb342ae62021-01-12 13:26:54 +0100504static int config_parse_pair(const char *key, const char *value,
505 config_fn_t fn, void *data)
506{
507 char *canonical_name;
508 int ret;
509
510 if (!strlen(key))
511 return error(_("empty config key"));
512 if (git_config_parse_key(key, &canonical_name, NULL))
513 return -1;
514
515 ret = (fn(canonical_name, value, data) < 0) ? -1 : 0;
516 free(canonical_name);
517 return ret;
518}
519
Jeff King24968442011-06-09 11:56:42 -0400520int git_config_parse_parameter(const char *text,
521 config_fn_t fn, void *data)
Alex Riesen8b1fa772010-03-26 23:53:57 +0100522{
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700523 const char *value;
Alex Riesen572e4f62010-03-26 23:56:01 +0100524 struct strbuf **pair;
Junio C Hamano1274a152017-02-23 15:04:40 -0800525 int ret;
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700526
Jeff Kingf77bcca2011-06-09 11:55:09 -0400527 pair = strbuf_split_str(text, '=', 2);
Jeff Kingc5d63502011-06-09 11:52:43 -0400528 if (!pair[0])
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200529 return error(_("bogus config parameter: %s"), text);
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700530
531 if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
Alex Riesen572e4f62010-03-26 23:56:01 +0100532 strbuf_setlen(pair[0], pair[0]->len - 1);
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700533 value = pair[1] ? pair[1]->buf : "";
534 } else {
535 value = NULL;
536 }
537
Alex Riesen572e4f62010-03-26 23:56:01 +0100538 strbuf_trim(pair[0]);
539 if (!pair[0]->len) {
540 strbuf_list_free(pair);
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +0200541 return error(_("bogus config parameter: %s"), text);
Jeff King06eb7082011-05-24 18:49:55 -0400542 }
Junio C Hamano1274a152017-02-23 15:04:40 -0800543
Patrick Steinhardtb342ae62021-01-12 13:26:54 +0100544 ret = config_parse_pair(pair[0]->buf, value, fn, data);
Alex Riesen572e4f62010-03-26 23:56:01 +0100545 strbuf_list_free(pair);
Junio C Hamano1274a152017-02-23 15:04:40 -0800546 return ret;
Alex Riesen8b1fa772010-03-26 23:53:57 +0100547}
548
Jeff Kingf9dbb642021-01-12 13:27:06 +0100549static int parse_config_env_list(char *env, config_fn_t fn, void *data)
550{
551 char *cur = env;
552 while (cur && *cur) {
553 const char *key = sq_dequote_step(cur, &cur);
554 if (!key)
555 return error(_("bogus format in %s"),
556 CONFIG_DATA_ENVIRONMENT);
557
558 if (!cur || isspace(*cur)) {
559 /* old-style 'key=value' */
560 if (git_config_parse_parameter(key, fn, data) < 0)
561 return -1;
562 }
563 else if (*cur == '=') {
564 /* new-style 'key'='value' */
565 const char *value;
566
567 cur++;
568 if (*cur == '\'') {
569 /* quoted value */
570 value = sq_dequote_step(cur, &cur);
571 if (!value || (cur && !isspace(*cur))) {
572 return error(_("bogus format in %s"),
573 CONFIG_DATA_ENVIRONMENT);
574 }
575 } else if (!*cur || isspace(*cur)) {
576 /* implicit bool: 'key'= */
577 value = NULL;
578 } else {
579 return error(_("bogus format in %s"),
580 CONFIG_DATA_ENVIRONMENT);
581 }
582
583 if (config_parse_pair(key, value, fn, data) < 0)
584 return -1;
585 }
586 else {
587 /* unknown format */
588 return error(_("bogus format in %s"),
589 CONFIG_DATA_ENVIRONMENT);
590 }
591
592 if (cur) {
593 while (isspace(*cur))
594 cur++;
595 }
596 }
597 return 0;
598}
599
Jeff King06eb7082011-05-24 18:49:55 -0400600int git_config_from_parameters(config_fn_t fn, void *data)
601{
Patrick Steinhardtd8d77152021-01-12 13:27:14 +0100602 const char *env;
603 struct strbuf envvar = STRBUF_INIT;
604 struct strvec to_free = STRVEC_INIT;
Jeff Kinga77d6db2016-05-18 18:39:49 -0400605 int ret = 0;
Patrick Steinhardtd8d77152021-01-12 13:27:14 +0100606 char *envw = NULL;
Jeff King32582582016-05-18 18:41:08 -0400607 struct config_source source;
Jeff King2b64fc82010-08-23 15:16:00 -0400608
Jeff King32582582016-05-18 18:41:08 -0400609 memset(&source, 0, sizeof(source));
610 source.prev = cf;
Vasco Almeida1b8132d2016-07-28 13:14:03 +0000611 source.origin_type = CONFIG_ORIGIN_CMDLINE;
Jeff King32582582016-05-18 18:41:08 -0400612 cf = &source;
613
Patrick Steinhardtd8d77152021-01-12 13:27:14 +0100614 env = getenv(CONFIG_COUNT_ENVIRONMENT);
615 if (env) {
616 unsigned long count;
617 char *endp;
618 int i;
Jeff King2b64fc82010-08-23 15:16:00 -0400619
Patrick Steinhardtd8d77152021-01-12 13:27:14 +0100620 count = strtoul(env, &endp, 10);
621 if (*endp) {
622 ret = error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT);
623 goto out;
624 }
625 if (count > INT_MAX) {
626 ret = error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT);
627 goto out;
628 }
629
630 for (i = 0; i < count; i++) {
631 const char *key, *value;
632
633 strbuf_addf(&envvar, "GIT_CONFIG_KEY_%d", i);
634 key = getenv_safe(&to_free, envvar.buf);
635 if (!key) {
636 ret = error(_("missing config key %s"), envvar.buf);
637 goto out;
638 }
639 strbuf_reset(&envvar);
640
641 strbuf_addf(&envvar, "GIT_CONFIG_VALUE_%d", i);
642 value = getenv_safe(&to_free, envvar.buf);
643 if (!value) {
644 ret = error(_("missing config value %s"), envvar.buf);
645 goto out;
646 }
647 strbuf_reset(&envvar);
648
649 if (config_parse_pair(key, value, fn, data) < 0) {
650 ret = -1;
651 goto out;
652 }
653 }
Jeff King2b64fc82010-08-23 15:16:00 -0400654 }
655
Patrick Steinhardtd8d77152021-01-12 13:27:14 +0100656 env = getenv(CONFIG_DATA_ENVIRONMENT);
657 if (env) {
658 /* sq_dequote will write over it */
659 envw = xstrdup(env);
660 if (parse_config_env_list(envw, fn, data) < 0) {
Jeff Kinga77d6db2016-05-18 18:39:49 -0400661 ret = -1;
662 goto out;
Jeff King2b64fc82010-08-23 15:16:00 -0400663 }
664 }
665
Jeff Kinga77d6db2016-05-18 18:39:49 -0400666out:
Patrick Steinhardtd8d77152021-01-12 13:27:14 +0100667 strbuf_release(&envvar);
668 strvec_clear(&to_free);
Jeff King2b64fc82010-08-23 15:16:00 -0400669 free(envw);
Jeff King32582582016-05-18 18:41:08 -0400670 cf = source.prev;
Jeff Kinga77d6db2016-05-18 18:39:49 -0400671 return ret;
Jeff King2b64fc82010-08-23 15:16:00 -0400672}
673
Linus Torvalds17712992005-10-10 16:31:08 -0700674static int get_next_char(void)
675{
Jeff King49d6cfa2013-08-26 17:57:18 -0400676 int c = cf->do_fgetc(cf);
Linus Torvalds17712992005-10-10 16:31:08 -0700677
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200678 if (c == '\r') {
679 /* DOS like systems */
Jeff King49d6cfa2013-08-26 17:57:18 -0400680 c = cf->do_fgetc(cf);
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200681 if (c != '\n') {
Jeff King5e0be132015-02-05 01:53:28 -0500682 if (c != EOF)
683 cf->do_ungetc(c, cf);
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200684 c = '\r';
Junio C Hamanodb2c0752005-11-02 13:02:57 -0800685 }
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200686 }
Jeff King348482d2020-04-10 15:50:07 -0400687
688 if (c != EOF && ++cf->total_len > INT_MAX) {
689 /*
690 * This is an absurdly long config file; refuse to parse
691 * further in order to protect downstream code from integer
692 * overflows. Note that we can't return an error specifically,
693 * but we can mark EOF and put trash in the return value,
694 * which will trigger a parse error.
695 */
696 cf->eof = 1;
697 return 0;
698 }
699
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200700 if (c == '\n')
701 cf->linenr++;
702 if (c == EOF) {
703 cf->eof = 1;
Matthieu Moyb3b3f602014-08-07 04:59:13 -0700704 cf->linenr++;
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200705 c = '\n';
Linus Torvalds17712992005-10-10 16:31:08 -0700706 }
707 return c;
708}
709
710static char *parse_value(void)
711{
Erik Faye-Lunde96c19c2011-04-10 22:54:18 +0200712 int quote = 0, comment = 0, space = 0;
Linus Torvalds17712992005-10-10 16:31:08 -0700713
Ramsay Jones924aaf32011-06-16 21:24:51 +0100714 strbuf_reset(&cf->value);
Linus Torvalds17712992005-10-10 16:31:08 -0700715 for (;;) {
716 int c = get_next_char();
Linus Torvalds17712992005-10-10 16:31:08 -0700717 if (c == '\n') {
Martin Stenberg4b340592012-03-09 22:57:54 +0100718 if (quote) {
719 cf->linenr--;
Linus Torvalds17712992005-10-10 16:31:08 -0700720 return NULL;
Martin Stenberg4b340592012-03-09 22:57:54 +0100721 }
Ramsay Jones924aaf32011-06-16 21:24:51 +0100722 return cf->value.buf;
Linus Torvalds17712992005-10-10 16:31:08 -0700723 }
724 if (comment)
725 continue;
726 if (isspace(c) && !quote) {
Ramsay Jones924aaf32011-06-16 21:24:51 +0100727 if (cf->value.len)
Björn Steinbrinkebdaae32009-07-30 13:41:57 +0200728 space++;
Linus Torvalds17712992005-10-10 16:31:08 -0700729 continue;
730 }
Johannes Schindelin7ebdba62006-05-02 16:58:37 +0200731 if (!quote) {
732 if (c == ';' || c == '#') {
733 comment = 1;
734 continue;
735 }
736 }
Björn Steinbrinkebdaae32009-07-30 13:41:57 +0200737 for (; space; space--)
Ramsay Jones924aaf32011-06-16 21:24:51 +0100738 strbuf_addch(&cf->value, ' ');
Linus Torvalds17712992005-10-10 16:31:08 -0700739 if (c == '\\') {
740 c = get_next_char();
741 switch (c) {
742 case '\n':
743 continue;
744 case 't':
745 c = '\t';
746 break;
747 case 'b':
748 c = '\b';
749 break;
750 case 'n':
751 c = '\n';
752 break;
Linus Torvalds5cbb4012005-10-11 15:24:11 -0700753 /* Some characters escape as themselves */
754 case '\\': case '"':
755 break;
756 /* Reject unknown escape sequences */
757 default:
758 return NULL;
Linus Torvalds17712992005-10-10 16:31:08 -0700759 }
Ramsay Jones924aaf32011-06-16 21:24:51 +0100760 strbuf_addch(&cf->value, c);
Linus Torvalds17712992005-10-10 16:31:08 -0700761 continue;
762 }
763 if (c == '"') {
764 quote = 1-quote;
765 continue;
766 }
Ramsay Jones924aaf32011-06-16 21:24:51 +0100767 strbuf_addch(&cf->value, c);
Linus Torvalds17712992005-10-10 16:31:08 -0700768 }
769}
770
Ben Walton0971e992012-09-30 20:44:36 +0100771static int get_value(config_fn_t fn, void *data, struct strbuf *name)
Linus Torvalds17712992005-10-10 16:31:08 -0700772{
773 int c;
774 char *value;
Matthieu Moyb3b3f602014-08-07 04:59:13 -0700775 int ret;
Linus Torvalds17712992005-10-10 16:31:08 -0700776
777 /* Get the full name */
778 for (;;) {
779 c = get_next_char();
Ramsay Jones924aaf32011-06-16 21:24:51 +0100780 if (cf->eof)
Linus Torvalds17712992005-10-10 16:31:08 -0700781 break;
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800782 if (!iskeychar(c))
Linus Torvalds17712992005-10-10 16:31:08 -0700783 break;
Ben Walton0971e992012-09-30 20:44:36 +0100784 strbuf_addch(name, tolower(c));
Linus Torvalds17712992005-10-10 16:31:08 -0700785 }
Ben Walton0971e992012-09-30 20:44:36 +0100786
Linus Torvalds17712992005-10-10 16:31:08 -0700787 while (c == ' ' || c == '\t')
788 c = get_next_char();
789
790 value = NULL;
791 if (c != '\n') {
792 if (c != '=')
793 return -1;
794 value = parse_value();
795 if (!value)
796 return -1;
797 }
Matthieu Moyb3b3f602014-08-07 04:59:13 -0700798 /*
799 * We already consumed the \n, but we need linenr to point to
800 * the line we just parsed during the call to fn to get
801 * accurate line number in error messages.
802 */
803 cf->linenr--;
804 ret = fn(name->buf, value, data);
Johannes Schindeline2e14252017-06-14 13:35:46 +0200805 if (ret >= 0)
806 cf->linenr++;
Matthieu Moyb3b3f602014-08-07 04:59:13 -0700807 return ret;
Linus Torvalds17712992005-10-10 16:31:08 -0700808}
809
Ben Walton0971e992012-09-30 20:44:36 +0100810static int get_extended_base_var(struct strbuf *name, int c)
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700811{
Stefan Beller2d84f132018-08-08 12:50:19 -0700812 cf->subsection_case_sensitive = 0;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700813 do {
814 if (c == '\n')
Martin Stenberg4b340592012-03-09 22:57:54 +0100815 goto error_incomplete_line;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700816 c = get_next_char();
817 } while (isspace(c));
818
819 /* We require the format to be '[base "extension"]' */
820 if (c != '"')
821 return -1;
Ben Walton0971e992012-09-30 20:44:36 +0100822 strbuf_addch(name, '.');
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700823
824 for (;;) {
825 int c = get_next_char();
826 if (c == '\n')
Martin Stenberg4b340592012-03-09 22:57:54 +0100827 goto error_incomplete_line;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700828 if (c == '"')
829 break;
830 if (c == '\\') {
831 c = get_next_char();
832 if (c == '\n')
Martin Stenberg4b340592012-03-09 22:57:54 +0100833 goto error_incomplete_line;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700834 }
Ben Walton0971e992012-09-30 20:44:36 +0100835 strbuf_addch(name, c);
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700836 }
837
838 /* Final ']' */
839 if (get_next_char() != ']')
840 return -1;
Ben Walton0971e992012-09-30 20:44:36 +0100841 return 0;
Martin Stenberg4b340592012-03-09 22:57:54 +0100842error_incomplete_line:
843 cf->linenr--;
844 return -1;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700845}
846
Ben Walton0971e992012-09-30 20:44:36 +0100847static int get_base_var(struct strbuf *name)
Linus Torvalds17712992005-10-10 16:31:08 -0700848{
Stefan Beller2d84f132018-08-08 12:50:19 -0700849 cf->subsection_case_sensitive = 1;
Linus Torvalds17712992005-10-10 16:31:08 -0700850 for (;;) {
851 int c = get_next_char();
Ramsay Jones924aaf32011-06-16 21:24:51 +0100852 if (cf->eof)
Linus Torvalds17712992005-10-10 16:31:08 -0700853 return -1;
854 if (c == ']')
Ben Walton0971e992012-09-30 20:44:36 +0100855 return 0;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700856 if (isspace(c))
Ben Walton0971e992012-09-30 20:44:36 +0100857 return get_extended_base_var(name, c);
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800858 if (!iskeychar(c) && c != '.')
Linus Torvalds17712992005-10-10 16:31:08 -0700859 return -1;
Ben Walton0971e992012-09-30 20:44:36 +0100860 strbuf_addch(name, tolower(c));
Linus Torvalds17712992005-10-10 16:31:08 -0700861 }
862}
863
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200864struct parse_event_data {
865 enum config_event_t previous_type;
866 size_t previous_offset;
867 const struct config_options *opts;
868};
869
870static int do_event(enum config_event_t type, struct parse_event_data *data)
871{
872 size_t offset;
873
874 if (!data->opts || !data->opts->event_fn)
875 return 0;
876
877 if (type == CONFIG_EVENT_WHITESPACE &&
878 data->previous_type == type)
879 return 0;
880
881 offset = cf->do_ftell(cf);
882 /*
883 * At EOF, the parser always "inserts" an extra '\n', therefore
884 * the end offset of the event is the current file position, otherwise
885 * we will already have advanced to the next event.
886 */
887 if (type != CONFIG_EVENT_EOF)
888 offset--;
889
890 if (data->previous_type != CONFIG_EVENT_EOF &&
891 data->opts->event_fn(data->previous_type, data->previous_offset,
892 offset, data->opts->event_fn_data) < 0)
893 return -1;
894
895 data->previous_type = type;
896 data->previous_offset = offset;
897
898 return 0;
899}
900
901static int git_parse_source(config_fn_t fn, void *data,
902 const struct config_options *opts)
Linus Torvalds17712992005-10-10 16:31:08 -0700903{
904 int comment = 0;
Jeff King6a9c2352020-04-10 15:47:51 -0400905 size_t baselen = 0;
Ben Walton0971e992012-09-30 20:44:36 +0100906 struct strbuf *var = &cf->var;
Vasco Almeida1b8132d2016-07-28 13:14:03 +0000907 int error_return = 0;
908 char *error_msg = NULL;
Linus Torvalds17712992005-10-10 16:31:08 -0700909
Petr Baudisde056402008-10-01 22:13:02 +0200910 /* U+FEFF Byte Order Mark in UTF8 */
Junio C Hamano599446d2015-04-16 10:47:45 -0700911 const char *bomptr = utf8_bom;
Petr Baudisde056402008-10-01 22:13:02 +0200912
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200913 /* For the parser event callback */
914 struct parse_event_data event_data = {
915 CONFIG_EVENT_EOF, 0, opts
916 };
917
Linus Torvalds17712992005-10-10 16:31:08 -0700918 for (;;) {
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200919 int c;
920
921 c = get_next_char();
Petr Baudisde056402008-10-01 22:13:02 +0200922 if (bomptr && *bomptr) {
923 /* We are at the file beginning; skip UTF8-encoded BOM
924 * if present. Sane editors won't put this in on their
925 * own, but e.g. Windows Notepad will do it happily. */
Junio C Hamano599446d2015-04-16 10:47:45 -0700926 if (c == (*bomptr & 0377)) {
Petr Baudisde056402008-10-01 22:13:02 +0200927 bomptr++;
928 continue;
929 } else {
930 /* Do not tolerate partial BOM. */
931 if (bomptr != utf8_bom)
932 break;
933 /* No BOM at file beginning. Cool. */
934 bomptr = NULL;
935 }
936 }
Linus Torvalds17712992005-10-10 16:31:08 -0700937 if (c == '\n') {
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200938 if (cf->eof) {
939 if (do_event(CONFIG_EVENT_EOF, &event_data) < 0)
940 return -1;
Linus Torvalds17712992005-10-10 16:31:08 -0700941 return 0;
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200942 }
943 if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0)
944 return -1;
Linus Torvalds17712992005-10-10 16:31:08 -0700945 comment = 0;
946 continue;
947 }
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200948 if (comment)
Linus Torvalds17712992005-10-10 16:31:08 -0700949 continue;
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200950 if (isspace(c)) {
951 if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0)
952 return -1;
953 continue;
954 }
Linus Torvalds17712992005-10-10 16:31:08 -0700955 if (c == '#' || c == ';') {
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200956 if (do_event(CONFIG_EVENT_COMMENT, &event_data) < 0)
957 return -1;
Linus Torvalds17712992005-10-10 16:31:08 -0700958 comment = 1;
959 continue;
960 }
961 if (c == '[') {
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200962 if (do_event(CONFIG_EVENT_SECTION, &event_data) < 0)
963 return -1;
964
Ben Walton0971e992012-09-30 20:44:36 +0100965 /* Reset prior to determining a new stem */
966 strbuf_reset(var);
967 if (get_base_var(var) < 0 || var->len < 1)
Linus Torvalds17712992005-10-10 16:31:08 -0700968 break;
Ben Walton0971e992012-09-30 20:44:36 +0100969 strbuf_addch(var, '.');
970 baselen = var->len;
Linus Torvalds17712992005-10-10 16:31:08 -0700971 continue;
972 }
973 if (!isalpha(c))
974 break;
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200975
976 if (do_event(CONFIG_EVENT_ENTRY, &event_data) < 0)
977 return -1;
978
Ben Walton0971e992012-09-30 20:44:36 +0100979 /*
980 * Truncate the var name back to the section header
981 * stem prior to grabbing the suffix part of the name
982 * and the value.
983 */
984 strbuf_setlen(var, baselen);
985 strbuf_addch(var, tolower(c));
986 if (get_value(fn, data, var) < 0)
Linus Torvalds17712992005-10-10 16:31:08 -0700987 break;
988 }
Vasco Almeida1b8132d2016-07-28 13:14:03 +0000989
Johannes Schindelin8032cc42018-04-09 10:32:05 +0200990 if (do_event(CONFIG_EVENT_ERROR, &event_data) < 0)
991 return -1;
992
Vasco Almeida1b8132d2016-07-28 13:14:03 +0000993 switch (cf->origin_type) {
994 case CONFIG_ORIGIN_BLOB:
995 error_msg = xstrfmt(_("bad config line %d in blob %s"),
996 cf->linenr, cf->name);
997 break;
998 case CONFIG_ORIGIN_FILE:
999 error_msg = xstrfmt(_("bad config line %d in file %s"),
1000 cf->linenr, cf->name);
1001 break;
1002 case CONFIG_ORIGIN_STDIN:
1003 error_msg = xstrfmt(_("bad config line %d in standard input"),
1004 cf->linenr);
1005 break;
1006 case CONFIG_ORIGIN_SUBMODULE_BLOB:
1007 error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"),
1008 cf->linenr, cf->name);
1009 break;
1010 case CONFIG_ORIGIN_CMDLINE:
1011 error_msg = xstrfmt(_("bad config line %d in command line %s"),
1012 cf->linenr, cf->name);
1013 break;
1014 default:
1015 error_msg = xstrfmt(_("bad config line %d in %s"),
1016 cf->linenr, cf->name);
1017 }
1018
Jeff King66f97222018-06-28 18:05:00 -04001019 switch (opts && opts->error_action ?
1020 opts->error_action :
1021 cf->default_error_action) {
1022 case CONFIG_ERROR_DIE:
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001023 die("%s", error_msg);
Jeff King66f97222018-06-28 18:05:00 -04001024 break;
1025 case CONFIG_ERROR_ERROR:
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001026 error_return = error("%s", error_msg);
Jeff King66f97222018-06-28 18:05:00 -04001027 break;
Jeff King63583202018-06-28 18:05:09 -04001028 case CONFIG_ERROR_SILENT:
1029 error_return = -1;
1030 break;
Jeff King66f97222018-06-28 18:05:00 -04001031 case CONFIG_ERROR_UNSET:
1032 BUG("config error action unset");
1033 }
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001034
1035 free(error_msg);
1036 return error_return;
Linus Torvalds17712992005-10-10 16:31:08 -07001037}
1038
René Scharfe39c575c2019-06-22 12:03:40 +02001039static uintmax_t get_unit_factor(const char *end)
Brian Downing0b87b6e2007-07-12 08:32:26 -05001040{
1041 if (!*end)
1042 return 1;
René Scharfe39c575c2019-06-22 12:03:40 +02001043 else if (!strcasecmp(end, "k"))
1044 return 1024;
1045 else if (!strcasecmp(end, "m"))
1046 return 1024 * 1024;
1047 else if (!strcasecmp(end, "g"))
1048 return 1024 * 1024 * 1024;
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -05001049 return 0;
Brian Downing0b87b6e2007-07-12 08:32:26 -05001050}
1051
Jeff King71927772013-09-08 04:29:27 -04001052static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
Linus Torvalds17712992005-10-10 16:31:08 -07001053{
1054 if (value && *value) {
1055 char *end;
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001056 intmax_t val;
1057 uintmax_t uval;
René Scharfe664178e2019-06-22 12:03:36 +02001058 uintmax_t factor;
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001059
1060 errno = 0;
1061 val = strtoimax(value, &end, 0);
1062 if (errno == ERANGE)
1063 return 0;
René Scharfe39c575c2019-06-22 12:03:40 +02001064 factor = get_unit_factor(end);
1065 if (!factor) {
Jeff King33fdd772013-09-08 04:36:42 -04001066 errno = EINVAL;
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -05001067 return 0;
Jeff King33fdd772013-09-08 04:36:42 -04001068 }
Johannes Schindelin9dae4fe2019-06-13 04:49:47 -07001069 uval = val < 0 ? -val : val;
René Scharfe3fb72c72019-06-22 12:03:30 +02001070 if (unsigned_mult_overflows(factor, uval) ||
1071 factor * uval > max) {
Jeff King33fdd772013-09-08 04:36:42 -04001072 errno = ERANGE;
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001073 return 0;
Jeff King33fdd772013-09-08 04:36:42 -04001074 }
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001075 val *= factor;
1076 *ret = val;
Brian Downing0b87b6e2007-07-12 08:32:26 -05001077 return 1;
Linus Torvalds17712992005-10-10 16:31:08 -07001078 }
Jeff King33fdd772013-09-08 04:36:42 -04001079 errno = EINVAL;
Brian Downing0b87b6e2007-07-12 08:32:26 -05001080 return 0;
1081}
1082
Ramsay Jones0b4dc662013-10-06 21:48:29 +01001083static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
Brian Downing0b87b6e2007-07-12 08:32:26 -05001084{
1085 if (value && *value) {
1086 char *end;
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001087 uintmax_t val;
René Scharfe664178e2019-06-22 12:03:36 +02001088 uintmax_t factor;
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001089
1090 errno = 0;
1091 val = strtoumax(value, &end, 0);
1092 if (errno == ERANGE)
1093 return 0;
René Scharfe39c575c2019-06-22 12:03:40 +02001094 factor = get_unit_factor(end);
1095 if (!factor) {
Jeff King33fdd772013-09-08 04:36:42 -04001096 errno = EINVAL;
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -05001097 return 0;
Jeff King33fdd772013-09-08 04:36:42 -04001098 }
René Scharfe3fb72c72019-06-22 12:03:30 +02001099 if (unsigned_mult_overflows(factor, val) ||
1100 factor * val > max) {
Jeff King33fdd772013-09-08 04:36:42 -04001101 errno = ERANGE;
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001102 return 0;
Jeff King33fdd772013-09-08 04:36:42 -04001103 }
René Scharfe3fb72c72019-06-22 12:03:30 +02001104 val *= factor;
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -05001105 *ret = val;
Brian Downing0b87b6e2007-07-12 08:32:26 -05001106 return 1;
1107 }
Jeff King33fdd772013-09-08 04:36:42 -04001108 errno = EINVAL;
Brian Downing0b87b6e2007-07-12 08:32:26 -05001109 return 0;
1110}
1111
Jeff King42d194e2013-09-08 04:33:08 -04001112static int git_parse_int(const char *value, int *ret)
Jeff Kingc1867ce2008-02-20 19:00:32 -05001113{
Jeff King71927772013-09-08 04:29:27 -04001114 intmax_t tmp;
Jeff King42d194e2013-09-08 04:33:08 -04001115 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
Jeff King71927772013-09-08 04:29:27 -04001116 return 0;
1117 *ret = tmp;
1118 return 1;
1119}
1120
Jeff King00160242013-09-08 04:40:02 -04001121static int git_parse_int64(const char *value, int64_t *ret)
1122{
1123 intmax_t tmp;
1124 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
1125 return 0;
1126 *ret = tmp;
1127 return 1;
1128}
1129
Jeff King71927772013-09-08 04:29:27 -04001130int git_parse_ulong(const char *value, unsigned long *ret)
1131{
1132 uintmax_t tmp;
1133 if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
1134 return 0;
1135 *ret = tmp;
1136 return 1;
1137}
1138
Max Kirillovc79edf72018-06-10 18:05:20 +03001139int git_parse_ssize_t(const char *value, ssize_t *ret)
David Turner37ee6802017-04-11 14:13:57 -04001140{
1141 intmax_t tmp;
1142 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t)))
1143 return 0;
1144 *ret = tmp;
1145 return 1;
1146}
1147
Jeff King06bdc232014-04-16 12:51:47 -04001148NORETURN
Jeff King2f666582013-09-08 04:38:22 -04001149static void die_bad_number(const char *name, const char *value)
Jeff Kingc1867ce2008-02-20 19:00:32 -05001150{
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001151 const char *error_type = (errno == ERANGE) ?
1152 N_("out of range") : N_("invalid unit");
1153 const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s");
Jean-Noel Avila078fe302016-08-21 16:50:39 +02001154
Jeff King2f666582013-09-08 04:38:22 -04001155 if (!value)
1156 value = "";
1157
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001158 if (!(cf && cf->name))
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001159 die(_(bad_numeric), value, name, _(error_type));
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001160
1161 switch (cf->origin_type) {
1162 case CONFIG_ORIGIN_BLOB:
Jean-Noel Avila078fe302016-08-21 16:50:39 +02001163 die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001164 value, name, cf->name, _(error_type));
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001165 case CONFIG_ORIGIN_FILE:
Jean-Noel Avila078fe302016-08-21 16:50:39 +02001166 die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001167 value, name, cf->name, _(error_type));
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001168 case CONFIG_ORIGIN_STDIN:
Jean-Noel Avila078fe302016-08-21 16:50:39 +02001169 die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001170 value, name, _(error_type));
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001171 case CONFIG_ORIGIN_SUBMODULE_BLOB:
Jean-Noel Avila078fe302016-08-21 16:50:39 +02001172 die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001173 value, name, cf->name, _(error_type));
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001174 case CONFIG_ORIGIN_CMDLINE:
Jean-Noel Avila078fe302016-08-21 16:50:39 +02001175 die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001176 value, name, cf->name, _(error_type));
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001177 default:
Jean-Noel Avila078fe302016-08-21 16:50:39 +02001178 die(_("bad numeric config value '%s' for '%s' in %s: %s"),
Ævar Arnfjörð Bjarmason2e43cd42019-06-21 12:18:07 +02001179 value, name, cf->name, _(error_type));
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001180 }
Jeff Kingc1867ce2008-02-20 19:00:32 -05001181}
1182
Brian Downing0b87b6e2007-07-12 08:32:26 -05001183int git_config_int(const char *name, const char *value)
1184{
Jeff King42d194e2013-09-08 04:33:08 -04001185 int ret;
1186 if (!git_parse_int(value, &ret))
Jeff King2f666582013-09-08 04:38:22 -04001187 die_bad_number(name, value);
Brian Downing0b87b6e2007-07-12 08:32:26 -05001188 return ret;
1189}
1190
Jeff King00160242013-09-08 04:40:02 -04001191int64_t git_config_int64(const char *name, const char *value)
1192{
1193 int64_t ret;
1194 if (!git_parse_int64(value, &ret))
1195 die_bad_number(name, value);
Brian Downing0b87b6e2007-07-12 08:32:26 -05001196 return ret;
1197}
1198
1199unsigned long git_config_ulong(const char *name, const char *value)
1200{
1201 unsigned long ret;
1202 if (!git_parse_ulong(value, &ret))
Jeff King2f666582013-09-08 04:38:22 -04001203 die_bad_number(name, value);
Brian Downing0b87b6e2007-07-12 08:32:26 -05001204 return ret;
Linus Torvalds17712992005-10-10 16:31:08 -07001205}
1206
David Turner37ee6802017-04-11 14:13:57 -04001207ssize_t git_config_ssize_t(const char *name, const char *value)
1208{
1209 ssize_t ret;
1210 if (!git_parse_ssize_t(value, &ret))
1211 die_bad_number(name, value);
1212 return ret;
1213}
1214
Martin Ågren9be04d62017-08-07 20:20:47 +02001215static int git_parse_maybe_bool_text(const char *value)
Linus Torvalds17712992005-10-10 16:31:08 -07001216{
1217 if (!value)
1218 return 1;
1219 if (!*value)
1220 return 0;
Junio C Hamano8420ccd2010-02-16 23:59:46 -08001221 if (!strcasecmp(value, "true")
1222 || !strcasecmp(value, "yes")
1223 || !strcasecmp(value, "on"))
Linus Torvalds17712992005-10-10 16:31:08 -07001224 return 1;
Junio C Hamano8420ccd2010-02-16 23:59:46 -08001225 if (!strcasecmp(value, "false")
1226 || !strcasecmp(value, "no")
1227 || !strcasecmp(value, "off"))
Linus Torvalds17712992005-10-10 16:31:08 -07001228 return 0;
Junio C Hamano8420ccd2010-02-16 23:59:46 -08001229 return -1;
1230}
1231
Martin Ågren9be04d62017-08-07 20:20:47 +02001232int git_parse_maybe_bool(const char *value)
Jeff Kingb2be2f62010-11-17 12:00:45 -05001233{
Martin Ågren9be04d62017-08-07 20:20:47 +02001234 int v = git_parse_maybe_bool_text(value);
Jeff Kingb2be2f62010-11-17 12:00:45 -05001235 if (0 <= v)
1236 return v;
Jeff King42d194e2013-09-08 04:33:08 -04001237 if (git_parse_int(value, &v))
Jeff Kingdb6195e2010-12-18 22:36:41 -05001238 return !!v;
Jeff Kingb2be2f62010-11-17 12:00:45 -05001239 return -1;
1240}
1241
Junio C Hamano8420ccd2010-02-16 23:59:46 -08001242int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
1243{
Martin Ågren9be04d62017-08-07 20:20:47 +02001244 int v = git_parse_maybe_bool_text(value);
Junio C Hamano8420ccd2010-02-16 23:59:46 -08001245 if (0 <= v) {
1246 *is_bool = 1;
1247 return v;
1248 }
Junio C Hamanoa53f2ec2008-04-12 18:33:31 -07001249 *is_bool = 0;
Junio C Hamanoc35b0b52008-04-13 12:11:11 -07001250 return git_config_int(name, value);
Linus Torvalds17712992005-10-10 16:31:08 -07001251}
1252
Junio C Hamanoa53f2ec2008-04-12 18:33:31 -07001253int git_config_bool(const char *name, const char *value)
1254{
Andrew Klotzf276e2a2021-02-11 20:30:53 +00001255 int v = git_parse_maybe_bool(value);
1256 if (v < 0)
Ævar Arnfjörð Bjarmason39e12652021-04-08 15:25:55 +02001257 die(_("bad boolean config value '%s' for '%s'"), value, name);
Andrew Klotzf276e2a2021-02-11 20:30:53 +00001258 return v;
Junio C Hamanoa53f2ec2008-04-12 18:33:31 -07001259}
1260
Christian Couderea5105a2008-02-16 06:00:24 +01001261int git_config_string(const char **dest, const char *var, const char *value)
1262{
1263 if (!value)
1264 return config_error_nonbool(var);
1265 *dest = xstrdup(value);
1266 return 0;
1267}
1268
Matthieu Moy395de252009-11-17 18:24:25 +01001269int git_config_pathname(const char **dest, const char *var, const char *value)
1270{
1271 if (!value)
1272 return config_error_nonbool(var);
Nguyễn Thái Ngọc Duy4aad2f12017-04-05 17:24:38 +07001273 *dest = expand_user_path(value, 0);
Matthieu Moy395de252009-11-17 18:24:25 +01001274 if (!*dest)
Matthieu Moy8262aaa2014-08-07 04:59:12 -07001275 die(_("failed to expand user dir in: '%s'"), value);
Matthieu Moy395de252009-11-17 18:24:25 +01001276 return 0;
1277}
1278
Haaris Mehmood5f967422017-11-18 02:27:27 +00001279int git_config_expiry_date(timestamp_t *timestamp, const char *var, const char *value)
1280{
1281 if (!value)
1282 return config_error_nonbool(var);
1283 if (parse_expiry_date(value, timestamp))
1284 return error(_("'%s' for '%s' is not a valid timestamp"),
1285 value, var);
1286 return 0;
1287}
1288
Taylor Blau6d2f9ac2018-04-09 17:18:28 -07001289int git_config_color(char *dest, const char *var, const char *value)
1290{
1291 if (!value)
1292 return config_error_nonbool(var);
1293 if (color_parse(value, dest) < 0)
1294 return -1;
1295 return 0;
1296}
1297
Johannes Schindelin70fc5792018-10-30 11:40:04 -07001298static int git_default_core_config(const char *var, const char *value, void *cb)
Linus Torvalds17712992005-10-10 16:31:08 -07001299{
1300 /* This needs a better name */
1301 if (!strcmp(var, "core.filemode")) {
1302 trust_executable_bit = git_config_bool(var, value);
1303 return 0;
1304 }
Alex Riesen1ce47902008-07-28 08:31:28 +02001305 if (!strcmp(var, "core.trustctime")) {
1306 trust_ctime = git_config_bool(var, value);
1307 return 0;
1308 }
Junio C Hamanoc1b5d732013-05-06 22:32:58 -07001309 if (!strcmp(var, "core.checkstat")) {
Robin Rosenbergc08e4d52013-01-22 08:49:22 +01001310 if (!strcasecmp(value, "default"))
1311 check_stat = 1;
1312 else if (!strcasecmp(value, "minimal"))
1313 check_stat = 0;
1314 }
Linus Torvalds17712992005-10-10 16:31:08 -07001315
Junio C Hamano9378c162007-06-24 15:11:24 -07001316 if (!strcmp(var, "core.quotepath")) {
1317 quote_path_fully = git_config_bool(var, value);
1318 return 0;
1319 }
1320
Johannes Sixt78a8d642007-03-02 22:11:30 +01001321 if (!strcmp(var, "core.symlinks")) {
1322 has_symlinks = git_config_bool(var, value);
1323 return 0;
1324 }
1325
Linus Torvalds0a9b88b2008-03-21 16:52:46 -07001326 if (!strcmp(var, "core.ignorecase")) {
1327 ignore_case = git_config_bool(var, value);
1328 return 0;
1329 }
1330
Junio C Hamano64589a02011-10-06 13:22:24 -05001331 if (!strcmp(var, "core.attributesfile"))
1332 return git_config_pathname(&git_attributes_file, var, value);
1333
Ævar Arnfjörð Bjarmason867ad082016-05-04 22:58:12 +00001334 if (!strcmp(var, "core.hookspath"))
1335 return git_config_pathname(&git_hooks_path, var, value);
1336
Junio C Hamano7d1864c2007-01-07 02:00:28 -08001337 if (!strcmp(var, "core.bare")) {
1338 is_bare_repository_cfg = git_config_bool(var, value);
1339 return 0;
1340 }
1341
Junio C Hamano5f730762006-02-08 21:15:24 -08001342 if (!strcmp(var, "core.ignorestat")) {
1343 assume_unchanged = git_config_bool(var, value);
1344 return 0;
1345 }
1346
Junio C Hamanoe388c732006-05-02 00:40:24 -07001347 if (!strcmp(var, "core.prefersymlinkrefs")) {
1348 prefer_symlink_refs = git_config_bool(var, value);
Johannes Schindelinf8348be2005-11-15 19:24:19 +01001349 return 0;
1350 }
1351
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001352 if (!strcmp(var, "core.logallrefupdates")) {
Cornelius Weig341fb282017-01-27 11:09:47 +01001353 if (value && !strcasecmp(value, "always"))
1354 log_all_ref_updates = LOG_REFS_ALWAYS;
1355 else if (git_config_bool(var, value))
1356 log_all_ref_updates = LOG_REFS_NORMAL;
1357 else
1358 log_all_ref_updates = LOG_REFS_NONE;
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001359 return 0;
1360 }
1361
Junio C Hamano2f8acdb2006-03-20 18:45:47 -08001362 if (!strcmp(var, "core.warnambiguousrefs")) {
1363 warn_ambiguous_refs = git_config_bool(var, value);
1364 return 0;
1365 }
1366
Junio C Hamanoa71f09f2011-03-20 22:26:24 -07001367 if (!strcmp(var, "core.abbrev")) {
Junio C Hamano48d50142016-11-01 18:34:07 -07001368 if (!value)
1369 return config_error_nonbool(var);
1370 if (!strcasecmp(value, "auto"))
1371 default_abbrev = -1;
Eric Wonga9ecaa02020-09-01 07:43:55 +00001372 else if (!git_parse_maybe_bool_text(value))
1373 default_abbrev = the_hash_algo->hexsz;
Junio C Hamano48d50142016-11-01 18:34:07 -07001374 else {
1375 int abbrev = git_config_int(var, value);
brian m. carlsonfe9fec42019-08-18 20:04:13 +00001376 if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001377 return error(_("abbrev length out of range: %d"), abbrev);
Junio C Hamano48d50142016-11-01 18:34:07 -07001378 default_abbrev = abbrev;
1379 }
Linus Torvaldsdce96482010-10-28 11:28:04 -07001380 return 0;
1381 }
1382
Jeff King5b33cb12016-09-27 08:38:01 -04001383 if (!strcmp(var, "core.disambiguate"))
1384 return set_disambiguate_hint_config(var, value);
1385
Dana How960ccca2007-05-09 13:56:50 -07001386 if (!strcmp(var, "core.loosecompression")) {
Joachim B Haga12f6c302006-07-03 22:11:47 +02001387 int level = git_config_int(var, value);
1388 if (level == -1)
1389 level = Z_DEFAULT_COMPRESSION;
1390 else if (level < 0 || level > Z_BEST_COMPRESSION)
Matthieu Moy8262aaa2014-08-07 04:59:12 -07001391 die(_("bad zlib compression level %d"), level);
Joachim B Haga12f6c302006-07-03 22:11:47 +02001392 zlib_compression_level = level;
Dana How960ccca2007-05-09 13:56:50 -07001393 zlib_compression_seen = 1;
1394 return 0;
1395 }
1396
1397 if (!strcmp(var, "core.compression")) {
1398 int level = git_config_int(var, value);
1399 if (level == -1)
1400 level = Z_DEFAULT_COMPRESSION;
1401 else if (level < 0 || level > Z_BEST_COMPRESSION)
Matthieu Moy8262aaa2014-08-07 04:59:12 -07001402 die(_("bad zlib compression level %d"), level);
Dana How960ccca2007-05-09 13:56:50 -07001403 core_compression_level = level;
1404 core_compression_seen = 1;
1405 if (!zlib_compression_seen)
1406 zlib_compression_level = level;
Junio C Hamano8de7eeb2016-11-15 17:42:40 -08001407 if (!pack_compression_seen)
1408 pack_compression_level = level;
Joachim B Haga12f6c302006-07-03 22:11:47 +02001409 return 0;
1410 }
1411
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001412 if (!strcmp(var, "core.packedgitwindowsize")) {
Junio C Hamano5faaf242007-02-14 13:20:41 -08001413 int pgsz_x2 = getpagesize() * 2;
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001414 packed_git_window_size = git_config_ulong(var, value);
Junio C Hamano5faaf242007-02-14 13:20:41 -08001415
1416 /* This value must be multiple of (pagesize * 2) */
1417 packed_git_window_size /= pgsz_x2;
1418 if (packed_git_window_size < 1)
1419 packed_git_window_size = 1;
1420 packed_git_window_size *= pgsz_x2;
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -05001421 return 0;
1422 }
1423
Junio C Hamano15366282011-04-05 10:44:11 -07001424 if (!strcmp(var, "core.bigfilethreshold")) {
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001425 big_file_threshold = git_config_ulong(var, value);
Junio C Hamano15366282011-04-05 10:44:11 -07001426 return 0;
1427 }
1428
Shawn O. Pearce77ccc5b2006-12-23 02:33:35 -05001429 if (!strcmp(var, "core.packedgitlimit")) {
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001430 packed_git_limit = git_config_ulong(var, value);
Shawn O. Pearce77ccc5b2006-12-23 02:33:35 -05001431 return 0;
1432 }
1433
Shawn O. Pearce18bdec12007-03-19 01:14:37 -04001434 if (!strcmp(var, "core.deltabasecachelimit")) {
Nick Alcockebaa1bd2011-11-02 15:46:23 +00001435 delta_base_cache_limit = git_config_ulong(var, value);
Shawn O. Pearce18bdec12007-03-19 01:14:37 -04001436 return 0;
1437 }
1438
Linus Torvalds6c510be2007-02-13 11:07:23 -08001439 if (!strcmp(var, "core.autocrlf")) {
Linus Torvaldsd7f46332007-02-13 18:16:12 -08001440 if (value && !strcasecmp(value, "input")) {
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +02001441 auto_crlf = AUTO_CRLF_INPUT;
Linus Torvaldsd7f46332007-02-13 18:16:12 -08001442 return 0;
1443 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08001444 auto_crlf = git_config_bool(var, value);
1445 return 0;
1446 }
1447
Steffen Prohaska21e5ad52008-02-06 12:25:58 +01001448 if (!strcmp(var, "core.safecrlf")) {
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001449 int eol_rndtrp_die;
Steffen Prohaska21e5ad52008-02-06 12:25:58 +01001450 if (value && !strcasecmp(value, "warn")) {
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001451 global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
Steffen Prohaska21e5ad52008-02-06 12:25:58 +01001452 return 0;
1453 }
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001454 eol_rndtrp_die = git_config_bool(var, value);
1455 global_conv_flags_eol = eol_rndtrp_die ?
Anthony Sottile6cb09122018-06-04 13:17:42 -07001456 CONV_EOL_RNDTRP_DIE : 0;
Steffen Prohaska21e5ad52008-02-06 12:25:58 +01001457 return 0;
1458 }
1459
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +02001460 if (!strcmp(var, "core.eol")) {
1461 if (value && !strcasecmp(value, "lf"))
Junio C Hamanoec70f522011-05-09 12:52:12 -07001462 core_eol = EOL_LF;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +02001463 else if (value && !strcasecmp(value, "crlf"))
Junio C Hamanoec70f522011-05-09 12:52:12 -07001464 core_eol = EOL_CRLF;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +02001465 else if (value && !strcasecmp(value, "native"))
Junio C Hamanoec70f522011-05-09 12:52:12 -07001466 core_eol = EOL_NATIVE;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +02001467 else
Junio C Hamanoec70f522011-05-09 12:52:12 -07001468 core_eol = EOL_UNSET;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +02001469 return 0;
1470 }
1471
Lars Schneidere92d6222018-04-15 20:16:10 +02001472 if (!strcmp(var, "core.checkroundtripencoding")) {
1473 check_roundtrip_encoding = xstrdup(value);
1474 return 0;
1475 }
1476
Johannes Schindelina97a7462009-10-09 12:21:57 +02001477 if (!strcmp(var, "core.notesref")) {
1478 notes_ref_name = xstrdup(value);
1479 return 0;
1480 }
1481
Linus Torvalds806e2ad2008-06-18 14:37:18 -07001482 if (!strcmp(var, "core.editor"))
1483 return git_config_string(&editor_program, var, value);
1484
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001485 if (!strcmp(var, "core.commentchar")) {
Jeff King649409b2014-07-24 00:42:39 -04001486 if (!value)
1487 return config_error_nonbool(var);
Junio C Hamanoad524f82014-07-28 11:30:41 -07001488 else if (!strcasecmp(value, "auto"))
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +07001489 auto_comment_line_char = 1;
Junio C Hamanoad524f82014-07-28 11:30:41 -07001490 else if (value[0] && !value[1]) {
Jeff King649409b2014-07-24 00:42:39 -04001491 comment_line_char = value[0];
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +07001492 auto_comment_line_char = 0;
Nguyễn Thái Ngọc Duy50b54fd2014-05-17 08:52:22 +07001493 } else
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001494 return error(_("core.commentChar should only be one character"));
Nguyễn Thái Ngọc Duy50b54fd2014-05-17 08:52:22 +07001495 return 0;
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001496 }
1497
Anselm Kruisd3e7da82010-08-30 15:38:38 +02001498 if (!strcmp(var, "core.askpass"))
1499 return git_config_string(&askpass_program, var, value);
1500
Linus Torvalds806e2ad2008-06-18 14:37:18 -07001501 if (!strcmp(var, "core.excludesfile"))
Matthieu Moy395de252009-11-17 18:24:25 +01001502 return git_config_pathname(&excludes_file, var, value);
Linus Torvalds806e2ad2008-06-18 14:37:18 -07001503
1504 if (!strcmp(var, "core.whitespace")) {
1505 if (!value)
1506 return config_error_nonbool(var);
1507 whitespace_rule_cfg = parse_whitespace_rule(value);
1508 return 0;
1509 }
1510
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -07001511 if (!strcmp(var, "core.fsyncobjectfiles")) {
1512 fsync_object_files = git_config_bool(var, value);
1513 return 0;
1514 }
1515
Linus Torvalds671c9b72008-11-13 16:36:30 -08001516 if (!strcmp(var, "core.preloadindex")) {
1517 core_preload_index = git_config_bool(var, value);
1518 return 0;
1519 }
1520
Johannes Schindelin348df162009-04-28 00:32:25 +02001521 if (!strcmp(var, "core.createobject")) {
1522 if (!strcmp(value, "rename"))
1523 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
1524 else if (!strcmp(value, "link"))
1525 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
1526 else
Matthieu Moy8262aaa2014-08-07 04:59:12 -07001527 die(_("invalid mode for object creation: %s"), value);
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001528 return 0;
1529 }
1530
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001531 if (!strcmp(var, "core.sparsecheckout")) {
1532 core_apply_sparse_checkout = git_config_bool(var, value);
1533 return 0;
1534 }
1535
Derrick Stolee879321e2019-11-21 22:04:40 +00001536 if (!strcmp(var, "core.sparsecheckoutcone")) {
1537 core_sparse_checkout_cone = git_config_bool(var, value);
1538 return 0;
1539 }
1540
Torsten Bögershausen76759c72012-07-08 15:50:25 +02001541 if (!strcmp(var, "core.precomposeunicode")) {
1542 precomposed_unicode = git_config_bool(var, value);
1543 return 0;
1544 }
1545
Jeff Kinga42643a2014-12-15 18:15:20 -05001546 if (!strcmp(var, "core.protecthfs")) {
1547 protect_hfs = git_config_bool(var, value);
1548 return 0;
1549 }
1550
Johannes Schindelin2b4c6ef2014-12-16 23:46:59 +01001551 if (!strcmp(var, "core.protectntfs")) {
1552 protect_ntfs = git_config_bool(var, value);
1553 return 0;
1554 }
1555
Jeff Kingda4398d2018-07-18 16:45:25 -04001556 if (!strcmp(var, "core.usereplacerefs")) {
1557 read_replace_refs = git_config_bool(var, value);
1558 return 0;
1559 }
1560
Linus Torvalds806e2ad2008-06-18 14:37:18 -07001561 /* Add other config variables here and to Documentation/config.txt. */
Johannes Schindelin70fc5792018-10-30 11:40:04 -07001562 return platform_core_config(var, value, cb);
Linus Torvalds806e2ad2008-06-18 14:37:18 -07001563}
1564
Linus Torvalds1141f492008-06-18 15:00:11 -07001565static int git_default_i18n_config(const char *var, const char *value)
Linus Torvaldsd1364522008-06-18 14:40:35 -07001566{
Christian Couderea5105a2008-02-16 06:00:24 +01001567 if (!strcmp(var, "i18n.commitencoding"))
1568 return git_config_string(&git_commit_encoding, var, value);
Junio C Hamano4e72dce2005-11-27 16:09:40 -08001569
Christian Couderea5105a2008-02-16 06:00:24 +01001570 if (!strcmp(var, "i18n.logoutputencoding"))
1571 return git_config_string(&git_log_output_encoding, var, value);
Junio C Hamanod2c11a32006-12-27 16:41:33 -08001572
Linus Torvalds1141f492008-06-18 15:00:11 -07001573 /* Add other config variables here and to Documentation/config.txt. */
1574 return 0;
1575}
Matthias Lederhoferaa086eb2006-07-30 00:27:43 +02001576
Linus Torvalds1141f492008-06-18 15:00:11 -07001577static int git_default_branch_config(const char *var, const char *value)
1578{
Jay Soffian9ed36cf2008-02-19 11:24:37 -05001579 if (!strcmp(var, "branch.autosetupmerge")) {
1580 if (value && !strcasecmp(value, "always")) {
1581 git_branch_track = BRANCH_TRACK_ALWAYS;
1582 return 0;
1583 }
1584 git_branch_track = git_config_bool(var, value);
1585 return 0;
1586 }
Dustin Sallingsc998ae92008-05-10 15:36:29 -07001587 if (!strcmp(var, "branch.autosetuprebase")) {
1588 if (!value)
1589 return config_error_nonbool(var);
1590 else if (!strcmp(value, "never"))
1591 autorebase = AUTOREBASE_NEVER;
1592 else if (!strcmp(value, "local"))
1593 autorebase = AUTOREBASE_LOCAL;
1594 else if (!strcmp(value, "remote"))
1595 autorebase = AUTOREBASE_REMOTE;
1596 else if (!strcmp(value, "always"))
1597 autorebase = AUTOREBASE_ALWAYS;
1598 else
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001599 return error(_("malformed value for %s"), var);
Dustin Sallingsc998ae92008-05-10 15:36:29 -07001600 return 0;
1601 }
Junio C Hamanoa9cc8572007-11-02 00:24:27 -07001602
Petr Baudis1ab661d2006-04-25 00:59:33 +02001603 /* Add other config variables here and to Documentation/config.txt. */
Linus Torvalds17712992005-10-10 16:31:08 -07001604 return 0;
1605}
1606
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001607static int git_default_push_config(const char *var, const char *value)
1608{
1609 if (!strcmp(var, "push.default")) {
1610 if (!value)
1611 return config_error_nonbool(var);
1612 else if (!strcmp(value, "nothing"))
1613 push_default = PUSH_DEFAULT_NOTHING;
1614 else if (!strcmp(value, "matching"))
1615 push_default = PUSH_DEFAULT_MATCHING;
Matthieu Moyb55e6772012-04-24 09:50:03 +02001616 else if (!strcmp(value, "simple"))
1617 push_default = PUSH_DEFAULT_SIMPLE;
Johan Herland53c40312011-02-16 01:54:24 +01001618 else if (!strcmp(value, "upstream"))
1619 push_default = PUSH_DEFAULT_UPSTREAM;
1620 else if (!strcmp(value, "tracking")) /* deprecated */
1621 push_default = PUSH_DEFAULT_UPSTREAM;
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001622 else if (!strcmp(value, "current"))
1623 push_default = PUSH_DEFAULT_CURRENT;
1624 else {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001625 error(_("malformed value for %s: %s"), var, value);
1626 return error(_("must be one of nothing, matching, simple, "
1627 "upstream or current"));
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001628 }
1629 return 0;
1630 }
1631
1632 /* Add other config variables here and to Documentation/config.txt. */
1633 return 0;
1634}
1635
Marius Storm-Olsend551a482009-02-08 15:34:27 +01001636static int git_default_mailmap_config(const char *var, const char *value)
1637{
1638 if (!strcmp(var, "mailmap.file"))
Øystein Walle9352fd52014-05-27 10:45:58 +02001639 return git_config_pathname(&git_mailmap_file, var, value);
Jeff King08610902012-12-12 06:04:04 -05001640 if (!strcmp(var, "mailmap.blob"))
1641 return git_config_string(&git_mailmap_blob, var, value);
Marius Storm-Olsend551a482009-02-08 15:34:27 +01001642
1643 /* Add other config variables here and to Documentation/config.txt. */
1644 return 0;
1645}
1646
Johannes Schindelin409670f2018-10-30 11:40:03 -07001647int git_default_config(const char *var, const char *value, void *cb)
Linus Torvalds1141f492008-06-18 15:00:11 -07001648{
Christian Couder59556542013-11-30 21:55:40 +01001649 if (starts_with(var, "core."))
Johannes Schindelin70fc5792018-10-30 11:40:04 -07001650 return git_default_core_config(var, value, cb);
Linus Torvalds1141f492008-06-18 15:00:11 -07001651
William Hubbs39ab4d02019-02-04 12:48:50 -06001652 if (starts_with(var, "user.") ||
1653 starts_with(var, "author.") ||
1654 starts_with(var, "committer."))
Johannes Schindelin409670f2018-10-30 11:40:03 -07001655 return git_ident_config(var, value, cb);
Linus Torvalds1141f492008-06-18 15:00:11 -07001656
Christian Couder59556542013-11-30 21:55:40 +01001657 if (starts_with(var, "i18n."))
Linus Torvalds1141f492008-06-18 15:00:11 -07001658 return git_default_i18n_config(var, value);
1659
Christian Couder59556542013-11-30 21:55:40 +01001660 if (starts_with(var, "branch."))
Linus Torvalds1141f492008-06-18 15:00:11 -07001661 return git_default_branch_config(var, value);
1662
Christian Couder59556542013-11-30 21:55:40 +01001663 if (starts_with(var, "push."))
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001664 return git_default_push_config(var, value);
1665
Christian Couder59556542013-11-30 21:55:40 +01001666 if (starts_with(var, "mailmap."))
Marius Storm-Olsend551a482009-02-08 15:34:27 +01001667 return git_default_mailmap_config(var, value);
1668
Ryan Dammrose960786e2018-04-21 12:10:00 +02001669 if (starts_with(var, "advice.") || starts_with(var, "color.advice"))
Jeff King75194432009-09-09 07:38:58 -04001670 return git_default_advice_config(var, value);
1671
Linus Torvalds1141f492008-06-18 15:00:11 -07001672 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
1673 pager_use_color = git_config_bool(var,value);
1674 return 0;
1675 }
1676
Junio C Hamano568508e2011-10-28 14:48:40 -07001677 if (!strcmp(var, "pack.packsizelimit")) {
1678 pack_size_limit_cfg = git_config_ulong(var, value);
1679 return 0;
1680 }
Junio C Hamano8de7eeb2016-11-15 17:42:40 -08001681
1682 if (!strcmp(var, "pack.compression")) {
1683 int level = git_config_int(var, value);
1684 if (level == -1)
1685 level = Z_DEFAULT_COMPRESSION;
1686 else if (level < 0 || level > Z_BEST_COMPRESSION)
1687 die(_("bad pack compression level %d"), level);
1688 pack_compression_level = level;
1689 pack_compression_seen = 1;
1690 return 0;
1691 }
1692
Linus Torvalds1141f492008-06-18 15:00:11 -07001693 /* Add other config variables here and to Documentation/config.txt. */
1694 return 0;
1695}
1696
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001697/*
Heiko Voigtb2dc0942013-07-12 00:48:30 +02001698 * All source specific fields in the union, die_on_error, name and the callbacks
Heiko Voigt4d8dd142013-07-12 00:44:39 +02001699 * fgetc, ungetc, ftell of top need to be initialized before calling
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001700 * this function.
1701 */
Johannes Schindelin8032cc42018-04-09 10:32:05 +02001702static int do_config_from(struct config_source *top, config_fn_t fn, void *data,
1703 const struct config_options *opts)
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001704{
1705 int ret;
1706
1707 /* push config-file parsing state stack */
1708 top->prev = cf;
1709 top->linenr = 1;
1710 top->eof = 0;
Jeff King348482d2020-04-10 15:50:07 -04001711 top->total_len = 0;
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001712 strbuf_init(&top->value, 1024);
1713 strbuf_init(&top->var, 1024);
1714 cf = top;
1715
Johannes Schindelin8032cc42018-04-09 10:32:05 +02001716 ret = git_parse_source(fn, data, opts);
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001717
1718 /* pop config-file parsing state stack */
1719 strbuf_release(&top->value);
1720 strbuf_release(&top->var);
1721 cf = top->prev;
1722
1723 return ret;
1724}
1725
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001726static int do_config_from_file(config_fn_t fn,
Vasco Almeida1b8132d2016-07-28 13:14:03 +00001727 const enum config_origin_type origin_type,
1728 const char *name, const char *path, FILE *f,
Johannes Schindelin8032cc42018-04-09 10:32:05 +02001729 void *data, const struct config_options *opts)
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001730{
1731 struct config_source top;
Jeff King05e293c2018-03-30 15:26:15 -04001732 int ret;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001733
1734 top.u.file = f;
Lars Schneider473166b2016-02-19 10:16:01 +01001735 top.origin_type = origin_type;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001736 top.name = name;
1737 top.path = path;
Jeff King66f97222018-06-28 18:05:00 -04001738 top.default_error_action = CONFIG_ERROR_DIE;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001739 top.do_fgetc = config_file_fgetc;
1740 top.do_ungetc = config_file_ungetc;
1741 top.do_ftell = config_file_ftell;
1742
Jeff King05e293c2018-03-30 15:26:15 -04001743 flockfile(f);
Junio C Hamano4f4d0b42018-05-08 15:59:18 +09001744 ret = do_config_from(&top, fn, data, opts);
Jeff King05e293c2018-03-30 15:26:15 -04001745 funlockfile(f);
1746 return ret;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001747}
1748
1749static int git_config_from_stdin(config_fn_t fn, void *data)
1750{
Johannes Schindelin8032cc42018-04-09 10:32:05 +02001751 return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin,
1752 data, NULL);
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001753}
1754
Johannes Schindelin8032cc42018-04-09 10:32:05 +02001755int git_config_from_file_with_options(config_fn_t fn, const char *filename,
1756 void *data,
1757 const struct config_options *opts)
Linus Torvalds17712992005-10-10 16:31:08 -07001758{
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001759 int ret = -1;
1760 FILE *f;
Linus Torvalds17712992005-10-10 16:31:08 -07001761
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +07001762 f = fopen_or_warn(filename, "r");
Linus Torvalds17712992005-10-10 16:31:08 -07001763 if (f) {
Johannes Schindelin8032cc42018-04-09 10:32:05 +02001764 ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
1765 filename, f, data, opts);
Linus Torvalds17712992005-10-10 16:31:08 -07001766 fclose(f);
1767 }
1768 return ret;
1769}
Johannes Schindelin10bea152005-11-17 22:32:36 +01001770
Johannes Schindelin8032cc42018-04-09 10:32:05 +02001771int git_config_from_file(config_fn_t fn, const char *filename, void *data)
1772{
1773 return git_config_from_file_with_options(fn, filename, data, NULL);
1774}
1775
Jeff King4574f1a2018-06-28 18:05:24 -04001776int git_config_from_mem(config_fn_t fn,
1777 const enum config_origin_type origin_type,
1778 const char *name, const char *buf, size_t len,
1779 void *data, const struct config_options *opts)
Heiko Voigt1bc88812013-07-12 00:46:47 +02001780{
1781 struct config_source top;
1782
1783 top.u.buf.buf = buf;
1784 top.u.buf.len = len;
1785 top.u.buf.pos = 0;
Lars Schneider473166b2016-02-19 10:16:01 +01001786 top.origin_type = origin_type;
Heiko Voigt1bc88812013-07-12 00:46:47 +02001787 top.name = name;
Jeff Kingd14d4242014-02-19 00:58:52 +02001788 top.path = NULL;
Jeff King66f97222018-06-28 18:05:00 -04001789 top.default_error_action = CONFIG_ERROR_ERROR;
Jeff King49d6cfa2013-08-26 17:57:18 -04001790 top.do_fgetc = config_buf_fgetc;
1791 top.do_ungetc = config_buf_ungetc;
1792 top.do_ftell = config_buf_ftell;
Heiko Voigt1bc88812013-07-12 00:46:47 +02001793
Jeff King4574f1a2018-06-28 18:05:24 -04001794 return do_config_from(&top, fn, data, opts);
Heiko Voigt1bc88812013-07-12 00:46:47 +02001795}
1796
brian m. carlsoncd73de42017-07-13 23:49:20 +00001797int git_config_from_blob_oid(config_fn_t fn,
Brandon Williams9ebf6892016-12-16 11:03:18 -08001798 const char *name,
brian m. carlsoncd73de42017-07-13 23:49:20 +00001799 const struct object_id *oid,
Brandon Williams9ebf6892016-12-16 11:03:18 -08001800 void *data)
Heiko Voigt1bc88812013-07-12 00:46:47 +02001801{
1802 enum object_type type;
1803 char *buf;
1804 unsigned long size;
1805 int ret;
1806
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001807 buf = read_object_file(oid, &type, &size);
Heiko Voigt1bc88812013-07-12 00:46:47 +02001808 if (!buf)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001809 return error(_("unable to load config blob object '%s'"), name);
Heiko Voigt1bc88812013-07-12 00:46:47 +02001810 if (type != OBJ_BLOB) {
1811 free(buf);
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001812 return error(_("reference '%s' does not point to a blob"), name);
Heiko Voigt1bc88812013-07-12 00:46:47 +02001813 }
1814
Jeff King4574f1a2018-06-28 18:05:24 -04001815 ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size,
1816 data, NULL);
Heiko Voigt1bc88812013-07-12 00:46:47 +02001817 free(buf);
1818
1819 return ret;
1820}
1821
1822static int git_config_from_blob_ref(config_fn_t fn,
1823 const char *name,
1824 void *data)
1825{
brian m. carlsoncd73de42017-07-13 23:49:20 +00001826 struct object_id oid;
Heiko Voigt1bc88812013-07-12 00:46:47 +02001827
brian m. carlsoncd73de42017-07-13 23:49:20 +00001828 if (get_oid(name, &oid) < 0)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001829 return error(_("unable to resolve config blob '%s'"), name);
brian m. carlsoncd73de42017-07-13 23:49:20 +00001830 return git_config_from_blob_oid(fn, name, &oid, data);
Heiko Voigt1bc88812013-07-12 00:46:47 +02001831}
1832
Patrick Steinhardtc62a9992021-04-19 14:31:08 +02001833char *git_system_config(void)
Johannes Sixt506b17b2007-11-13 21:05:05 +01001834{
Patrick Steinhardt4179b482021-04-19 14:31:16 +02001835 char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
1836 if (system_config)
1837 return system_config;
Patrick Steinhardtc62a9992021-04-19 14:31:08 +02001838 return system_path(ETC_GITCONFIG);
Johannes Sixt506b17b2007-11-13 21:05:05 +01001839}
1840
Patrick Steinhardt4179b482021-04-19 14:31:16 +02001841void git_global_config(char **user_out, char **xdg_out)
Patrick Steinhardt1e06eb92021-04-19 14:31:12 +02001842{
Patrick Steinhardt4179b482021-04-19 14:31:16 +02001843 char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
1844 char *xdg_config = NULL;
1845
1846 if (!user_config) {
1847 user_config = expand_user_path("~/.gitconfig", 0);
1848 xdg_config = xdg_config_home("config");
1849 }
1850
1851 *user_out = user_config;
1852 *xdg_out = xdg_config;
Linus Torvalds17712992005-10-10 16:31:08 -07001853}
Johannes Schindelin10bea152005-11-17 22:32:36 +01001854
Steffen Prohaska23b0c472014-08-26 17:23:21 +02001855/*
1856 * Parse environment variable 'k' as a boolean (in various
1857 * possible spellings); if missing, use the default value 'def'.
1858 */
Lars R. Damerow0ef37162010-03-17 12:55:51 -07001859int git_env_bool(const char *k, int def)
Jeff Kingab88c362008-02-06 05:11:18 -05001860{
1861 const char *v = getenv(k);
1862 return v ? git_config_bool(k, v) : def;
1863}
1864
Steffen Prohaska23b0c472014-08-26 17:23:21 +02001865/*
1866 * Parse environment variable 'k' as ulong with possibly a unit
1867 * suffix; if missing, use the default value 'val'.
1868 */
1869unsigned long git_env_ulong(const char *k, unsigned long val)
1870{
1871 const char *v = getenv(k);
1872 if (v && !git_parse_ulong(v, &val))
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02001873 die(_("failed to parse %s"), k);
Steffen Prohaska23b0c472014-08-26 17:23:21 +02001874 return val;
1875}
1876
Jeff Kingab88c362008-02-06 05:11:18 -05001877int git_config_system(void)
1878{
1879 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
1880}
1881
Nguyễn Thái Ngọc Duye145a0b2017-04-17 17:10:02 +07001882static int do_git_config_sequence(const struct config_options *opts,
1883 config_fn_t fn, void *data)
Junio C Hamano4f629532005-11-25 16:03:56 -08001884{
Jeff Kingc72ee442016-05-18 18:39:02 -04001885 int ret = 0;
Patrick Steinhardtc62a9992021-04-19 14:31:08 +02001886 char *system_config = git_system_config();
Patrick Steinhardt1e06eb92021-04-19 14:31:12 +02001887 char *xdg_config = NULL;
1888 char *user_config = NULL;
Nguyễn Thái Ngọc Duye145a0b2017-04-17 17:10:02 +07001889 char *repo_config;
Matthew Rogers5c105a82020-02-10 00:30:56 +00001890 enum config_scope prev_parsing_scope = current_parsing_scope;
Nguyễn Thái Ngọc Duye145a0b2017-04-17 17:10:02 +07001891
Brandon Williamsa577fb52017-06-14 11:07:38 -07001892 if (opts->commondir)
1893 repo_config = mkpathdup("%s/config", opts->commondir);
Johannes Schindelin44004872018-11-14 05:59:02 -08001894 else if (opts->git_dir)
1895 BUG("git_dir without commondir");
Nguyễn Thái Ngọc Duye145a0b2017-04-17 17:10:02 +07001896 else
1897 repo_config = NULL;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +02001898
Jeff King9acc5912016-05-18 18:44:23 -04001899 current_parsing_scope = CONFIG_SCOPE_SYSTEM;
Patrick Steinhardtc62a9992021-04-19 14:31:08 +02001900 if (git_config_system() && system_config &&
1901 !access_or_die(system_config, R_OK,
1902 opts->system_gently ? ACCESS_EACCES_OK : 0))
1903 ret += git_config_from_file(fn, system_config, data);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +02001904
Jeff King9acc5912016-05-18 18:44:23 -04001905 current_parsing_scope = CONFIG_SCOPE_GLOBAL;
Patrick Steinhardt1e06eb92021-04-19 14:31:12 +02001906 git_global_config(&user_config, &xdg_config);
1907
Jeff Kingc72ee442016-05-18 18:39:02 -04001908 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001909 ret += git_config_from_file(fn, xdg_config, data);
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001910
Jeff Kingc72ee442016-05-18 18:39:02 -04001911 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001912 ret += git_config_from_file(fn, user_config, data);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +02001913
Matthew Rogers6dc905d2020-02-10 00:30:54 +00001914 current_parsing_scope = CONFIG_SCOPE_LOCAL;
Jeff Hostetler800a7f92019-04-15 13:39:46 -07001915 if (!opts->ignore_repo && repo_config &&
1916 !access_or_die(repo_config, R_OK, 0))
Felipe Contrerasaa387402009-02-21 02:48:55 +02001917 ret += git_config_from_file(fn, repo_config, data);
Alex Riesen8b1fa772010-03-26 23:53:57 +01001918
Matthew Rogers6dc905d2020-02-10 00:30:54 +00001919 current_parsing_scope = CONFIG_SCOPE_WORKTREE;
Jeff Hostetler800a7f92019-04-15 13:39:46 -07001920 if (!opts->ignore_worktree && repository_format_worktree_config) {
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +02001921 char *path = git_pathdup("config.worktree");
1922 if (!access_or_die(path, R_OK, 0))
1923 ret += git_config_from_file(fn, path, data);
1924 free(path);
1925 }
1926
Matthew Rogers6766e412020-02-10 00:30:55 +00001927 current_parsing_scope = CONFIG_SCOPE_COMMAND;
Jeff Hostetler800a7f92019-04-15 13:39:46 -07001928 if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
Matthieu Moy8262aaa2014-08-07 04:59:12 -07001929 die(_("unable to parse command-line config"));
Alex Riesen8b1fa772010-03-26 23:53:57 +01001930
Matthew Rogers5c105a82020-02-10 00:30:56 +00001931 current_parsing_scope = prev_parsing_scope;
Patrick Steinhardtc62a9992021-04-19 14:31:08 +02001932 free(system_config);
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001933 free(xdg_config);
1934 free(user_config);
Jeff King80181862016-03-11 17:37:03 -05001935 free(repo_config);
Jeff Kingc72ee442016-05-18 18:39:02 -04001936 return ret;
Junio C Hamano4f629532005-11-25 16:03:56 -08001937}
1938
Brandon Williamsdc8441f2017-06-14 11:07:39 -07001939int config_with_options(config_fn_t fn, void *data,
1940 struct git_config_source *config_source,
1941 const struct config_options *opts)
Nguyễn Thái Ngọc Duydbdf5852010-11-26 22:32:33 +07001942{
Jeff King9b25a0b2012-02-06 04:54:04 -05001943 struct config_include_data inc = CONFIG_INCLUDE_INIT;
1944
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +07001945 if (opts->respect_includes) {
Jeff King9b25a0b2012-02-06 04:54:04 -05001946 inc.fn = fn;
1947 inc.data = data;
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +07001948 inc.opts = opts;
Jeff King9b25a0b2012-02-06 04:54:04 -05001949 fn = git_config_include;
1950 data = &inc;
1951 }
Nguyễn Thái Ngọc Duydbdf5852010-11-26 22:32:33 +07001952
Matthew Rogerse37efa42020-02-10 00:30:57 +00001953 if (config_source)
1954 current_parsing_scope = config_source->scope;
1955
Jeff Kingc9b5e2a2012-02-16 03:05:56 -05001956 /*
1957 * If we have a specific filename, use it. Otherwise, follow the
1958 * regular lookup sequence.
1959 */
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001960 if (config_source && config_source->use_stdin)
1961 return git_config_from_stdin(fn, data);
1962 else if (config_source && config_source->file)
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +02001963 return git_config_from_file(fn, config_source->file, data);
1964 else if (config_source && config_source->blob)
1965 return git_config_from_blob_ref(fn, config_source->blob, data);
Jeff Kingc9b5e2a2012-02-16 03:05:56 -05001966
Nguyễn Thái Ngọc Duye145a0b2017-04-17 17:10:02 +07001967 return do_git_config_sequence(opts, fn, data);
Nguyễn Thái Ngọc Duydbdf5852010-11-26 22:32:33 +07001968}
1969
Tanay Abhra155ef252014-08-07 04:59:17 -07001970static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
1971{
1972 int i, value_index;
1973 struct string_list *values;
1974 struct config_set_element *entry;
1975 struct configset_list *list = &cs->list;
Tanay Abhra155ef252014-08-07 04:59:17 -07001976
1977 for (i = 0; i < list->nr; i++) {
1978 entry = list->items[i].e;
1979 value_index = list->items[i].value_index;
1980 values = &entry->value_list;
Jeff King0d44a2d2016-05-26 20:32:23 -04001981
1982 current_config_kvi = values->items[value_index].util;
1983
1984 if (fn(entry->key, values->items[value_index].string, data) < 0)
1985 git_die_config_linenr(entry->key,
1986 current_config_kvi->filename,
1987 current_config_kvi->linenr);
1988
1989 current_config_kvi = NULL;
Tanay Abhra155ef252014-08-07 04:59:17 -07001990 }
1991}
1992
Johannes Schindelin0654aa52017-03-13 21:11:04 +01001993void read_early_config(config_fn_t cb, void *data)
1994{
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +07001995 struct config_options opts = {0};
Brandon Williamsd3fb71b2017-06-14 11:07:37 -07001996 struct strbuf commondir = STRBUF_INIT;
1997 struct strbuf gitdir = STRBUF_INIT;
Johannes Schindelin1a274092017-03-13 21:11:12 +01001998
Nguyễn Thái Ngọc Duyc48f4b32017-04-17 17:10:00 +07001999 opts.respect_includes = 1;
Johannes Schindelin0654aa52017-03-13 21:11:04 +01002000
Brandon Williamsa577fb52017-06-14 11:07:38 -07002001 if (have_git_dir()) {
2002 opts.commondir = get_git_common_dir();
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +07002003 opts.git_dir = get_git_dir();
Johannes Schindelin0654aa52017-03-13 21:11:04 +01002004 /*
Johannes Schindelin1a274092017-03-13 21:11:12 +01002005 * When setup_git_directory() was not yet asked to discover the
2006 * GIT_DIR, we ask discover_git_directory() to figure out whether there
2007 * is any repository config we should use (but unlike
2008 * setup_git_directory_gently(), no global state is changed, most
2009 * notably, the current working directory is still the same after the
2010 * call).
Johannes Schindelin0654aa52017-03-13 21:11:04 +01002011 */
Brandon Williamsa577fb52017-06-14 11:07:38 -07002012 } else if (!discover_git_directory(&commondir, &gitdir)) {
2013 opts.commondir = commondir.buf;
Brandon Williamsd3fb71b2017-06-14 11:07:37 -07002014 opts.git_dir = gitdir.buf;
Brandon Williamsa577fb52017-06-14 11:07:38 -07002015 }
Johannes Schindelin0654aa52017-03-13 21:11:04 +01002016
Brandon Williamsdc8441f2017-06-14 11:07:39 -07002017 config_with_options(cb, data, NULL, &opts);
Nguyễn Thái Ngọc Duy2185fde2017-04-17 17:10:01 +07002018
Brandon Williamsd3fb71b2017-06-14 11:07:37 -07002019 strbuf_release(&commondir);
2020 strbuf_release(&gitdir);
Johannes Schindelin0654aa52017-03-13 21:11:04 +01002021}
2022
Jeff Hostetler800a7f92019-04-15 13:39:46 -07002023/*
2024 * Read config but only enumerate system and global settings.
2025 * Omit any repo-local, worktree-local, or command-line settings.
2026 */
2027void read_very_early_config(config_fn_t cb, void *data)
2028{
2029 struct config_options opts = { 0 };
2030
2031 opts.respect_includes = 1;
2032 opts.ignore_repo = 1;
2033 opts.ignore_worktree = 1;
2034 opts.ignore_cmdline = 1;
Jeff Hostetlerf672dee2019-04-29 13:14:22 -07002035 opts.system_gently = 1;
Jeff Hostetler800a7f92019-04-15 13:39:46 -07002036
2037 config_with_options(cb, data, NULL, &opts);
2038}
2039
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002040static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
2041{
2042 struct config_set_element k;
2043 struct config_set_element *found_entry;
2044 char *normalized_key;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002045 /*
2046 * `key` may come from the user, so normalize it before using it
2047 * for querying entries from the hashmap.
2048 */
Stefan Beller270cd9e2016-04-27 14:30:39 -07002049 if (git_config_parse_key(key, &normalized_key, NULL))
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002050 return NULL;
2051
Eric Wongd22245a2019-10-06 23:30:27 +00002052 hashmap_entry_init(&k.ent, strhash(normalized_key));
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002053 k.key = normalized_key;
Eric Wong404ab782019-10-06 23:30:42 +00002054 found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002055 free(normalized_key);
2056 return found_entry;
2057}
2058
2059static int configset_add_value(struct config_set *cs, const char *key, const char *value)
2060{
2061 struct config_set_element *e;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07002062 struct string_list_item *si;
Tanay Abhra155ef252014-08-07 04:59:17 -07002063 struct configset_list_item *l_item;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07002064 struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
2065
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002066 e = configset_find_element(cs, key);
2067 /*
2068 * Since the keys are being fed by git_config*() callback mechanism, they
2069 * are already normalized. So simply add them without any further munging.
2070 */
2071 if (!e) {
2072 e = xmalloc(sizeof(*e));
Eric Wongd22245a2019-10-06 23:30:27 +00002073 hashmap_entry_init(&e->ent, strhash(key));
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002074 e->key = xstrdup(key);
2075 string_list_init(&e->value_list, 1);
Eric Wongb94e5c12019-10-06 23:30:29 +00002076 hashmap_add(&cs->config_hash, &e->ent);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002077 }
Jeff King8c53f072015-01-12 20:59:09 -05002078 si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
Tanay Abhra155ef252014-08-07 04:59:17 -07002079
2080 ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
2081 l_item = &cs->list.items[cs->list.nr++];
2082 l_item->e = e;
2083 l_item->value_index = e->value_list.nr - 1;
2084
Jeff King32582582016-05-18 18:41:08 -04002085 if (!cf)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002086 BUG("configset_add_value has no source");
Jeff King32582582016-05-18 18:41:08 -04002087 if (cf->name) {
Tanay Abhra3df8fd62014-08-07 04:59:14 -07002088 kv_info->filename = strintern(cf->name);
2089 kv_info->linenr = cf->linenr;
Vasco Almeida1b8132d2016-07-28 13:14:03 +00002090 kv_info->origin_type = cf->origin_type;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07002091 } else {
2092 /* for values read from `git_config_from_parameters()` */
2093 kv_info->filename = NULL;
2094 kv_info->linenr = -1;
Vasco Almeida1b8132d2016-07-28 13:14:03 +00002095 kv_info->origin_type = CONFIG_ORIGIN_CMDLINE;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07002096 }
Jeff King9acc5912016-05-18 18:44:23 -04002097 kv_info->scope = current_parsing_scope;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07002098 si->util = kv_info;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002099
2100 return 0;
2101}
2102
Stefan Beller7663cdc2017-06-30 12:14:05 -07002103static int config_set_element_cmp(const void *unused_cmp_data,
Eric Wong939af162019-10-06 23:30:37 +00002104 const struct hashmap_entry *eptr,
2105 const struct hashmap_entry *entry_or_key,
Stefan Beller7663cdc2017-06-30 12:14:05 -07002106 const void *unused_keydata)
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002107{
Eric Wong939af162019-10-06 23:30:37 +00002108 const struct config_set_element *e1, *e2;
2109
2110 e1 = container_of(eptr, const struct config_set_element, ent);
2111 e2 = container_of(entry_or_key, const struct config_set_element, ent);
Stefan Beller77bdc092017-06-30 17:28:32 -07002112
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002113 return strcmp(e1->key, e2->key);
2114}
2115
2116void git_configset_init(struct config_set *cs)
2117{
Stefan Beller77bdc092017-06-30 17:28:32 -07002118 hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002119 cs->hash_initialized = 1;
Tanay Abhra155ef252014-08-07 04:59:17 -07002120 cs->list.nr = 0;
2121 cs->list.alloc = 0;
2122 cs->list.items = NULL;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002123}
2124
2125void git_configset_clear(struct config_set *cs)
2126{
2127 struct config_set_element *entry;
2128 struct hashmap_iter iter;
2129 if (!cs->hash_initialized)
2130 return;
2131
Eric Wong87571c32019-10-06 23:30:38 +00002132 hashmap_for_each_entry(&cs->config_hash, &iter, entry,
Eric Wong87571c32019-10-06 23:30:38 +00002133 ent /* member name */) {
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002134 free(entry->key);
Tanay Abhra3df8fd62014-08-07 04:59:14 -07002135 string_list_clear(&entry->value_list, 1);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002136 }
Elijah Newren6da1a252020-11-02 18:55:05 +00002137 hashmap_clear_and_free(&cs->config_hash, struct config_set_element, ent);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002138 cs->hash_initialized = 0;
Tanay Abhra155ef252014-08-07 04:59:17 -07002139 free(cs->list.items);
2140 cs->list.nr = 0;
2141 cs->list.alloc = 0;
2142 cs->list.items = NULL;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002143}
2144
2145static int config_set_callback(const char *key, const char *value, void *cb)
2146{
2147 struct config_set *cs = cb;
2148 configset_add_value(cs, key, value);
2149 return 0;
2150}
2151
2152int git_configset_add_file(struct config_set *cs, const char *filename)
2153{
2154 return git_config_from_file(config_set_callback, filename, cs);
2155}
2156
2157int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
2158{
2159 const struct string_list *values = NULL;
2160 /*
2161 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
2162 * queried key in the files of the configset, the value returned will be the last
2163 * value in the value list for that key.
2164 */
2165 values = git_configset_get_value_multi(cs, key);
2166
2167 if (!values)
2168 return 1;
2169 assert(values->nr > 0);
2170 *value = values->items[values->nr - 1].string;
2171 return 0;
2172}
2173
2174const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
2175{
2176 struct config_set_element *e = configset_find_element(cs, key);
2177 return e ? &e->value_list : NULL;
2178}
2179
Jeff King9a532192020-08-17 17:33:11 -04002180int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002181{
2182 const char *value;
2183 if (!git_configset_get_value(cs, key, &value))
Jeff King9a532192020-08-17 17:33:11 -04002184 return git_config_string((const char **)dest, key, value);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002185 else
2186 return 1;
2187}
2188
Jeff Kingf1de9812020-08-14 12:17:36 -04002189int git_configset_get_string_tmp(struct config_set *cs, const char *key,
2190 const char **dest)
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002191{
Jeff Kingf1de9812020-08-14 12:17:36 -04002192 const char *value;
2193 if (!git_configset_get_value(cs, key, &value)) {
2194 if (!value)
2195 return config_error_nonbool(key);
2196 *dest = value;
2197 return 0;
2198 } else {
2199 return 1;
2200 }
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002201}
2202
2203int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
2204{
2205 const char *value;
2206 if (!git_configset_get_value(cs, key, &value)) {
2207 *dest = git_config_int(key, value);
2208 return 0;
2209 } else
2210 return 1;
2211}
2212
2213int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
2214{
2215 const char *value;
2216 if (!git_configset_get_value(cs, key, &value)) {
2217 *dest = git_config_ulong(key, value);
2218 return 0;
2219 } else
2220 return 1;
2221}
2222
2223int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
2224{
2225 const char *value;
2226 if (!git_configset_get_value(cs, key, &value)) {
2227 *dest = git_config_bool(key, value);
2228 return 0;
2229 } else
2230 return 1;
2231}
2232
2233int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
2234 int *is_bool, int *dest)
2235{
2236 const char *value;
2237 if (!git_configset_get_value(cs, key, &value)) {
2238 *dest = git_config_bool_or_int(key, value, is_bool);
2239 return 0;
2240 } else
2241 return 1;
2242}
2243
2244int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
2245{
2246 const char *value;
2247 if (!git_configset_get_value(cs, key, &value)) {
Martin Ågren89576612017-08-07 20:20:49 +02002248 *dest = git_parse_maybe_bool(value);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002249 if (*dest == -1)
2250 return -1;
2251 return 0;
2252 } else
2253 return 1;
2254}
2255
2256int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
2257{
2258 const char *value;
2259 if (!git_configset_get_value(cs, key, &value))
2260 return git_config_pathname(dest, key, value);
2261 else
2262 return 1;
2263}
2264
Brandon Williams3b256222017-06-22 11:43:42 -07002265/* Functions use to read configuration from a repository */
2266static void repo_read_config(struct repository *repo)
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002267{
Jeff Hostetler17037512019-04-15 13:39:42 -07002268 struct config_options opts = { 0 };
Brandon Williams3b256222017-06-22 11:43:42 -07002269
2270 opts.respect_includes = 1;
2271 opts.commondir = repo->commondir;
2272 opts.git_dir = repo->gitdir;
2273
2274 if (!repo->config)
René Scharfeca56dad2021-03-13 17:17:22 +01002275 CALLOC_ARRAY(repo->config, 1);
Brandon Williams3b256222017-06-22 11:43:42 -07002276 else
2277 git_configset_clear(repo->config);
2278
2279 git_configset_init(repo->config);
2280
2281 if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
2282 /*
2283 * config_with_options() normally returns only
2284 * zero, as most errors are fatal, and
2285 * non-fatal potential errors are guarded by "if"
2286 * statements that are entered only when no error is
2287 * possible.
2288 *
2289 * If we ever encounter a non-fatal error, it means
2290 * something went really wrong and we should stop
2291 * immediately.
2292 */
2293 die(_("unknown error occurred while reading the configuration files"));
2294}
2295
2296static void git_config_check_init(struct repository *repo)
2297{
2298 if (repo->config && repo->config->hash_initialized)
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002299 return;
Brandon Williams3b256222017-06-22 11:43:42 -07002300 repo_read_config(repo);
2301}
2302
2303static void repo_config_clear(struct repository *repo)
2304{
2305 if (!repo->config || !repo->config->hash_initialized)
2306 return;
2307 git_configset_clear(repo->config);
2308}
2309
2310void repo_config(struct repository *repo, config_fn_t fn, void *data)
2311{
2312 git_config_check_init(repo);
2313 configset_iter(repo->config, fn, data);
2314}
2315
2316int repo_config_get_value(struct repository *repo,
2317 const char *key, const char **value)
2318{
2319 git_config_check_init(repo);
2320 return git_configset_get_value(repo->config, key, value);
2321}
2322
2323const struct string_list *repo_config_get_value_multi(struct repository *repo,
2324 const char *key)
2325{
2326 git_config_check_init(repo);
2327 return git_configset_get_value_multi(repo->config, key);
2328}
2329
Brandon Williams3b256222017-06-22 11:43:42 -07002330int repo_config_get_string(struct repository *repo,
2331 const char *key, char **dest)
Brandon Williams3b256222017-06-22 11:43:42 -07002332{
2333 int ret;
2334 git_config_check_init(repo);
Jeff King9a532192020-08-17 17:33:11 -04002335 ret = git_configset_get_string(repo->config, key, dest);
Brandon Williams3b256222017-06-22 11:43:42 -07002336 if (ret < 0)
2337 git_die_config(key, NULL);
2338 return ret;
2339}
2340
Jeff Kingf1de9812020-08-14 12:17:36 -04002341int repo_config_get_string_tmp(struct repository *repo,
2342 const char *key, const char **dest)
Brandon Williams3b256222017-06-22 11:43:42 -07002343{
Jeff Kingf1de9812020-08-14 12:17:36 -04002344 int ret;
Brandon Williams3b256222017-06-22 11:43:42 -07002345 git_config_check_init(repo);
Jeff Kingf1de9812020-08-14 12:17:36 -04002346 ret = git_configset_get_string_tmp(repo->config, key, dest);
2347 if (ret < 0)
2348 git_die_config(key, NULL);
2349 return ret;
Brandon Williams3b256222017-06-22 11:43:42 -07002350}
2351
2352int repo_config_get_int(struct repository *repo,
2353 const char *key, int *dest)
2354{
2355 git_config_check_init(repo);
2356 return git_configset_get_int(repo->config, key, dest);
2357}
2358
2359int repo_config_get_ulong(struct repository *repo,
2360 const char *key, unsigned long *dest)
2361{
2362 git_config_check_init(repo);
2363 return git_configset_get_ulong(repo->config, key, dest);
2364}
2365
2366int repo_config_get_bool(struct repository *repo,
2367 const char *key, int *dest)
2368{
2369 git_config_check_init(repo);
2370 return git_configset_get_bool(repo->config, key, dest);
2371}
2372
2373int repo_config_get_bool_or_int(struct repository *repo,
2374 const char *key, int *is_bool, int *dest)
2375{
2376 git_config_check_init(repo);
2377 return git_configset_get_bool_or_int(repo->config, key, is_bool, dest);
2378}
2379
2380int repo_config_get_maybe_bool(struct repository *repo,
2381 const char *key, int *dest)
2382{
2383 git_config_check_init(repo);
2384 return git_configset_get_maybe_bool(repo->config, key, dest);
2385}
2386
2387int repo_config_get_pathname(struct repository *repo,
2388 const char *key, const char **dest)
2389{
2390 int ret;
2391 git_config_check_init(repo);
2392 ret = git_configset_get_pathname(repo->config, key, dest);
2393 if (ret < 0)
2394 git_die_config(key, NULL);
2395 return ret;
2396}
2397
2398/* Functions used historically to read configuration from 'the_repository' */
2399void git_config(config_fn_t fn, void *data)
2400{
2401 repo_config(the_repository, fn, data);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002402}
2403
2404void git_config_clear(void)
2405{
Brandon Williams3b256222017-06-22 11:43:42 -07002406 repo_config_clear(the_repository);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002407}
2408
2409int git_config_get_value(const char *key, const char **value)
2410{
Brandon Williams3b256222017-06-22 11:43:42 -07002411 return repo_config_get_value(the_repository, key, value);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002412}
2413
2414const struct string_list *git_config_get_value_multi(const char *key)
2415{
Brandon Williams3b256222017-06-22 11:43:42 -07002416 return repo_config_get_value_multi(the_repository, key);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002417}
2418
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002419int git_config_get_string(const char *key, char **dest)
2420{
Brandon Williams3b256222017-06-22 11:43:42 -07002421 return repo_config_get_string(the_repository, key, dest);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002422}
2423
Jeff Kingf1de9812020-08-14 12:17:36 -04002424int git_config_get_string_tmp(const char *key, const char **dest)
2425{
2426 return repo_config_get_string_tmp(the_repository, key, dest);
2427}
2428
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002429int git_config_get_int(const char *key, int *dest)
2430{
Brandon Williams3b256222017-06-22 11:43:42 -07002431 return repo_config_get_int(the_repository, key, dest);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002432}
2433
2434int git_config_get_ulong(const char *key, unsigned long *dest)
2435{
Brandon Williams3b256222017-06-22 11:43:42 -07002436 return repo_config_get_ulong(the_repository, key, dest);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002437}
2438
2439int git_config_get_bool(const char *key, int *dest)
2440{
Brandon Williams3b256222017-06-22 11:43:42 -07002441 return repo_config_get_bool(the_repository, key, dest);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002442}
2443
2444int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
2445{
Brandon Williams3b256222017-06-22 11:43:42 -07002446 return repo_config_get_bool_or_int(the_repository, key, is_bool, dest);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002447}
2448
2449int git_config_get_maybe_bool(const char *key, int *dest)
2450{
Brandon Williams3b256222017-06-22 11:43:42 -07002451 return repo_config_get_maybe_bool(the_repository, key, dest);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002452}
2453
2454int git_config_get_pathname(const char *key, const char **dest)
2455{
Brandon Williams3b256222017-06-22 11:43:42 -07002456 return repo_config_get_pathname(the_repository, key, dest);
Tanay Abhra5a80e972014-08-07 04:59:16 -07002457}
2458
Christian Couder77d67972017-02-27 19:00:13 +01002459int git_config_get_expiry(const char *key, const char **output)
2460{
Jeff King9a532192020-08-17 17:33:11 -04002461 int ret = git_config_get_string(key, (char **)output);
Christian Couder77d67972017-02-27 19:00:13 +01002462 if (ret)
2463 return ret;
2464 if (strcmp(*output, "now")) {
Johannes Schindelindddbad72017-04-26 21:29:31 +02002465 timestamp_t now = approxidate("now");
Christian Couder77d67972017-02-27 19:00:13 +01002466 if (approxidate(*output) >= now)
2467 git_die_config(key, _("Invalid %s: '%s'"), key, *output);
2468 }
2469 return ret;
2470}
2471
Junio C Hamano6e96cb52017-08-19 11:43:39 -07002472int git_config_get_expiry_in_days(const char *key, timestamp_t *expiry, timestamp_t now)
2473{
Jeff King1c890012020-08-17 17:33:13 -04002474 const char *expiry_string;
Junio C Hamano6e96cb52017-08-19 11:43:39 -07002475 intmax_t days;
2476 timestamp_t when;
2477
Jeff King1c890012020-08-17 17:33:13 -04002478 if (git_config_get_string_tmp(key, &expiry_string))
Junio C Hamano6e96cb52017-08-19 11:43:39 -07002479 return 1; /* no such thing */
2480
2481 if (git_parse_signed(expiry_string, &days, maximum_signed_value_of_type(int))) {
2482 const int scale = 86400;
2483 *expiry = now - days * scale;
2484 return 0;
2485 }
2486
2487 if (!parse_expiry_date(expiry_string, &when)) {
2488 *expiry = when;
2489 return 0;
2490 }
2491 return -1; /* thing exists but cannot be parsed */
2492}
2493
Christian Couder1f44b092017-02-27 19:00:00 +01002494int git_config_get_split_index(void)
2495{
2496 int val;
2497
2498 if (!git_config_get_maybe_bool("core.splitindex", &val))
2499 return val;
2500
2501 return -1; /* default value */
2502}
2503
Christian Couder72dcb7b2017-02-27 19:00:07 +01002504int git_config_get_max_percent_split_change(void)
2505{
2506 int val = -1;
2507
2508 if (!git_config_get_int("splitindex.maxpercentchange", &val)) {
2509 if (0 <= val && val <= 100)
2510 return val;
2511
2512 return error(_("splitIndex.maxPercentChange value '%d' "
2513 "should be between 0 and 100"), val);
2514 }
2515
2516 return -1; /* default value */
2517}
2518
Ben Peart883e2482017-09-22 12:35:40 -04002519int git_config_get_fsmonitor(void)
2520{
2521 if (git_config_get_pathname("core.fsmonitor", &core_fsmonitor))
Ben Peart4cb54d02018-09-18 23:29:35 +00002522 core_fsmonitor = getenv("GIT_TEST_FSMONITOR");
Ben Peart883e2482017-09-22 12:35:40 -04002523
2524 if (core_fsmonitor && !*core_fsmonitor)
2525 core_fsmonitor = NULL;
2526
2527 if (core_fsmonitor)
2528 return 1;
2529
2530 return 0;
2531}
2532
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002533int git_config_get_index_threads(int *dest)
Ben Peartc780b9c2018-10-10 11:59:35 -04002534{
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002535 int is_bool, val;
Ben Peartc780b9c2018-10-10 11:59:35 -04002536
2537 val = git_env_ulong("GIT_TEST_INDEX_THREADS", 0);
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002538 if (val) {
2539 *dest = val;
2540 return 0;
2541 }
Ben Peartc780b9c2018-10-10 11:59:35 -04002542
2543 if (!git_config_get_bool_or_int("index.threads", &is_bool, &val)) {
2544 if (is_bool)
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002545 *dest = val ? 0 : 1;
Ben Peartc780b9c2018-10-10 11:59:35 -04002546 else
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002547 *dest = val;
2548 return 0;
Ben Peartc780b9c2018-10-10 11:59:35 -04002549 }
2550
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002551 return 1;
Ben Peartc780b9c2018-10-10 11:59:35 -04002552}
2553
Tanay Abhra5a80e972014-08-07 04:59:16 -07002554NORETURN
2555void git_die_config_linenr(const char *key, const char *filename, int linenr)
2556{
2557 if (!filename)
2558 die(_("unable to parse '%s' from command-line config"), key);
2559 else
2560 die(_("bad config variable '%s' in file '%s' at line %d"),
2561 key, filename, linenr);
2562}
2563
2564NORETURN __attribute__((format(printf, 2, 3)))
2565void git_die_config(const char *key, const char *err, ...)
2566{
2567 const struct string_list *values;
2568 struct key_value_info *kv_info;
2569
2570 if (err) {
2571 va_list params;
2572 va_start(params, err);
2573 vreportf("error: ", err, params);
2574 va_end(params);
2575 }
2576 values = git_config_get_value_multi(key);
2577 kv_info = values->items[values->nr - 1].util;
2578 git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002579}
2580
Johannes Schindelin10bea152005-11-17 22:32:36 +01002581/*
2582 * Find all the stuff for git_config_set() below.
2583 */
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002584
Johannes Schindelinfee85722018-04-09 10:32:09 +02002585struct config_store_data {
Jeff Kingf011a962020-04-10 15:46:07 -04002586 size_t baselen;
Felipe Contreras4b25d092009-05-01 12:06:36 +03002587 char *key;
Johannes Schindelinf98d8632005-11-20 13:24:18 +01002588 int do_not_match;
Derrick Stoleec90702a2020-11-25 22:12:54 +00002589 const char *fixed_value;
Derrick Stolee247e2f82020-11-25 22:12:50 +00002590 regex_t *value_pattern;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002591 int multi_replace;
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002592 struct {
2593 size_t begin, end;
2594 enum config_event_t type;
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002595 int is_keys_section;
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002596 } *parsed;
2597 unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc;
Johannes Schindelin5221c312018-04-09 10:32:17 +02002598 unsigned int key_seen:1, section_seen:1, is_keys_section:1;
Johannes Schindelinfee85722018-04-09 10:32:09 +02002599};
Johannes Schindelin10bea152005-11-17 22:32:36 +01002600
Martin Ågren2a00e592018-05-20 12:42:33 +02002601static void config_store_data_clear(struct config_store_data *store)
2602{
Martin Ågrene7347cb2018-05-20 12:42:35 +02002603 free(store->key);
Derrick Stolee247e2f82020-11-25 22:12:50 +00002604 if (store->value_pattern != NULL &&
2605 store->value_pattern != CONFIG_REGEX_NONE) {
2606 regfree(store->value_pattern);
2607 free(store->value_pattern);
Martin Ågren3b825422018-05-20 12:42:34 +02002608 }
Martin Ågren2a00e592018-05-20 12:42:33 +02002609 free(store->parsed);
2610 free(store->seen);
2611 memset(store, 0, sizeof(*store));
2612}
2613
Johannes Schindelinfee85722018-04-09 10:32:09 +02002614static int matches(const char *key, const char *value,
2615 const struct config_store_data *store)
Johannes Schindelinf98d8632005-11-20 13:24:18 +01002616{
Johannes Schindelinfee85722018-04-09 10:32:09 +02002617 if (strcmp(key, store->key))
Jeff Kingc1063be2014-08-19 02:20:00 -04002618 return 0; /* not ours */
Derrick Stoleec90702a2020-11-25 22:12:54 +00002619 if (store->fixed_value)
2620 return !strcmp(store->fixed_value, value);
Derrick Stolee247e2f82020-11-25 22:12:50 +00002621 if (!store->value_pattern)
Jeff Kingc1063be2014-08-19 02:20:00 -04002622 return 1; /* always matches */
Derrick Stolee247e2f82020-11-25 22:12:50 +00002623 if (store->value_pattern == CONFIG_REGEX_NONE)
Jeff Kingc1063be2014-08-19 02:20:00 -04002624 return 0; /* never matches */
2625
Johannes Schindelinfee85722018-04-09 10:32:09 +02002626 return store->do_not_match ^
Derrick Stolee247e2f82020-11-25 22:12:50 +00002627 (value && !regexec(store->value_pattern, value, 0, NULL, 0));
Johannes Schindelinf98d8632005-11-20 13:24:18 +01002628}
2629
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002630static int store_aux_event(enum config_event_t type,
2631 size_t begin, size_t end, void *data)
2632{
2633 struct config_store_data *store = data;
2634
2635 ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
2636 store->parsed[store->parsed_nr].begin = begin;
2637 store->parsed[store->parsed_nr].end = end;
2638 store->parsed[store->parsed_nr].type = type;
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002639
2640 if (type == CONFIG_EVENT_SECTION) {
Stefan Beller2d84f132018-08-08 12:50:19 -07002641 int (*cmpfn)(const char *, const char *, size_t);
2642
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002643 if (cf->var.len < 2 || cf->var.buf[cf->var.len - 1] != '.')
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02002644 return error(_("invalid section name '%s'"), cf->var.buf);
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002645
Stefan Beller2d84f132018-08-08 12:50:19 -07002646 if (cf->subsection_case_sensitive)
2647 cmpfn = strncasecmp;
2648 else
2649 cmpfn = strncmp;
2650
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002651 /* Is this the section we were looking for? */
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002652 store->is_keys_section =
2653 store->parsed[store->parsed_nr].is_keys_section =
2654 cf->var.len - 1 == store->baselen &&
Stefan Beller2d84f132018-08-08 12:50:19 -07002655 !cmpfn(cf->var.buf, store->key, store->baselen);
Johannes Schindelinc71d8bb2018-04-09 10:32:29 +02002656 if (store->is_keys_section) {
2657 store->section_seen = 1;
2658 ALLOC_GROW(store->seen, store->seen_nr + 1,
2659 store->seen_alloc);
2660 store->seen[store->seen_nr] = store->parsed_nr;
2661 }
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002662 }
2663
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002664 store->parsed_nr++;
2665
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002666 return 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002667}
2668
Felipe Contreras4b25d092009-05-01 12:06:36 +03002669static int store_aux(const char *key, const char *value, void *cb)
Johannes Schindelin10bea152005-11-17 22:32:36 +01002670{
Johannes Schindelinfee85722018-04-09 10:32:09 +02002671 struct config_store_data *store = cb;
Junio C Hamanoae9ee412007-05-12 21:49:33 -07002672
Johannes Schindelin5221c312018-04-09 10:32:17 +02002673 if (store->key_seen) {
Johannes Schindelinfee85722018-04-09 10:32:09 +02002674 if (matches(key, value, store)) {
Johannes Schindelin668b9ad2018-04-09 10:32:13 +02002675 if (store->seen_nr == 1 && store->multi_replace == 0) {
Matthieu Moy8262aaa2014-08-07 04:59:12 -07002676 warning(_("%s has multiple values"), key);
Johannes Schindelin10bea152005-11-17 22:32:36 +01002677 }
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002678
Johannes Schindelin668b9ad2018-04-09 10:32:13 +02002679 ALLOC_GROW(store->seen, store->seen_nr + 1,
2680 store->seen_alloc);
Thomas Rast83786fa2013-11-13 11:19:00 +01002681
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002682 store->seen[store->seen_nr] = store->parsed_nr;
Johannes Schindelin668b9ad2018-04-09 10:32:13 +02002683 store->seen_nr++;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002684 }
Johannes Schindelin5221c312018-04-09 10:32:17 +02002685 } else if (store->is_keys_section) {
Junio C Hamanoae9ee412007-05-12 21:49:33 -07002686 /*
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002687 * Do not increment matches yet: this may not be a match, but we
2688 * are in the desired section.
Junio C Hamanoae9ee412007-05-12 21:49:33 -07002689 */
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002690 ALLOC_GROW(store->seen, store->seen_nr + 1, store->seen_alloc);
2691 store->seen[store->seen_nr] = store->parsed_nr;
Johannes Schindelin5221c312018-04-09 10:32:17 +02002692 store->section_seen = 1;
Junio C Hamanoae9ee412007-05-12 21:49:33 -07002693
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002694 if (matches(key, value, store)) {
2695 store->seen_nr++;
2696 store->key_seen = 1;
seanbdf0ef02006-05-06 14:14:02 -04002697 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01002698 }
Johannes Schindelin5221c312018-04-09 10:32:17 +02002699
Johannes Schindelin10bea152005-11-17 22:32:36 +01002700 return 0;
2701}
2702
Alex Riesen64c0d712008-05-12 23:41:04 +02002703static int write_error(const char *filename)
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002704{
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02002705 error(_("failed to write new configuration file %s"), filename);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002706
2707 /* Same error code as "failed to rename". */
2708 return 4;
2709}
2710
Johannes Schindelinfee85722018-04-09 10:32:09 +02002711static struct strbuf store_create_section(const char *key,
2712 const struct config_store_data *store)
Johannes Schindelin10bea152005-11-17 22:32:36 +01002713{
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002714 const char *dot;
Jeff Kingf011a962020-04-10 15:46:07 -04002715 size_t i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002716 struct strbuf sb = STRBUF_INIT;
Linus Torvaldsd14f7762006-05-09 12:24:02 -07002717
Johannes Schindelinfee85722018-04-09 10:32:09 +02002718 dot = memchr(key, '.', store->baselen);
Linus Torvaldsd14f7762006-05-09 12:24:02 -07002719 if (dot) {
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002720 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
Johannes Schindelinfee85722018-04-09 10:32:09 +02002721 for (i = dot - key + 1; i < store->baselen; i++) {
Bryan Donlane5c349b2008-05-04 01:37:52 -04002722 if (key[i] == '"' || key[i] == '\\')
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002723 strbuf_addch(&sb, '\\');
2724 strbuf_addch(&sb, key[i]);
Linus Torvaldsd14f7762006-05-09 12:24:02 -07002725 }
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002726 strbuf_addstr(&sb, "\"]\n");
2727 } else {
Jeff Kingf011a962020-04-10 15:46:07 -04002728 strbuf_addch(&sb, '[');
2729 strbuf_add(&sb, key, store->baselen);
2730 strbuf_addstr(&sb, "]\n");
Linus Torvaldsd14f7762006-05-09 12:24:02 -07002731 }
2732
Sahil Dua5463caa2017-06-18 23:16:31 +02002733 return sb;
2734}
2735
Johannes Schindelinfee85722018-04-09 10:32:09 +02002736static ssize_t write_section(int fd, const char *key,
2737 const struct config_store_data *store)
Sahil Dua5463caa2017-06-18 23:16:31 +02002738{
Johannes Schindelinfee85722018-04-09 10:32:09 +02002739 struct strbuf sb = store_create_section(key, store);
Junio C Hamano3b480452017-10-03 15:42:48 +09002740 ssize_t ret;
Sahil Dua5463caa2017-06-18 23:16:31 +02002741
René Scharfe782c0302017-11-18 11:20:04 +01002742 ret = write_in_full(fd, sb.buf, sb.len);
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002743 strbuf_release(&sb);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002744
Jeff Kingd9bd4cb2017-09-13 13:17:57 -04002745 return ret;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002746}
2747
Johannes Schindelinfee85722018-04-09 10:32:09 +02002748static ssize_t write_pair(int fd, const char *key, const char *value,
2749 const struct config_store_data *store)
Johannes Schindelin10bea152005-11-17 22:32:36 +01002750{
Jeff Kingd9bd4cb2017-09-13 13:17:57 -04002751 int i;
2752 ssize_t ret;
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002753 const char *quote = "";
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002754 struct strbuf sb = STRBUF_INIT;
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -05002755
Jim Meyering6281f392007-12-08 16:48:05 +01002756 /*
2757 * Check to see if the value needs to be surrounded with a dq pair.
2758 * Note that problematic characters are always backslash-quoted; this
2759 * check is about not losing leading or trailing SP and strings that
2760 * follow beginning-of-comment characters (i.e. ';' and '#') by the
2761 * configuration parser.
2762 */
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -05002763 if (value[0] == ' ')
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002764 quote = "\"";
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -05002765 for (i = 0; value[i]; i++)
2766 if (value[i] == ';' || value[i] == '#')
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002767 quote = "\"";
2768 if (i && value[i - 1] == ' ')
2769 quote = "\"";
Johannes Schindelin10bea152005-11-17 22:32:36 +01002770
Jeff King6c7e6962020-04-10 15:44:45 -04002771 strbuf_addf(&sb, "\t%s = %s", key + store->baselen + 1, quote);
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002772
Johannes Schindelin10bea152005-11-17 22:32:36 +01002773 for (i = 0; value[i]; i++)
2774 switch (value[i]) {
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002775 case '\n':
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002776 strbuf_addstr(&sb, "\\n");
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002777 break;
2778 case '\t':
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002779 strbuf_addstr(&sb, "\\t");
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002780 break;
2781 case '"':
2782 case '\\':
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002783 strbuf_addch(&sb, '\\');
Jeff King1cf01a32017-09-21 02:25:41 -04002784 /* fallthrough */
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002785 default:
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002786 strbuf_addch(&sb, value[i]);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002787 break;
2788 }
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002789 strbuf_addf(&sb, "%s\n", quote);
2790
Jeff Kingd9bd4cb2017-09-13 13:17:57 -04002791 ret = write_in_full(fd, sb.buf, sb.len);
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05002792 strbuf_release(&sb);
2793
Jeff Kingd9bd4cb2017-09-13 13:17:57 -04002794 return ret;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002795}
2796
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002797/*
2798 * If we are about to unset the last key(s) in a section, and if there are
2799 * no comments surrounding (or included in) the section, we will want to
2800 * extend begin/end to remove the entire section.
2801 *
2802 * Note: the parameter `seen_ptr` points to the index into the store.seen
2803 * array. * This index may be incremented if a section has more than one
2804 * entry (which all are to be removed).
2805 */
2806static void maybe_remove_section(struct config_store_data *store,
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002807 size_t *begin_offset, size_t *end_offset,
2808 int *seen_ptr)
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002809{
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002810 size_t begin;
2811 int i, seen, section_seen = 0;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002812
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002813 /*
2814 * First, ensure that this is the first key, and that there are no
2815 * comments before the entry nor before the section header.
2816 */
2817 seen = *seen_ptr;
2818 for (i = store->seen[seen]; i > 0; i--) {
2819 enum config_event_t type = store->parsed[i - 1].type;
2820
2821 if (type == CONFIG_EVENT_COMMENT)
2822 /* There is a comment before this entry or section */
2823 return;
2824 if (type == CONFIG_EVENT_ENTRY) {
2825 if (!section_seen)
2826 /* This is not the section's first entry. */
2827 return;
2828 /* We encountered no comment before the section. */
2829 break;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002830 }
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002831 if (type == CONFIG_EVENT_SECTION) {
2832 if (!store->parsed[i - 1].is_keys_section)
2833 break;
2834 section_seen = 1;
2835 }
Frank Lichtenheld7a31cc02008-02-11 01:23:03 +01002836 }
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002837 begin = store->parsed[i].begin;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002838
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02002839 /*
2840 * Next, make sure that we are removing he last key(s) in the section,
2841 * and that there are no comments that are possibly about the current
2842 * section.
2843 */
2844 for (i = store->seen[seen] + 1; i < store->parsed_nr; i++) {
2845 enum config_event_t type = store->parsed[i].type;
2846
2847 if (type == CONFIG_EVENT_COMMENT)
2848 return;
2849 if (type == CONFIG_EVENT_SECTION) {
2850 if (store->parsed[i].is_keys_section)
2851 continue;
2852 break;
2853 }
2854 if (type == CONFIG_EVENT_ENTRY) {
2855 if (++seen < store->seen_nr &&
2856 i == store->seen[seen])
2857 /* We want to remove this entry, too */
2858 continue;
2859 /* There is another entry in this section. */
2860 return;
2861 }
2862 }
2863
2864 /*
2865 * We are really removing the last entry/entries from this section, and
2866 * there are no enclosed or surrounding comments. Remove the entire,
2867 * now-empty section.
2868 */
2869 *seen_ptr = seen;
2870 *begin_offset = begin;
2871 if (i < store->parsed_nr)
2872 *end_offset = store->parsed[i].begin;
2873 else
2874 *end_offset = store->parsed[store->parsed_nr - 1].end;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002875}
2876
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002877int git_config_set_in_file_gently(const char *config_filename,
2878 const char *key, const char *value)
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05302879{
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002880 return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0);
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05302881}
2882
Patrick Steinhardt3d180642016-02-22 12:23:36 +01002883void git_config_set_in_file(const char *config_filename,
2884 const char *key, const char *value)
Johannes Schindelin10bea152005-11-17 22:32:36 +01002885{
Patrick Steinhardt3d180642016-02-22 12:23:36 +01002886 git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01002887}
2888
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002889int git_config_set_gently(const char *key, const char *value)
Johannes Schindelin10bea152005-11-17 22:32:36 +01002890{
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002891 return git_config_set_multivar_gently(key, value, NULL, 0);
Johannes Schindelin10bea152005-11-17 22:32:36 +01002892}
2893
Patrick Steinhardt3d180642016-02-22 12:23:36 +01002894void git_config_set(const char *key, const char *value)
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01002895{
Patrick Steinhardt3d180642016-02-22 12:23:36 +01002896 git_config_set_multivar(key, value, NULL, 0);
Jeff Hostetleree4512e2019-02-22 14:25:01 -08002897
2898 trace2_cmd_set_config(key, value);
Johannes Schindelin10bea152005-11-17 22:32:36 +01002899}
2900
2901/*
2902 * If value==NULL, unset in (remove from) config,
Derrick Stolee247e2f82020-11-25 22:12:50 +00002903 * if value_pattern!=NULL, disregard key/value pairs where value does not match.
2904 * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values
Jeff Kingc1063be2014-08-19 02:20:00 -04002905 * (only add a new one)
Derrick Stolee504ee122020-11-25 22:12:49 +00002906 * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching
2907 * key/values are removed before a single new pair is written. If the
2908 * flag is not present, then replace only the first match.
Johannes Schindelin10bea152005-11-17 22:32:36 +01002909 *
2910 * Returns 0 on success.
2911 *
2912 * This function does this:
2913 *
2914 * - it locks the config file by creating ".git/config.lock"
2915 *
2916 * - it then parses the config using store_aux() as validator to find
2917 * the position on the key/value pair to replace. If it is to be unset,
2918 * it must be found exactly once.
2919 *
2920 * - the config file is mmap()ed and the part before the match (if any) is
2921 * written to the lock file, then the changed part and the rest.
2922 *
2923 * - the config file is removed and the lock file rename()d to it.
2924 *
2925 */
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002926int git_config_set_multivar_in_file_gently(const char *config_filename,
2927 const char *key, const char *value,
Derrick Stolee247e2f82020-11-25 22:12:50 +00002928 const char *value_pattern,
Derrick Stolee504ee122020-11-25 22:12:49 +00002929 unsigned flags)
Johannes Schindelin10bea152005-11-17 22:32:36 +01002930{
Sven Strickroth54d160e2015-08-14 22:21:17 +02002931 int fd = -1, in_fd = -1;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002932 int ret;
Jeff Kingbfffb482017-09-05 08:15:21 -04002933 struct lock_file lock = LOCK_INIT;
Jeff King0a5f5752012-02-16 03:04:05 -05002934 char *filename_buf = NULL;
Jeff King3a1b3122015-05-28 03:54:43 -04002935 char *contents = NULL;
2936 size_t contents_sz;
Johannes Schindelinfee85722018-04-09 10:32:09 +02002937 struct config_store_data store;
2938
2939 memset(&store, 0, sizeof(store));
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002940
Libor Pechacekb09c53a2011-01-30 20:40:41 +01002941 /* parse-key returns negative; flip the sign to feed exit(3) */
2942 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
2943 if (ret)
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002944 goto out_free;
Johannes Schindelinb17e6592005-11-20 21:22:19 +01002945
Derrick Stolee504ee122020-11-25 22:12:49 +00002946 store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002947
Jeff King0a5f5752012-02-16 03:04:05 -05002948 if (!config_filename)
2949 config_filename = filename_buf = git_pathdup("config");
Johannes Schindelin10bea152005-11-17 22:32:36 +01002950
2951 /*
Bradford C. Smith6cbf9732007-07-26 12:55:28 -04002952 * The lock serves a purpose in addition to locking: the new
Johannes Schindelin10bea152005-11-17 22:32:36 +01002953 * contents of .git/config will be written into it.
2954 */
Jeff Kingbfffb482017-09-05 08:15:21 -04002955 fd = hold_lock_file_for_update(&lock, config_filename, 0);
Bradford C. Smith6cbf9732007-07-26 12:55:28 -04002956 if (fd < 0) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02002957 error_errno(_("could not lock config file %s"), config_filename);
Michael J Gruber7a397412011-05-17 17:38:52 +02002958 ret = CONFIG_NO_LOCK;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002959 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002960 }
2961
2962 /*
2963 * If .git/config does not exist yet, write a minimal version.
2964 */
Alex Riesen88fb9582006-01-05 12:43:34 +01002965 in_fd = open(config_filename, O_RDONLY);
2966 if ( in_fd < 0 ) {
Alex Riesen88fb9582006-01-05 12:43:34 +01002967 if ( ENOENT != errno ) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02002968 error_errno(_("opening %s"), config_filename);
Michael J Gruber7a397412011-05-17 17:38:52 +02002969 ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002970 goto out_free;
Alex Riesen88fb9582006-01-05 12:43:34 +01002971 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01002972 /* if nothing to unset, error out */
2973 if (value == NULL) {
Michael J Gruber7a397412011-05-17 17:38:52 +02002974 ret = CONFIG_NOTHING_SET;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002975 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002976 }
2977
Martin Ågrene7347cb2018-05-20 12:42:35 +02002978 free(store.key);
2979 store.key = xstrdup(key);
Johannes Schindelinfee85722018-04-09 10:32:09 +02002980 if (write_section(fd, key, &store) < 0 ||
2981 write_pair(fd, key, value, &store) < 0)
Junio C Hamano93c1e072007-01-11 13:16:26 -08002982 goto write_err_out;
2983 } else {
Alex Riesen88fb9582006-01-05 12:43:34 +01002984 struct stat st;
Jeff King3a1b3122015-05-28 03:54:43 -04002985 size_t copy_begin, copy_end;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002986 int i, new_line = 0;
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02002987 struct config_options opts;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002988
Derrick Stolee247e2f82020-11-25 22:12:50 +00002989 if (value_pattern == NULL)
2990 store.value_pattern = NULL;
2991 else if (value_pattern == CONFIG_REGEX_NONE)
2992 store.value_pattern = CONFIG_REGEX_NONE;
Derrick Stoleec90702a2020-11-25 22:12:54 +00002993 else if (flags & CONFIG_FLAGS_FIXED_VALUE)
2994 store.fixed_value = value_pattern;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002995 else {
Derrick Stolee247e2f82020-11-25 22:12:50 +00002996 if (value_pattern[0] == '!') {
Johannes Schindelinf98d8632005-11-20 13:24:18 +01002997 store.do_not_match = 1;
Derrick Stolee247e2f82020-11-25 22:12:50 +00002998 value_pattern++;
Johannes Schindelinf98d8632005-11-20 13:24:18 +01002999 } else
3000 store.do_not_match = 0;
3001
Derrick Stolee247e2f82020-11-25 22:12:50 +00003002 store.value_pattern = (regex_t*)xmalloc(sizeof(regex_t));
3003 if (regcomp(store.value_pattern, value_pattern,
Johannes Schindelin10bea152005-11-17 22:32:36 +01003004 REG_EXTENDED)) {
Derrick Stolee247e2f82020-11-25 22:12:50 +00003005 error(_("invalid pattern: %s"), value_pattern);
3006 FREE_AND_NULL(store.value_pattern);
Michael J Gruber7a397412011-05-17 17:38:52 +02003007 ret = CONFIG_INVALID_PATTERN;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05003008 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003009 }
3010 }
3011
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003012 ALLOC_GROW(store.parsed, 1, store.parsed_alloc);
3013 store.parsed[0].end = 0;
3014
3015 memset(&opts, 0, sizeof(opts));
3016 opts.event_fn = store_aux_event;
3017 opts.event_fn_data = &store;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003018
3019 /*
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003020 * After this, store.parsed will contain offsets of all the
3021 * parsed elements, and store.seen will contain a list of
3022 * matches, as indices into store.parsed.
3023 *
Johannes Schindelin10bea152005-11-17 22:32:36 +01003024 * As a side effect, we make sure to transform only a valid
3025 * existing config file.
3026 */
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003027 if (git_config_from_file_with_options(store_aux,
3028 config_filename,
3029 &store, &opts)) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003030 error(_("invalid config file %s"), config_filename);
Michael J Gruber7a397412011-05-17 17:38:52 +02003031 ret = CONFIG_INVALID_FILE;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05003032 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003033 }
3034
Johannes Schindelin4ddba792005-11-20 06:52:22 +01003035 /* if nothing to unset, or too many matches, error out */
Johannes Schindelin668b9ad2018-04-09 10:32:13 +02003036 if ((store.seen_nr == 0 && value == NULL) ||
Derrick Stolee504ee122020-11-25 22:12:49 +00003037 (store.seen_nr > 1 && !store.multi_replace)) {
Michael J Gruber7a397412011-05-17 17:38:52 +02003038 ret = CONFIG_NOTHING_SET;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05003039 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003040 }
3041
Nguyễn Thái Ngọc Duy29647d72016-12-19 16:21:55 +07003042 if (fstat(in_fd, &st) == -1) {
3043 error_errno(_("fstat on %s failed"), config_filename);
3044 ret = CONFIG_INVALID_FILE;
3045 goto out_free;
3046 }
3047
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05003048 contents_sz = xsize_t(st.st_size);
Jeff King15708562015-05-28 03:56:15 -04003049 contents = xmmap_gently(NULL, contents_sz, PROT_READ,
3050 MAP_PRIVATE, in_fd, 0);
3051 if (contents == MAP_FAILED) {
Jeff King0e8771f2015-05-28 04:03:01 -04003052 if (errno == ENODEV && S_ISDIR(st.st_mode))
3053 errno = EISDIR;
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003054 error_errno(_("unable to mmap '%s'"), config_filename);
Jeff King15708562015-05-28 03:56:15 -04003055 ret = CONFIG_INVALID_FILE;
3056 contents = NULL;
3057 goto out_free;
3058 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01003059 close(in_fd);
Sven Strickroth54d160e2015-08-14 22:21:17 +02003060 in_fd = -1;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003061
Jeff Kingbfffb482017-09-05 08:15:21 -04003062 if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003063 error_errno(_("chmod on %s failed"), get_lock_file_path(&lock));
Eric Wongdaa22c62014-05-06 00:17:14 +00003064 ret = CONFIG_NO_WRITE;
3065 goto out_free;
3066 }
3067
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003068 if (store.seen_nr == 0) {
3069 if (!store.seen_alloc) {
3070 /* Did not see key nor section */
3071 ALLOC_GROW(store.seen, 1, store.seen_alloc);
3072 store.seen[0] = store.parsed_nr
3073 - !!store.parsed_nr;
3074 }
Johannes Schindelin668b9ad2018-04-09 10:32:13 +02003075 store.seen_nr = 1;
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003076 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01003077
Johannes Schindelin668b9ad2018-04-09 10:32:13 +02003078 for (i = 0, copy_begin = 0; i < store.seen_nr; i++) {
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003079 size_t replace_end;
3080 int j = store.seen[i];
3081
Johannes Schindelin46fc89c2018-04-03 18:28:14 +02003082 new_line = 0;
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003083 if (!store.key_seen) {
Johannes Schindelinc71d8bb2018-04-09 10:32:29 +02003084 copy_end = store.parsed[j].end;
3085 /* include '\n' when copying section header */
3086 if (copy_end > 0 && copy_end < contents_sz &&
3087 contents[copy_end - 1] != '\n' &&
3088 contents[copy_end] == '\n')
3089 copy_end++;
3090 replace_end = copy_end;
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003091 } else {
3092 replace_end = store.parsed[j].end;
3093 copy_end = store.parsed[j].begin;
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02003094 if (!value)
Jeff Kinga263ea82019-01-24 08:12:32 -05003095 maybe_remove_section(&store,
Johannes Schindelin22aedfc2018-04-09 10:32:24 +02003096 &copy_end,
3097 &replace_end, &i);
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003098 /*
3099 * Swallow preceding white-space on the same
3100 * line.
3101 */
3102 while (copy_end > 0 ) {
3103 char c = contents[copy_end - 1];
3104
3105 if (isspace(c) && c != '\n')
3106 copy_end--;
3107 else
3108 break;
3109 }
3110 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01003111
Jeff King02e5ba42008-01-01 01:17:34 -05003112 if (copy_end > 0 && contents[copy_end-1] != '\n')
3113 new_line = 1;
3114
Johannes Schindelin4ddba792005-11-20 06:52:22 +01003115 /* write the first part of the config */
3116 if (copy_end > copy_begin) {
Junio C Hamano93c1e072007-01-11 13:16:26 -08003117 if (write_in_full(fd, contents + copy_begin,
Jeff Kingefacf602017-09-13 14:15:16 -04003118 copy_end - copy_begin) < 0)
Junio C Hamano93c1e072007-01-11 13:16:26 -08003119 goto write_err_out;
3120 if (new_line &&
Jeff King06f46f22017-09-13 13:16:03 -04003121 write_str_in_full(fd, "\n") < 0)
Junio C Hamano93c1e072007-01-11 13:16:26 -08003122 goto write_err_out;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01003123 }
Johannes Schindelin6ae996f2018-04-09 10:32:20 +02003124 copy_begin = replace_end;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01003125 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01003126
3127 /* write the pair (value == NULL means unset) */
3128 if (value != NULL) {
Johannes Schindelin5221c312018-04-09 10:32:17 +02003129 if (!store.section_seen) {
Johannes Schindelinfee85722018-04-09 10:32:09 +02003130 if (write_section(fd, key, &store) < 0)
Junio C Hamano93c1e072007-01-11 13:16:26 -08003131 goto write_err_out;
Andy Whitcroft480c9e52007-01-08 15:58:38 +00003132 }
Johannes Schindelinfee85722018-04-09 10:32:09 +02003133 if (write_pair(fd, key, value, &store) < 0)
Junio C Hamano93c1e072007-01-11 13:16:26 -08003134 goto write_err_out;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003135 }
3136
3137 /* write the rest of the config */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05003138 if (copy_begin < contents_sz)
Junio C Hamano93c1e072007-01-11 13:16:26 -08003139 if (write_in_full(fd, contents + copy_begin,
Jeff Kingefacf602017-09-13 14:15:16 -04003140 contents_sz - copy_begin) < 0)
Junio C Hamano93c1e072007-01-11 13:16:26 -08003141 goto write_err_out;
Karsten Blees7a645922015-06-30 16:34:13 +02003142
3143 munmap(contents, contents_sz);
3144 contents = NULL;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003145 }
3146
Jeff Kingbfffb482017-09-05 08:15:21 -04003147 if (commit_lock_file(&lock) < 0) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003148 error_errno(_("could not write config file %s"), config_filename);
Michael J Gruber7a397412011-05-17 17:38:52 +02003149 ret = CONFIG_NO_WRITE;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05003150 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01003151 }
3152
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05003153 ret = 0;
3154
Tanay Abhra3c8687a2014-07-28 03:10:38 -07003155 /* Invalidate the config cache */
3156 git_config_clear();
3157
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05003158out_free:
Jeff Kingbfffb482017-09-05 08:15:21 -04003159 rollback_lock_file(&lock);
Jeff King0a5f5752012-02-16 03:04:05 -05003160 free(filename_buf);
Jeff King3a1b3122015-05-28 03:54:43 -04003161 if (contents)
3162 munmap(contents, contents_sz);
Sven Strickroth54d160e2015-08-14 22:21:17 +02003163 if (in_fd >= 0)
3164 close(in_fd);
Martin Ågren2a00e592018-05-20 12:42:33 +02003165 config_store_data_clear(&store);
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05003166 return ret;
Junio C Hamano93c1e072007-01-11 13:16:26 -08003167
3168write_err_out:
Jeff Kingbfffb482017-09-05 08:15:21 -04003169 ret = write_error(get_lock_file_path(&lock));
Junio C Hamano93c1e072007-01-11 13:16:26 -08003170 goto out_free;
3171
Johannes Schindelin10bea152005-11-17 22:32:36 +01003172}
3173
Patrick Steinhardt3d180642016-02-22 12:23:36 +01003174void git_config_set_multivar_in_file(const char *config_filename,
3175 const char *key, const char *value,
Derrick Stolee247e2f82020-11-25 22:12:50 +00003176 const char *value_pattern, unsigned flags)
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05303177{
Jeff King1cae4282016-04-09 13:43:54 -04003178 if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
Derrick Stolee247e2f82020-11-25 22:12:50 +00003179 value_pattern, flags))
Jeff King1cae4282016-04-09 13:43:54 -04003180 return;
3181 if (value)
Jeff King8c3ca352016-04-09 13:42:31 -04003182 die(_("could not set '%s' to '%s'"), key, value);
Jeff King1cae4282016-04-09 13:43:54 -04003183 else
3184 die(_("could not unset '%s'"), key);
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01003185}
3186
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01003187int git_config_set_multivar_gently(const char *key, const char *value,
Derrick Stolee247e2f82020-11-25 22:12:50 +00003188 const char *value_pattern, unsigned flags)
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05303189{
Derrick Stolee247e2f82020-11-25 22:12:50 +00003190 return git_config_set_multivar_in_file_gently(NULL, key, value, value_pattern,
Derrick Stolee504ee122020-11-25 22:12:49 +00003191 flags);
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05303192}
3193
Patrick Steinhardt3d180642016-02-22 12:23:36 +01003194void git_config_set_multivar(const char *key, const char *value,
Derrick Stolee247e2f82020-11-25 22:12:50 +00003195 const char *value_pattern, unsigned flags)
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01003196{
Derrick Stolee247e2f82020-11-25 22:12:50 +00003197 git_config_set_multivar_in_file(NULL, key, value, value_pattern,
Derrick Stolee504ee122020-11-25 22:12:49 +00003198 flags);
Paolo Bonzini118f8b22007-03-02 21:53:33 +01003199}
3200
3201static int section_name_match (const char *buf, const char *name)
3202{
3203 int i = 0, j = 0, dot = 0;
3204 if (buf[i] != '[')
3205 return 0;
3206 for (i = 1; buf[i] && buf[i] != ']'; i++) {
3207 if (!dot && isspace(buf[i])) {
3208 dot = 1;
Alex Vandivera4c0d462009-07-24 17:21:43 -04003209 if (name[j++] != '.')
3210 break;
3211 for (i++; isspace(buf[i]); i++)
3212 ; /* do nothing */
3213 if (buf[i] != '"')
3214 break;
3215 continue;
3216 }
3217 if (buf[i] == '\\' && dot)
3218 i++;
3219 else if (buf[i] == '"' && dot) {
Paolo Bonzini118f8b22007-03-02 21:53:33 +01003220 for (i++; isspace(buf[i]); i++)
3221 ; /* do_nothing */
3222 break;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003223 }
3224 if (buf[i] != name[j++])
Paolo Bonzini118f8b22007-03-02 21:53:33 +01003225 break;
Junio C Hamanofc1905b2006-12-19 21:55:27 -08003226 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003227 if (buf[i] == ']' && name[j] == 0) {
3228 /*
3229 * We match, now just find the right length offset by
Ramsay Jones924aaf32011-06-16 21:24:51 +01003230 * gobbling up any whitespace after it, as well
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003231 */
Daniel Barkalowdc871832008-06-30 03:37:47 -04003232 i++;
3233 for (; buf[i] && isspace(buf[i]); i++)
3234 ; /* do nothing */
Alex Riesena4f34cb2008-10-27 11:22:09 +01003235 return i;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003236 }
Junio C Hamanofc1905b2006-12-19 21:55:27 -08003237 return 0;
Alex Riesen64c0d712008-05-12 23:41:04 +02003238}
Junio C Hamanofc1905b2006-12-19 21:55:27 -08003239
Jeff King94a35b12012-04-25 21:47:14 -04003240static int section_name_is_ok(const char *name)
3241{
3242 /* Empty section names are bogus. */
3243 if (!*name)
3244 return 0;
3245
3246 /*
3247 * Before a dot, we must be alphanumeric or dash. After the first dot,
3248 * anything goes, so we can stop checking.
3249 */
3250 for (; *name && *name != '.'; name++)
3251 if (*name != '-' && !isalnum(*name))
3252 return 0;
3253 return 1;
3254}
3255
Junio C Hamanofc1905b2006-12-19 21:55:27 -08003256/* if new_name == NULL, the section is removed instead */
Sahil Dua52d59cc2017-06-18 23:19:16 +02003257static int git_config_copy_or_rename_section_in_file(const char *config_filename,
Johannes Schindelinfee85722018-04-09 10:32:09 +02003258 const char *old_name,
3259 const char *new_name, int copy)
Geert Bosch01ebb9d2007-04-05 10:20:55 -04003260{
Junio C Hamanofc1905b2006-12-19 21:55:27 -08003261 int ret = 0, remove = 0;
Jeff King42bd39b2012-02-16 03:04:25 -05003262 char *filename_buf = NULL;
Martin Ågren837e34e2017-10-05 22:32:04 +02003263 struct lock_file lock = LOCK_INIT;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003264 int out_fd;
Andy Whitcroft480c9e52007-01-08 15:58:38 +00003265 char buf[1024];
Johannes Schindelin4db7dbd2017-05-04 15:55:41 +02003266 FILE *config_file = NULL;
Eric Wongdaa22c62014-05-06 00:17:14 +00003267 struct stat st;
Sahil Dua52d59cc2017-06-18 23:19:16 +02003268 struct strbuf copystr = STRBUF_INIT;
Johannes Schindelinfee85722018-04-09 10:32:09 +02003269 struct config_store_data store;
3270
3271 memset(&store, 0, sizeof(store));
Linus Torvalds17712992005-10-10 16:31:08 -07003272
Jeff King94a35b12012-04-25 21:47:14 -04003273 if (new_name && !section_name_is_ok(new_name)) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003274 ret = error(_("invalid section name: %s"), new_name);
Nguyễn Thái Ngọc Duyc06fa622016-12-20 16:48:36 +07003275 goto out_no_rollback;
Jeff King94a35b12012-04-25 21:47:14 -04003276 }
3277
Jeff King42bd39b2012-02-16 03:04:25 -05003278 if (!config_filename)
3279 config_filename = filename_buf = git_pathdup("config");
3280
Martin Ågren837e34e2017-10-05 22:32:04 +02003281 out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003282 if (out_fd < 0) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003283 ret = error(_("could not lock config file %s"), config_filename);
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003284 goto out;
3285 }
3286
3287 if (!(config_file = fopen(config_filename, "rb"))) {
Nguyễn Thái Ngọc Duy11dc1fc2017-05-03 17:16:49 +07003288 ret = warn_on_fopen_errors(config_filename);
3289 if (ret)
3290 goto out;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003291 /* no config file means nothing to rename, no error */
Nguyễn Thái Ngọc Duy6e45b432016-12-20 16:48:35 +07003292 goto commit_and_out;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003293 }
3294
Nguyễn Thái Ngọc Duy29647d72016-12-19 16:21:55 +07003295 if (fstat(fileno(config_file), &st) == -1) {
3296 ret = error_errno(_("fstat on %s failed"), config_filename);
3297 goto out;
3298 }
Eric Wongdaa22c62014-05-06 00:17:14 +00003299
Martin Ågren837e34e2017-10-05 22:32:04 +02003300 if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003301 ret = error_errno(_("chmod on %s failed"),
Martin Ågren837e34e2017-10-05 22:32:04 +02003302 get_lock_file_path(&lock));
Eric Wongdaa22c62014-05-06 00:17:14 +00003303 goto out;
3304 }
3305
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003306 while (fgets(buf, sizeof(buf), config_file)) {
Jeff Kingaec0bba2020-08-04 03:43:53 -04003307 unsigned i;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003308 int length;
Sahil Dua52d59cc2017-06-18 23:19:16 +02003309 int is_section = 0;
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04003310 char *output = buf;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003311 for (i = 0; buf[i] && isspace(buf[i]); i++)
3312 ; /* do nothing */
3313 if (buf[i] == '[') {
3314 /* it's a section */
Sahil Dua52d59cc2017-06-18 23:19:16 +02003315 int offset;
3316 is_section = 1;
3317
3318 /*
3319 * When encountering a new section under -c we
3320 * need to flush out any section we're already
3321 * coping and begin anew. There might be
3322 * multiple [branch "$name"] sections.
3323 */
3324 if (copystr.len > 0) {
Phillip Woodc5e3bc62017-11-15 12:40:43 +00003325 if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
Junio C Hamano0b646bc2017-11-06 13:11:21 +09003326 ret = write_error(get_lock_file_path(&lock));
Sahil Dua52d59cc2017-06-18 23:19:16 +02003327 goto out;
3328 }
3329 strbuf_reset(&copystr);
3330 }
3331
3332 offset = section_name_match(&buf[i], old_name);
Alex Vandivera4c0d462009-07-24 17:21:43 -04003333 if (offset > 0) {
Paolo Bonzini118f8b22007-03-02 21:53:33 +01003334 ret++;
3335 if (new_name == NULL) {
3336 remove = 1;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003337 continue;
3338 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003339 store.baselen = strlen(new_name);
Sahil Dua52d59cc2017-06-18 23:19:16 +02003340 if (!copy) {
Johannes Schindelinfee85722018-04-09 10:32:09 +02003341 if (write_section(out_fd, new_name, &store) < 0) {
Junio C Hamano0b646bc2017-11-06 13:11:21 +09003342 ret = write_error(get_lock_file_path(&lock));
Sahil Dua52d59cc2017-06-18 23:19:16 +02003343 goto out;
3344 }
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04003345 /*
Sahil Dua52d59cc2017-06-18 23:19:16 +02003346 * We wrote out the new section, with
3347 * a newline, now skip the old
3348 * section's length
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04003349 */
Sahil Dua52d59cc2017-06-18 23:19:16 +02003350 output += offset + i;
3351 if (strlen(output) > 0) {
3352 /*
3353 * More content means there's
3354 * a declaration to put on the
3355 * next line; indent with a
3356 * tab
3357 */
3358 output -= 1;
3359 output[0] = '\t';
3360 }
3361 } else {
Johannes Schindelinfee85722018-04-09 10:32:09 +02003362 copystr = store_create_section(new_name, &store);
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04003363 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003364 }
Paolo Bonzini118f8b22007-03-02 21:53:33 +01003365 remove = 0;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003366 }
Paolo Bonzini118f8b22007-03-02 21:53:33 +01003367 if (remove)
3368 continue;
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04003369 length = strlen(output);
Sahil Dua52d59cc2017-06-18 23:19:16 +02003370
3371 if (!is_section && copystr.len > 0) {
3372 strbuf_add(&copystr, output, length);
3373 }
3374
Jeff King06f46f22017-09-13 13:16:03 -04003375 if (write_in_full(out_fd, output, length) < 0) {
Martin Ågren837e34e2017-10-05 22:32:04 +02003376 ret = write_error(get_lock_file_path(&lock));
Andy Whitcroft480c9e52007-01-08 15:58:38 +00003377 goto out;
3378 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003379 }
Sahil Dua52d59cc2017-06-18 23:19:16 +02003380
3381 /*
3382 * Copy a trailing section at the end of the config, won't be
3383 * flushed by the usual "flush because we have a new section
3384 * logic in the loop above.
3385 */
3386 if (copystr.len > 0) {
Phillip Woodc5e3bc62017-11-15 12:40:43 +00003387 if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
Junio C Hamano0b646bc2017-11-06 13:11:21 +09003388 ret = write_error(get_lock_file_path(&lock));
Sahil Dua52d59cc2017-06-18 23:19:16 +02003389 goto out;
3390 }
3391 strbuf_reset(&copystr);
3392 }
3393
Junio C Hamanofc1905b2006-12-19 21:55:27 -08003394 fclose(config_file);
Johannes Schindelin4db7dbd2017-05-04 15:55:41 +02003395 config_file = NULL;
Nguyễn Thái Ngọc Duy6e45b432016-12-20 16:48:35 +07003396commit_and_out:
Martin Ågren837e34e2017-10-05 22:32:04 +02003397 if (commit_lock_file(&lock) < 0)
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003398 ret = error_errno(_("could not write config file %s"),
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07003399 config_filename);
Pavan Kumar Sunkara8b590072011-07-17 03:25:52 +05303400out:
Johannes Schindelin4db7dbd2017-05-04 15:55:41 +02003401 if (config_file)
3402 fclose(config_file);
Martin Ågren837e34e2017-10-05 22:32:04 +02003403 rollback_lock_file(&lock);
Nguyễn Thái Ngọc Duyc06fa622016-12-20 16:48:36 +07003404out_no_rollback:
Jeff King42bd39b2012-02-16 03:04:25 -05003405 free(filename_buf);
Martin Ågren2a00e592018-05-20 12:42:33 +02003406 config_store_data_clear(&store);
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01003407 return ret;
3408}
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08003409
Sahil Dua52d59cc2017-06-18 23:19:16 +02003410int git_config_rename_section_in_file(const char *config_filename,
3411 const char *old_name, const char *new_name)
3412{
3413 return git_config_copy_or_rename_section_in_file(config_filename,
3414 old_name, new_name, 0);
3415}
3416
Jeff King42bd39b2012-02-16 03:04:25 -05003417int git_config_rename_section(const char *old_name, const char *new_name)
3418{
Jeff King4a7bb5b2012-02-16 03:09:32 -05003419 return git_config_rename_section_in_file(NULL, old_name, new_name);
Jeff King42bd39b2012-02-16 03:04:25 -05003420}
3421
Sahil Dua52d59cc2017-06-18 23:19:16 +02003422int git_config_copy_section_in_file(const char *config_filename,
3423 const char *old_name, const char *new_name)
3424{
3425 return git_config_copy_or_rename_section_in_file(config_filename,
3426 old_name, new_name, 1);
3427}
3428
3429int git_config_copy_section(const char *old_name, const char *new_name)
3430{
3431 return git_config_copy_section_in_file(NULL, old_name, new_name);
3432}
3433
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08003434/*
3435 * Call this to report error for your variable that should not
3436 * get a boolean value (i.e. "[my] var" means "true").
3437 */
Jeff Kinga469a102012-12-15 12:42:10 -05003438#undef config_error_nonbool
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08003439int config_error_nonbool(const char *var)
3440{
Nguyễn Thái Ngọc Duya769bfc2018-07-21 09:49:27 +02003441 return error(_("missing value for '%s'"), var);
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08003442}
Jeff King1b86bbb2013-01-23 01:23:05 -05003443
3444int parse_config_key(const char *var,
3445 const char *section,
Jeff Kingf5914f42020-04-10 15:44:28 -04003446 const char **subsection, size_t *subsection_len,
Jeff King1b86bbb2013-01-23 01:23:05 -05003447 const char **key)
3448{
Jeff King1b86bbb2013-01-23 01:23:05 -05003449 const char *dot;
3450
3451 /* Does it start with "section." ? */
Jeff Kinge3394fd2017-02-24 16:07:19 -05003452 if (!skip_prefix(var, section, &var) || *var != '.')
Jeff King1b86bbb2013-01-23 01:23:05 -05003453 return -1;
3454
3455 /*
3456 * Find the key; we don't know yet if we have a subsection, but we must
3457 * parse backwards from the end, since the subsection may have dots in
3458 * it, too.
3459 */
3460 dot = strrchr(var, '.');
3461 *key = dot + 1;
3462
3463 /* Did we have a subsection at all? */
Jeff Kinge3394fd2017-02-24 16:07:19 -05003464 if (dot == var) {
Jeff King48f8d9f2017-02-24 16:08:02 -05003465 if (subsection) {
3466 *subsection = NULL;
3467 *subsection_len = 0;
3468 }
Jeff King1b86bbb2013-01-23 01:23:05 -05003469 }
3470 else {
Jeff King48f8d9f2017-02-24 16:08:02 -05003471 if (!subsection)
3472 return -1;
Jeff Kinge3394fd2017-02-24 16:07:19 -05003473 *subsection = var + 1;
Jeff King1b86bbb2013-01-23 01:23:05 -05003474 *subsection_len = dot - *subsection;
3475 }
3476
3477 return 0;
3478}
Lars Schneider473166b2016-02-19 10:16:01 +01003479
3480const char *current_config_origin_type(void)
3481{
Vasco Almeida1b8132d2016-07-28 13:14:03 +00003482 int type;
Jeff King0d44a2d2016-05-26 20:32:23 -04003483 if (current_config_kvi)
3484 type = current_config_kvi->origin_type;
3485 else if(cf)
3486 type = cf->origin_type;
3487 else
Johannes Schindelin033abf92018-05-02 11:38:39 +02003488 BUG("current_config_origin_type called outside config callback");
Vasco Almeida1b8132d2016-07-28 13:14:03 +00003489
3490 switch (type) {
3491 case CONFIG_ORIGIN_BLOB:
3492 return "blob";
3493 case CONFIG_ORIGIN_FILE:
3494 return "file";
3495 case CONFIG_ORIGIN_STDIN:
3496 return "standard input";
3497 case CONFIG_ORIGIN_SUBMODULE_BLOB:
3498 return "submodule-blob";
3499 case CONFIG_ORIGIN_CMDLINE:
3500 return "command line";
3501 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02003502 BUG("unknown config origin type");
Vasco Almeida1b8132d2016-07-28 13:14:03 +00003503 }
Lars Schneider473166b2016-02-19 10:16:01 +01003504}
3505
Matthew Rogersa5cb4202020-02-10 00:30:53 +00003506const char *config_scope_name(enum config_scope scope)
3507{
3508 switch (scope) {
3509 case CONFIG_SCOPE_SYSTEM:
3510 return "system";
3511 case CONFIG_SCOPE_GLOBAL:
3512 return "global";
Matthew Rogers6dc905d2020-02-10 00:30:54 +00003513 case CONFIG_SCOPE_LOCAL:
3514 return "local";
3515 case CONFIG_SCOPE_WORKTREE:
3516 return "worktree";
Matthew Rogers6766e412020-02-10 00:30:55 +00003517 case CONFIG_SCOPE_COMMAND:
3518 return "command";
Matthew Rogers9a83d082020-02-10 00:30:58 +00003519 case CONFIG_SCOPE_SUBMODULE:
3520 return "submodule";
Matthew Rogersa5cb4202020-02-10 00:30:53 +00003521 default:
3522 return "unknown";
3523 }
3524}
3525
Lars Schneider473166b2016-02-19 10:16:01 +01003526const char *current_config_name(void)
3527{
Jeff King0d44a2d2016-05-26 20:32:23 -04003528 const char *name;
3529 if (current_config_kvi)
3530 name = current_config_kvi->filename;
3531 else if (cf)
3532 name = cf->name;
3533 else
Johannes Schindelin033abf92018-05-02 11:38:39 +02003534 BUG("current_config_name called outside config callback");
Jeff King0d44a2d2016-05-26 20:32:23 -04003535 return name ? name : "";
Lars Schneider473166b2016-02-19 10:16:01 +01003536}
Jeff King9acc5912016-05-18 18:44:23 -04003537
3538enum config_scope current_config_scope(void)
3539{
3540 if (current_config_kvi)
3541 return current_config_kvi->scope;
3542 else
3543 return current_parsing_scope;
Linus Torvalds17712992005-10-10 16:31:08 -07003544}
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02003545
Bert Wesargf2a23272020-01-27 08:04:31 +01003546int current_config_line(void)
3547{
3548 if (current_config_kvi)
3549 return current_config_kvi->linenr;
3550 else
3551 return cf->linenr;
3552}
3553
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +02003554int lookup_config(const char **mapping, int nr_mapping, const char *var)
3555{
3556 int i;
3557
3558 for (i = 0; i < nr_mapping; i++) {
3559 const char *name = mapping[i];
3560
3561 if (name && !strcasecmp(var, name))
3562 return i;
3563 }
3564 return -1;
3565}