]>
Commit | Line | Data |
---|---|---|
10bea152 JS |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | * Copyright (C) Johannes Schindelin, 2005 | |
6 | * | |
7 | */ | |
17712991 | 8 | #include "cache.h" |
7f0e39fa | 9 | #include "exec_cmd.h" |
572e4f6a | 10 | #include "strbuf.h" |
2b64fc89 | 11 | #include "quote.h" |
17712991 | 12 | |
4d8dd149 HV |
13 | struct config_source { |
14 | struct config_source *prev; | |
15 | union { | |
16 | FILE *file; | |
17 | } u; | |
924aaf3e RJ |
18 | const char *name; |
19 | int linenr; | |
20 | int eof; | |
21 | struct strbuf value; | |
0971e992 | 22 | struct strbuf var; |
924aaf3e | 23 | |
4d8dd149 HV |
24 | int (*fgetc)(struct config_source *c); |
25 | int (*ungetc)(int c, struct config_source *conf); | |
26 | long (*ftell)(struct config_source *c); | |
27 | }; | |
28 | ||
29 | static struct config_source *cf; | |
924aaf3e | 30 | |
960ccca6 DH |
31 | static int zlib_compression_seen; |
32 | ||
4d8dd149 HV |
33 | static int config_file_fgetc(struct config_source *conf) |
34 | { | |
35 | return fgetc(conf->u.file); | |
36 | } | |
37 | ||
38 | static int config_file_ungetc(int c, struct config_source *conf) | |
39 | { | |
40 | return ungetc(c, conf->u.file); | |
41 | } | |
42 | ||
43 | static long config_file_ftell(struct config_source *conf) | |
44 | { | |
45 | return ftell(conf->u.file); | |
46 | } | |
47 | ||
9b25a0b5 JK |
48 | #define MAX_INCLUDE_DEPTH 10 |
49 | static const char include_depth_advice[] = | |
50 | "exceeded maximum include depth (%d) while including\n" | |
51 | " %s\n" | |
52 | "from\n" | |
53 | " %s\n" | |
54 | "Do you have circular includes?"; | |
55 | static int handle_path_include(const char *path, struct config_include_data *inc) | |
56 | { | |
57 | int ret = 0; | |
58 | struct strbuf buf = STRBUF_INIT; | |
4c0a89fc JK |
59 | char *expanded = expand_user_path(path); |
60 | ||
61 | if (!expanded) | |
62 | return error("Could not expand include path '%s'", path); | |
63 | path = expanded; | |
9b25a0b5 JK |
64 | |
65 | /* | |
66 | * Use an absolute path as-is, but interpret relative paths | |
67 | * based on the including config file. | |
68 | */ | |
69 | if (!is_absolute_path(path)) { | |
70 | char *slash; | |
71 | ||
72 | if (!cf || !cf->name) | |
73 | return error("relative config includes must come from files"); | |
74 | ||
75 | slash = find_last_dir_sep(cf->name); | |
76 | if (slash) | |
77 | strbuf_add(&buf, cf->name, slash - cf->name + 1); | |
78 | strbuf_addstr(&buf, path); | |
79 | path = buf.buf; | |
80 | } | |
81 | ||
8f2bbe45 | 82 | if (!access_or_die(path, R_OK)) { |
9b25a0b5 JK |
83 | if (++inc->depth > MAX_INCLUDE_DEPTH) |
84 | die(include_depth_advice, MAX_INCLUDE_DEPTH, path, | |
85 | cf && cf->name ? cf->name : "the command line"); | |
86 | ret = git_config_from_file(git_config_include, path, inc); | |
87 | inc->depth--; | |
88 | } | |
89 | strbuf_release(&buf); | |
4c0a89fc | 90 | free(expanded); |
9b25a0b5 JK |
91 | return ret; |
92 | } | |
93 | ||
94 | int git_config_include(const char *var, const char *value, void *data) | |
95 | { | |
96 | struct config_include_data *inc = data; | |
97 | const char *type; | |
98 | int ret; | |
99 | ||
100 | /* | |
101 | * Pass along all values, including "include" directives; this makes it | |
102 | * possible to query information on the includes themselves. | |
103 | */ | |
104 | ret = inc->fn(var, value, inc->data); | |
105 | if (ret < 0) | |
106 | return ret; | |
107 | ||
108 | type = skip_prefix(var, "include."); | |
109 | if (!type) | |
110 | return ret; | |
111 | ||
112 | if (!strcmp(type, "path")) | |
113 | ret = handle_path_include(value, inc); | |
114 | return ret; | |
115 | } | |
116 | ||
8b1fa778 AR |
117 | static void lowercase(char *p) |
118 | { | |
119 | for (; *p; p++) | |
120 | *p = tolower(*p); | |
121 | } | |
8b1fa778 | 122 | |
2b64fc89 JK |
123 | void git_config_push_parameter(const char *text) |
124 | { | |
125 | struct strbuf env = STRBUF_INIT; | |
126 | const char *old = getenv(CONFIG_DATA_ENVIRONMENT); | |
127 | if (old) { | |
128 | strbuf_addstr(&env, old); | |
129 | strbuf_addch(&env, ' '); | |
130 | } | |
131 | sq_quote_buf(&env, text); | |
132 | setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1); | |
133 | strbuf_release(&env); | |
134 | } | |
135 | ||
2496844b JK |
136 | int git_config_parse_parameter(const char *text, |
137 | config_fn_t fn, void *data) | |
8b1fa778 | 138 | { |
572e4f6a | 139 | struct strbuf **pair; |
f77bccae | 140 | pair = strbuf_split_str(text, '=', 2); |
c5d6350b JK |
141 | if (!pair[0]) |
142 | return error("bogus config parameter: %s", text); | |
572e4f6a AR |
143 | if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') |
144 | strbuf_setlen(pair[0], pair[0]->len - 1); | |
145 | strbuf_trim(pair[0]); | |
146 | if (!pair[0]->len) { | |
147 | strbuf_list_free(pair); | |
06eb708f | 148 | return error("bogus config parameter: %s", text); |
8b1fa778 | 149 | } |
06eb708f JK |
150 | lowercase(pair[0]->buf); |
151 | if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) { | |
152 | strbuf_list_free(pair); | |
153 | return -1; | |
572e4f6a AR |
154 | } |
155 | strbuf_list_free(pair); | |
8b1fa778 AR |
156 | return 0; |
157 | } | |
158 | ||
06eb708f JK |
159 | int git_config_from_parameters(config_fn_t fn, void *data) |
160 | { | |
2b64fc89 JK |
161 | const char *env = getenv(CONFIG_DATA_ENVIRONMENT); |
162 | char *envw; | |
163 | const char **argv = NULL; | |
164 | int nr = 0, alloc = 0; | |
165 | int i; | |
166 | ||
167 | if (!env) | |
168 | return 0; | |
169 | /* sq_dequote will write over it */ | |
170 | envw = xstrdup(env); | |
171 | ||
172 | if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) { | |
173 | free(envw); | |
174 | return error("bogus format in " CONFIG_DATA_ENVIRONMENT); | |
175 | } | |
176 | ||
177 | for (i = 0; i < nr; i++) { | |
06eb708f | 178 | if (git_config_parse_parameter(argv[i], fn, data) < 0) { |
2b64fc89 JK |
179 | free(argv); |
180 | free(envw); | |
181 | return -1; | |
182 | } | |
183 | } | |
184 | ||
185 | free(argv); | |
186 | free(envw); | |
06eb708f | 187 | return nr > 0; |
2b64fc89 JK |
188 | } |
189 | ||
17712991 LT |
190 | static int get_next_char(void) |
191 | { | |
4d8dd149 | 192 | int c = cf->fgetc(cf); |
17712991 | 193 | |
dbb9a812 HV |
194 | if (c == '\r') { |
195 | /* DOS like systems */ | |
4d8dd149 | 196 | c = cf->fgetc(cf); |
dbb9a812 | 197 | if (c != '\n') { |
4d8dd149 | 198 | cf->ungetc(c, cf); |
dbb9a812 | 199 | c = '\r'; |
17712991 LT |
200 | } |
201 | } | |
dbb9a812 HV |
202 | if (c == '\n') |
203 | cf->linenr++; | |
204 | if (c == EOF) { | |
205 | cf->eof = 1; | |
206 | c = '\n'; | |
207 | } | |
17712991 LT |
208 | return c; |
209 | } | |
210 | ||
211 | static char *parse_value(void) | |
212 | { | |
e96c19c5 | 213 | int quote = 0, comment = 0, space = 0; |
17712991 | 214 | |
924aaf3e | 215 | strbuf_reset(&cf->value); |
17712991 LT |
216 | for (;;) { |
217 | int c = get_next_char(); | |
17712991 | 218 | if (c == '\n') { |
4b340593 MS |
219 | if (quote) { |
220 | cf->linenr--; | |
17712991 | 221 | return NULL; |
4b340593 | 222 | } |
924aaf3e | 223 | return cf->value.buf; |
17712991 LT |
224 | } |
225 | if (comment) | |
226 | continue; | |
227 | if (isspace(c) && !quote) { | |
924aaf3e | 228 | if (cf->value.len) |
ebdaae37 | 229 | space++; |
17712991 LT |
230 | continue; |
231 | } | |
7ebdba61 JS |
232 | if (!quote) { |
233 | if (c == ';' || c == '#') { | |
234 | comment = 1; | |
235 | continue; | |
236 | } | |
237 | } | |
ebdaae37 | 238 | for (; space; space--) |
924aaf3e | 239 | strbuf_addch(&cf->value, ' '); |
17712991 LT |
240 | if (c == '\\') { |
241 | c = get_next_char(); | |
242 | switch (c) { | |
243 | case '\n': | |
244 | continue; | |
245 | case 't': | |
246 | c = '\t'; | |
247 | break; | |
248 | case 'b': | |
249 | c = '\b'; | |
250 | break; | |
251 | case 'n': | |
252 | c = '\n'; | |
253 | break; | |
5cbb401d LT |
254 | /* Some characters escape as themselves */ |
255 | case '\\': case '"': | |
256 | break; | |
257 | /* Reject unknown escape sequences */ | |
258 | default: | |
259 | return NULL; | |
17712991 | 260 | } |
924aaf3e | 261 | strbuf_addch(&cf->value, c); |
17712991 LT |
262 | continue; |
263 | } | |
264 | if (c == '"') { | |
265 | quote = 1-quote; | |
266 | continue; | |
267 | } | |
924aaf3e | 268 | strbuf_addch(&cf->value, c); |
17712991 LT |
269 | } |
270 | } | |
271 | ||
38c5afa8 LT |
272 | static inline int iskeychar(int c) |
273 | { | |
274 | return isalnum(c) || c == '-'; | |
275 | } | |
276 | ||
0971e992 | 277 | static int get_value(config_fn_t fn, void *data, struct strbuf *name) |
17712991 LT |
278 | { |
279 | int c; | |
280 | char *value; | |
281 | ||
282 | /* Get the full name */ | |
283 | for (;;) { | |
284 | c = get_next_char(); | |
924aaf3e | 285 | if (cf->eof) |
17712991 | 286 | break; |
38c5afa8 | 287 | if (!iskeychar(c)) |
17712991 | 288 | break; |
0971e992 | 289 | strbuf_addch(name, tolower(c)); |
17712991 | 290 | } |
0971e992 | 291 | |
17712991 LT |
292 | while (c == ' ' || c == '\t') |
293 | c = get_next_char(); | |
294 | ||
295 | value = NULL; | |
296 | if (c != '\n') { | |
297 | if (c != '=') | |
298 | return -1; | |
299 | value = parse_value(); | |
300 | if (!value) | |
301 | return -1; | |
302 | } | |
0971e992 | 303 | return fn(name->buf, value, data); |
17712991 LT |
304 | } |
305 | ||
0971e992 | 306 | static int get_extended_base_var(struct strbuf *name, int c) |
d14f7764 LT |
307 | { |
308 | do { | |
309 | if (c == '\n') | |
4b340593 | 310 | goto error_incomplete_line; |
d14f7764 LT |
311 | c = get_next_char(); |
312 | } while (isspace(c)); | |
313 | ||
314 | /* We require the format to be '[base "extension"]' */ | |
315 | if (c != '"') | |
316 | return -1; | |
0971e992 | 317 | strbuf_addch(name, '.'); |
d14f7764 LT |
318 | |
319 | for (;;) { | |
320 | int c = get_next_char(); | |
321 | if (c == '\n') | |
4b340593 | 322 | goto error_incomplete_line; |
d14f7764 LT |
323 | if (c == '"') |
324 | break; | |
325 | if (c == '\\') { | |
326 | c = get_next_char(); | |
327 | if (c == '\n') | |
4b340593 | 328 | goto error_incomplete_line; |
d14f7764 | 329 | } |
0971e992 | 330 | strbuf_addch(name, c); |
d14f7764 LT |
331 | } |
332 | ||
333 | /* Final ']' */ | |
334 | if (get_next_char() != ']') | |
335 | return -1; | |
0971e992 | 336 | return 0; |
4b340593 MS |
337 | error_incomplete_line: |
338 | cf->linenr--; | |
339 | return -1; | |
d14f7764 LT |
340 | } |
341 | ||
0971e992 | 342 | static int get_base_var(struct strbuf *name) |
17712991 | 343 | { |
17712991 LT |
344 | for (;;) { |
345 | int c = get_next_char(); | |
924aaf3e | 346 | if (cf->eof) |
17712991 LT |
347 | return -1; |
348 | if (c == ']') | |
0971e992 | 349 | return 0; |
d14f7764 | 350 | if (isspace(c)) |
0971e992 | 351 | return get_extended_base_var(name, c); |
38c5afa8 | 352 | if (!iskeychar(c) && c != '.') |
17712991 | 353 | return -1; |
0971e992 | 354 | strbuf_addch(name, tolower(c)); |
17712991 LT |
355 | } |
356 | } | |
357 | ||
4d8dd149 | 358 | static int git_parse_source(config_fn_t fn, void *data) |
17712991 LT |
359 | { |
360 | int comment = 0; | |
361 | int baselen = 0; | |
0971e992 | 362 | struct strbuf *var = &cf->var; |
17712991 | 363 | |
de056402 PB |
364 | /* U+FEFF Byte Order Mark in UTF8 */ |
365 | static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; | |
366 | const unsigned char *bomptr = utf8_bom; | |
367 | ||
17712991 LT |
368 | for (;;) { |
369 | int c = get_next_char(); | |
de056402 PB |
370 | if (bomptr && *bomptr) { |
371 | /* We are at the file beginning; skip UTF8-encoded BOM | |
372 | * if present. Sane editors won't put this in on their | |
373 | * own, but e.g. Windows Notepad will do it happily. */ | |
374 | if ((unsigned char) c == *bomptr) { | |
375 | bomptr++; | |
376 | continue; | |
377 | } else { | |
378 | /* Do not tolerate partial BOM. */ | |
379 | if (bomptr != utf8_bom) | |
380 | break; | |
381 | /* No BOM at file beginning. Cool. */ | |
382 | bomptr = NULL; | |
383 | } | |
384 | } | |
17712991 | 385 | if (c == '\n') { |
924aaf3e | 386 | if (cf->eof) |
17712991 LT |
387 | return 0; |
388 | comment = 0; | |
389 | continue; | |
390 | } | |
391 | if (comment || isspace(c)) | |
392 | continue; | |
393 | if (c == '#' || c == ';') { | |
394 | comment = 1; | |
395 | continue; | |
396 | } | |
397 | if (c == '[') { | |
0971e992 BW |
398 | /* Reset prior to determining a new stem */ |
399 | strbuf_reset(var); | |
400 | if (get_base_var(var) < 0 || var->len < 1) | |
17712991 | 401 | break; |
0971e992 BW |
402 | strbuf_addch(var, '.'); |
403 | baselen = var->len; | |
17712991 LT |
404 | continue; |
405 | } | |
406 | if (!isalpha(c)) | |
407 | break; | |
0971e992 BW |
408 | /* |
409 | * Truncate the var name back to the section header | |
410 | * stem prior to grabbing the suffix part of the name | |
411 | * and the value. | |
412 | */ | |
413 | strbuf_setlen(var, baselen); | |
414 | strbuf_addch(var, tolower(c)); | |
415 | if (get_value(fn, data, var) < 0) | |
17712991 LT |
416 | break; |
417 | } | |
924aaf3e | 418 | die("bad config file line %d in %s", cf->linenr, cf->name); |
17712991 LT |
419 | } |
420 | ||
ebaa1bd4 | 421 | static int parse_unit_factor(const char *end, uintmax_t *val) |
0b87b6e0 BD |
422 | { |
423 | if (!*end) | |
424 | return 1; | |
c8deb5a1 SP |
425 | else if (!strcasecmp(end, "k")) { |
426 | *val *= 1024; | |
427 | return 1; | |
428 | } | |
429 | else if (!strcasecmp(end, "m")) { | |
430 | *val *= 1024 * 1024; | |
431 | return 1; | |
432 | } | |
433 | else if (!strcasecmp(end, "g")) { | |
434 | *val *= 1024 * 1024 * 1024; | |
435 | return 1; | |
436 | } | |
437 | return 0; | |
0b87b6e0 BD |
438 | } |
439 | ||
0433bcd9 | 440 | static int git_parse_long(const char *value, long *ret) |
0b87b6e0 BD |
441 | { |
442 | if (value && *value) { | |
443 | char *end; | |
ebaa1bd4 NA |
444 | intmax_t val; |
445 | uintmax_t uval; | |
446 | uintmax_t factor = 1; | |
447 | ||
448 | errno = 0; | |
449 | val = strtoimax(value, &end, 0); | |
450 | if (errno == ERANGE) | |
451 | return 0; | |
c8deb5a1 SP |
452 | if (!parse_unit_factor(end, &factor)) |
453 | return 0; | |
ebaa1bd4 NA |
454 | uval = abs(val); |
455 | uval *= factor; | |
456 | if ((uval > maximum_signed_value_of_type(long)) || | |
457 | (abs(val) > uval)) | |
458 | return 0; | |
459 | val *= factor; | |
460 | *ret = val; | |
0b87b6e0 BD |
461 | return 1; |
462 | } | |
463 | return 0; | |
464 | } | |
465 | ||
466 | int git_parse_ulong(const char *value, unsigned long *ret) | |
17712991 LT |
467 | { |
468 | if (value && *value) { | |
469 | char *end; | |
ebaa1bd4 NA |
470 | uintmax_t val; |
471 | uintmax_t oldval; | |
472 | ||
473 | errno = 0; | |
474 | val = strtoumax(value, &end, 0); | |
475 | if (errno == ERANGE) | |
476 | return 0; | |
477 | oldval = val; | |
c8deb5a1 SP |
478 | if (!parse_unit_factor(end, &val)) |
479 | return 0; | |
ebaa1bd4 NA |
480 | if ((val > maximum_unsigned_value_of_type(long)) || |
481 | (oldval > val)) | |
482 | return 0; | |
c8deb5a1 | 483 | *ret = val; |
0b87b6e0 | 484 | return 1; |
17712991 | 485 | } |
0b87b6e0 BD |
486 | return 0; |
487 | } | |
488 | ||
c1867cea JK |
489 | static void die_bad_config(const char *name) |
490 | { | |
924aaf3e RJ |
491 | if (cf && cf->name) |
492 | die("bad config value for '%s' in %s", name, cf->name); | |
c1867cea JK |
493 | die("bad config value for '%s'", name); |
494 | } | |
495 | ||
0b87b6e0 BD |
496 | int git_config_int(const char *name, const char *value) |
497 | { | |
0433bcd9 | 498 | long ret = 0; |
0b87b6e0 | 499 | if (!git_parse_long(value, &ret)) |
c1867cea | 500 | die_bad_config(name); |
0b87b6e0 BD |
501 | return ret; |
502 | } | |
503 | ||
504 | unsigned long git_config_ulong(const char *name, const char *value) | |
505 | { | |
506 | unsigned long ret; | |
507 | if (!git_parse_ulong(value, &ret)) | |
c1867cea | 508 | die_bad_config(name); |
0b87b6e0 | 509 | return ret; |
17712991 LT |
510 | } |
511 | ||
b2be2f6a | 512 | static int git_config_maybe_bool_text(const char *name, const char *value) |
17712991 LT |
513 | { |
514 | if (!value) | |
515 | return 1; | |
516 | if (!*value) | |
517 | return 0; | |
8420ccd8 JH |
518 | if (!strcasecmp(value, "true") |
519 | || !strcasecmp(value, "yes") | |
520 | || !strcasecmp(value, "on")) | |
17712991 | 521 | return 1; |
8420ccd8 JH |
522 | if (!strcasecmp(value, "false") |
523 | || !strcasecmp(value, "no") | |
524 | || !strcasecmp(value, "off")) | |
17712991 | 525 | return 0; |
8420ccd8 JH |
526 | return -1; |
527 | } | |
528 | ||
b2be2f6a JK |
529 | int git_config_maybe_bool(const char *name, const char *value) |
530 | { | |
db6195ef | 531 | long v = git_config_maybe_bool_text(name, value); |
b2be2f6a JK |
532 | if (0 <= v) |
533 | return v; | |
db6195ef JK |
534 | if (git_parse_long(value, &v)) |
535 | return !!v; | |
b2be2f6a JK |
536 | return -1; |
537 | } | |
538 | ||
8420ccd8 JH |
539 | int git_config_bool_or_int(const char *name, const char *value, int *is_bool) |
540 | { | |
b2be2f6a | 541 | int v = git_config_maybe_bool_text(name, value); |
8420ccd8 JH |
542 | if (0 <= v) { |
543 | *is_bool = 1; | |
544 | return v; | |
545 | } | |
a53f2ec6 | 546 | *is_bool = 0; |
c35b0b58 | 547 | return git_config_int(name, value); |
17712991 LT |
548 | } |
549 | ||
a53f2ec6 JH |
550 | int git_config_bool(const char *name, const char *value) |
551 | { | |
552 | int discard; | |
c35b0b58 | 553 | return !!git_config_bool_or_int(name, value, &discard); |
a53f2ec6 JH |
554 | } |
555 | ||
ea5105a5 CC |
556 | int git_config_string(const char **dest, const char *var, const char *value) |
557 | { | |
558 | if (!value) | |
559 | return config_error_nonbool(var); | |
560 | *dest = xstrdup(value); | |
561 | return 0; | |
562 | } | |
563 | ||
395de250 MM |
564 | int git_config_pathname(const char **dest, const char *var, const char *value) |
565 | { | |
566 | if (!value) | |
567 | return config_error_nonbool(var); | |
568 | *dest = expand_user_path(value); | |
569 | if (!*dest) | |
570 | die("Failed to expand user dir in: '%s'", value); | |
571 | return 0; | |
572 | } | |
573 | ||
806e2ad7 | 574 | static int git_default_core_config(const char *var, const char *value) |
17712991 LT |
575 | { |
576 | /* This needs a better name */ | |
577 | if (!strcmp(var, "core.filemode")) { | |
578 | trust_executable_bit = git_config_bool(var, value); | |
579 | return 0; | |
580 | } | |
1ce4790b AR |
581 | if (!strcmp(var, "core.trustctime")) { |
582 | trust_ctime = git_config_bool(var, value); | |
583 | return 0; | |
584 | } | |
c08e4d5b RR |
585 | if (!strcmp(var, "core.statinfo")) { |
586 | if (!strcasecmp(value, "default")) | |
587 | check_stat = 1; | |
588 | else if (!strcasecmp(value, "minimal")) | |
589 | check_stat = 0; | |
590 | } | |
17712991 | 591 | |
9378c161 JH |
592 | if (!strcmp(var, "core.quotepath")) { |
593 | quote_path_fully = git_config_bool(var, value); | |
594 | return 0; | |
595 | } | |
596 | ||
78a8d641 JS |
597 | if (!strcmp(var, "core.symlinks")) { |
598 | has_symlinks = git_config_bool(var, value); | |
599 | return 0; | |
600 | } | |
601 | ||
0a9b88b7 LT |
602 | if (!strcmp(var, "core.ignorecase")) { |
603 | ignore_case = git_config_bool(var, value); | |
604 | return 0; | |
605 | } | |
606 | ||
64589a03 JH |
607 | if (!strcmp(var, "core.attributesfile")) |
608 | return git_config_pathname(&git_attributes_file, var, value); | |
609 | ||
7d1864ce JH |
610 | if (!strcmp(var, "core.bare")) { |
611 | is_bare_repository_cfg = git_config_bool(var, value); | |
612 | return 0; | |
613 | } | |
614 | ||
5f73076c JH |
615 | if (!strcmp(var, "core.ignorestat")) { |
616 | assume_unchanged = git_config_bool(var, value); | |
617 | return 0; | |
618 | } | |
619 | ||
e388c738 JH |
620 | if (!strcmp(var, "core.prefersymlinkrefs")) { |
621 | prefer_symlink_refs = git_config_bool(var, value); | |
f8348be3 JS |
622 | return 0; |
623 | } | |
624 | ||
6de08ae6 SP |
625 | if (!strcmp(var, "core.logallrefupdates")) { |
626 | log_all_ref_updates = git_config_bool(var, value); | |
627 | return 0; | |
628 | } | |
629 | ||
2f8acdb3 JH |
630 | if (!strcmp(var, "core.warnambiguousrefs")) { |
631 | warn_ambiguous_refs = git_config_bool(var, value); | |
632 | return 0; | |
633 | } | |
634 | ||
a71f09fe | 635 | if (!strcmp(var, "core.abbrev")) { |
dce96489 LT |
636 | int abbrev = git_config_int(var, value); |
637 | if (abbrev < minimum_abbrev || abbrev > 40) | |
638 | return -1; | |
639 | default_abbrev = abbrev; | |
640 | return 0; | |
641 | } | |
642 | ||
960ccca6 | 643 | if (!strcmp(var, "core.loosecompression")) { |
12f6c308 JBH |
644 | int level = git_config_int(var, value); |
645 | if (level == -1) | |
646 | level = Z_DEFAULT_COMPRESSION; | |
647 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
648 | die("bad zlib compression level %d", level); | |
649 | zlib_compression_level = level; | |
960ccca6 DH |
650 | zlib_compression_seen = 1; |
651 | return 0; | |
652 | } | |
653 | ||
654 | if (!strcmp(var, "core.compression")) { | |
655 | int level = git_config_int(var, value); | |
656 | if (level == -1) | |
657 | level = Z_DEFAULT_COMPRESSION; | |
658 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
659 | die("bad zlib compression level %d", level); | |
660 | core_compression_level = level; | |
661 | core_compression_seen = 1; | |
662 | if (!zlib_compression_seen) | |
663 | zlib_compression_level = level; | |
12f6c308 JBH |
664 | return 0; |
665 | } | |
666 | ||
60bb8b14 | 667 | if (!strcmp(var, "core.packedgitwindowsize")) { |
5faaf246 | 668 | int pgsz_x2 = getpagesize() * 2; |
ebaa1bd4 | 669 | packed_git_window_size = git_config_ulong(var, value); |
5faaf246 JH |
670 | |
671 | /* This value must be multiple of (pagesize * 2) */ | |
672 | packed_git_window_size /= pgsz_x2; | |
673 | if (packed_git_window_size < 1) | |
674 | packed_git_window_size = 1; | |
675 | packed_git_window_size *= pgsz_x2; | |
60bb8b14 SP |
676 | return 0; |
677 | } | |
678 | ||
15366280 | 679 | if (!strcmp(var, "core.bigfilethreshold")) { |
ebaa1bd4 | 680 | big_file_threshold = git_config_ulong(var, value); |
15366280 JH |
681 | return 0; |
682 | } | |
683 | ||
77ccc5bb | 684 | if (!strcmp(var, "core.packedgitlimit")) { |
ebaa1bd4 | 685 | packed_git_limit = git_config_ulong(var, value); |
77ccc5bb SP |
686 | return 0; |
687 | } | |
688 | ||
18bdec11 | 689 | if (!strcmp(var, "core.deltabasecachelimit")) { |
ebaa1bd4 | 690 | delta_base_cache_limit = git_config_ulong(var, value); |
18bdec11 SP |
691 | return 0; |
692 | } | |
693 | ||
5f44324d JH |
694 | if (!strcmp(var, "core.logpackaccess")) |
695 | return git_config_string(&log_pack_access, var, value); | |
696 | ||
6c510bee | 697 | if (!strcmp(var, "core.autocrlf")) { |
d7f46334 | 698 | if (value && !strcasecmp(value, "input")) { |
ec70f52f | 699 | if (core_eol == EOL_CRLF) |
942e7747 | 700 | return error("core.autocrlf=input conflicts with core.eol=crlf"); |
fd6cce9e | 701 | auto_crlf = AUTO_CRLF_INPUT; |
d7f46334 LT |
702 | return 0; |
703 | } | |
6c510bee LT |
704 | auto_crlf = git_config_bool(var, value); |
705 | return 0; | |
706 | } | |
707 | ||
21e5ad50 SP |
708 | if (!strcmp(var, "core.safecrlf")) { |
709 | if (value && !strcasecmp(value, "warn")) { | |
710 | safe_crlf = SAFE_CRLF_WARN; | |
711 | return 0; | |
712 | } | |
713 | safe_crlf = git_config_bool(var, value); | |
714 | return 0; | |
715 | } | |
716 | ||
942e7747 EB |
717 | if (!strcmp(var, "core.eol")) { |
718 | if (value && !strcasecmp(value, "lf")) | |
ec70f52f | 719 | core_eol = EOL_LF; |
942e7747 | 720 | else if (value && !strcasecmp(value, "crlf")) |
ec70f52f | 721 | core_eol = EOL_CRLF; |
942e7747 | 722 | else if (value && !strcasecmp(value, "native")) |
ec70f52f | 723 | core_eol = EOL_NATIVE; |
942e7747 | 724 | else |
ec70f52f JH |
725 | core_eol = EOL_UNSET; |
726 | if (core_eol == EOL_CRLF && auto_crlf == AUTO_CRLF_INPUT) | |
942e7747 EB |
727 | return error("core.autocrlf=input conflicts with core.eol=crlf"); |
728 | return 0; | |
729 | } | |
730 | ||
a97a7468 JS |
731 | if (!strcmp(var, "core.notesref")) { |
732 | notes_ref_name = xstrdup(value); | |
733 | return 0; | |
734 | } | |
735 | ||
806e2ad7 LT |
736 | if (!strcmp(var, "core.pager")) |
737 | return git_config_string(&pager_program, var, value); | |
738 | ||
739 | if (!strcmp(var, "core.editor")) | |
740 | return git_config_string(&editor_program, var, value); | |
741 | ||
eff80a9f JH |
742 | if (!strcmp(var, "core.commentchar")) { |
743 | const char *comment; | |
744 | int ret = git_config_string(&comment, var, value); | |
745 | if (!ret) | |
746 | comment_line_char = comment[0]; | |
747 | return ret; | |
748 | } | |
749 | ||
d3e7da89 AK |
750 | if (!strcmp(var, "core.askpass")) |
751 | return git_config_string(&askpass_program, var, value); | |
752 | ||
806e2ad7 | 753 | if (!strcmp(var, "core.excludesfile")) |
395de250 | 754 | return git_config_pathname(&excludes_file, var, value); |
806e2ad7 LT |
755 | |
756 | if (!strcmp(var, "core.whitespace")) { | |
757 | if (!value) | |
758 | return config_error_nonbool(var); | |
759 | whitespace_rule_cfg = parse_whitespace_rule(value); | |
760 | return 0; | |
761 | } | |
762 | ||
aafe9fba LT |
763 | if (!strcmp(var, "core.fsyncobjectfiles")) { |
764 | fsync_object_files = git_config_bool(var, value); | |
765 | return 0; | |
766 | } | |
767 | ||
671c9b7e LT |
768 | if (!strcmp(var, "core.preloadindex")) { |
769 | core_preload_index = git_config_bool(var, value); | |
770 | return 0; | |
771 | } | |
772 | ||
348df166 JS |
773 | if (!strcmp(var, "core.createobject")) { |
774 | if (!strcmp(value, "rename")) | |
775 | object_creation_mode = OBJECT_CREATION_USES_RENAMES; | |
776 | else if (!strcmp(value, "link")) | |
777 | object_creation_mode = OBJECT_CREATION_USES_HARDLINKS; | |
778 | else | |
779 | die("Invalid mode for object creation: %s", value); | |
be66a6c4 JS |
780 | return 0; |
781 | } | |
782 | ||
08aefc9e NTND |
783 | if (!strcmp(var, "core.sparsecheckout")) { |
784 | core_apply_sparse_checkout = git_config_bool(var, value); | |
785 | return 0; | |
786 | } | |
787 | ||
76759c7d TB |
788 | if (!strcmp(var, "core.precomposeunicode")) { |
789 | precomposed_unicode = git_config_bool(var, value); | |
790 | return 0; | |
791 | } | |
792 | ||
806e2ad7 LT |
793 | /* Add other config variables here and to Documentation/config.txt. */ |
794 | return 0; | |
795 | } | |
796 | ||
1141f492 | 797 | static int git_default_i18n_config(const char *var, const char *value) |
d1364529 | 798 | { |
ea5105a5 CC |
799 | if (!strcmp(var, "i18n.commitencoding")) |
800 | return git_config_string(&git_commit_encoding, var, value); | |
d2c11a38 | 801 | |
ea5105a5 CC |
802 | if (!strcmp(var, "i18n.logoutputencoding")) |
803 | return git_config_string(&git_log_output_encoding, var, value); | |
d2c11a38 | 804 | |
1141f492 LT |
805 | /* Add other config variables here and to Documentation/config.txt. */ |
806 | return 0; | |
807 | } | |
039bc64e | 808 | |
1141f492 LT |
809 | static int git_default_branch_config(const char *var, const char *value) |
810 | { | |
9ed36cfa JS |
811 | if (!strcmp(var, "branch.autosetupmerge")) { |
812 | if (value && !strcasecmp(value, "always")) { | |
813 | git_branch_track = BRANCH_TRACK_ALWAYS; | |
814 | return 0; | |
815 | } | |
816 | git_branch_track = git_config_bool(var, value); | |
817 | return 0; | |
818 | } | |
c998ae9b DS |
819 | if (!strcmp(var, "branch.autosetuprebase")) { |
820 | if (!value) | |
821 | return config_error_nonbool(var); | |
822 | else if (!strcmp(value, "never")) | |
823 | autorebase = AUTOREBASE_NEVER; | |
824 | else if (!strcmp(value, "local")) | |
825 | autorebase = AUTOREBASE_LOCAL; | |
826 | else if (!strcmp(value, "remote")) | |
827 | autorebase = AUTOREBASE_REMOTE; | |
828 | else if (!strcmp(value, "always")) | |
829 | autorebase = AUTOREBASE_ALWAYS; | |
830 | else | |
831 | return error("Malformed value for %s", var); | |
832 | return 0; | |
833 | } | |
a9cc857a | 834 | |
1ab661dd | 835 | /* Add other config variables here and to Documentation/config.txt. */ |
17712991 LT |
836 | return 0; |
837 | } | |
838 | ||
52153747 FAG |
839 | static int git_default_push_config(const char *var, const char *value) |
840 | { | |
841 | if (!strcmp(var, "push.default")) { | |
842 | if (!value) | |
843 | return config_error_nonbool(var); | |
844 | else if (!strcmp(value, "nothing")) | |
845 | push_default = PUSH_DEFAULT_NOTHING; | |
846 | else if (!strcmp(value, "matching")) | |
847 | push_default = PUSH_DEFAULT_MATCHING; | |
b55e6775 MM |
848 | else if (!strcmp(value, "simple")) |
849 | push_default = PUSH_DEFAULT_SIMPLE; | |
53c40311 JH |
850 | else if (!strcmp(value, "upstream")) |
851 | push_default = PUSH_DEFAULT_UPSTREAM; | |
852 | else if (!strcmp(value, "tracking")) /* deprecated */ | |
853 | push_default = PUSH_DEFAULT_UPSTREAM; | |
52153747 FAG |
854 | else if (!strcmp(value, "current")) |
855 | push_default = PUSH_DEFAULT_CURRENT; | |
856 | else { | |
857 | error("Malformed value for %s: %s", var, value); | |
b55e6775 MM |
858 | return error("Must be one of nothing, matching, simple, " |
859 | "upstream or current."); | |
52153747 FAG |
860 | } |
861 | return 0; | |
862 | } | |
863 | ||
864 | /* Add other config variables here and to Documentation/config.txt. */ | |
865 | return 0; | |
866 | } | |
867 | ||
d551a488 MSO |
868 | static int git_default_mailmap_config(const char *var, const char *value) |
869 | { | |
870 | if (!strcmp(var, "mailmap.file")) | |
871 | return git_config_string(&git_mailmap_file, var, value); | |
08610900 JK |
872 | if (!strcmp(var, "mailmap.blob")) |
873 | return git_config_string(&git_mailmap_blob, var, value); | |
d551a488 MSO |
874 | |
875 | /* Add other config variables here and to Documentation/config.txt. */ | |
876 | return 0; | |
877 | } | |
878 | ||
1141f492 LT |
879 | int git_default_config(const char *var, const char *value, void *dummy) |
880 | { | |
881 | if (!prefixcmp(var, "core.")) | |
882 | return git_default_core_config(var, value); | |
883 | ||
884 | if (!prefixcmp(var, "user.")) | |
9597921b | 885 | return git_ident_config(var, value, dummy); |
1141f492 LT |
886 | |
887 | if (!prefixcmp(var, "i18n.")) | |
888 | return git_default_i18n_config(var, value); | |
889 | ||
890 | if (!prefixcmp(var, "branch.")) | |
891 | return git_default_branch_config(var, value); | |
892 | ||
52153747 FAG |
893 | if (!prefixcmp(var, "push.")) |
894 | return git_default_push_config(var, value); | |
895 | ||
d551a488 MSO |
896 | if (!prefixcmp(var, "mailmap.")) |
897 | return git_default_mailmap_config(var, value); | |
898 | ||
75194438 JK |
899 | if (!prefixcmp(var, "advice.")) |
900 | return git_default_advice_config(var, value); | |
901 | ||
1141f492 LT |
902 | if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { |
903 | pager_use_color = git_config_bool(var,value); | |
904 | return 0; | |
905 | } | |
906 | ||
568508e7 JH |
907 | if (!strcmp(var, "pack.packsizelimit")) { |
908 | pack_size_limit_cfg = git_config_ulong(var, value); | |
909 | return 0; | |
910 | } | |
1141f492 LT |
911 | /* Add other config variables here and to Documentation/config.txt. */ |
912 | return 0; | |
913 | } | |
914 | ||
ca4b5de2 | 915 | /* |
4d8dd149 HV |
916 | * All source specific fields in the union, name and the callbacks |
917 | * fgetc, ungetc, ftell of top need to be initialized before calling | |
ca4b5de2 HV |
918 | * this function. |
919 | */ | |
4d8dd149 | 920 | static int do_config_from(struct config_source *top, config_fn_t fn, void *data) |
ca4b5de2 HV |
921 | { |
922 | int ret; | |
923 | ||
924 | /* push config-file parsing state stack */ | |
925 | top->prev = cf; | |
926 | top->linenr = 1; | |
927 | top->eof = 0; | |
928 | strbuf_init(&top->value, 1024); | |
929 | strbuf_init(&top->var, 1024); | |
930 | cf = top; | |
931 | ||
4d8dd149 | 932 | ret = git_parse_source(fn, data); |
ca4b5de2 HV |
933 | |
934 | /* pop config-file parsing state stack */ | |
935 | strbuf_release(&top->value); | |
936 | strbuf_release(&top->var); | |
937 | cf = top->prev; | |
938 | ||
939 | return ret; | |
940 | } | |
941 | ||
ef90d6d4 | 942 | int git_config_from_file(config_fn_t fn, const char *filename, void *data) |
17712991 LT |
943 | { |
944 | int ret; | |
4f629539 | 945 | FILE *f = fopen(filename, "r"); |
17712991 LT |
946 | |
947 | ret = -1; | |
948 | if (f) { | |
4d8dd149 | 949 | struct config_source top; |
924aaf3e | 950 | |
4d8dd149 | 951 | top.u.file = f; |
924aaf3e | 952 | top.name = filename; |
4d8dd149 HV |
953 | top.fgetc = config_file_fgetc; |
954 | top.ungetc = config_file_ungetc; | |
955 | top.ftell = config_file_ftell; | |
ca4b5de2 HV |
956 | |
957 | ret = do_config_from(&top, fn, data); | |
924aaf3e | 958 | |
17712991 LT |
959 | fclose(f); |
960 | } | |
961 | return ret; | |
962 | } | |
10bea152 | 963 | |
506b17b1 JS |
964 | const char *git_etc_gitconfig(void) |
965 | { | |
7f0e39fa | 966 | static const char *system_wide; |
2de9de5e SP |
967 | if (!system_wide) |
968 | system_wide = system_path(ETC_GITCONFIG); | |
7f0e39fa | 969 | return system_wide; |
506b17b1 JS |
970 | } |
971 | ||
0ef37164 | 972 | int git_env_bool(const char *k, int def) |
ab88c363 JK |
973 | { |
974 | const char *v = getenv(k); | |
975 | return v ? git_config_bool(k, v) : def; | |
976 | } | |
977 | ||
978 | int git_config_system(void) | |
979 | { | |
980 | return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); | |
981 | } | |
982 | ||
dbdf5854 | 983 | int git_config_early(config_fn_t fn, void *data, const char *repo_config) |
4f629539 | 984 | { |
aa387407 | 985 | int ret = 0, found = 0; |
21cf3227 HKNN |
986 | char *xdg_config = NULL; |
987 | char *user_config = NULL; | |
988 | ||
989 | home_config_paths(&user_config, &xdg_config, "config"); | |
5f1a63e0 | 990 | |
8f2bbe45 | 991 | if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK)) { |
dc871831 DB |
992 | ret += git_config_from_file(fn, git_etc_gitconfig(), |
993 | data); | |
aa387407 FC |
994 | found += 1; |
995 | } | |
5f1a63e0 | 996 | |
96b9e0e3 | 997 | if (xdg_config && !access_or_die(xdg_config, R_OK)) { |
21cf3227 HKNN |
998 | ret += git_config_from_file(fn, xdg_config, data); |
999 | found += 1; | |
1000 | } | |
1001 | ||
96b9e0e3 | 1002 | if (user_config && !access_or_die(user_config, R_OK)) { |
21cf3227 HKNN |
1003 | ret += git_config_from_file(fn, user_config, data); |
1004 | found += 1; | |
5f1a63e0 JS |
1005 | } |
1006 | ||
8f2bbe45 | 1007 | if (repo_config && !access_or_die(repo_config, R_OK)) { |
aa387407 FC |
1008 | ret += git_config_from_file(fn, repo_config, data); |
1009 | found += 1; | |
1010 | } | |
8b1fa778 | 1011 | |
5a0c9eeb JK |
1012 | switch (git_config_from_parameters(fn, data)) { |
1013 | case -1: /* error */ | |
1c2c9bee | 1014 | die("unable to parse command-line config"); |
5a0c9eeb JK |
1015 | break; |
1016 | case 0: /* found nothing */ | |
1017 | break; | |
1018 | default: /* found at least one item */ | |
1019 | found++; | |
1020 | break; | |
1021 | } | |
8b1fa778 | 1022 | |
21cf3227 HKNN |
1023 | free(xdg_config); |
1024 | free(user_config); | |
1f2baa78 | 1025 | return ret == 0 ? found : ret; |
4f629539 JH |
1026 | } |
1027 | ||
c9b5e2a5 | 1028 | int git_config_with_options(config_fn_t fn, void *data, |
9b25a0b5 | 1029 | const char *filename, int respect_includes) |
dbdf5854 NTND |
1030 | { |
1031 | char *repo_config = NULL; | |
1032 | int ret; | |
9b25a0b5 JK |
1033 | struct config_include_data inc = CONFIG_INCLUDE_INIT; |
1034 | ||
1035 | if (respect_includes) { | |
1036 | inc.fn = fn; | |
1037 | inc.data = data; | |
1038 | fn = git_config_include; | |
1039 | data = &inc; | |
1040 | } | |
dbdf5854 | 1041 | |
c9b5e2a5 JK |
1042 | /* |
1043 | * If we have a specific filename, use it. Otherwise, follow the | |
1044 | * regular lookup sequence. | |
1045 | */ | |
1046 | if (filename) | |
1047 | return git_config_from_file(fn, filename, data); | |
1048 | ||
dbdf5854 NTND |
1049 | repo_config = git_pathdup("config"); |
1050 | ret = git_config_early(fn, data, repo_config); | |
1051 | if (repo_config) | |
1052 | free(repo_config); | |
1053 | return ret; | |
1054 | } | |
1055 | ||
c9b5e2a5 JK |
1056 | int git_config(config_fn_t fn, void *data) |
1057 | { | |
9b25a0b5 | 1058 | return git_config_with_options(fn, data, NULL, 1); |
c9b5e2a5 JK |
1059 | } |
1060 | ||
10bea152 JS |
1061 | /* |
1062 | * Find all the stuff for git_config_set() below. | |
1063 | */ | |
4ddba79d JS |
1064 | |
1065 | #define MAX_MATCHES 512 | |
1066 | ||
10bea152 JS |
1067 | static struct { |
1068 | int baselen; | |
4b25d091 | 1069 | char *key; |
f98d863d | 1070 | int do_not_match; |
4b25d091 | 1071 | regex_t *value_regex; |
4ddba79d | 1072 | int multi_replace; |
dc49cd76 | 1073 | size_t offset[MAX_MATCHES]; |
10bea152 JS |
1074 | enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; |
1075 | int seen; | |
1076 | } store; | |
1077 | ||
4b25d091 | 1078 | static int matches(const char *key, const char *value) |
f98d863d JS |
1079 | { |
1080 | return !strcmp(key, store.key) && | |
1081 | (store.value_regex == NULL || | |
1082 | (store.do_not_match ^ | |
1083 | !regexec(store.value_regex, value, 0, NULL, 0))); | |
1084 | } | |
1085 | ||
4b25d091 | 1086 | static int store_aux(const char *key, const char *value, void *cb) |
10bea152 | 1087 | { |
ae9ee41d JH |
1088 | const char *ep; |
1089 | size_t section_len; | |
1090 | ||
10bea152 JS |
1091 | switch (store.state) { |
1092 | case KEY_SEEN: | |
f98d863d | 1093 | if (matches(key, value)) { |
4ddba79d | 1094 | if (store.seen == 1 && store.multi_replace == 0) { |
64c0d71c | 1095 | warning("%s has multiple values", key); |
4ddba79d | 1096 | } else if (store.seen >= MAX_MATCHES) { |
64c0d71c | 1097 | error("too many matches for %s", key); |
4ddba79d | 1098 | return 1; |
10bea152 | 1099 | } |
4ddba79d | 1100 | |
4d8dd149 | 1101 | store.offset[store.seen] = cf->ftell(cf); |
10bea152 JS |
1102 | store.seen++; |
1103 | } | |
1104 | break; | |
1105 | case SECTION_SEEN: | |
ae9ee41d JH |
1106 | /* |
1107 | * What we are looking for is in store.key (both | |
1108 | * section and var), and its section part is baselen | |
1109 | * long. We found key (again, both section and var). | |
1110 | * We would want to know if this key is in the same | |
1111 | * section as what we are looking for. We already | |
1112 | * know we are in the same section as what should | |
1113 | * hold store.key. | |
1114 | */ | |
1115 | ep = strrchr(key, '.'); | |
1116 | section_len = ep - key; | |
1117 | ||
1118 | if ((section_len != store.baselen) || | |
1119 | memcmp(key, store.key, section_len+1)) { | |
10bea152 JS |
1120 | store.state = SECTION_END_SEEN; |
1121 | break; | |
ae9ee41d JH |
1122 | } |
1123 | ||
1124 | /* | |
1125 | * Do not increment matches: this is no match, but we | |
1126 | * just made sure we are in the desired section. | |
1127 | */ | |
4d8dd149 | 1128 | store.offset[store.seen] = cf->ftell(cf); |
10bea152 JS |
1129 | /* fallthru */ |
1130 | case SECTION_END_SEEN: | |
1131 | case START: | |
f98d863d | 1132 | if (matches(key, value)) { |
4d8dd149 | 1133 | store.offset[store.seen] = cf->ftell(cf); |
10bea152 JS |
1134 | store.state = KEY_SEEN; |
1135 | store.seen++; | |
d14f7764 LT |
1136 | } else { |
1137 | if (strrchr(key, '.') - key == store.baselen && | |
bdf0ef08 | 1138 | !strncmp(key, store.key, store.baselen)) { |
93ddef3e | 1139 | store.state = SECTION_SEEN; |
4d8dd149 | 1140 | store.offset[store.seen] = cf->ftell(cf); |
d14f7764 | 1141 | } |
bdf0ef08 | 1142 | } |
10bea152 JS |
1143 | } |
1144 | return 0; | |
1145 | } | |
1146 | ||
64c0d71c | 1147 | static int write_error(const char *filename) |
480c9e52 | 1148 | { |
64c0d71c | 1149 | error("failed to write new configuration file %s", filename); |
480c9e52 AW |
1150 | |
1151 | /* Same error code as "failed to rename". */ | |
1152 | return 4; | |
1153 | } | |
1154 | ||
4b25d091 | 1155 | static int store_write_section(int fd, const char *key) |
10bea152 | 1156 | { |
cb891a59 KH |
1157 | const char *dot; |
1158 | int i, success; | |
f285a2d7 | 1159 | struct strbuf sb = STRBUF_INIT; |
d14f7764 | 1160 | |
cb891a59 | 1161 | dot = memchr(key, '.', store.baselen); |
d14f7764 | 1162 | if (dot) { |
cb891a59 KH |
1163 | strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key); |
1164 | for (i = dot - key + 1; i < store.baselen; i++) { | |
e5c349ba | 1165 | if (key[i] == '"' || key[i] == '\\') |
cb891a59 KH |
1166 | strbuf_addch(&sb, '\\'); |
1167 | strbuf_addch(&sb, key[i]); | |
d14f7764 | 1168 | } |
cb891a59 KH |
1169 | strbuf_addstr(&sb, "\"]\n"); |
1170 | } else { | |
1171 | strbuf_addf(&sb, "[%.*s]\n", store.baselen, key); | |
d14f7764 LT |
1172 | } |
1173 | ||
cb891a59 KH |
1174 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; |
1175 | strbuf_release(&sb); | |
480c9e52 | 1176 | |
cb891a59 | 1177 | return success; |
10bea152 JS |
1178 | } |
1179 | ||
4b25d091 | 1180 | static int store_write_pair(int fd, const char *key, const char *value) |
10bea152 | 1181 | { |
cb891a59 KH |
1182 | int i, success; |
1183 | int length = strlen(key + store.baselen + 1); | |
1184 | const char *quote = ""; | |
f285a2d7 | 1185 | struct strbuf sb = STRBUF_INIT; |
cdd4fb15 | 1186 | |
6281f394 JM |
1187 | /* |
1188 | * Check to see if the value needs to be surrounded with a dq pair. | |
1189 | * Note that problematic characters are always backslash-quoted; this | |
1190 | * check is about not losing leading or trailing SP and strings that | |
1191 | * follow beginning-of-comment characters (i.e. ';' and '#') by the | |
1192 | * configuration parser. | |
1193 | */ | |
cdd4fb15 | 1194 | if (value[0] == ' ') |
cb891a59 | 1195 | quote = "\""; |
cdd4fb15 BG |
1196 | for (i = 0; value[i]; i++) |
1197 | if (value[i] == ';' || value[i] == '#') | |
cb891a59 KH |
1198 | quote = "\""; |
1199 | if (i && value[i - 1] == ' ') | |
1200 | quote = "\""; | |
1201 | ||
cb891a59 KH |
1202 | strbuf_addf(&sb, "\t%.*s = %s", |
1203 | length, key + store.baselen + 1, quote); | |
10bea152 | 1204 | |
10bea152 JS |
1205 | for (i = 0; value[i]; i++) |
1206 | switch (value[i]) { | |
480c9e52 | 1207 | case '\n': |
cb891a59 | 1208 | strbuf_addstr(&sb, "\\n"); |
480c9e52 AW |
1209 | break; |
1210 | case '\t': | |
cb891a59 | 1211 | strbuf_addstr(&sb, "\\t"); |
480c9e52 AW |
1212 | break; |
1213 | case '"': | |
1214 | case '\\': | |
cb891a59 | 1215 | strbuf_addch(&sb, '\\'); |
480c9e52 | 1216 | default: |
cb891a59 | 1217 | strbuf_addch(&sb, value[i]); |
480c9e52 AW |
1218 | break; |
1219 | } | |
cb891a59 KH |
1220 | strbuf_addf(&sb, "%s\n", quote); |
1221 | ||
1222 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; | |
1223 | strbuf_release(&sb); | |
1224 | ||
1225 | return success; | |
10bea152 JS |
1226 | } |
1227 | ||
4b25d091 FC |
1228 | static ssize_t find_beginning_of_line(const char *contents, size_t size, |
1229 | size_t offset_, int *found_bracket) | |
4ddba79d | 1230 | { |
dc49cd76 SP |
1231 | size_t equal_offset = size, bracket_offset = size; |
1232 | ssize_t offset; | |
4ddba79d | 1233 | |
7a31cc0f | 1234 | contline: |
a6080a0a | 1235 | for (offset = offset_-2; offset > 0 |
4ddba79d JS |
1236 | && contents[offset] != '\n'; offset--) |
1237 | switch (contents[offset]) { | |
1238 | case '=': equal_offset = offset; break; | |
1239 | case ']': bracket_offset = offset; break; | |
1240 | } | |
7a31cc0f FL |
1241 | if (offset > 0 && contents[offset-1] == '\\') { |
1242 | offset_ = offset; | |
1243 | goto contline; | |
1244 | } | |
4ddba79d JS |
1245 | if (bracket_offset < equal_offset) { |
1246 | *found_bracket = 1; | |
1247 | offset = bracket_offset+1; | |
1248 | } else | |
1249 | offset++; | |
1250 | ||
1251 | return offset; | |
1252 | } | |
1253 | ||
5ec31182 RR |
1254 | int git_config_set_in_file(const char *config_filename, |
1255 | const char *key, const char *value) | |
1256 | { | |
1257 | return git_config_set_multivar_in_file(config_filename, key, value, NULL, 0); | |
1258 | } | |
1259 | ||
4b25d091 | 1260 | int git_config_set(const char *key, const char *value) |
10bea152 | 1261 | { |
4ddba79d | 1262 | return git_config_set_multivar(key, value, NULL, 0); |
10bea152 JS |
1263 | } |
1264 | ||
b09c53a3 LP |
1265 | /* |
1266 | * Auxiliary function to sanity-check and split the key into the section | |
1267 | * identifier and variable name. | |
1268 | * | |
1269 | * Returns 0 on success, -1 when there is an invalid character in the key and | |
1270 | * -2 if there is no section name in the key. | |
1271 | * | |
1272 | * store_key - pointer to char* which will hold a copy of the key with | |
1273 | * lowercase section and variable name | |
1274 | * baselen - pointer to int which will hold the length of the | |
1275 | * section + subsection part, can be NULL | |
1276 | */ | |
1277 | int git_config_parse_key(const char *key, char **store_key, int *baselen_) | |
1278 | { | |
1279 | int i, dot, baselen; | |
1280 | const char *last_dot = strrchr(key, '.'); | |
1281 | ||
1282 | /* | |
1283 | * Since "key" actually contains the section name and the real | |
1284 | * key name separated by a dot, we have to know where the dot is. | |
1285 | */ | |
1286 | ||
2169ddc0 | 1287 | if (last_dot == NULL || last_dot == key) { |
b09c53a3 | 1288 | error("key does not contain a section: %s", key); |
7a397419 | 1289 | return -CONFIG_NO_SECTION_OR_NAME; |
b09c53a3 LP |
1290 | } |
1291 | ||
2169ddc0 LP |
1292 | if (!last_dot[1]) { |
1293 | error("key does not contain variable name: %s", key); | |
7a397419 | 1294 | return -CONFIG_NO_SECTION_OR_NAME; |
2169ddc0 LP |
1295 | } |
1296 | ||
b09c53a3 LP |
1297 | baselen = last_dot - key; |
1298 | if (baselen_) | |
1299 | *baselen_ = baselen; | |
1300 | ||
1301 | /* | |
1302 | * Validate the key and while at it, lower case it for matching. | |
1303 | */ | |
1304 | *store_key = xmalloc(strlen(key) + 1); | |
1305 | ||
1306 | dot = 0; | |
1307 | for (i = 0; key[i]; i++) { | |
1308 | unsigned char c = key[i]; | |
1309 | if (c == '.') | |
1310 | dot = 1; | |
1311 | /* Leave the extended basename untouched.. */ | |
1312 | if (!dot || i > baselen) { | |
1313 | if (!iskeychar(c) || | |
1314 | (i == baselen + 1 && !isalpha(c))) { | |
1315 | error("invalid key: %s", key); | |
1316 | goto out_free_ret_1; | |
1317 | } | |
1318 | c = tolower(c); | |
1319 | } else if (c == '\n') { | |
1320 | error("invalid key (newline): %s", key); | |
1321 | goto out_free_ret_1; | |
1322 | } | |
1323 | (*store_key)[i] = c; | |
1324 | } | |
1325 | (*store_key)[i] = 0; | |
1326 | ||
1327 | return 0; | |
1328 | ||
1329 | out_free_ret_1: | |
1330 | free(*store_key); | |
97ed50f9 | 1331 | *store_key = NULL; |
7a397419 | 1332 | return -CONFIG_INVALID_KEY; |
b09c53a3 LP |
1333 | } |
1334 | ||
10bea152 JS |
1335 | /* |
1336 | * If value==NULL, unset in (remove from) config, | |
1337 | * if value_regex!=NULL, disregard key/value pairs where value does not match. | |
4ddba79d JS |
1338 | * if multi_replace==0, nothing, or only one matching key/value is replaced, |
1339 | * else all matching key/values (regardless how many) are removed, | |
1340 | * before the new pair is written. | |
10bea152 JS |
1341 | * |
1342 | * Returns 0 on success. | |
1343 | * | |
1344 | * This function does this: | |
1345 | * | |
1346 | * - it locks the config file by creating ".git/config.lock" | |
1347 | * | |
1348 | * - it then parses the config using store_aux() as validator to find | |
1349 | * the position on the key/value pair to replace. If it is to be unset, | |
1350 | * it must be found exactly once. | |
1351 | * | |
1352 | * - the config file is mmap()ed and the part before the match (if any) is | |
1353 | * written to the lock file, then the changed part and the rest. | |
1354 | * | |
1355 | * - the config file is removed and the lock file rename()d to it. | |
1356 | * | |
1357 | */ | |
5ec31182 RR |
1358 | int git_config_set_multivar_in_file(const char *config_filename, |
1359 | const char *key, const char *value, | |
1360 | const char *value_regex, int multi_replace) | |
10bea152 | 1361 | { |
f8ba655e | 1362 | int fd = -1, in_fd; |
dafc88b1 | 1363 | int ret; |
6cbf973c | 1364 | struct lock_file *lock = NULL; |
0a5f5759 | 1365 | char *filename_buf = NULL; |
4ddba79d | 1366 | |
b09c53a3 LP |
1367 | /* parse-key returns negative; flip the sign to feed exit(3) */ |
1368 | ret = 0 - git_config_parse_key(key, &store.key, &store.baselen); | |
1369 | if (ret) | |
dafc88b1 | 1370 | goto out_free; |
b17e659d JS |
1371 | |
1372 | store.multi_replace = multi_replace; | |
10bea152 | 1373 | |
0a5f5759 JK |
1374 | if (!config_filename) |
1375 | config_filename = filename_buf = git_pathdup("config"); | |
10bea152 JS |
1376 | |
1377 | /* | |
6cbf973c | 1378 | * The lock serves a purpose in addition to locking: the new |
10bea152 JS |
1379 | * contents of .git/config will be written into it. |
1380 | */ | |
6cbf973c BS |
1381 | lock = xcalloc(sizeof(struct lock_file), 1); |
1382 | fd = hold_lock_file_for_update(lock, config_filename, 0); | |
1383 | if (fd < 0) { | |
6ffd567b | 1384 | error("could not lock config file %s: %s", config_filename, strerror(errno)); |
10bea152 | 1385 | free(store.key); |
7a397419 | 1386 | ret = CONFIG_NO_LOCK; |
dafc88b1 | 1387 | goto out_free; |
10bea152 JS |
1388 | } |
1389 | ||
1390 | /* | |
1391 | * If .git/config does not exist yet, write a minimal version. | |
1392 | */ | |
88fb958b AR |
1393 | in_fd = open(config_filename, O_RDONLY); |
1394 | if ( in_fd < 0 ) { | |
10bea152 JS |
1395 | free(store.key); |
1396 | ||
88fb958b AR |
1397 | if ( ENOENT != errno ) { |
1398 | error("opening %s: %s", config_filename, | |
1399 | strerror(errno)); | |
7a397419 | 1400 | ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */ |
dafc88b1 | 1401 | goto out_free; |
88fb958b | 1402 | } |
10bea152 JS |
1403 | /* if nothing to unset, error out */ |
1404 | if (value == NULL) { | |
7a397419 | 1405 | ret = CONFIG_NOTHING_SET; |
dafc88b1 | 1406 | goto out_free; |
10bea152 JS |
1407 | } |
1408 | ||
4b25d091 | 1409 | store.key = (char *)key; |
480c9e52 | 1410 | if (!store_write_section(fd, key) || |
93c1e079 JH |
1411 | !store_write_pair(fd, key, value)) |
1412 | goto write_err_out; | |
1413 | } else { | |
88fb958b | 1414 | struct stat st; |
4b25d091 | 1415 | char *contents; |
dc49cd76 SP |
1416 | size_t contents_sz, copy_begin, copy_end; |
1417 | int i, new_line = 0; | |
10bea152 JS |
1418 | |
1419 | if (value_regex == NULL) | |
1420 | store.value_regex = NULL; | |
1421 | else { | |
f98d863d JS |
1422 | if (value_regex[0] == '!') { |
1423 | store.do_not_match = 1; | |
1424 | value_regex++; | |
1425 | } else | |
1426 | store.do_not_match = 0; | |
1427 | ||
2d7320d0 | 1428 | store.value_regex = (regex_t*)xmalloc(sizeof(regex_t)); |
10bea152 JS |
1429 | if (regcomp(store.value_regex, value_regex, |
1430 | REG_EXTENDED)) { | |
64c0d71c | 1431 | error("invalid pattern: %s", value_regex); |
10bea152 | 1432 | free(store.value_regex); |
7a397419 | 1433 | ret = CONFIG_INVALID_PATTERN; |
dafc88b1 | 1434 | goto out_free; |
10bea152 JS |
1435 | } |
1436 | } | |
1437 | ||
4ddba79d | 1438 | store.offset[0] = 0; |
10bea152 JS |
1439 | store.state = START; |
1440 | store.seen = 0; | |
1441 | ||
1442 | /* | |
1443 | * After this, store.offset will contain the *end* offset | |
1444 | * of the last match, or remain at 0 if no match was found. | |
1445 | * As a side effect, we make sure to transform only a valid | |
1446 | * existing config file. | |
1447 | */ | |
ef90d6d4 | 1448 | if (git_config_from_file(store_aux, config_filename, NULL)) { |
64c0d71c | 1449 | error("invalid config file %s", config_filename); |
10bea152 JS |
1450 | free(store.key); |
1451 | if (store.value_regex != NULL) { | |
1452 | regfree(store.value_regex); | |
1453 | free(store.value_regex); | |
1454 | } | |
7a397419 | 1455 | ret = CONFIG_INVALID_FILE; |
dafc88b1 | 1456 | goto out_free; |
10bea152 JS |
1457 | } |
1458 | ||
1459 | free(store.key); | |
1460 | if (store.value_regex != NULL) { | |
1461 | regfree(store.value_regex); | |
1462 | free(store.value_regex); | |
1463 | } | |
1464 | ||
4ddba79d JS |
1465 | /* if nothing to unset, or too many matches, error out */ |
1466 | if ((store.seen == 0 && value == NULL) || | |
1467 | (store.seen > 1 && multi_replace == 0)) { | |
7a397419 | 1468 | ret = CONFIG_NOTHING_SET; |
dafc88b1 | 1469 | goto out_free; |
10bea152 JS |
1470 | } |
1471 | ||
88fb958b | 1472 | fstat(in_fd, &st); |
dc49cd76 SP |
1473 | contents_sz = xsize_t(st.st_size); |
1474 | contents = xmmap(NULL, contents_sz, PROT_READ, | |
10bea152 JS |
1475 | MAP_PRIVATE, in_fd, 0); |
1476 | close(in_fd); | |
1477 | ||
4ddba79d JS |
1478 | if (store.seen == 0) |
1479 | store.seen = 1; | |
1480 | ||
1481 | for (i = 0, copy_begin = 0; i < store.seen; i++) { | |
1482 | if (store.offset[i] == 0) { | |
dc49cd76 | 1483 | store.offset[i] = copy_end = contents_sz; |
4ddba79d JS |
1484 | } else if (store.state != KEY_SEEN) { |
1485 | copy_end = store.offset[i]; | |
10bea152 | 1486 | } else |
4ddba79d | 1487 | copy_end = find_beginning_of_line( |
dc49cd76 | 1488 | contents, contents_sz, |
4ddba79d JS |
1489 | store.offset[i]-2, &new_line); |
1490 | ||
02e5ba4a JK |
1491 | if (copy_end > 0 && contents[copy_end-1] != '\n') |
1492 | new_line = 1; | |
1493 | ||
4ddba79d JS |
1494 | /* write the first part of the config */ |
1495 | if (copy_end > copy_begin) { | |
93c1e079 JH |
1496 | if (write_in_full(fd, contents + copy_begin, |
1497 | copy_end - copy_begin) < | |
1498 | copy_end - copy_begin) | |
1499 | goto write_err_out; | |
1500 | if (new_line && | |
2b7ca830 | 1501 | write_str_in_full(fd, "\n") != 1) |
93c1e079 | 1502 | goto write_err_out; |
4ddba79d JS |
1503 | } |
1504 | copy_begin = store.offset[i]; | |
10bea152 JS |
1505 | } |
1506 | ||
10bea152 JS |
1507 | /* write the pair (value == NULL means unset) */ |
1508 | if (value != NULL) { | |
93c1e079 JH |
1509 | if (store.state == START) { |
1510 | if (!store_write_section(fd, key)) | |
1511 | goto write_err_out; | |
480c9e52 | 1512 | } |
93c1e079 JH |
1513 | if (!store_write_pair(fd, key, value)) |
1514 | goto write_err_out; | |
10bea152 JS |
1515 | } |
1516 | ||
1517 | /* write the rest of the config */ | |
dc49cd76 | 1518 | if (copy_begin < contents_sz) |
93c1e079 | 1519 | if (write_in_full(fd, contents + copy_begin, |
dc49cd76 SP |
1520 | contents_sz - copy_begin) < |
1521 | contents_sz - copy_begin) | |
93c1e079 | 1522 | goto write_err_out; |
10bea152 | 1523 | |
dc49cd76 | 1524 | munmap(contents, contents_sz); |
10bea152 JS |
1525 | } |
1526 | ||
4ed7cd3a | 1527 | if (commit_lock_file(lock) < 0) { |
64c0d71c | 1528 | error("could not commit config file %s", config_filename); |
7a397419 | 1529 | ret = CONFIG_NO_WRITE; |
dafc88b1 | 1530 | goto out_free; |
10bea152 JS |
1531 | } |
1532 | ||
6cbf973c BS |
1533 | /* |
1534 | * lock is committed, so don't try to roll it back below. | |
1535 | * NOTE: Since lockfile.c keeps a linked list of all created | |
1536 | * lock_file structures, it isn't safe to free(lock). It's | |
1537 | * better to just leave it hanging around. | |
1538 | */ | |
1539 | lock = NULL; | |
dafc88b1 SH |
1540 | ret = 0; |
1541 | ||
1542 | out_free: | |
6cbf973c BS |
1543 | if (lock) |
1544 | rollback_lock_file(lock); | |
0a5f5759 | 1545 | free(filename_buf); |
dafc88b1 | 1546 | return ret; |
93c1e079 JH |
1547 | |
1548 | write_err_out: | |
64c0d71c | 1549 | ret = write_error(lock->filename); |
93c1e079 JH |
1550 | goto out_free; |
1551 | ||
10bea152 JS |
1552 | } |
1553 | ||
5ec31182 RR |
1554 | int git_config_set_multivar(const char *key, const char *value, |
1555 | const char *value_regex, int multi_replace) | |
1556 | { | |
4a7bb5ba | 1557 | return git_config_set_multivar_in_file(NULL, key, value, value_regex, |
0a5f5759 | 1558 | multi_replace); |
5ec31182 RR |
1559 | } |
1560 | ||
118f8b24 PB |
1561 | static int section_name_match (const char *buf, const char *name) |
1562 | { | |
1563 | int i = 0, j = 0, dot = 0; | |
a4c0d463 AV |
1564 | if (buf[i] != '[') |
1565 | return 0; | |
1566 | for (i = 1; buf[i] && buf[i] != ']'; i++) { | |
118f8b24 PB |
1567 | if (!dot && isspace(buf[i])) { |
1568 | dot = 1; | |
1569 | if (name[j++] != '.') | |
1570 | break; | |
1571 | for (i++; isspace(buf[i]); i++) | |
1572 | ; /* do nothing */ | |
1573 | if (buf[i] != '"') | |
1574 | break; | |
1575 | continue; | |
1576 | } | |
1577 | if (buf[i] == '\\' && dot) | |
1578 | i++; | |
1579 | else if (buf[i] == '"' && dot) { | |
1580 | for (i++; isspace(buf[i]); i++) | |
1581 | ; /* do_nothing */ | |
1582 | break; | |
1583 | } | |
1584 | if (buf[i] != name[j++]) | |
1585 | break; | |
1586 | } | |
a4c0d463 AV |
1587 | if (buf[i] == ']' && name[j] == 0) { |
1588 | /* | |
1589 | * We match, now just find the right length offset by | |
1590 | * gobbling up any whitespace after it, as well | |
1591 | */ | |
1592 | i++; | |
1593 | for (; buf[i] && isspace(buf[i]); i++) | |
1594 | ; /* do nothing */ | |
1595 | return i; | |
1596 | } | |
1597 | return 0; | |
118f8b24 PB |
1598 | } |
1599 | ||
94a35b1a JK |
1600 | static int section_name_is_ok(const char *name) |
1601 | { | |
1602 | /* Empty section names are bogus. */ | |
1603 | if (!*name) | |
1604 | return 0; | |
1605 | ||
1606 | /* | |
1607 | * Before a dot, we must be alphanumeric or dash. After the first dot, | |
1608 | * anything goes, so we can stop checking. | |
1609 | */ | |
1610 | for (; *name && *name != '.'; name++) | |
1611 | if (*name != '-' && !isalnum(*name)) | |
1612 | return 0; | |
1613 | return 1; | |
1614 | } | |
1615 | ||
118f8b24 | 1616 | /* if new_name == NULL, the section is removed instead */ |
42bd39b5 JK |
1617 | int git_config_rename_section_in_file(const char *config_filename, |
1618 | const char *old_name, const char *new_name) | |
0667fcfb | 1619 | { |
118f8b24 | 1620 | int ret = 0, remove = 0; |
42bd39b5 | 1621 | char *filename_buf = NULL; |
94a35b1a | 1622 | struct lock_file *lock; |
0667fcfb JS |
1623 | int out_fd; |
1624 | char buf[1024]; | |
924aaf3e | 1625 | FILE *config_file; |
0667fcfb | 1626 | |
94a35b1a JK |
1627 | if (new_name && !section_name_is_ok(new_name)) { |
1628 | ret = error("invalid section name: %s", new_name); | |
1629 | goto out; | |
1630 | } | |
1631 | ||
42bd39b5 JK |
1632 | if (!config_filename) |
1633 | config_filename = filename_buf = git_pathdup("config"); | |
1634 | ||
94a35b1a | 1635 | lock = xcalloc(sizeof(struct lock_file), 1); |
0667fcfb | 1636 | out_fd = hold_lock_file_for_update(lock, config_filename, 0); |
fc1905bb | 1637 | if (out_fd < 0) { |
64c0d71c | 1638 | ret = error("could not lock config file %s", config_filename); |
fc1905bb JH |
1639 | goto out; |
1640 | } | |
0667fcfb | 1641 | |
fc1905bb | 1642 | if (!(config_file = fopen(config_filename, "rb"))) { |
01ebb9dc GB |
1643 | /* no config file means nothing to rename, no error */ |
1644 | goto unlock_and_out; | |
fc1905bb | 1645 | } |
0667fcfb JS |
1646 | |
1647 | while (fgets(buf, sizeof(buf), config_file)) { | |
1648 | int i; | |
480c9e52 | 1649 | int length; |
9a5abfc7 | 1650 | char *output = buf; |
0667fcfb JS |
1651 | for (i = 0; buf[i] && isspace(buf[i]); i++) |
1652 | ; /* do nothing */ | |
1653 | if (buf[i] == '[') { | |
1654 | /* it's a section */ | |
a4c0d463 AV |
1655 | int offset = section_name_match(&buf[i], old_name); |
1656 | if (offset > 0) { | |
118f8b24 PB |
1657 | ret++; |
1658 | if (new_name == NULL) { | |
1659 | remove = 1; | |
0667fcfb JS |
1660 | continue; |
1661 | } | |
0667fcfb | 1662 | store.baselen = strlen(new_name); |
480c9e52 | 1663 | if (!store_write_section(out_fd, new_name)) { |
64c0d71c | 1664 | ret = write_error(lock->filename); |
480c9e52 AW |
1665 | goto out; |
1666 | } | |
9a5abfc7 AV |
1667 | /* |
1668 | * We wrote out the new section, with | |
1669 | * a newline, now skip the old | |
1670 | * section's length | |
1671 | */ | |
1672 | output += offset + i; | |
1673 | if (strlen(output) > 0) { | |
1674 | /* | |
1675 | * More content means there's | |
1676 | * a declaration to put on the | |
1677 | * next line; indent with a | |
1678 | * tab | |
1679 | */ | |
1680 | output -= 1; | |
1681 | output[0] = '\t'; | |
1682 | } | |
0667fcfb | 1683 | } |
118f8b24 | 1684 | remove = 0; |
0667fcfb | 1685 | } |
118f8b24 PB |
1686 | if (remove) |
1687 | continue; | |
9a5abfc7 AV |
1688 | length = strlen(output); |
1689 | if (write_in_full(out_fd, output, length) != length) { | |
64c0d71c | 1690 | ret = write_error(lock->filename); |
480c9e52 AW |
1691 | goto out; |
1692 | } | |
0667fcfb | 1693 | } |
fc1905bb | 1694 | fclose(config_file); |
8b590075 | 1695 | unlock_and_out: |
4ed7cd3a | 1696 | if (commit_lock_file(lock) < 0) |
64c0d71c | 1697 | ret = error("could not commit config file %s", config_filename); |
8b590075 | 1698 | out: |
42bd39b5 | 1699 | free(filename_buf); |
0667fcfb JS |
1700 | return ret; |
1701 | } | |
40ea4ed9 | 1702 | |
42bd39b5 JK |
1703 | int git_config_rename_section(const char *old_name, const char *new_name) |
1704 | { | |
4a7bb5ba | 1705 | return git_config_rename_section_in_file(NULL, old_name, new_name); |
42bd39b5 JK |
1706 | } |
1707 | ||
40ea4ed9 JH |
1708 | /* |
1709 | * Call this to report error for your variable that should not | |
1710 | * get a boolean value (i.e. "[my] var" means "true"). | |
1711 | */ | |
a469a101 | 1712 | #undef config_error_nonbool |
40ea4ed9 JH |
1713 | int config_error_nonbool(const char *var) |
1714 | { | |
1715 | return error("Missing value for '%s'", var); | |
1716 | } | |
1b86bbb0 JK |
1717 | |
1718 | int parse_config_key(const char *var, | |
1719 | const char *section, | |
1720 | const char **subsection, int *subsection_len, | |
1721 | const char **key) | |
1722 | { | |
1723 | int section_len = strlen(section); | |
1724 | const char *dot; | |
1725 | ||
1726 | /* Does it start with "section." ? */ | |
1727 | if (prefixcmp(var, section) || var[section_len] != '.') | |
1728 | return -1; | |
1729 | ||
1730 | /* | |
1731 | * Find the key; we don't know yet if we have a subsection, but we must | |
1732 | * parse backwards from the end, since the subsection may have dots in | |
1733 | * it, too. | |
1734 | */ | |
1735 | dot = strrchr(var, '.'); | |
1736 | *key = dot + 1; | |
1737 | ||
1738 | /* Did we have a subsection at all? */ | |
1739 | if (dot == var + section_len) { | |
1740 | *subsection = NULL; | |
1741 | *subsection_len = 0; | |
1742 | } | |
1743 | else { | |
1744 | *subsection = var + section_len + 1; | |
1745 | *subsection_len = dot - *subsection; | |
1746 | } | |
1747 | ||
1748 | return 0; | |
1749 | } |