blob: bea937e4ecf8417a910882c1aafb263e792f2e1c [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"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02009#include "lockfile.h"
Johannes Sixt7f0e39f2007-11-13 21:05:06 +010010#include "exec_cmd.h"
Alex Riesen572e4f62010-03-26 23:56:01 +010011#include "strbuf.h"
Jeff King2b64fc82010-08-23 15:16:00 -040012#include "quote.h"
Tanay Abhra3c8687a2014-07-28 03:10:38 -070013#include "hashmap.h"
14#include "string-list.h"
Junio C Hamano599446d2015-04-16 10:47:45 -070015#include "utf8.h"
Linus Torvalds17712992005-10-10 16:31:08 -070016
Heiko Voigt4d8dd142013-07-12 00:44:39 +020017struct config_source {
18 struct config_source *prev;
19 union {
20 FILE *file;
Heiko Voigt1bc88812013-07-12 00:46:47 +020021 struct config_buf {
22 const char *buf;
23 size_t len;
24 size_t pos;
25 } buf;
Heiko Voigt4d8dd142013-07-12 00:44:39 +020026 } u;
Lars Schneider473166b2016-02-19 10:16:01 +010027 const char *origin_type;
Ramsay Jones924aaf32011-06-16 21:24:51 +010028 const char *name;
Jeff Kingd14d4242014-02-19 00:58:52 +020029 const char *path;
Heiko Voigtb2dc0942013-07-12 00:48:30 +020030 int die_on_error;
Ramsay Jones924aaf32011-06-16 21:24:51 +010031 int linenr;
32 int eof;
33 struct strbuf value;
Ben Walton0971e992012-09-30 20:44:36 +010034 struct strbuf var;
Ramsay Jones924aaf32011-06-16 21:24:51 +010035
Jeff King49d6cfa2013-08-26 17:57:18 -040036 int (*do_fgetc)(struct config_source *c);
37 int (*do_ungetc)(int c, struct config_source *conf);
38 long (*do_ftell)(struct config_source *c);
Heiko Voigt4d8dd142013-07-12 00:44:39 +020039};
40
Jeff King0d44a2d2016-05-26 20:32:23 -040041/*
42 * These variables record the "current" config source, which
43 * can be accessed by parsing callbacks.
44 *
45 * The "cf" variable will be non-NULL only when we are actually parsing a real
46 * config source (file, blob, cmdline, etc).
47 *
48 * The "current_config_kvi" variable will be non-NULL only when we are feeding
49 * cached config from a configset into a callback.
50 *
51 * They should generally never be non-NULL at the same time. If they are both
52 * NULL, then we aren't parsing anything (and depending on the function looking
53 * at the variables, it's either a bug for it to be called in the first place,
54 * or it's a function which can be reused for non-config purposes, and should
55 * fall back to some sane behavior).
56 */
Heiko Voigt4d8dd142013-07-12 00:44:39 +020057static struct config_source *cf;
Jeff King0d44a2d2016-05-26 20:32:23 -040058static struct key_value_info *current_config_kvi;
Ramsay Jones924aaf32011-06-16 21:24:51 +010059
Jeff King9acc5912016-05-18 18:44:23 -040060/*
61 * Similar to the variables above, this gives access to the "scope" of the
62 * current value (repo, global, etc). For cached values, it can be found via
63 * the current_config_kvi as above. During parsing, the current value can be
64 * found in this variable. It's not part of "cf" because it transcends a single
65 * file (i.e., a file included from .git/config is still in "repo" scope).
66 */
67static enum config_scope current_parsing_scope;
Jeff King02e5ba42008-01-01 01:17:34 -050068
Dana How960ccca2007-05-09 13:56:50 -070069static int zlib_compression_seen;
70
Tanay Abhra3c8687a2014-07-28 03:10:38 -070071/*
72 * Default config_set that contains key-value pairs from the usual set of config
73 * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
74 * config file and the global /etc/gitconfig)
75 */
76static struct config_set the_config_set;
77
Heiko Voigt4d8dd142013-07-12 00:44:39 +020078static int config_file_fgetc(struct config_source *conf)
79{
Jeff King260d4082015-04-16 04:51:18 -040080 return getc_unlocked(conf->u.file);
Heiko Voigt4d8dd142013-07-12 00:44:39 +020081}
82
83static int config_file_ungetc(int c, struct config_source *conf)
84{
85 return ungetc(c, conf->u.file);
86}
87
88static long config_file_ftell(struct config_source *conf)
89{
90 return ftell(conf->u.file);
91}
92
Heiko Voigt1bc88812013-07-12 00:46:47 +020093
94static int config_buf_fgetc(struct config_source *conf)
95{
96 if (conf->u.buf.pos < conf->u.buf.len)
97 return conf->u.buf.buf[conf->u.buf.pos++];
98
99 return EOF;
100}
101
102static int config_buf_ungetc(int c, struct config_source *conf)
103{
Jeff King1d0655c2015-02-05 16:00:24 -0500104 if (conf->u.buf.pos > 0) {
105 conf->u.buf.pos--;
106 if (conf->u.buf.buf[conf->u.buf.pos] != c)
107 die("BUG: config_buf can only ungetc the same character");
108 return c;
109 }
Heiko Voigt1bc88812013-07-12 00:46:47 +0200110
111 return EOF;
112}
113
114static long config_buf_ftell(struct config_source *conf)
115{
116 return conf->u.buf.pos;
117}
118
Jeff King9b25a0b2012-02-06 04:54:04 -0500119#define MAX_INCLUDE_DEPTH 10
120static const char include_depth_advice[] =
121"exceeded maximum include depth (%d) while including\n"
122" %s\n"
123"from\n"
124" %s\n"
125"Do you have circular includes?";
126static int handle_path_include(const char *path, struct config_include_data *inc)
127{
128 int ret = 0;
129 struct strbuf buf = STRBUF_INIT;
Jeff King67beb602014-01-27 20:37:30 -0500130 char *expanded;
Jeff King4c0a89f2012-04-25 08:00:36 -0400131
Jeff King67beb602014-01-27 20:37:30 -0500132 if (!path)
133 return config_error_nonbool("include.path");
134
135 expanded = expand_user_path(path);
Jeff King4c0a89f2012-04-25 08:00:36 -0400136 if (!expanded)
Jeff King8c3ca352016-04-09 13:42:31 -0400137 return error("could not expand include path '%s'", path);
Jeff King4c0a89f2012-04-25 08:00:36 -0400138 path = expanded;
Jeff King9b25a0b2012-02-06 04:54:04 -0500139
140 /*
141 * Use an absolute path as-is, but interpret relative paths
142 * based on the including config file.
143 */
144 if (!is_absolute_path(path)) {
145 char *slash;
146
Jeff Kingd14d4242014-02-19 00:58:52 +0200147 if (!cf || !cf->path)
Jeff King9b25a0b2012-02-06 04:54:04 -0500148 return error("relative config includes must come from files");
149
Jeff Kingd14d4242014-02-19 00:58:52 +0200150 slash = find_last_dir_sep(cf->path);
Jeff King9b25a0b2012-02-06 04:54:04 -0500151 if (slash)
Jeff Kingd14d4242014-02-19 00:58:52 +0200152 strbuf_add(&buf, cf->path, slash - cf->path + 1);
Jeff King9b25a0b2012-02-06 04:54:04 -0500153 strbuf_addstr(&buf, path);
154 path = buf.buf;
155 }
156
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700157 if (!access_or_die(path, R_OK, 0)) {
Jeff King9b25a0b2012-02-06 04:54:04 -0500158 if (++inc->depth > MAX_INCLUDE_DEPTH)
159 die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
Jeff King32582582016-05-18 18:41:08 -0400160 !cf ? "<unknown>" :
161 cf->name ? cf->name :
162 "the command line");
Jeff King9b25a0b2012-02-06 04:54:04 -0500163 ret = git_config_from_file(git_config_include, path, inc);
164 inc->depth--;
165 }
166 strbuf_release(&buf);
Jeff King4c0a89f2012-04-25 08:00:36 -0400167 free(expanded);
Jeff King9b25a0b2012-02-06 04:54:04 -0500168 return ret;
169}
170
171int git_config_include(const char *var, const char *value, void *data)
172{
173 struct config_include_data *inc = data;
Jeff King9b25a0b2012-02-06 04:54:04 -0500174 int ret;
175
176 /*
177 * Pass along all values, including "include" directives; this makes it
178 * possible to query information on the includes themselves.
179 */
180 ret = inc->fn(var, value, inc->data);
181 if (ret < 0)
182 return ret;
183
René Scharfe37007c32014-08-30 18:07:05 +0200184 if (!strcmp(var, "include.path"))
Jeff King9b25a0b2012-02-06 04:54:04 -0500185 ret = handle_path_include(value, inc);
186 return ret;
187}
188
Jeff King2b64fc82010-08-23 15:16:00 -0400189void git_config_push_parameter(const char *text)
190{
191 struct strbuf env = STRBUF_INIT;
192 const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
Jeff Kingd1f88492016-03-22 15:50:51 -0400193 if (old && *old) {
Jeff King2b64fc82010-08-23 15:16:00 -0400194 strbuf_addstr(&env, old);
195 strbuf_addch(&env, ' ');
196 }
197 sq_quote_buf(&env, text);
198 setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
199 strbuf_release(&env);
200}
201
Jeff King24968442011-06-09 11:56:42 -0400202int git_config_parse_parameter(const char *text,
203 config_fn_t fn, void *data)
Alex Riesen8b1fa772010-03-26 23:53:57 +0100204{
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700205 const char *value;
Alex Riesen572e4f62010-03-26 23:56:01 +0100206 struct strbuf **pair;
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700207
Jeff Kingf77bcca2011-06-09 11:55:09 -0400208 pair = strbuf_split_str(text, '=', 2);
Jeff Kingc5d63502011-06-09 11:52:43 -0400209 if (!pair[0])
210 return error("bogus config parameter: %s", text);
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700211
212 if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
Alex Riesen572e4f62010-03-26 23:56:01 +0100213 strbuf_setlen(pair[0], pair[0]->len - 1);
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700214 value = pair[1] ? pair[1]->buf : "";
215 } else {
216 value = NULL;
217 }
218
Alex Riesen572e4f62010-03-26 23:56:01 +0100219 strbuf_trim(pair[0]);
220 if (!pair[0]->len) {
221 strbuf_list_free(pair);
Jeff King06eb7082011-05-24 18:49:55 -0400222 return error("bogus config parameter: %s", text);
223 }
Jeff Kingffb20ce2014-05-23 16:03:47 -0400224 strbuf_tolower(pair[0]);
Junio C Hamanoa789ca72014-08-04 15:40:19 -0700225 if (fn(pair[0]->buf, value, data) < 0) {
Jeff King06eb7082011-05-24 18:49:55 -0400226 strbuf_list_free(pair);
Alex Riesen8b1fa772010-03-26 23:53:57 +0100227 return -1;
Alex Riesen8b1fa772010-03-26 23:53:57 +0100228 }
Alex Riesen572e4f62010-03-26 23:56:01 +0100229 strbuf_list_free(pair);
Alex Riesen8b1fa772010-03-26 23:53:57 +0100230 return 0;
231}
232
Jeff King06eb7082011-05-24 18:49:55 -0400233int git_config_from_parameters(config_fn_t fn, void *data)
234{
Jeff King2b64fc82010-08-23 15:16:00 -0400235 const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
Jeff Kinga77d6db2016-05-18 18:39:49 -0400236 int ret = 0;
Jeff King2b64fc82010-08-23 15:16:00 -0400237 char *envw;
238 const char **argv = NULL;
239 int nr = 0, alloc = 0;
240 int i;
Jeff King32582582016-05-18 18:41:08 -0400241 struct config_source source;
Jeff King2b64fc82010-08-23 15:16:00 -0400242
243 if (!env)
244 return 0;
Jeff King32582582016-05-18 18:41:08 -0400245
246 memset(&source, 0, sizeof(source));
247 source.prev = cf;
248 cf = &source;
249
Jeff King2b64fc82010-08-23 15:16:00 -0400250 /* sq_dequote will write over it */
251 envw = xstrdup(env);
252
253 if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
Jeff Kinga77d6db2016-05-18 18:39:49 -0400254 ret = error("bogus format in " CONFIG_DATA_ENVIRONMENT);
255 goto out;
Jeff King2b64fc82010-08-23 15:16:00 -0400256 }
257
258 for (i = 0; i < nr; i++) {
Jeff King06eb7082011-05-24 18:49:55 -0400259 if (git_config_parse_parameter(argv[i], fn, data) < 0) {
Jeff Kinga77d6db2016-05-18 18:39:49 -0400260 ret = -1;
261 goto out;
Jeff King2b64fc82010-08-23 15:16:00 -0400262 }
263 }
264
Jeff Kinga77d6db2016-05-18 18:39:49 -0400265out:
Jeff King2b64fc82010-08-23 15:16:00 -0400266 free(argv);
267 free(envw);
Jeff King32582582016-05-18 18:41:08 -0400268 cf = source.prev;
Jeff Kinga77d6db2016-05-18 18:39:49 -0400269 return ret;
Jeff King2b64fc82010-08-23 15:16:00 -0400270}
271
Linus Torvalds17712992005-10-10 16:31:08 -0700272static int get_next_char(void)
273{
Jeff King49d6cfa2013-08-26 17:57:18 -0400274 int c = cf->do_fgetc(cf);
Linus Torvalds17712992005-10-10 16:31:08 -0700275
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200276 if (c == '\r') {
277 /* DOS like systems */
Jeff King49d6cfa2013-08-26 17:57:18 -0400278 c = cf->do_fgetc(cf);
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200279 if (c != '\n') {
Jeff King5e0be132015-02-05 01:53:28 -0500280 if (c != EOF)
281 cf->do_ungetc(c, cf);
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200282 c = '\r';
Junio C Hamanodb2c0752005-11-02 13:02:57 -0800283 }
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200284 }
285 if (c == '\n')
286 cf->linenr++;
287 if (c == EOF) {
288 cf->eof = 1;
Matthieu Moyb3b3f602014-08-07 04:59:13 -0700289 cf->linenr++;
Heiko Voigtdbb9a812013-05-11 15:19:29 +0200290 c = '\n';
Linus Torvalds17712992005-10-10 16:31:08 -0700291 }
292 return c;
293}
294
295static char *parse_value(void)
296{
Erik Faye-Lunde96c19c2011-04-10 22:54:18 +0200297 int quote = 0, comment = 0, space = 0;
Linus Torvalds17712992005-10-10 16:31:08 -0700298
Ramsay Jones924aaf32011-06-16 21:24:51 +0100299 strbuf_reset(&cf->value);
Linus Torvalds17712992005-10-10 16:31:08 -0700300 for (;;) {
301 int c = get_next_char();
Linus Torvalds17712992005-10-10 16:31:08 -0700302 if (c == '\n') {
Martin Stenberg4b340592012-03-09 22:57:54 +0100303 if (quote) {
304 cf->linenr--;
Linus Torvalds17712992005-10-10 16:31:08 -0700305 return NULL;
Martin Stenberg4b340592012-03-09 22:57:54 +0100306 }
Ramsay Jones924aaf32011-06-16 21:24:51 +0100307 return cf->value.buf;
Linus Torvalds17712992005-10-10 16:31:08 -0700308 }
309 if (comment)
310 continue;
311 if (isspace(c) && !quote) {
Ramsay Jones924aaf32011-06-16 21:24:51 +0100312 if (cf->value.len)
Björn Steinbrinkebdaae32009-07-30 13:41:57 +0200313 space++;
Linus Torvalds17712992005-10-10 16:31:08 -0700314 continue;
315 }
Johannes Schindelin7ebdba62006-05-02 16:58:37 +0200316 if (!quote) {
317 if (c == ';' || c == '#') {
318 comment = 1;
319 continue;
320 }
321 }
Björn Steinbrinkebdaae32009-07-30 13:41:57 +0200322 for (; space; space--)
Ramsay Jones924aaf32011-06-16 21:24:51 +0100323 strbuf_addch(&cf->value, ' ');
Linus Torvalds17712992005-10-10 16:31:08 -0700324 if (c == '\\') {
325 c = get_next_char();
326 switch (c) {
327 case '\n':
328 continue;
329 case 't':
330 c = '\t';
331 break;
332 case 'b':
333 c = '\b';
334 break;
335 case 'n':
336 c = '\n';
337 break;
Linus Torvalds5cbb4012005-10-11 15:24:11 -0700338 /* Some characters escape as themselves */
339 case '\\': case '"':
340 break;
341 /* Reject unknown escape sequences */
342 default:
343 return NULL;
Linus Torvalds17712992005-10-10 16:31:08 -0700344 }
Ramsay Jones924aaf32011-06-16 21:24:51 +0100345 strbuf_addch(&cf->value, c);
Linus Torvalds17712992005-10-10 16:31:08 -0700346 continue;
347 }
348 if (c == '"') {
349 quote = 1-quote;
350 continue;
351 }
Ramsay Jones924aaf32011-06-16 21:24:51 +0100352 strbuf_addch(&cf->value, c);
Linus Torvalds17712992005-10-10 16:31:08 -0700353 }
354}
355
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800356static inline int iskeychar(int c)
357{
358 return isalnum(c) || c == '-';
359}
360
Ben Walton0971e992012-09-30 20:44:36 +0100361static int get_value(config_fn_t fn, void *data, struct strbuf *name)
Linus Torvalds17712992005-10-10 16:31:08 -0700362{
363 int c;
364 char *value;
Matthieu Moyb3b3f602014-08-07 04:59:13 -0700365 int ret;
Linus Torvalds17712992005-10-10 16:31:08 -0700366
367 /* Get the full name */
368 for (;;) {
369 c = get_next_char();
Ramsay Jones924aaf32011-06-16 21:24:51 +0100370 if (cf->eof)
Linus Torvalds17712992005-10-10 16:31:08 -0700371 break;
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800372 if (!iskeychar(c))
Linus Torvalds17712992005-10-10 16:31:08 -0700373 break;
Ben Walton0971e992012-09-30 20:44:36 +0100374 strbuf_addch(name, tolower(c));
Linus Torvalds17712992005-10-10 16:31:08 -0700375 }
Ben Walton0971e992012-09-30 20:44:36 +0100376
Linus Torvalds17712992005-10-10 16:31:08 -0700377 while (c == ' ' || c == '\t')
378 c = get_next_char();
379
380 value = NULL;
381 if (c != '\n') {
382 if (c != '=')
383 return -1;
384 value = parse_value();
385 if (!value)
386 return -1;
387 }
Matthieu Moyb3b3f602014-08-07 04:59:13 -0700388 /*
389 * We already consumed the \n, but we need linenr to point to
390 * the line we just parsed during the call to fn to get
391 * accurate line number in error messages.
392 */
393 cf->linenr--;
394 ret = fn(name->buf, value, data);
395 cf->linenr++;
396 return ret;
Linus Torvalds17712992005-10-10 16:31:08 -0700397}
398
Ben Walton0971e992012-09-30 20:44:36 +0100399static int get_extended_base_var(struct strbuf *name, int c)
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700400{
401 do {
402 if (c == '\n')
Martin Stenberg4b340592012-03-09 22:57:54 +0100403 goto error_incomplete_line;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700404 c = get_next_char();
405 } while (isspace(c));
406
407 /* We require the format to be '[base "extension"]' */
408 if (c != '"')
409 return -1;
Ben Walton0971e992012-09-30 20:44:36 +0100410 strbuf_addch(name, '.');
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700411
412 for (;;) {
413 int c = get_next_char();
414 if (c == '\n')
Martin Stenberg4b340592012-03-09 22:57:54 +0100415 goto error_incomplete_line;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700416 if (c == '"')
417 break;
418 if (c == '\\') {
419 c = get_next_char();
420 if (c == '\n')
Martin Stenberg4b340592012-03-09 22:57:54 +0100421 goto error_incomplete_line;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700422 }
Ben Walton0971e992012-09-30 20:44:36 +0100423 strbuf_addch(name, c);
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700424 }
425
426 /* Final ']' */
427 if (get_next_char() != ']')
428 return -1;
Ben Walton0971e992012-09-30 20:44:36 +0100429 return 0;
Martin Stenberg4b340592012-03-09 22:57:54 +0100430error_incomplete_line:
431 cf->linenr--;
432 return -1;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700433}
434
Ben Walton0971e992012-09-30 20:44:36 +0100435static int get_base_var(struct strbuf *name)
Linus Torvalds17712992005-10-10 16:31:08 -0700436{
Linus Torvalds17712992005-10-10 16:31:08 -0700437 for (;;) {
438 int c = get_next_char();
Ramsay Jones924aaf32011-06-16 21:24:51 +0100439 if (cf->eof)
Linus Torvalds17712992005-10-10 16:31:08 -0700440 return -1;
441 if (c == ']')
Ben Walton0971e992012-09-30 20:44:36 +0100442 return 0;
Linus Torvaldsd14f7762006-05-09 12:24:02 -0700443 if (isspace(c))
Ben Walton0971e992012-09-30 20:44:36 +0100444 return get_extended_base_var(name, c);
Linus Torvalds38c5afa2006-10-30 08:25:36 -0800445 if (!iskeychar(c) && c != '.')
Linus Torvalds17712992005-10-10 16:31:08 -0700446 return -1;
Ben Walton0971e992012-09-30 20:44:36 +0100447 strbuf_addch(name, tolower(c));
Linus Torvalds17712992005-10-10 16:31:08 -0700448 }
449}
450
Heiko Voigt4d8dd142013-07-12 00:44:39 +0200451static int git_parse_source(config_fn_t fn, void *data)
Linus Torvalds17712992005-10-10 16:31:08 -0700452{
453 int comment = 0;
454 int baselen = 0;
Ben Walton0971e992012-09-30 20:44:36 +0100455 struct strbuf *var = &cf->var;
Linus Torvalds17712992005-10-10 16:31:08 -0700456
Petr Baudisde056402008-10-01 22:13:02 +0200457 /* U+FEFF Byte Order Mark in UTF8 */
Junio C Hamano599446d2015-04-16 10:47:45 -0700458 const char *bomptr = utf8_bom;
Petr Baudisde056402008-10-01 22:13:02 +0200459
Linus Torvalds17712992005-10-10 16:31:08 -0700460 for (;;) {
461 int c = get_next_char();
Petr Baudisde056402008-10-01 22:13:02 +0200462 if (bomptr && *bomptr) {
463 /* We are at the file beginning; skip UTF8-encoded BOM
464 * if present. Sane editors won't put this in on their
465 * own, but e.g. Windows Notepad will do it happily. */
Junio C Hamano599446d2015-04-16 10:47:45 -0700466 if (c == (*bomptr & 0377)) {
Petr Baudisde056402008-10-01 22:13:02 +0200467 bomptr++;
468 continue;
469 } else {
470 /* Do not tolerate partial BOM. */
471 if (bomptr != utf8_bom)
472 break;
473 /* No BOM at file beginning. Cool. */
474 bomptr = NULL;
475 }
476 }
Linus Torvalds17712992005-10-10 16:31:08 -0700477 if (c == '\n') {
Ramsay Jones924aaf32011-06-16 21:24:51 +0100478 if (cf->eof)
Linus Torvalds17712992005-10-10 16:31:08 -0700479 return 0;
480 comment = 0;
481 continue;
482 }
483 if (comment || isspace(c))
484 continue;
485 if (c == '#' || c == ';') {
486 comment = 1;
487 continue;
488 }
489 if (c == '[') {
Ben Walton0971e992012-09-30 20:44:36 +0100490 /* Reset prior to determining a new stem */
491 strbuf_reset(var);
492 if (get_base_var(var) < 0 || var->len < 1)
Linus Torvalds17712992005-10-10 16:31:08 -0700493 break;
Ben Walton0971e992012-09-30 20:44:36 +0100494 strbuf_addch(var, '.');
495 baselen = var->len;
Linus Torvalds17712992005-10-10 16:31:08 -0700496 continue;
497 }
498 if (!isalpha(c))
499 break;
Ben Walton0971e992012-09-30 20:44:36 +0100500 /*
501 * Truncate the var name back to the section header
502 * stem prior to grabbing the suffix part of the name
503 * and the value.
504 */
505 strbuf_setlen(var, baselen);
506 strbuf_addch(var, tolower(c));
507 if (get_value(fn, data, var) < 0)
Linus Torvalds17712992005-10-10 16:31:08 -0700508 break;
509 }
Heiko Voigtb2dc0942013-07-12 00:48:30 +0200510 if (cf->die_on_error)
Lars Schneider473166b2016-02-19 10:16:01 +0100511 die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
Heiko Voigtb2dc0942013-07-12 00:48:30 +0200512 else
Lars Schneider473166b2016-02-19 10:16:01 +0100513 return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
Linus Torvalds17712992005-10-10 16:31:08 -0700514}
515
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000516static int parse_unit_factor(const char *end, uintmax_t *val)
Brian Downing0b87b6e2007-07-12 08:32:26 -0500517{
518 if (!*end)
519 return 1;
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -0500520 else if (!strcasecmp(end, "k")) {
521 *val *= 1024;
522 return 1;
523 }
524 else if (!strcasecmp(end, "m")) {
525 *val *= 1024 * 1024;
526 return 1;
527 }
528 else if (!strcasecmp(end, "g")) {
529 *val *= 1024 * 1024 * 1024;
530 return 1;
531 }
532 return 0;
Brian Downing0b87b6e2007-07-12 08:32:26 -0500533}
534
Jeff King71927772013-09-08 04:29:27 -0400535static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
Linus Torvalds17712992005-10-10 16:31:08 -0700536{
537 if (value && *value) {
538 char *end;
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000539 intmax_t val;
540 uintmax_t uval;
541 uintmax_t factor = 1;
542
543 errno = 0;
544 val = strtoimax(value, &end, 0);
545 if (errno == ERANGE)
546 return 0;
Jeff King33fdd772013-09-08 04:36:42 -0400547 if (!parse_unit_factor(end, &factor)) {
548 errno = EINVAL;
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -0500549 return 0;
Jeff King33fdd772013-09-08 04:36:42 -0400550 }
René Scharfe83915ba2014-11-15 14:27:21 +0100551 uval = labs(val);
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000552 uval *= factor;
René Scharfe83915ba2014-11-15 14:27:21 +0100553 if (uval > max || labs(val) > uval) {
Jeff King33fdd772013-09-08 04:36:42 -0400554 errno = ERANGE;
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000555 return 0;
Jeff King33fdd772013-09-08 04:36:42 -0400556 }
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000557 val *= factor;
558 *ret = val;
Brian Downing0b87b6e2007-07-12 08:32:26 -0500559 return 1;
Linus Torvalds17712992005-10-10 16:31:08 -0700560 }
Jeff King33fdd772013-09-08 04:36:42 -0400561 errno = EINVAL;
Brian Downing0b87b6e2007-07-12 08:32:26 -0500562 return 0;
563}
564
Ramsay Jones0b4dc662013-10-06 21:48:29 +0100565static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
Brian Downing0b87b6e2007-07-12 08:32:26 -0500566{
567 if (value && *value) {
568 char *end;
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000569 uintmax_t val;
570 uintmax_t oldval;
571
572 errno = 0;
573 val = strtoumax(value, &end, 0);
574 if (errno == ERANGE)
575 return 0;
576 oldval = val;
Jeff King33fdd772013-09-08 04:36:42 -0400577 if (!parse_unit_factor(end, &val)) {
578 errno = EINVAL;
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -0500579 return 0;
Jeff King33fdd772013-09-08 04:36:42 -0400580 }
581 if (val > max || oldval > val) {
582 errno = ERANGE;
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000583 return 0;
Jeff King33fdd772013-09-08 04:36:42 -0400584 }
Shawn O. Pearcec8deb5a2007-12-25 02:18:05 -0500585 *ret = val;
Brian Downing0b87b6e2007-07-12 08:32:26 -0500586 return 1;
587 }
Jeff King33fdd772013-09-08 04:36:42 -0400588 errno = EINVAL;
Brian Downing0b87b6e2007-07-12 08:32:26 -0500589 return 0;
590}
591
Jeff King42d194e2013-09-08 04:33:08 -0400592static int git_parse_int(const char *value, int *ret)
Jeff Kingc1867ce2008-02-20 19:00:32 -0500593{
Jeff King71927772013-09-08 04:29:27 -0400594 intmax_t tmp;
Jeff King42d194e2013-09-08 04:33:08 -0400595 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
Jeff King71927772013-09-08 04:29:27 -0400596 return 0;
597 *ret = tmp;
598 return 1;
599}
600
Jeff King00160242013-09-08 04:40:02 -0400601static int git_parse_int64(const char *value, int64_t *ret)
602{
603 intmax_t tmp;
604 if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
605 return 0;
606 *ret = tmp;
607 return 1;
608}
609
Jeff King71927772013-09-08 04:29:27 -0400610int git_parse_ulong(const char *value, unsigned long *ret)
611{
612 uintmax_t tmp;
613 if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
614 return 0;
615 *ret = tmp;
616 return 1;
617}
618
Jeff King06bdc232014-04-16 12:51:47 -0400619NORETURN
Jeff King2f666582013-09-08 04:38:22 -0400620static void die_bad_number(const char *name, const char *value)
Jeff Kingc1867ce2008-02-20 19:00:32 -0500621{
Jeff King2f666582013-09-08 04:38:22 -0400622 const char *reason = errno == ERANGE ?
623 "out of range" :
624 "invalid unit";
625 if (!value)
626 value = "";
627
Lars Schneider473166b2016-02-19 10:16:01 +0100628 if (cf && cf->origin_type && cf->name)
629 die(_("bad numeric config value '%s' for '%s' in %s %s: %s"),
630 value, name, cf->origin_type, cf->name, reason);
Matthieu Moy8262aaa2014-08-07 04:59:12 -0700631 die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
Jeff Kingc1867ce2008-02-20 19:00:32 -0500632}
633
Brian Downing0b87b6e2007-07-12 08:32:26 -0500634int git_config_int(const char *name, const char *value)
635{
Jeff King42d194e2013-09-08 04:33:08 -0400636 int ret;
637 if (!git_parse_int(value, &ret))
Jeff King2f666582013-09-08 04:38:22 -0400638 die_bad_number(name, value);
Brian Downing0b87b6e2007-07-12 08:32:26 -0500639 return ret;
640}
641
Jeff King00160242013-09-08 04:40:02 -0400642int64_t git_config_int64(const char *name, const char *value)
643{
644 int64_t ret;
645 if (!git_parse_int64(value, &ret))
646 die_bad_number(name, value);
Brian Downing0b87b6e2007-07-12 08:32:26 -0500647 return ret;
648}
649
650unsigned long git_config_ulong(const char *name, const char *value)
651{
652 unsigned long ret;
653 if (!git_parse_ulong(value, &ret))
Jeff King2f666582013-09-08 04:38:22 -0400654 die_bad_number(name, value);
Brian Downing0b87b6e2007-07-12 08:32:26 -0500655 return ret;
Linus Torvalds17712992005-10-10 16:31:08 -0700656}
657
Dave Borowitz9a549d42015-08-19 11:26:44 -0400658int git_parse_maybe_bool(const char *value)
Linus Torvalds17712992005-10-10 16:31:08 -0700659{
660 if (!value)
661 return 1;
662 if (!*value)
663 return 0;
Junio C Hamano8420ccd2010-02-16 23:59:46 -0800664 if (!strcasecmp(value, "true")
665 || !strcasecmp(value, "yes")
666 || !strcasecmp(value, "on"))
Linus Torvalds17712992005-10-10 16:31:08 -0700667 return 1;
Junio C Hamano8420ccd2010-02-16 23:59:46 -0800668 if (!strcasecmp(value, "false")
669 || !strcasecmp(value, "no")
670 || !strcasecmp(value, "off"))
Linus Torvalds17712992005-10-10 16:31:08 -0700671 return 0;
Junio C Hamano8420ccd2010-02-16 23:59:46 -0800672 return -1;
673}
674
Jeff Kingb2be2f62010-11-17 12:00:45 -0500675int git_config_maybe_bool(const char *name, const char *value)
676{
Dave Borowitz9a549d42015-08-19 11:26:44 -0400677 int v = git_parse_maybe_bool(value);
Jeff Kingb2be2f62010-11-17 12:00:45 -0500678 if (0 <= v)
679 return v;
Jeff King42d194e2013-09-08 04:33:08 -0400680 if (git_parse_int(value, &v))
Jeff Kingdb6195e2010-12-18 22:36:41 -0500681 return !!v;
Jeff Kingb2be2f62010-11-17 12:00:45 -0500682 return -1;
683}
684
Junio C Hamano8420ccd2010-02-16 23:59:46 -0800685int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
686{
Dave Borowitz9a549d42015-08-19 11:26:44 -0400687 int v = git_parse_maybe_bool(value);
Junio C Hamano8420ccd2010-02-16 23:59:46 -0800688 if (0 <= v) {
689 *is_bool = 1;
690 return v;
691 }
Junio C Hamanoa53f2ec2008-04-12 18:33:31 -0700692 *is_bool = 0;
Junio C Hamanoc35b0b52008-04-13 12:11:11 -0700693 return git_config_int(name, value);
Linus Torvalds17712992005-10-10 16:31:08 -0700694}
695
Junio C Hamanoa53f2ec2008-04-12 18:33:31 -0700696int git_config_bool(const char *name, const char *value)
697{
698 int discard;
Junio C Hamanoc35b0b52008-04-13 12:11:11 -0700699 return !!git_config_bool_or_int(name, value, &discard);
Junio C Hamanoa53f2ec2008-04-12 18:33:31 -0700700}
701
Christian Couderea5105a2008-02-16 06:00:24 +0100702int git_config_string(const char **dest, const char *var, const char *value)
703{
704 if (!value)
705 return config_error_nonbool(var);
706 *dest = xstrdup(value);
707 return 0;
708}
709
Matthieu Moy395de252009-11-17 18:24:25 +0100710int git_config_pathname(const char **dest, const char *var, const char *value)
711{
712 if (!value)
713 return config_error_nonbool(var);
714 *dest = expand_user_path(value);
715 if (!*dest)
Matthieu Moy8262aaa2014-08-07 04:59:12 -0700716 die(_("failed to expand user dir in: '%s'"), value);
Matthieu Moy395de252009-11-17 18:24:25 +0100717 return 0;
718}
719
Linus Torvalds806e2ad2008-06-18 14:37:18 -0700720static int git_default_core_config(const char *var, const char *value)
Linus Torvalds17712992005-10-10 16:31:08 -0700721{
722 /* This needs a better name */
723 if (!strcmp(var, "core.filemode")) {
724 trust_executable_bit = git_config_bool(var, value);
725 return 0;
726 }
Alex Riesen1ce47902008-07-28 08:31:28 +0200727 if (!strcmp(var, "core.trustctime")) {
728 trust_ctime = git_config_bool(var, value);
729 return 0;
730 }
Junio C Hamanoc1b5d732013-05-06 22:32:58 -0700731 if (!strcmp(var, "core.checkstat")) {
Robin Rosenbergc08e4d52013-01-22 08:49:22 +0100732 if (!strcasecmp(value, "default"))
733 check_stat = 1;
734 else if (!strcasecmp(value, "minimal"))
735 check_stat = 0;
736 }
Linus Torvalds17712992005-10-10 16:31:08 -0700737
Junio C Hamano9378c162007-06-24 15:11:24 -0700738 if (!strcmp(var, "core.quotepath")) {
739 quote_path_fully = git_config_bool(var, value);
740 return 0;
741 }
742
Johannes Sixt78a8d642007-03-02 22:11:30 +0100743 if (!strcmp(var, "core.symlinks")) {
744 has_symlinks = git_config_bool(var, value);
745 return 0;
746 }
747
Linus Torvalds0a9b88b2008-03-21 16:52:46 -0700748 if (!strcmp(var, "core.ignorecase")) {
749 ignore_case = git_config_bool(var, value);
750 return 0;
751 }
752
Junio C Hamano64589a02011-10-06 13:22:24 -0500753 if (!strcmp(var, "core.attributesfile"))
754 return git_config_pathname(&git_attributes_file, var, value);
755
Ævar Arnfjörð Bjarmason867ad082016-05-04 22:58:12 +0000756 if (!strcmp(var, "core.hookspath"))
757 return git_config_pathname(&git_hooks_path, var, value);
758
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800759 if (!strcmp(var, "core.bare")) {
760 is_bare_repository_cfg = git_config_bool(var, value);
761 return 0;
762 }
763
Junio C Hamano5f730762006-02-08 21:15:24 -0800764 if (!strcmp(var, "core.ignorestat")) {
765 assume_unchanged = git_config_bool(var, value);
766 return 0;
767 }
768
Junio C Hamanoe388c732006-05-02 00:40:24 -0700769 if (!strcmp(var, "core.prefersymlinkrefs")) {
770 prefer_symlink_refs = git_config_bool(var, value);
Johannes Schindelinf8348be2005-11-15 19:24:19 +0100771 return 0;
772 }
773
Shawn Pearce6de08ae2006-05-17 05:55:40 -0400774 if (!strcmp(var, "core.logallrefupdates")) {
775 log_all_ref_updates = git_config_bool(var, value);
776 return 0;
777 }
778
Junio C Hamano2f8acdb2006-03-20 18:45:47 -0800779 if (!strcmp(var, "core.warnambiguousrefs")) {
780 warn_ambiguous_refs = git_config_bool(var, value);
781 return 0;
782 }
783
Junio C Hamanoa71f09f2011-03-20 22:26:24 -0700784 if (!strcmp(var, "core.abbrev")) {
Linus Torvaldsdce96482010-10-28 11:28:04 -0700785 int abbrev = git_config_int(var, value);
786 if (abbrev < minimum_abbrev || abbrev > 40)
787 return -1;
788 default_abbrev = abbrev;
789 return 0;
790 }
791
Dana How960ccca2007-05-09 13:56:50 -0700792 if (!strcmp(var, "core.loosecompression")) {
Joachim B Haga12f6c302006-07-03 22:11:47 +0200793 int level = git_config_int(var, value);
794 if (level == -1)
795 level = Z_DEFAULT_COMPRESSION;
796 else if (level < 0 || level > Z_BEST_COMPRESSION)
Matthieu Moy8262aaa2014-08-07 04:59:12 -0700797 die(_("bad zlib compression level %d"), level);
Joachim B Haga12f6c302006-07-03 22:11:47 +0200798 zlib_compression_level = level;
Dana How960ccca2007-05-09 13:56:50 -0700799 zlib_compression_seen = 1;
800 return 0;
801 }
802
803 if (!strcmp(var, "core.compression")) {
804 int level = git_config_int(var, value);
805 if (level == -1)
806 level = Z_DEFAULT_COMPRESSION;
807 else if (level < 0 || level > Z_BEST_COMPRESSION)
Matthieu Moy8262aaa2014-08-07 04:59:12 -0700808 die(_("bad zlib compression level %d"), level);
Dana How960ccca2007-05-09 13:56:50 -0700809 core_compression_level = level;
810 core_compression_seen = 1;
811 if (!zlib_compression_seen)
812 zlib_compression_level = level;
Joachim B Haga12f6c302006-07-03 22:11:47 +0200813 return 0;
814 }
815
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -0500816 if (!strcmp(var, "core.packedgitwindowsize")) {
Junio C Hamano5faaf242007-02-14 13:20:41 -0800817 int pgsz_x2 = getpagesize() * 2;
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000818 packed_git_window_size = git_config_ulong(var, value);
Junio C Hamano5faaf242007-02-14 13:20:41 -0800819
820 /* This value must be multiple of (pagesize * 2) */
821 packed_git_window_size /= pgsz_x2;
822 if (packed_git_window_size < 1)
823 packed_git_window_size = 1;
824 packed_git_window_size *= pgsz_x2;
Shawn O. Pearce60bb8b12006-12-23 02:34:28 -0500825 return 0;
826 }
827
Junio C Hamano15366282011-04-05 10:44:11 -0700828 if (!strcmp(var, "core.bigfilethreshold")) {
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000829 big_file_threshold = git_config_ulong(var, value);
Junio C Hamano15366282011-04-05 10:44:11 -0700830 return 0;
831 }
832
Shawn O. Pearce77ccc5b2006-12-23 02:33:35 -0500833 if (!strcmp(var, "core.packedgitlimit")) {
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000834 packed_git_limit = git_config_ulong(var, value);
Shawn O. Pearce77ccc5b2006-12-23 02:33:35 -0500835 return 0;
836 }
837
Shawn O. Pearce18bdec12007-03-19 01:14:37 -0400838 if (!strcmp(var, "core.deltabasecachelimit")) {
Nick Alcockebaa1bd2011-11-02 15:46:23 +0000839 delta_base_cache_limit = git_config_ulong(var, value);
Shawn O. Pearce18bdec12007-03-19 01:14:37 -0400840 return 0;
841 }
842
Linus Torvalds6c510be2007-02-13 11:07:23 -0800843 if (!strcmp(var, "core.autocrlf")) {
Linus Torvaldsd7f46332007-02-13 18:16:12 -0800844 if (value && !strcasecmp(value, "input")) {
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200845 auto_crlf = AUTO_CRLF_INPUT;
Linus Torvaldsd7f46332007-02-13 18:16:12 -0800846 return 0;
847 }
Linus Torvalds6c510be2007-02-13 11:07:23 -0800848 auto_crlf = git_config_bool(var, value);
849 return 0;
850 }
851
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100852 if (!strcmp(var, "core.safecrlf")) {
853 if (value && !strcasecmp(value, "warn")) {
854 safe_crlf = SAFE_CRLF_WARN;
855 return 0;
856 }
857 safe_crlf = git_config_bool(var, value);
858 return 0;
859 }
860
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200861 if (!strcmp(var, "core.eol")) {
862 if (value && !strcasecmp(value, "lf"))
Junio C Hamanoec70f522011-05-09 12:52:12 -0700863 core_eol = EOL_LF;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200864 else if (value && !strcasecmp(value, "crlf"))
Junio C Hamanoec70f522011-05-09 12:52:12 -0700865 core_eol = EOL_CRLF;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200866 else if (value && !strcasecmp(value, "native"))
Junio C Hamanoec70f522011-05-09 12:52:12 -0700867 core_eol = EOL_NATIVE;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200868 else
Junio C Hamanoec70f522011-05-09 12:52:12 -0700869 core_eol = EOL_UNSET;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200870 return 0;
871 }
872
Johannes Schindelina97a7462009-10-09 12:21:57 +0200873 if (!strcmp(var, "core.notesref")) {
874 notes_ref_name = xstrdup(value);
875 return 0;
876 }
877
Linus Torvalds806e2ad2008-06-18 14:37:18 -0700878 if (!strcmp(var, "core.pager"))
879 return git_config_string(&pager_program, var, value);
880
881 if (!strcmp(var, "core.editor"))
882 return git_config_string(&editor_program, var, value);
883
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100884 if (!strcmp(var, "core.commentchar")) {
Jeff King649409b2014-07-24 00:42:39 -0400885 if (!value)
886 return config_error_nonbool(var);
Junio C Hamanoad524f82014-07-28 11:30:41 -0700887 else if (!strcasecmp(value, "auto"))
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700888 auto_comment_line_char = 1;
Junio C Hamanoad524f82014-07-28 11:30:41 -0700889 else if (value[0] && !value[1]) {
Jeff King649409b2014-07-24 00:42:39 -0400890 comment_line_char = value[0];
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +0700891 auto_comment_line_char = 0;
Nguyễn Thái Ngọc Duy50b54fd2014-05-17 08:52:22 +0700892 } else
893 return error("core.commentChar should only be one character");
894 return 0;
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100895 }
896
Anselm Kruisd3e7da82010-08-30 15:38:38 +0200897 if (!strcmp(var, "core.askpass"))
898 return git_config_string(&askpass_program, var, value);
899
Linus Torvalds806e2ad2008-06-18 14:37:18 -0700900 if (!strcmp(var, "core.excludesfile"))
Matthieu Moy395de252009-11-17 18:24:25 +0100901 return git_config_pathname(&excludes_file, var, value);
Linus Torvalds806e2ad2008-06-18 14:37:18 -0700902
903 if (!strcmp(var, "core.whitespace")) {
904 if (!value)
905 return config_error_nonbool(var);
906 whitespace_rule_cfg = parse_whitespace_rule(value);
907 return 0;
908 }
909
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -0700910 if (!strcmp(var, "core.fsyncobjectfiles")) {
911 fsync_object_files = git_config_bool(var, value);
912 return 0;
913 }
914
Linus Torvalds671c9b72008-11-13 16:36:30 -0800915 if (!strcmp(var, "core.preloadindex")) {
916 core_preload_index = git_config_bool(var, value);
917 return 0;
918 }
919
Johannes Schindelin348df162009-04-28 00:32:25 +0200920 if (!strcmp(var, "core.createobject")) {
921 if (!strcmp(value, "rename"))
922 object_creation_mode = OBJECT_CREATION_USES_RENAMES;
923 else if (!strcmp(value, "link"))
924 object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
925 else
Matthieu Moy8262aaa2014-08-07 04:59:12 -0700926 die(_("invalid mode for object creation: %s"), value);
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +0200927 return 0;
928 }
929
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +0700930 if (!strcmp(var, "core.sparsecheckout")) {
931 core_apply_sparse_checkout = git_config_bool(var, value);
932 return 0;
933 }
934
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200935 if (!strcmp(var, "core.precomposeunicode")) {
936 precomposed_unicode = git_config_bool(var, value);
937 return 0;
938 }
939
Jeff Kinga42643a2014-12-15 18:15:20 -0500940 if (!strcmp(var, "core.protecthfs")) {
941 protect_hfs = git_config_bool(var, value);
942 return 0;
943 }
944
Johannes Schindelin2b4c6ef2014-12-16 23:46:59 +0100945 if (!strcmp(var, "core.protectntfs")) {
946 protect_ntfs = git_config_bool(var, value);
947 return 0;
948 }
949
Johannes Schindelinf30afda2016-05-11 10:43:37 +0200950 if (!strcmp(var, "core.hidedotfiles")) {
951 if (value && !strcasecmp(value, "dotgitonly"))
952 hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
953 else
954 hide_dotfiles = git_config_bool(var, value);
955 return 0;
956 }
957
Linus Torvalds806e2ad2008-06-18 14:37:18 -0700958 /* Add other config variables here and to Documentation/config.txt. */
959 return 0;
960}
961
Linus Torvalds1141f492008-06-18 15:00:11 -0700962static int git_default_i18n_config(const char *var, const char *value)
Linus Torvaldsd1364522008-06-18 14:40:35 -0700963{
Christian Couderea5105a2008-02-16 06:00:24 +0100964 if (!strcmp(var, "i18n.commitencoding"))
965 return git_config_string(&git_commit_encoding, var, value);
Junio C Hamano4e72dce2005-11-27 16:09:40 -0800966
Christian Couderea5105a2008-02-16 06:00:24 +0100967 if (!strcmp(var, "i18n.logoutputencoding"))
968 return git_config_string(&git_log_output_encoding, var, value);
Junio C Hamanod2c11a32006-12-27 16:41:33 -0800969
Linus Torvalds1141f492008-06-18 15:00:11 -0700970 /* Add other config variables here and to Documentation/config.txt. */
971 return 0;
972}
Matthias Lederhoferaa086eb2006-07-30 00:27:43 +0200973
Linus Torvalds1141f492008-06-18 15:00:11 -0700974static int git_default_branch_config(const char *var, const char *value)
975{
Jay Soffian9ed36cf2008-02-19 11:24:37 -0500976 if (!strcmp(var, "branch.autosetupmerge")) {
977 if (value && !strcasecmp(value, "always")) {
978 git_branch_track = BRANCH_TRACK_ALWAYS;
979 return 0;
980 }
981 git_branch_track = git_config_bool(var, value);
982 return 0;
983 }
Dustin Sallingsc998ae92008-05-10 15:36:29 -0700984 if (!strcmp(var, "branch.autosetuprebase")) {
985 if (!value)
986 return config_error_nonbool(var);
987 else if (!strcmp(value, "never"))
988 autorebase = AUTOREBASE_NEVER;
989 else if (!strcmp(value, "local"))
990 autorebase = AUTOREBASE_LOCAL;
991 else if (!strcmp(value, "remote"))
992 autorebase = AUTOREBASE_REMOTE;
993 else if (!strcmp(value, "always"))
994 autorebase = AUTOREBASE_ALWAYS;
995 else
Jeff King8c3ca352016-04-09 13:42:31 -0400996 return error("malformed value for %s", var);
Dustin Sallingsc998ae92008-05-10 15:36:29 -0700997 return 0;
998 }
Junio C Hamanoa9cc8572007-11-02 00:24:27 -0700999
Petr Baudis1ab661d2006-04-25 00:59:33 +02001000 /* Add other config variables here and to Documentation/config.txt. */
Linus Torvalds17712992005-10-10 16:31:08 -07001001 return 0;
1002}
1003
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001004static int git_default_push_config(const char *var, const char *value)
1005{
1006 if (!strcmp(var, "push.default")) {
1007 if (!value)
1008 return config_error_nonbool(var);
1009 else if (!strcmp(value, "nothing"))
1010 push_default = PUSH_DEFAULT_NOTHING;
1011 else if (!strcmp(value, "matching"))
1012 push_default = PUSH_DEFAULT_MATCHING;
Matthieu Moyb55e6772012-04-24 09:50:03 +02001013 else if (!strcmp(value, "simple"))
1014 push_default = PUSH_DEFAULT_SIMPLE;
Johan Herland53c40312011-02-16 01:54:24 +01001015 else if (!strcmp(value, "upstream"))
1016 push_default = PUSH_DEFAULT_UPSTREAM;
1017 else if (!strcmp(value, "tracking")) /* deprecated */
1018 push_default = PUSH_DEFAULT_UPSTREAM;
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001019 else if (!strcmp(value, "current"))
1020 push_default = PUSH_DEFAULT_CURRENT;
1021 else {
Jeff King8c3ca352016-04-09 13:42:31 -04001022 error("malformed value for %s: %s", var, value);
Matthieu Moyb55e6772012-04-24 09:50:03 +02001023 return error("Must be one of nothing, matching, simple, "
1024 "upstream or current.");
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001025 }
1026 return 0;
1027 }
1028
1029 /* Add other config variables here and to Documentation/config.txt. */
1030 return 0;
1031}
1032
Marius Storm-Olsend551a482009-02-08 15:34:27 +01001033static int git_default_mailmap_config(const char *var, const char *value)
1034{
1035 if (!strcmp(var, "mailmap.file"))
Øystein Walle9352fd52014-05-27 10:45:58 +02001036 return git_config_pathname(&git_mailmap_file, var, value);
Jeff King08610902012-12-12 06:04:04 -05001037 if (!strcmp(var, "mailmap.blob"))
1038 return git_config_string(&git_mailmap_blob, var, value);
Marius Storm-Olsend551a482009-02-08 15:34:27 +01001039
1040 /* Add other config variables here and to Documentation/config.txt. */
1041 return 0;
1042}
1043
Linus Torvalds1141f492008-06-18 15:00:11 -07001044int git_default_config(const char *var, const char *value, void *dummy)
1045{
Christian Couder59556542013-11-30 21:55:40 +01001046 if (starts_with(var, "core."))
Linus Torvalds1141f492008-06-18 15:00:11 -07001047 return git_default_core_config(var, value);
1048
Christian Couder59556542013-11-30 21:55:40 +01001049 if (starts_with(var, "user."))
Jeff King95979212012-05-21 19:09:54 -04001050 return git_ident_config(var, value, dummy);
Linus Torvalds1141f492008-06-18 15:00:11 -07001051
Christian Couder59556542013-11-30 21:55:40 +01001052 if (starts_with(var, "i18n."))
Linus Torvalds1141f492008-06-18 15:00:11 -07001053 return git_default_i18n_config(var, value);
1054
Christian Couder59556542013-11-30 21:55:40 +01001055 if (starts_with(var, "branch."))
Linus Torvalds1141f492008-06-18 15:00:11 -07001056 return git_default_branch_config(var, value);
1057
Christian Couder59556542013-11-30 21:55:40 +01001058 if (starts_with(var, "push."))
Finn Arne Gangstad52153742009-03-16 16:42:51 +01001059 return git_default_push_config(var, value);
1060
Christian Couder59556542013-11-30 21:55:40 +01001061 if (starts_with(var, "mailmap."))
Marius Storm-Olsend551a482009-02-08 15:34:27 +01001062 return git_default_mailmap_config(var, value);
1063
Christian Couder59556542013-11-30 21:55:40 +01001064 if (starts_with(var, "advice."))
Jeff King75194432009-09-09 07:38:58 -04001065 return git_default_advice_config(var, value);
1066
Linus Torvalds1141f492008-06-18 15:00:11 -07001067 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
1068 pager_use_color = git_config_bool(var,value);
1069 return 0;
1070 }
1071
Junio C Hamano568508e2011-10-28 14:48:40 -07001072 if (!strcmp(var, "pack.packsizelimit")) {
1073 pack_size_limit_cfg = git_config_ulong(var, value);
1074 return 0;
1075 }
Linus Torvalds1141f492008-06-18 15:00:11 -07001076 /* Add other config variables here and to Documentation/config.txt. */
1077 return 0;
1078}
1079
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001080/*
Heiko Voigtb2dc0942013-07-12 00:48:30 +02001081 * All source specific fields in the union, die_on_error, name and the callbacks
Heiko Voigt4d8dd142013-07-12 00:44:39 +02001082 * fgetc, ungetc, ftell of top need to be initialized before calling
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001083 * this function.
1084 */
Heiko Voigt4d8dd142013-07-12 00:44:39 +02001085static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001086{
1087 int ret;
1088
1089 /* push config-file parsing state stack */
1090 top->prev = cf;
1091 top->linenr = 1;
1092 top->eof = 0;
1093 strbuf_init(&top->value, 1024);
1094 strbuf_init(&top->var, 1024);
1095 cf = top;
1096
Heiko Voigt4d8dd142013-07-12 00:44:39 +02001097 ret = git_parse_source(fn, data);
Heiko Voigtca4b5de2013-05-11 15:18:52 +02001098
1099 /* pop config-file parsing state stack */
1100 strbuf_release(&top->value);
1101 strbuf_release(&top->var);
1102 cf = top->prev;
1103
1104 return ret;
1105}
1106
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001107static int do_config_from_file(config_fn_t fn,
Lars Schneider473166b2016-02-19 10:16:01 +01001108 const char *origin_type, const char *name, const char *path, FILE *f,
1109 void *data)
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001110{
1111 struct config_source top;
1112
1113 top.u.file = f;
Lars Schneider473166b2016-02-19 10:16:01 +01001114 top.origin_type = origin_type;
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001115 top.name = name;
1116 top.path = path;
1117 top.die_on_error = 1;
1118 top.do_fgetc = config_file_fgetc;
1119 top.do_ungetc = config_file_ungetc;
1120 top.do_ftell = config_file_ftell;
1121
1122 return do_config_from(&top, fn, data);
1123}
1124
1125static int git_config_from_stdin(config_fn_t fn, void *data)
1126{
Lars Schneider473166b2016-02-19 10:16:01 +01001127 return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001128}
1129
Johannes Schindelinef90d6d2008-05-14 18:46:53 +01001130int git_config_from_file(config_fn_t fn, const char *filename, void *data)
Linus Torvalds17712992005-10-10 16:31:08 -07001131{
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001132 int ret = -1;
1133 FILE *f;
Linus Torvalds17712992005-10-10 16:31:08 -07001134
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001135 f = fopen(filename, "r");
Linus Torvalds17712992005-10-10 16:31:08 -07001136 if (f) {
Jeff King260d4082015-04-16 04:51:18 -04001137 flockfile(f);
Lars Schneider473166b2016-02-19 10:16:01 +01001138 ret = do_config_from_file(fn, "file", filename, filename, f, data);
Jeff King260d4082015-04-16 04:51:18 -04001139 funlockfile(f);
Linus Torvalds17712992005-10-10 16:31:08 -07001140 fclose(f);
1141 }
1142 return ret;
1143}
Johannes Schindelin10bea152005-11-17 22:32:36 +01001144
Lars Schneider473166b2016-02-19 10:16:01 +01001145int git_config_from_mem(config_fn_t fn, const char *origin_type,
1146 const char *name, const char *buf, size_t len, void *data)
Heiko Voigt1bc88812013-07-12 00:46:47 +02001147{
1148 struct config_source top;
1149
1150 top.u.buf.buf = buf;
1151 top.u.buf.len = len;
1152 top.u.buf.pos = 0;
Lars Schneider473166b2016-02-19 10:16:01 +01001153 top.origin_type = origin_type;
Heiko Voigt1bc88812013-07-12 00:46:47 +02001154 top.name = name;
Jeff Kingd14d4242014-02-19 00:58:52 +02001155 top.path = NULL;
Heiko Voigtb2dc0942013-07-12 00:48:30 +02001156 top.die_on_error = 0;
Jeff King49d6cfa2013-08-26 17:57:18 -04001157 top.do_fgetc = config_buf_fgetc;
1158 top.do_ungetc = config_buf_ungetc;
1159 top.do_ftell = config_buf_ftell;
Heiko Voigt1bc88812013-07-12 00:46:47 +02001160
1161 return do_config_from(&top, fn, data);
1162}
1163
1164static int git_config_from_blob_sha1(config_fn_t fn,
1165 const char *name,
1166 const unsigned char *sha1,
1167 void *data)
1168{
1169 enum object_type type;
1170 char *buf;
1171 unsigned long size;
1172 int ret;
1173
1174 buf = read_sha1_file(sha1, &type, &size);
1175 if (!buf)
1176 return error("unable to load config blob object '%s'", name);
1177 if (type != OBJ_BLOB) {
1178 free(buf);
1179 return error("reference '%s' does not point to a blob", name);
1180 }
1181
Lars Schneider473166b2016-02-19 10:16:01 +01001182 ret = git_config_from_mem(fn, "blob", name, buf, size, data);
Heiko Voigt1bc88812013-07-12 00:46:47 +02001183 free(buf);
1184
1185 return ret;
1186}
1187
1188static int git_config_from_blob_ref(config_fn_t fn,
1189 const char *name,
1190 void *data)
1191{
1192 unsigned char sha1[20];
1193
1194 if (get_sha1(name, sha1) < 0)
1195 return error("unable to resolve config blob '%s'", name);
1196 return git_config_from_blob_sha1(fn, name, sha1, data);
1197}
1198
Johannes Sixt506b17b2007-11-13 21:05:05 +01001199const char *git_etc_gitconfig(void)
1200{
Johannes Sixt7f0e39f2007-11-13 21:05:06 +01001201 static const char *system_wide;
Steffen Prohaska2de9de52008-07-13 22:31:18 +02001202 if (!system_wide)
1203 system_wide = system_path(ETC_GITCONFIG);
Johannes Sixt7f0e39f2007-11-13 21:05:06 +01001204 return system_wide;
Johannes Sixt506b17b2007-11-13 21:05:05 +01001205}
1206
Steffen Prohaska23b0c472014-08-26 17:23:21 +02001207/*
1208 * Parse environment variable 'k' as a boolean (in various
1209 * possible spellings); if missing, use the default value 'def'.
1210 */
Lars R. Damerow0ef37162010-03-17 12:55:51 -07001211int git_env_bool(const char *k, int def)
Jeff Kingab88c362008-02-06 05:11:18 -05001212{
1213 const char *v = getenv(k);
1214 return v ? git_config_bool(k, v) : def;
1215}
1216
Steffen Prohaska23b0c472014-08-26 17:23:21 +02001217/*
1218 * Parse environment variable 'k' as ulong with possibly a unit
1219 * suffix; if missing, use the default value 'val'.
1220 */
1221unsigned long git_env_ulong(const char *k, unsigned long val)
1222{
1223 const char *v = getenv(k);
1224 if (v && !git_parse_ulong(v, &val))
1225 die("failed to parse %s", k);
1226 return val;
1227}
1228
Jeff Kingab88c362008-02-06 05:11:18 -05001229int git_config_system(void)
1230{
1231 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
1232}
1233
Jeff King80181862016-03-11 17:37:03 -05001234static int do_git_config_sequence(config_fn_t fn, void *data)
Junio C Hamano4f629532005-11-25 16:03:56 -08001235{
Jeff Kingc72ee442016-05-18 18:39:02 -04001236 int ret = 0;
Paul Tan509adc32015-05-06 16:01:03 +08001237 char *xdg_config = xdg_config_home("config");
1238 char *user_config = expand_user_path("~/.gitconfig");
Jeff King80181862016-03-11 17:37:03 -05001239 char *repo_config = git_pathdup("config");
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +02001240
Jeff King9acc5912016-05-18 18:44:23 -04001241 current_parsing_scope = CONFIG_SCOPE_SYSTEM;
Jeff Kingc72ee442016-05-18 18:39:02 -04001242 if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0))
Daniel Barkalowdc871832008-06-30 03:37:47 -04001243 ret += git_config_from_file(fn, git_etc_gitconfig(),
1244 data);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +02001245
Jeff King9acc5912016-05-18 18:44:23 -04001246 current_parsing_scope = CONFIG_SCOPE_GLOBAL;
Jeff Kingc72ee442016-05-18 18:39:02 -04001247 if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001248 ret += git_config_from_file(fn, xdg_config, data);
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001249
Jeff Kingc72ee442016-05-18 18:39:02 -04001250 if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001251 ret += git_config_from_file(fn, user_config, data);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +02001252
Jeff King9acc5912016-05-18 18:44:23 -04001253 current_parsing_scope = CONFIG_SCOPE_REPO;
Jeff Kingc72ee442016-05-18 18:39:02 -04001254 if (repo_config && !access_or_die(repo_config, R_OK, 0))
Felipe Contrerasaa387402009-02-21 02:48:55 +02001255 ret += git_config_from_file(fn, repo_config, data);
Alex Riesen8b1fa772010-03-26 23:53:57 +01001256
Jeff King9acc5912016-05-18 18:44:23 -04001257 current_parsing_scope = CONFIG_SCOPE_CMDLINE;
Jeff Kingc72ee442016-05-18 18:39:02 -04001258 if (git_config_from_parameters(fn, data) < 0)
Matthieu Moy8262aaa2014-08-07 04:59:12 -07001259 die(_("unable to parse command-line config"));
Alex Riesen8b1fa772010-03-26 23:53:57 +01001260
Jeff King9acc5912016-05-18 18:44:23 -04001261 current_parsing_scope = CONFIG_SCOPE_UNKNOWN;
Huynh Khoi Nguyen Nguyen21cf3222012-06-22 11:03:23 +02001262 free(xdg_config);
1263 free(user_config);
Jeff King80181862016-03-11 17:37:03 -05001264 free(repo_config);
Jeff Kingc72ee442016-05-18 18:39:02 -04001265 return ret;
Junio C Hamano4f629532005-11-25 16:03:56 -08001266}
1267
Jeff Kingc9b5e2a2012-02-16 03:05:56 -05001268int git_config_with_options(config_fn_t fn, void *data,
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +02001269 struct git_config_source *config_source,
Heiko Voigt1bc88812013-07-12 00:46:47 +02001270 int respect_includes)
Nguyễn Thái Ngọc Duydbdf5852010-11-26 22:32:33 +07001271{
Jeff King9b25a0b2012-02-06 04:54:04 -05001272 struct config_include_data inc = CONFIG_INCLUDE_INIT;
1273
1274 if (respect_includes) {
1275 inc.fn = fn;
1276 inc.data = data;
1277 fn = git_config_include;
1278 data = &inc;
1279 }
Nguyễn Thái Ngọc Duydbdf5852010-11-26 22:32:33 +07001280
Jeff Kingc9b5e2a2012-02-16 03:05:56 -05001281 /*
1282 * If we have a specific filename, use it. Otherwise, follow the
1283 * regular lookup sequence.
1284 */
Kirill A. Shutemov3caec732014-02-19 00:58:55 +02001285 if (config_source && config_source->use_stdin)
1286 return git_config_from_stdin(fn, data);
1287 else if (config_source && config_source->file)
Kirill A. Shutemovc8985ce2014-02-19 00:58:54 +02001288 return git_config_from_file(fn, config_source->file, data);
1289 else if (config_source && config_source->blob)
1290 return git_config_from_blob_ref(fn, config_source->blob, data);
Jeff Kingc9b5e2a2012-02-16 03:05:56 -05001291
Jeff King80181862016-03-11 17:37:03 -05001292 return do_git_config_sequence(fn, data);
Nguyễn Thái Ngọc Duydbdf5852010-11-26 22:32:33 +07001293}
1294
Tanay Abhra155ef252014-08-07 04:59:17 -07001295static void git_config_raw(config_fn_t fn, void *data)
Jeff Kingc9b5e2a2012-02-16 03:05:56 -05001296{
Tanay Abhraaace4382014-08-07 04:59:15 -07001297 if (git_config_with_options(fn, data, NULL, 1) < 0)
1298 /*
1299 * git_config_with_options() normally returns only
Jeff Kingc72ee442016-05-18 18:39:02 -04001300 * zero, as most errors are fatal, and
Tanay Abhraaace4382014-08-07 04:59:15 -07001301 * non-fatal potential errors are guarded by "if"
1302 * statements that are entered only when no error is
1303 * possible.
1304 *
1305 * If we ever encounter a non-fatal error, it means
1306 * something went really wrong and we should stop
1307 * immediately.
1308 */
Peter Colberg3a39f612016-06-10 15:05:26 -04001309 die(_("unknown error occurred while reading the configuration files"));
Jeff Kingc9b5e2a2012-02-16 03:05:56 -05001310}
1311
Tanay Abhra155ef252014-08-07 04:59:17 -07001312static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
1313{
1314 int i, value_index;
1315 struct string_list *values;
1316 struct config_set_element *entry;
1317 struct configset_list *list = &cs->list;
Tanay Abhra155ef252014-08-07 04:59:17 -07001318
1319 for (i = 0; i < list->nr; i++) {
1320 entry = list->items[i].e;
1321 value_index = list->items[i].value_index;
1322 values = &entry->value_list;
Jeff King0d44a2d2016-05-26 20:32:23 -04001323
1324 current_config_kvi = values->items[value_index].util;
1325
1326 if (fn(entry->key, values->items[value_index].string, data) < 0)
1327 git_die_config_linenr(entry->key,
1328 current_config_kvi->filename,
1329 current_config_kvi->linenr);
1330
1331 current_config_kvi = NULL;
Tanay Abhra155ef252014-08-07 04:59:17 -07001332 }
1333}
1334
1335static void git_config_check_init(void);
1336
1337void git_config(config_fn_t fn, void *data)
1338{
1339 git_config_check_init();
1340 configset_iter(&the_config_set, fn, data);
Felipe Contreras4b25d092009-05-01 12:06:36 +03001341}
Johannes Schindelinf98d8632005-11-20 13:24:18 +01001342
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001343static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
1344{
1345 struct config_set_element k;
1346 struct config_set_element *found_entry;
1347 char *normalized_key;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001348 /*
1349 * `key` may come from the user, so normalize it before using it
1350 * for querying entries from the hashmap.
1351 */
Stefan Beller270cd9e2016-04-27 14:30:39 -07001352 if (git_config_parse_key(key, &normalized_key, NULL))
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001353 return NULL;
1354
1355 hashmap_entry_init(&k, strhash(normalized_key));
1356 k.key = normalized_key;
1357 found_entry = hashmap_get(&cs->config_hash, &k, NULL);
1358 free(normalized_key);
1359 return found_entry;
1360}
1361
1362static int configset_add_value(struct config_set *cs, const char *key, const char *value)
1363{
1364 struct config_set_element *e;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07001365 struct string_list_item *si;
Tanay Abhra155ef252014-08-07 04:59:17 -07001366 struct configset_list_item *l_item;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07001367 struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
1368
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001369 e = configset_find_element(cs, key);
1370 /*
1371 * Since the keys are being fed by git_config*() callback mechanism, they
1372 * are already normalized. So simply add them without any further munging.
1373 */
1374 if (!e) {
1375 e = xmalloc(sizeof(*e));
1376 hashmap_entry_init(e, strhash(key));
1377 e->key = xstrdup(key);
1378 string_list_init(&e->value_list, 1);
1379 hashmap_add(&cs->config_hash, e);
1380 }
Jeff King8c53f072015-01-12 20:59:09 -05001381 si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
Tanay Abhra155ef252014-08-07 04:59:17 -07001382
1383 ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
1384 l_item = &cs->list.items[cs->list.nr++];
1385 l_item->e = e;
1386 l_item->value_index = e->value_list.nr - 1;
1387
Jeff King32582582016-05-18 18:41:08 -04001388 if (!cf)
1389 die("BUG: configset_add_value has no source");
1390 if (cf->name) {
Tanay Abhra3df8fd62014-08-07 04:59:14 -07001391 kv_info->filename = strintern(cf->name);
1392 kv_info->linenr = cf->linenr;
Jeff King0d44a2d2016-05-26 20:32:23 -04001393 kv_info->origin_type = strintern(cf->origin_type);
Tanay Abhra3df8fd62014-08-07 04:59:14 -07001394 } else {
1395 /* for values read from `git_config_from_parameters()` */
1396 kv_info->filename = NULL;
1397 kv_info->linenr = -1;
Jeff King0d44a2d2016-05-26 20:32:23 -04001398 kv_info->origin_type = NULL;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07001399 }
Jeff King9acc5912016-05-18 18:44:23 -04001400 kv_info->scope = current_parsing_scope;
Tanay Abhra3df8fd62014-08-07 04:59:14 -07001401 si->util = kv_info;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001402
1403 return 0;
1404}
1405
1406static int config_set_element_cmp(const struct config_set_element *e1,
1407 const struct config_set_element *e2, const void *unused)
1408{
1409 return strcmp(e1->key, e2->key);
1410}
1411
1412void git_configset_init(struct config_set *cs)
1413{
1414 hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
1415 cs->hash_initialized = 1;
Tanay Abhra155ef252014-08-07 04:59:17 -07001416 cs->list.nr = 0;
1417 cs->list.alloc = 0;
1418 cs->list.items = NULL;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001419}
1420
1421void git_configset_clear(struct config_set *cs)
1422{
1423 struct config_set_element *entry;
1424 struct hashmap_iter iter;
1425 if (!cs->hash_initialized)
1426 return;
1427
1428 hashmap_iter_init(&cs->config_hash, &iter);
1429 while ((entry = hashmap_iter_next(&iter))) {
1430 free(entry->key);
Tanay Abhra3df8fd62014-08-07 04:59:14 -07001431 string_list_clear(&entry->value_list, 1);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001432 }
1433 hashmap_free(&cs->config_hash, 1);
1434 cs->hash_initialized = 0;
Tanay Abhra155ef252014-08-07 04:59:17 -07001435 free(cs->list.items);
1436 cs->list.nr = 0;
1437 cs->list.alloc = 0;
1438 cs->list.items = NULL;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001439}
1440
1441static int config_set_callback(const char *key, const char *value, void *cb)
1442{
1443 struct config_set *cs = cb;
1444 configset_add_value(cs, key, value);
1445 return 0;
1446}
1447
1448int git_configset_add_file(struct config_set *cs, const char *filename)
1449{
1450 return git_config_from_file(config_set_callback, filename, cs);
1451}
1452
1453int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
1454{
1455 const struct string_list *values = NULL;
1456 /*
1457 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
1458 * queried key in the files of the configset, the value returned will be the last
1459 * value in the value list for that key.
1460 */
1461 values = git_configset_get_value_multi(cs, key);
1462
1463 if (!values)
1464 return 1;
1465 assert(values->nr > 0);
1466 *value = values->items[values->nr - 1].string;
1467 return 0;
1468}
1469
1470const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
1471{
1472 struct config_set_element *e = configset_find_element(cs, key);
1473 return e ? &e->value_list : NULL;
1474}
1475
1476int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest)
1477{
1478 const char *value;
1479 if (!git_configset_get_value(cs, key, &value))
1480 return git_config_string(dest, key, value);
1481 else
1482 return 1;
1483}
1484
1485int git_configset_get_string(struct config_set *cs, const char *key, char **dest)
1486{
1487 return git_configset_get_string_const(cs, key, (const char **)dest);
1488}
1489
1490int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
1491{
1492 const char *value;
1493 if (!git_configset_get_value(cs, key, &value)) {
1494 *dest = git_config_int(key, value);
1495 return 0;
1496 } else
1497 return 1;
1498}
1499
1500int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
1501{
1502 const char *value;
1503 if (!git_configset_get_value(cs, key, &value)) {
1504 *dest = git_config_ulong(key, value);
1505 return 0;
1506 } else
1507 return 1;
1508}
1509
1510int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
1511{
1512 const char *value;
1513 if (!git_configset_get_value(cs, key, &value)) {
1514 *dest = git_config_bool(key, value);
1515 return 0;
1516 } else
1517 return 1;
1518}
1519
1520int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
1521 int *is_bool, int *dest)
1522{
1523 const char *value;
1524 if (!git_configset_get_value(cs, key, &value)) {
1525 *dest = git_config_bool_or_int(key, value, is_bool);
1526 return 0;
1527 } else
1528 return 1;
1529}
1530
1531int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
1532{
1533 const char *value;
1534 if (!git_configset_get_value(cs, key, &value)) {
1535 *dest = git_config_maybe_bool(key, value);
1536 if (*dest == -1)
1537 return -1;
1538 return 0;
1539 } else
1540 return 1;
1541}
1542
1543int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
1544{
1545 const char *value;
1546 if (!git_configset_get_value(cs, key, &value))
1547 return git_config_pathname(dest, key, value);
1548 else
1549 return 1;
1550}
1551
1552static void git_config_check_init(void)
1553{
1554 if (the_config_set.hash_initialized)
1555 return;
1556 git_configset_init(&the_config_set);
Tanay Abhra155ef252014-08-07 04:59:17 -07001557 git_config_raw(config_set_callback, &the_config_set);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001558}
1559
1560void git_config_clear(void)
1561{
1562 if (!the_config_set.hash_initialized)
1563 return;
1564 git_configset_clear(&the_config_set);
1565}
1566
1567int git_config_get_value(const char *key, const char **value)
1568{
1569 git_config_check_init();
1570 return git_configset_get_value(&the_config_set, key, value);
1571}
1572
1573const struct string_list *git_config_get_value_multi(const char *key)
1574{
1575 git_config_check_init();
1576 return git_configset_get_value_multi(&the_config_set, key);
1577}
1578
1579int git_config_get_string_const(const char *key, const char **dest)
1580{
Tanay Abhra5a80e972014-08-07 04:59:16 -07001581 int ret;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001582 git_config_check_init();
Tanay Abhra5a80e972014-08-07 04:59:16 -07001583 ret = git_configset_get_string_const(&the_config_set, key, dest);
1584 if (ret < 0)
1585 git_die_config(key, NULL);
1586 return ret;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001587}
1588
1589int git_config_get_string(const char *key, char **dest)
1590{
1591 git_config_check_init();
1592 return git_config_get_string_const(key, (const char **)dest);
1593}
1594
1595int git_config_get_int(const char *key, int *dest)
1596{
1597 git_config_check_init();
1598 return git_configset_get_int(&the_config_set, key, dest);
1599}
1600
1601int git_config_get_ulong(const char *key, unsigned long *dest)
1602{
1603 git_config_check_init();
1604 return git_configset_get_ulong(&the_config_set, key, dest);
1605}
1606
1607int git_config_get_bool(const char *key, int *dest)
1608{
1609 git_config_check_init();
1610 return git_configset_get_bool(&the_config_set, key, dest);
1611}
1612
1613int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
1614{
1615 git_config_check_init();
1616 return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
1617}
1618
1619int git_config_get_maybe_bool(const char *key, int *dest)
1620{
1621 git_config_check_init();
1622 return git_configset_get_maybe_bool(&the_config_set, key, dest);
1623}
1624
1625int git_config_get_pathname(const char *key, const char **dest)
1626{
Tanay Abhra5a80e972014-08-07 04:59:16 -07001627 int ret;
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001628 git_config_check_init();
Tanay Abhra5a80e972014-08-07 04:59:16 -07001629 ret = git_configset_get_pathname(&the_config_set, key, dest);
1630 if (ret < 0)
1631 git_die_config(key, NULL);
1632 return ret;
1633}
1634
Christian Couder435ec092016-01-27 07:58:05 +01001635int git_config_get_untracked_cache(void)
1636{
1637 int val = -1;
1638 const char *v;
1639
Christian Couderdae6c322016-01-27 07:58:06 +01001640 /* Hack for test programs like test-dump-untracked-cache */
1641 if (ignore_untracked_cache_config)
1642 return -1;
1643
Christian Couder435ec092016-01-27 07:58:05 +01001644 if (!git_config_get_maybe_bool("core.untrackedcache", &val))
1645 return val;
1646
1647 if (!git_config_get_value("core.untrackedcache", &v)) {
1648 if (!strcasecmp(v, "keep"))
1649 return -1;
1650
1651 error("unknown core.untrackedCache value '%s'; "
1652 "using 'keep' default value", v);
1653 return -1;
1654 }
1655
1656 return -1; /* default value */
1657}
1658
Tanay Abhra5a80e972014-08-07 04:59:16 -07001659NORETURN
1660void git_die_config_linenr(const char *key, const char *filename, int linenr)
1661{
1662 if (!filename)
1663 die(_("unable to parse '%s' from command-line config"), key);
1664 else
1665 die(_("bad config variable '%s' in file '%s' at line %d"),
1666 key, filename, linenr);
1667}
1668
1669NORETURN __attribute__((format(printf, 2, 3)))
1670void git_die_config(const char *key, const char *err, ...)
1671{
1672 const struct string_list *values;
1673 struct key_value_info *kv_info;
1674
1675 if (err) {
1676 va_list params;
1677 va_start(params, err);
1678 vreportf("error: ", err, params);
1679 va_end(params);
1680 }
1681 values = git_config_get_value_multi(key);
1682 kv_info = values->items[values->nr - 1].util;
1683 git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
Tanay Abhra3c8687a2014-07-28 03:10:38 -07001684}
1685
Johannes Schindelin10bea152005-11-17 22:32:36 +01001686/*
1687 * Find all the stuff for git_config_set() below.
1688 */
1689
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001690static struct {
Johannes Schindelin10bea152005-11-17 22:32:36 +01001691 int baselen;
1692 char *key;
1693 int do_not_match;
Felipe Contreras4b25d092009-05-01 12:06:36 +03001694 regex_t *value_regex;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001695 int multi_replace;
Thomas Rast83786fa2013-11-13 11:19:00 +01001696 size_t *offset;
1697 unsigned int offset_alloc;
Johannes Schindelin10bea152005-11-17 22:32:36 +01001698 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
1699 int seen;
1700} store;
1701
Felipe Contreras4b25d092009-05-01 12:06:36 +03001702static int matches(const char *key, const char *value)
Johannes Schindelinf98d8632005-11-20 13:24:18 +01001703{
Jeff Kingc1063be2014-08-19 02:20:00 -04001704 if (strcmp(key, store.key))
1705 return 0; /* not ours */
1706 if (!store.value_regex)
1707 return 1; /* always matches */
1708 if (store.value_regex == CONFIG_REGEX_NONE)
1709 return 0; /* never matches */
1710
1711 return store.do_not_match ^
1712 (value && !regexec(store.value_regex, value, 0, NULL, 0));
Johannes Schindelinf98d8632005-11-20 13:24:18 +01001713}
1714
Felipe Contreras4b25d092009-05-01 12:06:36 +03001715static int store_aux(const char *key, const char *value, void *cb)
Johannes Schindelin10bea152005-11-17 22:32:36 +01001716{
Junio C Hamanoae9ee412007-05-12 21:49:33 -07001717 const char *ep;
1718 size_t section_len;
1719
Johannes Schindelin10bea152005-11-17 22:32:36 +01001720 switch (store.state) {
1721 case KEY_SEEN:
Johannes Schindelinf98d8632005-11-20 13:24:18 +01001722 if (matches(key, value)) {
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001723 if (store.seen == 1 && store.multi_replace == 0) {
Matthieu Moy8262aaa2014-08-07 04:59:12 -07001724 warning(_("%s has multiple values"), key);
Johannes Schindelin10bea152005-11-17 22:32:36 +01001725 }
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001726
Thomas Rast83786fa2013-11-13 11:19:00 +01001727 ALLOC_GROW(store.offset, store.seen + 1,
1728 store.offset_alloc);
1729
Jeff King49d6cfa2013-08-26 17:57:18 -04001730 store.offset[store.seen] = cf->do_ftell(cf);
Johannes Schindelin10bea152005-11-17 22:32:36 +01001731 store.seen++;
1732 }
1733 break;
1734 case SECTION_SEEN:
Junio C Hamanoae9ee412007-05-12 21:49:33 -07001735 /*
1736 * What we are looking for is in store.key (both
1737 * section and var), and its section part is baselen
1738 * long. We found key (again, both section and var).
1739 * We would want to know if this key is in the same
1740 * section as what we are looking for. We already
1741 * know we are in the same section as what should
1742 * hold store.key.
1743 */
1744 ep = strrchr(key, '.');
1745 section_len = ep - key;
1746
1747 if ((section_len != store.baselen) ||
1748 memcmp(key, store.key, section_len+1)) {
Johannes Schindelin10bea152005-11-17 22:32:36 +01001749 store.state = SECTION_END_SEEN;
1750 break;
Junio C Hamanoae9ee412007-05-12 21:49:33 -07001751 }
1752
1753 /*
1754 * Do not increment matches: this is no match, but we
1755 * just made sure we are in the desired section.
1756 */
Thomas Rast83786fa2013-11-13 11:19:00 +01001757 ALLOC_GROW(store.offset, store.seen + 1,
1758 store.offset_alloc);
Jeff King49d6cfa2013-08-26 17:57:18 -04001759 store.offset[store.seen] = cf->do_ftell(cf);
Johannes Schindelin10bea152005-11-17 22:32:36 +01001760 /* fallthru */
1761 case SECTION_END_SEEN:
1762 case START:
Johannes Schindelinf98d8632005-11-20 13:24:18 +01001763 if (matches(key, value)) {
Thomas Rast83786fa2013-11-13 11:19:00 +01001764 ALLOC_GROW(store.offset, store.seen + 1,
1765 store.offset_alloc);
Jeff King49d6cfa2013-08-26 17:57:18 -04001766 store.offset[store.seen] = cf->do_ftell(cf);
Johannes Schindelin10bea152005-11-17 22:32:36 +01001767 store.state = KEY_SEEN;
1768 store.seen++;
Linus Torvaldsd14f7762006-05-09 12:24:02 -07001769 } else {
1770 if (strrchr(key, '.') - key == store.baselen &&
seanbdf0ef02006-05-06 14:14:02 -04001771 !strncmp(key, store.key, store.baselen)) {
sean93ddef32006-05-05 09:49:15 -04001772 store.state = SECTION_SEEN;
Thomas Rast83786fa2013-11-13 11:19:00 +01001773 ALLOC_GROW(store.offset,
1774 store.seen + 1,
1775 store.offset_alloc);
Jeff King49d6cfa2013-08-26 17:57:18 -04001776 store.offset[store.seen] = cf->do_ftell(cf);
Linus Torvaldsd14f7762006-05-09 12:24:02 -07001777 }
seanbdf0ef02006-05-06 14:14:02 -04001778 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01001779 }
1780 return 0;
1781}
1782
Alex Riesen64c0d712008-05-12 23:41:04 +02001783static int write_error(const char *filename)
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001784{
Alex Riesen64c0d712008-05-12 23:41:04 +02001785 error("failed to write new configuration file %s", filename);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001786
1787 /* Same error code as "failed to rename". */
1788 return 4;
1789}
1790
Felipe Contreras4b25d092009-05-01 12:06:36 +03001791static int store_write_section(int fd, const char *key)
Johannes Schindelin10bea152005-11-17 22:32:36 +01001792{
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001793 const char *dot;
1794 int i, success;
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001795 struct strbuf sb = STRBUF_INIT;
Linus Torvaldsd14f7762006-05-09 12:24:02 -07001796
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001797 dot = memchr(key, '.', store.baselen);
Linus Torvaldsd14f7762006-05-09 12:24:02 -07001798 if (dot) {
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001799 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
1800 for (i = dot - key + 1; i < store.baselen; i++) {
Bryan Donlane5c349b2008-05-04 01:37:52 -04001801 if (key[i] == '"' || key[i] == '\\')
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001802 strbuf_addch(&sb, '\\');
1803 strbuf_addch(&sb, key[i]);
Linus Torvaldsd14f7762006-05-09 12:24:02 -07001804 }
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001805 strbuf_addstr(&sb, "\"]\n");
1806 } else {
1807 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
Linus Torvaldsd14f7762006-05-09 12:24:02 -07001808 }
1809
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001810 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
1811 strbuf_release(&sb);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001812
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001813 return success;
Johannes Schindelin10bea152005-11-17 22:32:36 +01001814}
1815
Felipe Contreras4b25d092009-05-01 12:06:36 +03001816static int store_write_pair(int fd, const char *key, const char *value)
Johannes Schindelin10bea152005-11-17 22:32:36 +01001817{
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001818 int i, success;
1819 int length = strlen(key + store.baselen + 1);
1820 const char *quote = "";
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001821 struct strbuf sb = STRBUF_INIT;
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -05001822
Jim Meyering6281f392007-12-08 16:48:05 +01001823 /*
1824 * Check to see if the value needs to be surrounded with a dq pair.
1825 * Note that problematic characters are always backslash-quoted; this
1826 * check is about not losing leading or trailing SP and strings that
1827 * follow beginning-of-comment characters (i.e. ';' and '#') by the
1828 * configuration parser.
1829 */
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -05001830 if (value[0] == ' ')
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001831 quote = "\"";
Brian Gernhardtcdd4fb12007-01-09 00:27:41 -05001832 for (i = 0; value[i]; i++)
1833 if (value[i] == ';' || value[i] == '#')
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001834 quote = "\"";
1835 if (i && value[i - 1] == ' ')
1836 quote = "\"";
Johannes Schindelin10bea152005-11-17 22:32:36 +01001837
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001838 strbuf_addf(&sb, "\t%.*s = %s",
1839 length, key + store.baselen + 1, quote);
1840
Johannes Schindelin10bea152005-11-17 22:32:36 +01001841 for (i = 0; value[i]; i++)
1842 switch (value[i]) {
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001843 case '\n':
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001844 strbuf_addstr(&sb, "\\n");
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001845 break;
1846 case '\t':
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001847 strbuf_addstr(&sb, "\\t");
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001848 break;
1849 case '"':
1850 case '\\':
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001851 strbuf_addch(&sb, '\\');
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001852 default:
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001853 strbuf_addch(&sb, value[i]);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00001854 break;
1855 }
Kristian Høgsbergcb891a52007-12-14 15:59:58 -05001856 strbuf_addf(&sb, "%s\n", quote);
1857
1858 success = write_in_full(fd, sb.buf, sb.len) == sb.len;
1859 strbuf_release(&sb);
1860
1861 return success;
Johannes Schindelin10bea152005-11-17 22:32:36 +01001862}
1863
Felipe Contreras4b25d092009-05-01 12:06:36 +03001864static ssize_t find_beginning_of_line(const char *contents, size_t size,
1865 size_t offset_, int *found_bracket)
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001866{
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001867 size_t equal_offset = size, bracket_offset = size;
1868 ssize_t offset;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001869
Frank Lichtenheld7a31cc02008-02-11 01:23:03 +01001870contline:
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001871 for (offset = offset_-2; offset > 0
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001872 && contents[offset] != '\n'; offset--)
1873 switch (contents[offset]) {
1874 case '=': equal_offset = offset; break;
1875 case ']': bracket_offset = offset; break;
1876 }
Frank Lichtenheld7a31cc02008-02-11 01:23:03 +01001877 if (offset > 0 && contents[offset-1] == '\\') {
1878 offset_ = offset;
1879 goto contline;
1880 }
Johannes Schindelin4ddba792005-11-20 06:52:22 +01001881 if (bracket_offset < equal_offset) {
1882 *found_bracket = 1;
1883 offset = bracket_offset+1;
1884 } else
1885 offset++;
1886
1887 return offset;
1888}
1889
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01001890int git_config_set_in_file_gently(const char *config_filename,
1891 const char *key, const char *value)
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05301892{
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01001893 return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0);
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05301894}
1895
Patrick Steinhardt3d180642016-02-22 12:23:36 +01001896void git_config_set_in_file(const char *config_filename,
1897 const char *key, const char *value)
Johannes Schindelin10bea152005-11-17 22:32:36 +01001898{
Patrick Steinhardt3d180642016-02-22 12:23:36 +01001899 git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01001900}
1901
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01001902int git_config_set_gently(const char *key, const char *value)
Johannes Schindelin10bea152005-11-17 22:32:36 +01001903{
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01001904 return git_config_set_multivar_gently(key, value, NULL, 0);
Johannes Schindelin10bea152005-11-17 22:32:36 +01001905}
1906
Patrick Steinhardt3d180642016-02-22 12:23:36 +01001907void git_config_set(const char *key, const char *value)
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01001908{
Patrick Steinhardt3d180642016-02-22 12:23:36 +01001909 git_config_set_multivar(key, value, NULL, 0);
Johannes Schindelin10bea152005-11-17 22:32:36 +01001910}
1911
1912/*
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001913 * Auxiliary function to sanity-check and split the key into the section
1914 * identifier and variable name.
1915 *
1916 * Returns 0 on success, -1 when there is an invalid character in the key and
1917 * -2 if there is no section name in the key.
1918 *
1919 * store_key - pointer to char* which will hold a copy of the key with
1920 * lowercase section and variable name
1921 * baselen - pointer to int which will hold the length of the
1922 * section + subsection part, can be NULL
1923 */
Jeff King9e9de182015-08-24 02:11:33 -04001924static int git_config_parse_key_1(const char *key, char **store_key, int *baselen_, int quiet)
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001925{
1926 int i, dot, baselen;
1927 const char *last_dot = strrchr(key, '.');
1928
1929 /*
1930 * Since "key" actually contains the section name and the real
1931 * key name separated by a dot, we have to know where the dot is.
1932 */
1933
Libor Pechacek2169ddc2011-02-01 08:13:47 +01001934 if (last_dot == NULL || last_dot == key) {
Jeff King9e9de182015-08-24 02:11:33 -04001935 if (!quiet)
1936 error("key does not contain a section: %s", key);
Michael J Gruber7a397412011-05-17 17:38:52 +02001937 return -CONFIG_NO_SECTION_OR_NAME;
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001938 }
1939
Libor Pechacek2169ddc2011-02-01 08:13:47 +01001940 if (!last_dot[1]) {
Jeff King9e9de182015-08-24 02:11:33 -04001941 if (!quiet)
1942 error("key does not contain variable name: %s", key);
Michael J Gruber7a397412011-05-17 17:38:52 +02001943 return -CONFIG_NO_SECTION_OR_NAME;
Libor Pechacek2169ddc2011-02-01 08:13:47 +01001944 }
1945
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001946 baselen = last_dot - key;
1947 if (baselen_)
1948 *baselen_ = baselen;
1949
1950 /*
1951 * Validate the key and while at it, lower case it for matching.
1952 */
Jeff King9e9de182015-08-24 02:11:33 -04001953 if (store_key)
Jeff King3733e692016-02-22 17:44:28 -05001954 *store_key = xmallocz(strlen(key));
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001955
1956 dot = 0;
1957 for (i = 0; key[i]; i++) {
1958 unsigned char c = key[i];
1959 if (c == '.')
1960 dot = 1;
1961 /* Leave the extended basename untouched.. */
1962 if (!dot || i > baselen) {
1963 if (!iskeychar(c) ||
1964 (i == baselen + 1 && !isalpha(c))) {
Jeff King9e9de182015-08-24 02:11:33 -04001965 if (!quiet)
1966 error("invalid key: %s", key);
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001967 goto out_free_ret_1;
1968 }
1969 c = tolower(c);
1970 } else if (c == '\n') {
Jeff King9e9de182015-08-24 02:11:33 -04001971 if (!quiet)
1972 error("invalid key (newline): %s", key);
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001973 goto out_free_ret_1;
1974 }
Jeff King9e9de182015-08-24 02:11:33 -04001975 if (store_key)
1976 (*store_key)[i] = c;
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001977 }
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001978
1979 return 0;
1980
1981out_free_ret_1:
Jeff King9e9de182015-08-24 02:11:33 -04001982 if (store_key) {
1983 free(*store_key);
1984 *store_key = NULL;
1985 }
Michael J Gruber7a397412011-05-17 17:38:52 +02001986 return -CONFIG_INVALID_KEY;
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001987}
1988
Jeff King9e9de182015-08-24 02:11:33 -04001989int git_config_parse_key(const char *key, char **store_key, int *baselen)
1990{
1991 return git_config_parse_key_1(key, store_key, baselen, 0);
1992}
1993
1994int git_config_key_is_valid(const char *key)
1995{
1996 return !git_config_parse_key_1(key, NULL, NULL, 1);
1997}
1998
Libor Pechacekb09c53a2011-01-30 20:40:41 +01001999/*
Johannes Schindelin10bea152005-11-17 22:32:36 +01002000 * If value==NULL, unset in (remove from) config,
2001 * if value_regex!=NULL, disregard key/value pairs where value does not match.
Jeff Kingc1063be2014-08-19 02:20:00 -04002002 * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
2003 * (only add a new one)
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002004 * if multi_replace==0, nothing, or only one matching key/value is replaced,
2005 * else all matching key/values (regardless how many) are removed,
2006 * before the new pair is written.
Johannes Schindelin10bea152005-11-17 22:32:36 +01002007 *
2008 * Returns 0 on success.
2009 *
2010 * This function does this:
2011 *
2012 * - it locks the config file by creating ".git/config.lock"
2013 *
2014 * - it then parses the config using store_aux() as validator to find
2015 * the position on the key/value pair to replace. If it is to be unset,
2016 * it must be found exactly once.
2017 *
2018 * - the config file is mmap()ed and the part before the match (if any) is
2019 * written to the lock file, then the changed part and the rest.
2020 *
2021 * - the config file is removed and the lock file rename()d to it.
2022 *
2023 */
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002024int git_config_set_multivar_in_file_gently(const char *config_filename,
2025 const char *key, const char *value,
2026 const char *value_regex,
2027 int multi_replace)
Johannes Schindelin10bea152005-11-17 22:32:36 +01002028{
Sven Strickroth54d160e2015-08-14 22:21:17 +02002029 int fd = -1, in_fd = -1;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002030 int ret;
Bradford C. Smith6cbf9732007-07-26 12:55:28 -04002031 struct lock_file *lock = NULL;
Jeff King0a5f5752012-02-16 03:04:05 -05002032 char *filename_buf = NULL;
Jeff King3a1b3122015-05-28 03:54:43 -04002033 char *contents = NULL;
2034 size_t contents_sz;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002035
Libor Pechacekb09c53a2011-01-30 20:40:41 +01002036 /* parse-key returns negative; flip the sign to feed exit(3) */
2037 ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
2038 if (ret)
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002039 goto out_free;
Johannes Schindelinb17e6592005-11-20 21:22:19 +01002040
2041 store.multi_replace = multi_replace;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002042
Jeff King0a5f5752012-02-16 03:04:05 -05002043 if (!config_filename)
2044 config_filename = filename_buf = git_pathdup("config");
Johannes Schindelin10bea152005-11-17 22:32:36 +01002045
2046 /*
Bradford C. Smith6cbf9732007-07-26 12:55:28 -04002047 * The lock serves a purpose in addition to locking: the new
Johannes Schindelin10bea152005-11-17 22:32:36 +01002048 * contents of .git/config will be written into it.
2049 */
Brian Gesiakf1064f62014-05-27 00:33:46 +09002050 lock = xcalloc(1, sizeof(struct lock_file));
Bradford C. Smith6cbf9732007-07-26 12:55:28 -04002051 fd = hold_lock_file_for_update(lock, config_filename, 0);
2052 if (fd < 0) {
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07002053 error_errno("could not lock config file %s", config_filename);
Johannes Schindelin10bea152005-11-17 22:32:36 +01002054 free(store.key);
Michael J Gruber7a397412011-05-17 17:38:52 +02002055 ret = CONFIG_NO_LOCK;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002056 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002057 }
2058
2059 /*
2060 * If .git/config does not exist yet, write a minimal version.
2061 */
Alex Riesen88fb9582006-01-05 12:43:34 +01002062 in_fd = open(config_filename, O_RDONLY);
2063 if ( in_fd < 0 ) {
Johannes Schindelin10bea152005-11-17 22:32:36 +01002064 free(store.key);
2065
Alex Riesen88fb9582006-01-05 12:43:34 +01002066 if ( ENOENT != errno ) {
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07002067 error_errno("opening %s", config_filename);
Michael J Gruber7a397412011-05-17 17:38:52 +02002068 ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002069 goto out_free;
Alex Riesen88fb9582006-01-05 12:43:34 +01002070 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01002071 /* if nothing to unset, error out */
2072 if (value == NULL) {
Michael J Gruber7a397412011-05-17 17:38:52 +02002073 ret = CONFIG_NOTHING_SET;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002074 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002075 }
2076
Felipe Contreras4b25d092009-05-01 12:06:36 +03002077 store.key = (char *)key;
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002078 if (!store_write_section(fd, key) ||
Junio C Hamano93c1e072007-01-11 13:16:26 -08002079 !store_write_pair(fd, key, value))
2080 goto write_err_out;
2081 } else {
Alex Riesen88fb9582006-01-05 12:43:34 +01002082 struct stat st;
Jeff King3a1b3122015-05-28 03:54:43 -04002083 size_t copy_begin, copy_end;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002084 int i, new_line = 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002085
2086 if (value_regex == NULL)
2087 store.value_regex = NULL;
Jeff Kingc1063be2014-08-19 02:20:00 -04002088 else if (value_regex == CONFIG_REGEX_NONE)
2089 store.value_regex = CONFIG_REGEX_NONE;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002090 else {
Johannes Schindelinf98d8632005-11-20 13:24:18 +01002091 if (value_regex[0] == '!') {
2092 store.do_not_match = 1;
2093 value_regex++;
2094 } else
2095 store.do_not_match = 0;
2096
Jonas Fonseca2d7320d2006-09-01 00:32:39 +02002097 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
Johannes Schindelin10bea152005-11-17 22:32:36 +01002098 if (regcomp(store.value_regex, value_regex,
2099 REG_EXTENDED)) {
Alex Riesen64c0d712008-05-12 23:41:04 +02002100 error("invalid pattern: %s", value_regex);
Johannes Schindelin10bea152005-11-17 22:32:36 +01002101 free(store.value_regex);
Michael J Gruber7a397412011-05-17 17:38:52 +02002102 ret = CONFIG_INVALID_PATTERN;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002103 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002104 }
2105 }
2106
Thomas Rast83786fa2013-11-13 11:19:00 +01002107 ALLOC_GROW(store.offset, 1, store.offset_alloc);
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002108 store.offset[0] = 0;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002109 store.state = START;
2110 store.seen = 0;
2111
2112 /*
2113 * After this, store.offset will contain the *end* offset
2114 * of the last match, or remain at 0 if no match was found.
2115 * As a side effect, we make sure to transform only a valid
2116 * existing config file.
2117 */
Johannes Schindelinef90d6d2008-05-14 18:46:53 +01002118 if (git_config_from_file(store_aux, config_filename, NULL)) {
Alex Riesen64c0d712008-05-12 23:41:04 +02002119 error("invalid config file %s", config_filename);
Johannes Schindelin10bea152005-11-17 22:32:36 +01002120 free(store.key);
Jeff Kingc1063be2014-08-19 02:20:00 -04002121 if (store.value_regex != NULL &&
2122 store.value_regex != CONFIG_REGEX_NONE) {
Johannes Schindelin10bea152005-11-17 22:32:36 +01002123 regfree(store.value_regex);
2124 free(store.value_regex);
2125 }
Michael J Gruber7a397412011-05-17 17:38:52 +02002126 ret = CONFIG_INVALID_FILE;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002127 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002128 }
2129
2130 free(store.key);
Jeff Kingc1063be2014-08-19 02:20:00 -04002131 if (store.value_regex != NULL &&
2132 store.value_regex != CONFIG_REGEX_NONE) {
Johannes Schindelin10bea152005-11-17 22:32:36 +01002133 regfree(store.value_regex);
2134 free(store.value_regex);
2135 }
2136
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002137 /* if nothing to unset, or too many matches, error out */
2138 if ((store.seen == 0 && value == NULL) ||
2139 (store.seen > 1 && multi_replace == 0)) {
Michael J Gruber7a397412011-05-17 17:38:52 +02002140 ret = CONFIG_NOTHING_SET;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002141 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002142 }
2143
Alex Riesen88fb9582006-01-05 12:43:34 +01002144 fstat(in_fd, &st);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002145 contents_sz = xsize_t(st.st_size);
Jeff King15708562015-05-28 03:56:15 -04002146 contents = xmmap_gently(NULL, contents_sz, PROT_READ,
2147 MAP_PRIVATE, in_fd, 0);
2148 if (contents == MAP_FAILED) {
Jeff King0e8771f2015-05-28 04:03:01 -04002149 if (errno == ENODEV && S_ISDIR(st.st_mode))
2150 errno = EISDIR;
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07002151 error_errno("unable to mmap '%s'", config_filename);
Jeff King15708562015-05-28 03:56:15 -04002152 ret = CONFIG_INVALID_FILE;
2153 contents = NULL;
2154 goto out_free;
2155 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01002156 close(in_fd);
Sven Strickroth54d160e2015-08-14 22:21:17 +02002157 in_fd = -1;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002158
Michael Haggertyb4fb09e2015-08-10 11:47:39 +02002159 if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07002160 error_errno("chmod on %s failed", get_lock_file_path(lock));
Eric Wongdaa22c62014-05-06 00:17:14 +00002161 ret = CONFIG_NO_WRITE;
2162 goto out_free;
2163 }
2164
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002165 if (store.seen == 0)
2166 store.seen = 1;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002167
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002168 for (i = 0, copy_begin = 0; i < store.seen; i++) {
2169 if (store.offset[i] == 0) {
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002170 store.offset[i] = copy_end = contents_sz;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002171 } else if (store.state != KEY_SEEN) {
2172 copy_end = store.offset[i];
Johannes Schindelin10bea152005-11-17 22:32:36 +01002173 } else
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002174 copy_end = find_beginning_of_line(
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002175 contents, contents_sz,
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002176 store.offset[i]-2, &new_line);
Johannes Schindelin10bea152005-11-17 22:32:36 +01002177
Jeff King02e5ba42008-01-01 01:17:34 -05002178 if (copy_end > 0 && contents[copy_end-1] != '\n')
2179 new_line = 1;
2180
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002181 /* write the first part of the config */
2182 if (copy_end > copy_begin) {
Junio C Hamano93c1e072007-01-11 13:16:26 -08002183 if (write_in_full(fd, contents + copy_begin,
2184 copy_end - copy_begin) <
2185 copy_end - copy_begin)
2186 goto write_err_out;
2187 if (new_line &&
Jim Meyering2b7ca832009-09-12 10:54:32 +02002188 write_str_in_full(fd, "\n") != 1)
Junio C Hamano93c1e072007-01-11 13:16:26 -08002189 goto write_err_out;
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002190 }
2191 copy_begin = store.offset[i];
2192 }
Johannes Schindelin10bea152005-11-17 22:32:36 +01002193
2194 /* write the pair (value == NULL means unset) */
2195 if (value != NULL) {
Junio C Hamano93c1e072007-01-11 13:16:26 -08002196 if (store.state == START) {
2197 if (!store_write_section(fd, key))
2198 goto write_err_out;
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002199 }
Junio C Hamano93c1e072007-01-11 13:16:26 -08002200 if (!store_write_pair(fd, key, value))
2201 goto write_err_out;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002202 }
2203
2204 /* write the rest of the config */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002205 if (copy_begin < contents_sz)
Junio C Hamano93c1e072007-01-11 13:16:26 -08002206 if (write_in_full(fd, contents + copy_begin,
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002207 contents_sz - copy_begin) <
2208 contents_sz - copy_begin)
Junio C Hamano93c1e072007-01-11 13:16:26 -08002209 goto write_err_out;
Karsten Blees7a645922015-06-30 16:34:13 +02002210
2211 munmap(contents, contents_sz);
2212 contents = NULL;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002213 }
2214
Brandon Casey4ed7cd32008-01-16 13:12:46 -06002215 if (commit_lock_file(lock) < 0) {
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07002216 error_errno("could not write config file %s", config_filename);
Michael J Gruber7a397412011-05-17 17:38:52 +02002217 ret = CONFIG_NO_WRITE;
Michael Haggertye8318552014-10-01 12:28:26 +02002218 lock = NULL;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002219 goto out_free;
Johannes Schindelin10bea152005-11-17 22:32:36 +01002220 }
2221
Bradford C. Smith6cbf9732007-07-26 12:55:28 -04002222 /*
2223 * lock is committed, so don't try to roll it back below.
2224 * NOTE: Since lockfile.c keeps a linked list of all created
2225 * lock_file structures, it isn't safe to free(lock). It's
2226 * better to just leave it hanging around.
2227 */
2228 lock = NULL;
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002229 ret = 0;
2230
Tanay Abhra3c8687a2014-07-28 03:10:38 -07002231 /* Invalidate the config cache */
2232 git_config_clear();
2233
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002234out_free:
Bradford C. Smith6cbf9732007-07-26 12:55:28 -04002235 if (lock)
2236 rollback_lock_file(lock);
Jeff King0a5f5752012-02-16 03:04:05 -05002237 free(filename_buf);
Jeff King3a1b3122015-05-28 03:54:43 -04002238 if (contents)
2239 munmap(contents, contents_sz);
Sven Strickroth54d160e2015-08-14 22:21:17 +02002240 if (in_fd >= 0)
2241 close(in_fd);
Serge E. Hallyndafc88b2006-04-17 10:14:48 -05002242 return ret;
Junio C Hamano93c1e072007-01-11 13:16:26 -08002243
2244write_err_out:
Michael Haggertyb4fb09e2015-08-10 11:47:39 +02002245 ret = write_error(get_lock_file_path(lock));
Junio C Hamano93c1e072007-01-11 13:16:26 -08002246 goto out_free;
2247
Johannes Schindelin10bea152005-11-17 22:32:36 +01002248}
2249
Patrick Steinhardt3d180642016-02-22 12:23:36 +01002250void git_config_set_multivar_in_file(const char *config_filename,
2251 const char *key, const char *value,
2252 const char *value_regex, int multi_replace)
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05302253{
Jeff King1cae4282016-04-09 13:43:54 -04002254 if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
2255 value_regex, multi_replace))
2256 return;
2257 if (value)
Jeff King8c3ca352016-04-09 13:42:31 -04002258 die(_("could not set '%s' to '%s'"), key, value);
Jeff King1cae4282016-04-09 13:43:54 -04002259 else
2260 die(_("could not unset '%s'"), key);
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01002261}
2262
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002263int git_config_set_multivar_gently(const char *key, const char *value,
2264 const char *value_regex, int multi_replace)
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05302265{
Patrick Steinhardt30598ad2016-02-22 12:23:35 +01002266 return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
2267 multi_replace);
Ramkumar Ramachandra5ec31182011-08-04 16:09:00 +05302268}
2269
Patrick Steinhardt3d180642016-02-22 12:23:36 +01002270void git_config_set_multivar(const char *key, const char *value,
2271 const char *value_regex, int multi_replace)
Patrick Steinhardtb4c8aba2016-02-16 13:56:28 +01002272{
Patrick Steinhardt3d180642016-02-22 12:23:36 +01002273 git_config_set_multivar_in_file(NULL, key, value, value_regex,
2274 multi_replace);
Paolo Bonzini118f8b22007-03-02 21:53:33 +01002275}
2276
2277static int section_name_match (const char *buf, const char *name)
2278{
2279 int i = 0, j = 0, dot = 0;
2280 if (buf[i] != '[')
2281 return 0;
2282 for (i = 1; buf[i] && buf[i] != ']'; i++) {
2283 if (!dot && isspace(buf[i])) {
2284 dot = 1;
Alex Vandivera4c0d462009-07-24 17:21:43 -04002285 if (name[j++] != '.')
2286 break;
2287 for (i++; isspace(buf[i]); i++)
2288 ; /* do nothing */
2289 if (buf[i] != '"')
2290 break;
2291 continue;
2292 }
2293 if (buf[i] == '\\' && dot)
2294 i++;
2295 else if (buf[i] == '"' && dot) {
Paolo Bonzini118f8b22007-03-02 21:53:33 +01002296 for (i++; isspace(buf[i]); i++)
2297 ; /* do_nothing */
2298 break;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002299 }
2300 if (buf[i] != name[j++])
Paolo Bonzini118f8b22007-03-02 21:53:33 +01002301 break;
Junio C Hamanofc1905b2006-12-19 21:55:27 -08002302 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002303 if (buf[i] == ']' && name[j] == 0) {
2304 /*
2305 * We match, now just find the right length offset by
Ramsay Jones924aaf32011-06-16 21:24:51 +01002306 * gobbling up any whitespace after it, as well
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002307 */
Daniel Barkalowdc871832008-06-30 03:37:47 -04002308 i++;
2309 for (; buf[i] && isspace(buf[i]); i++)
2310 ; /* do nothing */
Alex Riesena4f34cb2008-10-27 11:22:09 +01002311 return i;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002312 }
Junio C Hamanofc1905b2006-12-19 21:55:27 -08002313 return 0;
Alex Riesen64c0d712008-05-12 23:41:04 +02002314}
Junio C Hamanofc1905b2006-12-19 21:55:27 -08002315
Jeff King94a35b12012-04-25 21:47:14 -04002316static int section_name_is_ok(const char *name)
2317{
2318 /* Empty section names are bogus. */
2319 if (!*name)
2320 return 0;
2321
2322 /*
2323 * Before a dot, we must be alphanumeric or dash. After the first dot,
2324 * anything goes, so we can stop checking.
2325 */
2326 for (; *name && *name != '.'; name++)
2327 if (*name != '-' && !isalnum(*name))
2328 return 0;
2329 return 1;
2330}
2331
Junio C Hamanofc1905b2006-12-19 21:55:27 -08002332/* if new_name == NULL, the section is removed instead */
Jeff King42bd39b2012-02-16 03:04:25 -05002333int git_config_rename_section_in_file(const char *config_filename,
2334 const char *old_name, const char *new_name)
Geert Bosch01ebb9d2007-04-05 10:20:55 -04002335{
Junio C Hamanofc1905b2006-12-19 21:55:27 -08002336 int ret = 0, remove = 0;
Jeff King42bd39b2012-02-16 03:04:25 -05002337 char *filename_buf = NULL;
Jeff King94a35b12012-04-25 21:47:14 -04002338 struct lock_file *lock;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002339 int out_fd;
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002340 char buf[1024];
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04002341 FILE *config_file;
Eric Wongdaa22c62014-05-06 00:17:14 +00002342 struct stat st;
Linus Torvalds17712992005-10-10 16:31:08 -07002343
Jeff King94a35b12012-04-25 21:47:14 -04002344 if (new_name && !section_name_is_ok(new_name)) {
2345 ret = error("invalid section name: %s", new_name);
2346 goto out;
2347 }
2348
Jeff King42bd39b2012-02-16 03:04:25 -05002349 if (!config_filename)
2350 config_filename = filename_buf = git_pathdup("config");
2351
Brian Gesiakf1064f62014-05-27 00:33:46 +09002352 lock = xcalloc(1, sizeof(struct lock_file));
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002353 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2354 if (out_fd < 0) {
2355 ret = error("could not lock config file %s", config_filename);
2356 goto out;
2357 }
2358
2359 if (!(config_file = fopen(config_filename, "rb"))) {
2360 /* no config file means nothing to rename, no error */
2361 goto unlock_and_out;
2362 }
2363
Eric Wongdaa22c62014-05-06 00:17:14 +00002364 fstat(fileno(config_file), &st);
2365
Michael Haggertyb4fb09e2015-08-10 11:47:39 +02002366 if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) {
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07002367 ret = error_errno("chmod on %s failed",
2368 get_lock_file_path(lock));
Eric Wongdaa22c62014-05-06 00:17:14 +00002369 goto out;
2370 }
2371
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002372 while (fgets(buf, sizeof(buf), config_file)) {
2373 int i;
2374 int length;
2375 char *output = buf;
2376 for (i = 0; buf[i] && isspace(buf[i]); i++)
2377 ; /* do nothing */
2378 if (buf[i] == '[') {
2379 /* it's a section */
Alex Vandivera4c0d462009-07-24 17:21:43 -04002380 int offset = section_name_match(&buf[i], old_name);
2381 if (offset > 0) {
Paolo Bonzini118f8b22007-03-02 21:53:33 +01002382 ret++;
2383 if (new_name == NULL) {
2384 remove = 1;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002385 continue;
2386 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002387 store.baselen = strlen(new_name);
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002388 if (!store_write_section(out_fd, new_name)) {
Michael Haggertyb4fb09e2015-08-10 11:47:39 +02002389 ret = write_error(get_lock_file_path(lock));
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002390 goto out;
2391 }
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04002392 /*
2393 * We wrote out the new section, with
2394 * a newline, now skip the old
2395 * section's length
2396 */
2397 output += offset + i;
2398 if (strlen(output) > 0) {
2399 /*
2400 * More content means there's
2401 * a declaration to put on the
2402 * next line; indent with a
2403 * tab
2404 */
2405 output -= 1;
2406 output[0] = '\t';
2407 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002408 }
Paolo Bonzini118f8b22007-03-02 21:53:33 +01002409 remove = 0;
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002410 }
Paolo Bonzini118f8b22007-03-02 21:53:33 +01002411 if (remove)
2412 continue;
Alex Vandiver9a5abfc2009-07-24 17:21:44 -04002413 length = strlen(output);
2414 if (write_in_full(out_fd, output, length) != length) {
Michael Haggertyb4fb09e2015-08-10 11:47:39 +02002415 ret = write_error(get_lock_file_path(lock));
Andy Whitcroft480c9e52007-01-08 15:58:38 +00002416 goto out;
2417 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002418 }
Junio C Hamanofc1905b2006-12-19 21:55:27 -08002419 fclose(config_file);
Pavan Kumar Sunkara8b590072011-07-17 03:25:52 +05302420unlock_and_out:
Brandon Casey4ed7cd32008-01-16 13:12:46 -06002421 if (commit_lock_file(lock) < 0)
Nguyễn Thái Ngọc Duyf0658ec2016-05-08 16:47:38 +07002422 ret = error_errno("could not write config file %s",
2423 config_filename);
Pavan Kumar Sunkara8b590072011-07-17 03:25:52 +05302424out:
Jeff King42bd39b2012-02-16 03:04:25 -05002425 free(filename_buf);
Johannes Schindelin0667fcf2006-12-16 15:14:14 +01002426 return ret;
2427}
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08002428
Jeff King42bd39b2012-02-16 03:04:25 -05002429int git_config_rename_section(const char *old_name, const char *new_name)
2430{
Jeff King4a7bb5b2012-02-16 03:09:32 -05002431 return git_config_rename_section_in_file(NULL, old_name, new_name);
Jeff King42bd39b2012-02-16 03:04:25 -05002432}
2433
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08002434/*
2435 * Call this to report error for your variable that should not
2436 * get a boolean value (i.e. "[my] var" means "true").
2437 */
Jeff Kinga469a102012-12-15 12:42:10 -05002438#undef config_error_nonbool
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08002439int config_error_nonbool(const char *var)
2440{
Jeff King8c3ca352016-04-09 13:42:31 -04002441 return error("missing value for '%s'", var);
Junio C Hamano40ea4ed2008-02-11 10:41:18 -08002442}
Jeff King1b86bbb2013-01-23 01:23:05 -05002443
2444int parse_config_key(const char *var,
2445 const char *section,
2446 const char **subsection, int *subsection_len,
2447 const char **key)
2448{
2449 int section_len = strlen(section);
2450 const char *dot;
2451
2452 /* Does it start with "section." ? */
Christian Couder59556542013-11-30 21:55:40 +01002453 if (!starts_with(var, section) || var[section_len] != '.')
Jeff King1b86bbb2013-01-23 01:23:05 -05002454 return -1;
2455
2456 /*
2457 * Find the key; we don't know yet if we have a subsection, but we must
2458 * parse backwards from the end, since the subsection may have dots in
2459 * it, too.
2460 */
2461 dot = strrchr(var, '.');
2462 *key = dot + 1;
2463
2464 /* Did we have a subsection at all? */
2465 if (dot == var + section_len) {
2466 *subsection = NULL;
2467 *subsection_len = 0;
2468 }
2469 else {
2470 *subsection = var + section_len + 1;
2471 *subsection_len = dot - *subsection;
2472 }
2473
2474 return 0;
2475}
Lars Schneider473166b2016-02-19 10:16:01 +01002476
2477const char *current_config_origin_type(void)
2478{
Jeff King0d44a2d2016-05-26 20:32:23 -04002479 const char *type;
2480 if (current_config_kvi)
2481 type = current_config_kvi->origin_type;
2482 else if(cf)
2483 type = cf->origin_type;
2484 else
Jeff King32582582016-05-18 18:41:08 -04002485 die("BUG: current_config_origin_type called outside config callback");
Jeff King0d44a2d2016-05-26 20:32:23 -04002486 return type ? type : "command line";
Lars Schneider473166b2016-02-19 10:16:01 +01002487}
2488
2489const char *current_config_name(void)
2490{
Jeff King0d44a2d2016-05-26 20:32:23 -04002491 const char *name;
2492 if (current_config_kvi)
2493 name = current_config_kvi->filename;
2494 else if (cf)
2495 name = cf->name;
2496 else
Jeff King32582582016-05-18 18:41:08 -04002497 die("BUG: current_config_name called outside config callback");
Jeff King0d44a2d2016-05-26 20:32:23 -04002498 return name ? name : "";
Lars Schneider473166b2016-02-19 10:16:01 +01002499}
Jeff King9acc5912016-05-18 18:44:23 -04002500
2501enum config_scope current_config_scope(void)
2502{
2503 if (current_config_kvi)
2504 return current_config_kvi->scope;
2505 else
2506 return current_parsing_scope;
Linus Torvalds17712992005-10-10 16:31:08 -07002507}