]>
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" |
17712991 LT |
10 | |
11 | #define MAXNAME (256) | |
12 | ||
13 | static FILE *config_file; | |
0dccc7dc | 14 | static const char *config_file_name; |
17712991 | 15 | static int config_linenr; |
02e5ba4a | 16 | static int config_file_eof; |
960ccca6 DH |
17 | static int zlib_compression_seen; |
18 | ||
dc871831 DB |
19 | const char *config_exclusive_filename = NULL; |
20 | ||
17712991 LT |
21 | static int get_next_char(void) |
22 | { | |
23 | int c; | |
24 | FILE *f; | |
25 | ||
26 | c = '\n'; | |
27 | if ((f = config_file) != NULL) { | |
28 | c = fgetc(f); | |
db2c075d JH |
29 | if (c == '\r') { |
30 | /* DOS like systems */ | |
31 | c = fgetc(f); | |
32 | if (c != '\n') { | |
33 | ungetc(c, f); | |
34 | c = '\r'; | |
35 | } | |
36 | } | |
17712991 LT |
37 | if (c == '\n') |
38 | config_linenr++; | |
39 | if (c == EOF) { | |
02e5ba4a | 40 | config_file_eof = 1; |
17712991 LT |
41 | c = '\n'; |
42 | } | |
43 | } | |
44 | return c; | |
45 | } | |
46 | ||
47 | static char *parse_value(void) | |
48 | { | |
49 | static char value[1024]; | |
50 | int quote = 0, comment = 0, len = 0, space = 0; | |
51 | ||
52 | for (;;) { | |
53 | int c = get_next_char(); | |
e0b3cc0d | 54 | if (len >= sizeof(value) - 1) |
17712991 LT |
55 | return NULL; |
56 | if (c == '\n') { | |
57 | if (quote) | |
58 | return NULL; | |
59 | value[len] = 0; | |
60 | return value; | |
61 | } | |
62 | if (comment) | |
63 | continue; | |
64 | if (isspace(c) && !quote) { | |
ebdaae37 BS |
65 | if (len) |
66 | space++; | |
17712991 LT |
67 | continue; |
68 | } | |
7ebdba61 JS |
69 | if (!quote) { |
70 | if (c == ';' || c == '#') { | |
71 | comment = 1; | |
72 | continue; | |
73 | } | |
74 | } | |
ebdaae37 BS |
75 | for (; space; space--) |
76 | value[len++] = ' '; | |
17712991 LT |
77 | if (c == '\\') { |
78 | c = get_next_char(); | |
79 | switch (c) { | |
80 | case '\n': | |
81 | continue; | |
82 | case 't': | |
83 | c = '\t'; | |
84 | break; | |
85 | case 'b': | |
86 | c = '\b'; | |
87 | break; | |
88 | case 'n': | |
89 | c = '\n'; | |
90 | break; | |
5cbb401d LT |
91 | /* Some characters escape as themselves */ |
92 | case '\\': case '"': | |
93 | break; | |
94 | /* Reject unknown escape sequences */ | |
95 | default: | |
96 | return NULL; | |
17712991 LT |
97 | } |
98 | value[len++] = c; | |
99 | continue; | |
100 | } | |
101 | if (c == '"') { | |
102 | quote = 1-quote; | |
103 | continue; | |
104 | } | |
17712991 LT |
105 | value[len++] = c; |
106 | } | |
107 | } | |
108 | ||
38c5afa8 LT |
109 | static inline int iskeychar(int c) |
110 | { | |
111 | return isalnum(c) || c == '-'; | |
112 | } | |
113 | ||
ef90d6d4 | 114 | static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) |
17712991 LT |
115 | { |
116 | int c; | |
117 | char *value; | |
118 | ||
119 | /* Get the full name */ | |
120 | for (;;) { | |
121 | c = get_next_char(); | |
02e5ba4a | 122 | if (config_file_eof) |
17712991 | 123 | break; |
38c5afa8 | 124 | if (!iskeychar(c)) |
17712991 LT |
125 | break; |
126 | name[len++] = tolower(c); | |
127 | if (len >= MAXNAME) | |
128 | return -1; | |
129 | } | |
130 | name[len] = 0; | |
131 | while (c == ' ' || c == '\t') | |
132 | c = get_next_char(); | |
133 | ||
134 | value = NULL; | |
135 | if (c != '\n') { | |
136 | if (c != '=') | |
137 | return -1; | |
138 | value = parse_value(); | |
139 | if (!value) | |
140 | return -1; | |
141 | } | |
ef90d6d4 | 142 | return fn(name, value, data); |
17712991 LT |
143 | } |
144 | ||
d14f7764 LT |
145 | static int get_extended_base_var(char *name, int baselen, int c) |
146 | { | |
147 | do { | |
148 | if (c == '\n') | |
149 | return -1; | |
150 | c = get_next_char(); | |
151 | } while (isspace(c)); | |
152 | ||
153 | /* We require the format to be '[base "extension"]' */ | |
154 | if (c != '"') | |
155 | return -1; | |
156 | name[baselen++] = '.'; | |
157 | ||
158 | for (;;) { | |
159 | int c = get_next_char(); | |
160 | if (c == '\n') | |
161 | return -1; | |
162 | if (c == '"') | |
163 | break; | |
164 | if (c == '\\') { | |
165 | c = get_next_char(); | |
166 | if (c == '\n') | |
167 | return -1; | |
168 | } | |
169 | name[baselen++] = c; | |
170 | if (baselen > MAXNAME / 2) | |
171 | return -1; | |
172 | } | |
173 | ||
174 | /* Final ']' */ | |
175 | if (get_next_char() != ']') | |
176 | return -1; | |
177 | return baselen; | |
178 | } | |
179 | ||
17712991 LT |
180 | static int get_base_var(char *name) |
181 | { | |
182 | int baselen = 0; | |
183 | ||
184 | for (;;) { | |
185 | int c = get_next_char(); | |
02e5ba4a | 186 | if (config_file_eof) |
17712991 LT |
187 | return -1; |
188 | if (c == ']') | |
189 | return baselen; | |
d14f7764 LT |
190 | if (isspace(c)) |
191 | return get_extended_base_var(name, baselen, c); | |
38c5afa8 | 192 | if (!iskeychar(c) && c != '.') |
17712991 LT |
193 | return -1; |
194 | if (baselen > MAXNAME / 2) | |
195 | return -1; | |
196 | name[baselen++] = tolower(c); | |
197 | } | |
198 | } | |
199 | ||
ef90d6d4 | 200 | static int git_parse_file(config_fn_t fn, void *data) |
17712991 LT |
201 | { |
202 | int comment = 0; | |
203 | int baselen = 0; | |
204 | static char var[MAXNAME]; | |
205 | ||
de056402 PB |
206 | /* U+FEFF Byte Order Mark in UTF8 */ |
207 | static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; | |
208 | const unsigned char *bomptr = utf8_bom; | |
209 | ||
17712991 LT |
210 | for (;;) { |
211 | int c = get_next_char(); | |
de056402 PB |
212 | if (bomptr && *bomptr) { |
213 | /* We are at the file beginning; skip UTF8-encoded BOM | |
214 | * if present. Sane editors won't put this in on their | |
215 | * own, but e.g. Windows Notepad will do it happily. */ | |
216 | if ((unsigned char) c == *bomptr) { | |
217 | bomptr++; | |
218 | continue; | |
219 | } else { | |
220 | /* Do not tolerate partial BOM. */ | |
221 | if (bomptr != utf8_bom) | |
222 | break; | |
223 | /* No BOM at file beginning. Cool. */ | |
224 | bomptr = NULL; | |
225 | } | |
226 | } | |
17712991 | 227 | if (c == '\n') { |
02e5ba4a | 228 | if (config_file_eof) |
17712991 LT |
229 | return 0; |
230 | comment = 0; | |
231 | continue; | |
232 | } | |
233 | if (comment || isspace(c)) | |
234 | continue; | |
235 | if (c == '#' || c == ';') { | |
236 | comment = 1; | |
237 | continue; | |
238 | } | |
239 | if (c == '[') { | |
240 | baselen = get_base_var(var); | |
241 | if (baselen <= 0) | |
242 | break; | |
243 | var[baselen++] = '.'; | |
244 | var[baselen] = 0; | |
245 | continue; | |
246 | } | |
247 | if (!isalpha(c)) | |
248 | break; | |
128af9d1 | 249 | var[baselen] = tolower(c); |
ef90d6d4 | 250 | if (get_value(fn, data, var, baselen+1) < 0) |
17712991 LT |
251 | break; |
252 | } | |
4f629539 | 253 | die("bad config file line %d in %s", config_linenr, config_file_name); |
17712991 LT |
254 | } |
255 | ||
c8deb5a1 | 256 | static int parse_unit_factor(const char *end, unsigned long *val) |
0b87b6e0 BD |
257 | { |
258 | if (!*end) | |
259 | return 1; | |
c8deb5a1 SP |
260 | else if (!strcasecmp(end, "k")) { |
261 | *val *= 1024; | |
262 | return 1; | |
263 | } | |
264 | else if (!strcasecmp(end, "m")) { | |
265 | *val *= 1024 * 1024; | |
266 | return 1; | |
267 | } | |
268 | else if (!strcasecmp(end, "g")) { | |
269 | *val *= 1024 * 1024 * 1024; | |
270 | return 1; | |
271 | } | |
272 | return 0; | |
0b87b6e0 BD |
273 | } |
274 | ||
0433bcd9 | 275 | static int git_parse_long(const char *value, long *ret) |
0b87b6e0 BD |
276 | { |
277 | if (value && *value) { | |
278 | char *end; | |
279 | long val = strtol(value, &end, 0); | |
c8deb5a1 SP |
280 | unsigned long factor = 1; |
281 | if (!parse_unit_factor(end, &factor)) | |
282 | return 0; | |
283 | *ret = val * factor; | |
0b87b6e0 BD |
284 | return 1; |
285 | } | |
286 | return 0; | |
287 | } | |
288 | ||
289 | int git_parse_ulong(const char *value, unsigned long *ret) | |
17712991 LT |
290 | { |
291 | if (value && *value) { | |
292 | char *end; | |
0b87b6e0 | 293 | unsigned long val = strtoul(value, &end, 0); |
c8deb5a1 SP |
294 | if (!parse_unit_factor(end, &val)) |
295 | return 0; | |
296 | *ret = val; | |
0b87b6e0 | 297 | return 1; |
17712991 | 298 | } |
0b87b6e0 BD |
299 | return 0; |
300 | } | |
301 | ||
c1867cea JK |
302 | static void die_bad_config(const char *name) |
303 | { | |
304 | if (config_file_name) | |
305 | die("bad config value for '%s' in %s", name, config_file_name); | |
306 | die("bad config value for '%s'", name); | |
307 | } | |
308 | ||
0b87b6e0 BD |
309 | int git_config_int(const char *name, const char *value) |
310 | { | |
0433bcd9 | 311 | long ret = 0; |
0b87b6e0 | 312 | if (!git_parse_long(value, &ret)) |
c1867cea | 313 | die_bad_config(name); |
0b87b6e0 BD |
314 | return ret; |
315 | } | |
316 | ||
317 | unsigned long git_config_ulong(const char *name, const char *value) | |
318 | { | |
319 | unsigned long ret; | |
320 | if (!git_parse_ulong(value, &ret)) | |
c1867cea | 321 | die_bad_config(name); |
0b87b6e0 | 322 | return ret; |
17712991 LT |
323 | } |
324 | ||
8420ccd8 | 325 | int git_config_maybe_bool(const char *name, const char *value) |
17712991 LT |
326 | { |
327 | if (!value) | |
328 | return 1; | |
329 | if (!*value) | |
330 | return 0; | |
8420ccd8 JH |
331 | if (!strcasecmp(value, "true") |
332 | || !strcasecmp(value, "yes") | |
333 | || !strcasecmp(value, "on")) | |
17712991 | 334 | return 1; |
8420ccd8 JH |
335 | if (!strcasecmp(value, "false") |
336 | || !strcasecmp(value, "no") | |
337 | || !strcasecmp(value, "off")) | |
17712991 | 338 | return 0; |
8420ccd8 JH |
339 | return -1; |
340 | } | |
341 | ||
342 | int git_config_bool_or_int(const char *name, const char *value, int *is_bool) | |
343 | { | |
344 | int v = git_config_maybe_bool(name, value); | |
345 | if (0 <= v) { | |
346 | *is_bool = 1; | |
347 | return v; | |
348 | } | |
a53f2ec6 | 349 | *is_bool = 0; |
c35b0b58 | 350 | return git_config_int(name, value); |
17712991 LT |
351 | } |
352 | ||
a53f2ec6 JH |
353 | int git_config_bool(const char *name, const char *value) |
354 | { | |
355 | int discard; | |
c35b0b58 | 356 | return !!git_config_bool_or_int(name, value, &discard); |
a53f2ec6 JH |
357 | } |
358 | ||
ea5105a5 CC |
359 | int git_config_string(const char **dest, const char *var, const char *value) |
360 | { | |
361 | if (!value) | |
362 | return config_error_nonbool(var); | |
363 | *dest = xstrdup(value); | |
364 | return 0; | |
365 | } | |
366 | ||
395de250 MM |
367 | int git_config_pathname(const char **dest, const char *var, const char *value) |
368 | { | |
369 | if (!value) | |
370 | return config_error_nonbool(var); | |
371 | *dest = expand_user_path(value); | |
372 | if (!*dest) | |
373 | die("Failed to expand user dir in: '%s'", value); | |
374 | return 0; | |
375 | } | |
376 | ||
806e2ad7 | 377 | static int git_default_core_config(const char *var, const char *value) |
17712991 LT |
378 | { |
379 | /* This needs a better name */ | |
380 | if (!strcmp(var, "core.filemode")) { | |
381 | trust_executable_bit = git_config_bool(var, value); | |
382 | return 0; | |
383 | } | |
1ce4790b AR |
384 | if (!strcmp(var, "core.trustctime")) { |
385 | trust_ctime = git_config_bool(var, value); | |
386 | return 0; | |
387 | } | |
17712991 | 388 | |
9378c161 JH |
389 | if (!strcmp(var, "core.quotepath")) { |
390 | quote_path_fully = git_config_bool(var, value); | |
391 | return 0; | |
392 | } | |
393 | ||
78a8d641 JS |
394 | if (!strcmp(var, "core.symlinks")) { |
395 | has_symlinks = git_config_bool(var, value); | |
396 | return 0; | |
397 | } | |
398 | ||
0a9b88b7 LT |
399 | if (!strcmp(var, "core.ignorecase")) { |
400 | ignore_case = git_config_bool(var, value); | |
401 | return 0; | |
402 | } | |
403 | ||
7d1864ce JH |
404 | if (!strcmp(var, "core.bare")) { |
405 | is_bare_repository_cfg = git_config_bool(var, value); | |
406 | return 0; | |
407 | } | |
408 | ||
5f73076c JH |
409 | if (!strcmp(var, "core.ignorestat")) { |
410 | assume_unchanged = git_config_bool(var, value); | |
411 | return 0; | |
412 | } | |
413 | ||
e388c738 JH |
414 | if (!strcmp(var, "core.prefersymlinkrefs")) { |
415 | prefer_symlink_refs = git_config_bool(var, value); | |
f8348be3 JS |
416 | return 0; |
417 | } | |
418 | ||
6de08ae6 SP |
419 | if (!strcmp(var, "core.logallrefupdates")) { |
420 | log_all_ref_updates = git_config_bool(var, value); | |
421 | return 0; | |
422 | } | |
423 | ||
2f8acdb3 JH |
424 | if (!strcmp(var, "core.warnambiguousrefs")) { |
425 | warn_ambiguous_refs = git_config_bool(var, value); | |
426 | return 0; | |
427 | } | |
428 | ||
960ccca6 | 429 | if (!strcmp(var, "core.loosecompression")) { |
12f6c308 JBH |
430 | int level = git_config_int(var, value); |
431 | if (level == -1) | |
432 | level = Z_DEFAULT_COMPRESSION; | |
433 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
434 | die("bad zlib compression level %d", level); | |
435 | zlib_compression_level = level; | |
960ccca6 DH |
436 | zlib_compression_seen = 1; |
437 | return 0; | |
438 | } | |
439 | ||
440 | if (!strcmp(var, "core.compression")) { | |
441 | int level = git_config_int(var, value); | |
442 | if (level == -1) | |
443 | level = Z_DEFAULT_COMPRESSION; | |
444 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
445 | die("bad zlib compression level %d", level); | |
446 | core_compression_level = level; | |
447 | core_compression_seen = 1; | |
448 | if (!zlib_compression_seen) | |
449 | zlib_compression_level = level; | |
12f6c308 JBH |
450 | return 0; |
451 | } | |
452 | ||
60bb8b14 | 453 | if (!strcmp(var, "core.packedgitwindowsize")) { |
5faaf246 | 454 | int pgsz_x2 = getpagesize() * 2; |
60bb8b14 | 455 | packed_git_window_size = git_config_int(var, value); |
5faaf246 JH |
456 | |
457 | /* This value must be multiple of (pagesize * 2) */ | |
458 | packed_git_window_size /= pgsz_x2; | |
459 | if (packed_git_window_size < 1) | |
460 | packed_git_window_size = 1; | |
461 | packed_git_window_size *= pgsz_x2; | |
60bb8b14 SP |
462 | return 0; |
463 | } | |
464 | ||
77ccc5bb SP |
465 | if (!strcmp(var, "core.packedgitlimit")) { |
466 | packed_git_limit = git_config_int(var, value); | |
467 | return 0; | |
468 | } | |
469 | ||
18bdec11 SP |
470 | if (!strcmp(var, "core.deltabasecachelimit")) { |
471 | delta_base_cache_limit = git_config_int(var, value); | |
472 | return 0; | |
473 | } | |
474 | ||
6c510bee | 475 | if (!strcmp(var, "core.autocrlf")) { |
d7f46334 LT |
476 | if (value && !strcasecmp(value, "input")) { |
477 | auto_crlf = -1; | |
478 | return 0; | |
479 | } | |
6c510bee LT |
480 | auto_crlf = git_config_bool(var, value); |
481 | return 0; | |
482 | } | |
483 | ||
21e5ad50 SP |
484 | if (!strcmp(var, "core.safecrlf")) { |
485 | if (value && !strcasecmp(value, "warn")) { | |
486 | safe_crlf = SAFE_CRLF_WARN; | |
487 | return 0; | |
488 | } | |
489 | safe_crlf = git_config_bool(var, value); | |
490 | return 0; | |
491 | } | |
492 | ||
a97a7468 JS |
493 | if (!strcmp(var, "core.notesref")) { |
494 | notes_ref_name = xstrdup(value); | |
495 | return 0; | |
496 | } | |
497 | ||
806e2ad7 LT |
498 | if (!strcmp(var, "core.pager")) |
499 | return git_config_string(&pager_program, var, value); | |
500 | ||
501 | if (!strcmp(var, "core.editor")) | |
502 | return git_config_string(&editor_program, var, value); | |
503 | ||
504 | if (!strcmp(var, "core.excludesfile")) | |
395de250 | 505 | return git_config_pathname(&excludes_file, var, value); |
806e2ad7 LT |
506 | |
507 | if (!strcmp(var, "core.whitespace")) { | |
508 | if (!value) | |
509 | return config_error_nonbool(var); | |
510 | whitespace_rule_cfg = parse_whitespace_rule(value); | |
511 | return 0; | |
512 | } | |
513 | ||
aafe9fba LT |
514 | if (!strcmp(var, "core.fsyncobjectfiles")) { |
515 | fsync_object_files = git_config_bool(var, value); | |
516 | return 0; | |
517 | } | |
518 | ||
671c9b7e LT |
519 | if (!strcmp(var, "core.preloadindex")) { |
520 | core_preload_index = git_config_bool(var, value); | |
521 | return 0; | |
522 | } | |
523 | ||
348df166 JS |
524 | if (!strcmp(var, "core.createobject")) { |
525 | if (!strcmp(value, "rename")) | |
526 | object_creation_mode = OBJECT_CREATION_USES_RENAMES; | |
527 | else if (!strcmp(value, "link")) | |
528 | object_creation_mode = OBJECT_CREATION_USES_HARDLINKS; | |
529 | else | |
530 | die("Invalid mode for object creation: %s", value); | |
be66a6c4 JS |
531 | return 0; |
532 | } | |
533 | ||
08aefc9e NTND |
534 | if (!strcmp(var, "core.sparsecheckout")) { |
535 | core_apply_sparse_checkout = git_config_bool(var, value); | |
536 | return 0; | |
537 | } | |
538 | ||
806e2ad7 LT |
539 | /* Add other config variables here and to Documentation/config.txt. */ |
540 | return 0; | |
541 | } | |
542 | ||
d1364529 | 543 | static int git_default_user_config(const char *var, const char *value) |
806e2ad7 | 544 | { |
e1b10391 | 545 | if (!strcmp(var, "user.name")) { |
6c47d0e8 JH |
546 | if (!value) |
547 | return config_error_nonbool(var); | |
817151e6 | 548 | strlcpy(git_default_name, value, sizeof(git_default_name)); |
91c38a21 | 549 | user_ident_explicitly_given |= IDENT_NAME_GIVEN; |
e1b10391 LT |
550 | return 0; |
551 | } | |
552 | ||
553 | if (!strcmp(var, "user.email")) { | |
6c47d0e8 JH |
554 | if (!value) |
555 | return config_error_nonbool(var); | |
817151e6 | 556 | strlcpy(git_default_email, value, sizeof(git_default_email)); |
91c38a21 | 557 | user_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
e1b10391 LT |
558 | return 0; |
559 | } | |
560 | ||
d1364529 LT |
561 | /* Add other config variables here and to Documentation/config.txt. */ |
562 | return 0; | |
563 | } | |
564 | ||
1141f492 | 565 | static int git_default_i18n_config(const char *var, const char *value) |
d1364529 | 566 | { |
ea5105a5 CC |
567 | if (!strcmp(var, "i18n.commitencoding")) |
568 | return git_config_string(&git_commit_encoding, var, value); | |
d2c11a38 | 569 | |
ea5105a5 CC |
570 | if (!strcmp(var, "i18n.logoutputencoding")) |
571 | return git_config_string(&git_log_output_encoding, var, value); | |
d2c11a38 | 572 | |
1141f492 LT |
573 | /* Add other config variables here and to Documentation/config.txt. */ |
574 | return 0; | |
575 | } | |
039bc64e | 576 | |
1141f492 LT |
577 | static int git_default_branch_config(const char *var, const char *value) |
578 | { | |
9ed36cfa JS |
579 | if (!strcmp(var, "branch.autosetupmerge")) { |
580 | if (value && !strcasecmp(value, "always")) { | |
581 | git_branch_track = BRANCH_TRACK_ALWAYS; | |
582 | return 0; | |
583 | } | |
584 | git_branch_track = git_config_bool(var, value); | |
585 | return 0; | |
586 | } | |
c998ae9b DS |
587 | if (!strcmp(var, "branch.autosetuprebase")) { |
588 | if (!value) | |
589 | return config_error_nonbool(var); | |
590 | else if (!strcmp(value, "never")) | |
591 | autorebase = AUTOREBASE_NEVER; | |
592 | else if (!strcmp(value, "local")) | |
593 | autorebase = AUTOREBASE_LOCAL; | |
594 | else if (!strcmp(value, "remote")) | |
595 | autorebase = AUTOREBASE_REMOTE; | |
596 | else if (!strcmp(value, "always")) | |
597 | autorebase = AUTOREBASE_ALWAYS; | |
598 | else | |
599 | return error("Malformed value for %s", var); | |
600 | return 0; | |
601 | } | |
a9cc857a | 602 | |
1ab661dd | 603 | /* Add other config variables here and to Documentation/config.txt. */ |
17712991 LT |
604 | return 0; |
605 | } | |
606 | ||
52153747 FAG |
607 | static int git_default_push_config(const char *var, const char *value) |
608 | { | |
609 | if (!strcmp(var, "push.default")) { | |
610 | if (!value) | |
611 | return config_error_nonbool(var); | |
612 | else if (!strcmp(value, "nothing")) | |
613 | push_default = PUSH_DEFAULT_NOTHING; | |
614 | else if (!strcmp(value, "matching")) | |
615 | push_default = PUSH_DEFAULT_MATCHING; | |
616 | else if (!strcmp(value, "tracking")) | |
617 | push_default = PUSH_DEFAULT_TRACKING; | |
618 | else if (!strcmp(value, "current")) | |
619 | push_default = PUSH_DEFAULT_CURRENT; | |
620 | else { | |
621 | error("Malformed value for %s: %s", var, value); | |
622 | return error("Must be one of nothing, matching, " | |
623 | "tracking or current."); | |
624 | } | |
625 | return 0; | |
626 | } | |
627 | ||
628 | /* Add other config variables here and to Documentation/config.txt. */ | |
629 | return 0; | |
630 | } | |
631 | ||
d551a488 MSO |
632 | static int git_default_mailmap_config(const char *var, const char *value) |
633 | { | |
634 | if (!strcmp(var, "mailmap.file")) | |
635 | return git_config_string(&git_mailmap_file, var, value); | |
636 | ||
637 | /* Add other config variables here and to Documentation/config.txt. */ | |
638 | return 0; | |
639 | } | |
640 | ||
1141f492 LT |
641 | int git_default_config(const char *var, const char *value, void *dummy) |
642 | { | |
643 | if (!prefixcmp(var, "core.")) | |
644 | return git_default_core_config(var, value); | |
645 | ||
646 | if (!prefixcmp(var, "user.")) | |
647 | return git_default_user_config(var, value); | |
648 | ||
649 | if (!prefixcmp(var, "i18n.")) | |
650 | return git_default_i18n_config(var, value); | |
651 | ||
652 | if (!prefixcmp(var, "branch.")) | |
653 | return git_default_branch_config(var, value); | |
654 | ||
52153747 FAG |
655 | if (!prefixcmp(var, "push.")) |
656 | return git_default_push_config(var, value); | |
657 | ||
d551a488 MSO |
658 | if (!prefixcmp(var, "mailmap.")) |
659 | return git_default_mailmap_config(var, value); | |
660 | ||
75194438 JK |
661 | if (!prefixcmp(var, "advice.")) |
662 | return git_default_advice_config(var, value); | |
663 | ||
1141f492 LT |
664 | if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { |
665 | pager_use_color = git_config_bool(var,value); | |
666 | return 0; | |
667 | } | |
668 | ||
669 | /* Add other config variables here and to Documentation/config.txt. */ | |
670 | return 0; | |
671 | } | |
672 | ||
ef90d6d4 | 673 | int git_config_from_file(config_fn_t fn, const char *filename, void *data) |
17712991 LT |
674 | { |
675 | int ret; | |
4f629539 | 676 | FILE *f = fopen(filename, "r"); |
17712991 LT |
677 | |
678 | ret = -1; | |
679 | if (f) { | |
680 | config_file = f; | |
4f629539 | 681 | config_file_name = filename; |
17712991 | 682 | config_linenr = 1; |
02e5ba4a | 683 | config_file_eof = 0; |
ef90d6d4 | 684 | ret = git_parse_file(fn, data); |
17712991 | 685 | fclose(f); |
4f629539 | 686 | config_file_name = NULL; |
17712991 LT |
687 | } |
688 | return ret; | |
689 | } | |
10bea152 | 690 | |
506b17b1 JS |
691 | const char *git_etc_gitconfig(void) |
692 | { | |
7f0e39fa | 693 | static const char *system_wide; |
2de9de5e SP |
694 | if (!system_wide) |
695 | system_wide = system_path(ETC_GITCONFIG); | |
7f0e39fa | 696 | return system_wide; |
506b17b1 JS |
697 | } |
698 | ||
e4bffb5a | 699 | static int git_env_bool(const char *k, int def) |
ab88c363 JK |
700 | { |
701 | const char *v = getenv(k); | |
702 | return v ? git_config_bool(k, v) : def; | |
703 | } | |
704 | ||
705 | int git_config_system(void) | |
706 | { | |
707 | return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); | |
708 | } | |
709 | ||
710 | int git_config_global(void) | |
711 | { | |
712 | return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0); | |
713 | } | |
714 | ||
ef90d6d4 | 715 | int git_config(config_fn_t fn, void *data) |
4f629539 | 716 | { |
aa387407 | 717 | int ret = 0, found = 0; |
5f1a63e0 | 718 | char *repo_config = NULL; |
dc871831 | 719 | const char *home = NULL; |
5f1a63e0 | 720 | |
8befc50c | 721 | /* Setting $GIT_CONFIG makes git read _only_ the given config file. */ |
dc871831 DB |
722 | if (config_exclusive_filename) |
723 | return git_config_from_file(fn, config_exclusive_filename, data); | |
aa387407 | 724 | if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) { |
dc871831 DB |
725 | ret += git_config_from_file(fn, git_etc_gitconfig(), |
726 | data); | |
aa387407 FC |
727 | found += 1; |
728 | } | |
5f1a63e0 | 729 | |
dc871831 | 730 | home = getenv("HOME"); |
ab88c363 | 731 | if (git_config_global() && home) { |
9befac47 | 732 | char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); |
aa387407 | 733 | if (!access(user_config, R_OK)) { |
dc871831 | 734 | ret += git_config_from_file(fn, user_config, data); |
aa387407 FC |
735 | found += 1; |
736 | } | |
5f1a63e0 JS |
737 | free(user_config); |
738 | } | |
739 | ||
a4f34cbb | 740 | repo_config = git_pathdup("config"); |
aa387407 FC |
741 | if (!access(repo_config, R_OK)) { |
742 | ret += git_config_from_file(fn, repo_config, data); | |
743 | found += 1; | |
744 | } | |
4cac42b1 | 745 | free(repo_config); |
aa387407 FC |
746 | if (found == 0) |
747 | return -1; | |
5f1a63e0 | 748 | return ret; |
4f629539 JH |
749 | } |
750 | ||
10bea152 JS |
751 | /* |
752 | * Find all the stuff for git_config_set() below. | |
753 | */ | |
4ddba79d JS |
754 | |
755 | #define MAX_MATCHES 512 | |
756 | ||
10bea152 JS |
757 | static struct { |
758 | int baselen; | |
4b25d091 | 759 | char *key; |
f98d863d | 760 | int do_not_match; |
4b25d091 | 761 | regex_t *value_regex; |
4ddba79d | 762 | int multi_replace; |
dc49cd76 | 763 | size_t offset[MAX_MATCHES]; |
10bea152 JS |
764 | enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; |
765 | int seen; | |
766 | } store; | |
767 | ||
4b25d091 | 768 | static int matches(const char *key, const char *value) |
f98d863d JS |
769 | { |
770 | return !strcmp(key, store.key) && | |
771 | (store.value_regex == NULL || | |
772 | (store.do_not_match ^ | |
773 | !regexec(store.value_regex, value, 0, NULL, 0))); | |
774 | } | |
775 | ||
4b25d091 | 776 | static int store_aux(const char *key, const char *value, void *cb) |
10bea152 | 777 | { |
ae9ee41d JH |
778 | const char *ep; |
779 | size_t section_len; | |
780 | ||
10bea152 JS |
781 | switch (store.state) { |
782 | case KEY_SEEN: | |
f98d863d | 783 | if (matches(key, value)) { |
4ddba79d | 784 | if (store.seen == 1 && store.multi_replace == 0) { |
64c0d71c | 785 | warning("%s has multiple values", key); |
4ddba79d | 786 | } else if (store.seen >= MAX_MATCHES) { |
64c0d71c | 787 | error("too many matches for %s", key); |
4ddba79d | 788 | return 1; |
10bea152 | 789 | } |
4ddba79d JS |
790 | |
791 | store.offset[store.seen] = ftell(config_file); | |
10bea152 JS |
792 | store.seen++; |
793 | } | |
794 | break; | |
795 | case SECTION_SEEN: | |
ae9ee41d JH |
796 | /* |
797 | * What we are looking for is in store.key (both | |
798 | * section and var), and its section part is baselen | |
799 | * long. We found key (again, both section and var). | |
800 | * We would want to know if this key is in the same | |
801 | * section as what we are looking for. We already | |
802 | * know we are in the same section as what should | |
803 | * hold store.key. | |
804 | */ | |
805 | ep = strrchr(key, '.'); | |
806 | section_len = ep - key; | |
807 | ||
808 | if ((section_len != store.baselen) || | |
809 | memcmp(key, store.key, section_len+1)) { | |
10bea152 JS |
810 | store.state = SECTION_END_SEEN; |
811 | break; | |
ae9ee41d JH |
812 | } |
813 | ||
814 | /* | |
815 | * Do not increment matches: this is no match, but we | |
816 | * just made sure we are in the desired section. | |
817 | */ | |
818 | store.offset[store.seen] = ftell(config_file); | |
10bea152 JS |
819 | /* fallthru */ |
820 | case SECTION_END_SEEN: | |
821 | case START: | |
f98d863d | 822 | if (matches(key, value)) { |
4ddba79d | 823 | store.offset[store.seen] = ftell(config_file); |
10bea152 JS |
824 | store.state = KEY_SEEN; |
825 | store.seen++; | |
d14f7764 LT |
826 | } else { |
827 | if (strrchr(key, '.') - key == store.baselen && | |
bdf0ef08 | 828 | !strncmp(key, store.key, store.baselen)) { |
93ddef3e | 829 | store.state = SECTION_SEEN; |
bdf0ef08 | 830 | store.offset[store.seen] = ftell(config_file); |
d14f7764 | 831 | } |
bdf0ef08 | 832 | } |
10bea152 JS |
833 | } |
834 | return 0; | |
835 | } | |
836 | ||
64c0d71c | 837 | static int write_error(const char *filename) |
480c9e52 | 838 | { |
64c0d71c | 839 | error("failed to write new configuration file %s", filename); |
480c9e52 AW |
840 | |
841 | /* Same error code as "failed to rename". */ | |
842 | return 4; | |
843 | } | |
844 | ||
4b25d091 | 845 | static int store_write_section(int fd, const char *key) |
10bea152 | 846 | { |
cb891a59 KH |
847 | const char *dot; |
848 | int i, success; | |
f285a2d7 | 849 | struct strbuf sb = STRBUF_INIT; |
d14f7764 | 850 | |
cb891a59 | 851 | dot = memchr(key, '.', store.baselen); |
d14f7764 | 852 | if (dot) { |
cb891a59 KH |
853 | strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key); |
854 | for (i = dot - key + 1; i < store.baselen; i++) { | |
e5c349ba | 855 | if (key[i] == '"' || key[i] == '\\') |
cb891a59 KH |
856 | strbuf_addch(&sb, '\\'); |
857 | strbuf_addch(&sb, key[i]); | |
d14f7764 | 858 | } |
cb891a59 KH |
859 | strbuf_addstr(&sb, "\"]\n"); |
860 | } else { | |
861 | strbuf_addf(&sb, "[%.*s]\n", store.baselen, key); | |
d14f7764 LT |
862 | } |
863 | ||
cb891a59 KH |
864 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; |
865 | strbuf_release(&sb); | |
480c9e52 | 866 | |
cb891a59 | 867 | return success; |
10bea152 JS |
868 | } |
869 | ||
4b25d091 | 870 | static int store_write_pair(int fd, const char *key, const char *value) |
10bea152 | 871 | { |
cb891a59 KH |
872 | int i, success; |
873 | int length = strlen(key + store.baselen + 1); | |
874 | const char *quote = ""; | |
f285a2d7 | 875 | struct strbuf sb = STRBUF_INIT; |
cdd4fb15 | 876 | |
6281f394 JM |
877 | /* |
878 | * Check to see if the value needs to be surrounded with a dq pair. | |
879 | * Note that problematic characters are always backslash-quoted; this | |
880 | * check is about not losing leading or trailing SP and strings that | |
881 | * follow beginning-of-comment characters (i.e. ';' and '#') by the | |
882 | * configuration parser. | |
883 | */ | |
cdd4fb15 | 884 | if (value[0] == ' ') |
cb891a59 | 885 | quote = "\""; |
cdd4fb15 BG |
886 | for (i = 0; value[i]; i++) |
887 | if (value[i] == ';' || value[i] == '#') | |
cb891a59 KH |
888 | quote = "\""; |
889 | if (i && value[i - 1] == ' ') | |
890 | quote = "\""; | |
891 | ||
cb891a59 KH |
892 | strbuf_addf(&sb, "\t%.*s = %s", |
893 | length, key + store.baselen + 1, quote); | |
10bea152 | 894 | |
10bea152 JS |
895 | for (i = 0; value[i]; i++) |
896 | switch (value[i]) { | |
480c9e52 | 897 | case '\n': |
cb891a59 | 898 | strbuf_addstr(&sb, "\\n"); |
480c9e52 AW |
899 | break; |
900 | case '\t': | |
cb891a59 | 901 | strbuf_addstr(&sb, "\\t"); |
480c9e52 AW |
902 | break; |
903 | case '"': | |
904 | case '\\': | |
cb891a59 | 905 | strbuf_addch(&sb, '\\'); |
480c9e52 | 906 | default: |
cb891a59 | 907 | strbuf_addch(&sb, value[i]); |
480c9e52 AW |
908 | break; |
909 | } | |
cb891a59 KH |
910 | strbuf_addf(&sb, "%s\n", quote); |
911 | ||
912 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; | |
913 | strbuf_release(&sb); | |
914 | ||
915 | return success; | |
10bea152 JS |
916 | } |
917 | ||
4b25d091 FC |
918 | static ssize_t find_beginning_of_line(const char *contents, size_t size, |
919 | size_t offset_, int *found_bracket) | |
4ddba79d | 920 | { |
dc49cd76 SP |
921 | size_t equal_offset = size, bracket_offset = size; |
922 | ssize_t offset; | |
4ddba79d | 923 | |
7a31cc0f | 924 | contline: |
a6080a0a | 925 | for (offset = offset_-2; offset > 0 |
4ddba79d JS |
926 | && contents[offset] != '\n'; offset--) |
927 | switch (contents[offset]) { | |
928 | case '=': equal_offset = offset; break; | |
929 | case ']': bracket_offset = offset; break; | |
930 | } | |
7a31cc0f FL |
931 | if (offset > 0 && contents[offset-1] == '\\') { |
932 | offset_ = offset; | |
933 | goto contline; | |
934 | } | |
4ddba79d JS |
935 | if (bracket_offset < equal_offset) { |
936 | *found_bracket = 1; | |
937 | offset = bracket_offset+1; | |
938 | } else | |
939 | offset++; | |
940 | ||
941 | return offset; | |
942 | } | |
943 | ||
4b25d091 | 944 | int git_config_set(const char *key, const char *value) |
10bea152 | 945 | { |
4ddba79d | 946 | return git_config_set_multivar(key, value, NULL, 0); |
10bea152 JS |
947 | } |
948 | ||
949 | /* | |
950 | * If value==NULL, unset in (remove from) config, | |
951 | * if value_regex!=NULL, disregard key/value pairs where value does not match. | |
4ddba79d JS |
952 | * if multi_replace==0, nothing, or only one matching key/value is replaced, |
953 | * else all matching key/values (regardless how many) are removed, | |
954 | * before the new pair is written. | |
10bea152 JS |
955 | * |
956 | * Returns 0 on success. | |
957 | * | |
958 | * This function does this: | |
959 | * | |
960 | * - it locks the config file by creating ".git/config.lock" | |
961 | * | |
962 | * - it then parses the config using store_aux() as validator to find | |
963 | * the position on the key/value pair to replace. If it is to be unset, | |
964 | * it must be found exactly once. | |
965 | * | |
966 | * - the config file is mmap()ed and the part before the match (if any) is | |
967 | * written to the lock file, then the changed part and the rest. | |
968 | * | |
969 | * - the config file is removed and the lock file rename()d to it. | |
970 | * | |
971 | */ | |
4b25d091 FC |
972 | int git_config_set_multivar(const char *key, const char *value, |
973 | const char *value_regex, int multi_replace) | |
10bea152 | 974 | { |
d14f7764 | 975 | int i, dot; |
f8ba655e | 976 | int fd = -1, in_fd; |
dafc88b1 | 977 | int ret; |
4b25d091 | 978 | char *config_filename; |
6cbf973c | 979 | struct lock_file *lock = NULL; |
4b25d091 | 980 | const char *last_dot = strrchr(key, '.'); |
4ddba79d | 981 | |
dc871831 DB |
982 | if (config_exclusive_filename) |
983 | config_filename = xstrdup(config_exclusive_filename); | |
984 | else | |
a4f34cbb | 985 | config_filename = git_pathdup("config"); |
9c3796fc | 986 | |
10bea152 JS |
987 | /* |
988 | * Since "key" actually contains the section name and the real | |
989 | * key name separated by a dot, we have to know where the dot is. | |
990 | */ | |
b17e659d | 991 | |
dafc88b1 | 992 | if (last_dot == NULL) { |
64c0d71c | 993 | error("key does not contain a section: %s", key); |
dafc88b1 SH |
994 | ret = 2; |
995 | goto out_free; | |
10bea152 | 996 | } |
b17e659d JS |
997 | store.baselen = last_dot - key; |
998 | ||
999 | store.multi_replace = multi_replace; | |
10bea152 JS |
1000 | |
1001 | /* | |
1002 | * Validate the key and while at it, lower case it for matching. | |
1003 | */ | |
2d7320d0 | 1004 | store.key = xmalloc(strlen(key) + 1); |
d14f7764 LT |
1005 | dot = 0; |
1006 | for (i = 0; key[i]; i++) { | |
1007 | unsigned char c = key[i]; | |
1008 | if (c == '.') | |
1009 | dot = 1; | |
1010 | /* Leave the extended basename untouched.. */ | |
1011 | if (!dot || i > store.baselen) { | |
38c5afa8 | 1012 | if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) { |
64c0d71c | 1013 | error("invalid key: %s", key); |
d14f7764 LT |
1014 | free(store.key); |
1015 | ret = 1; | |
1016 | goto out_free; | |
1017 | } | |
1018 | c = tolower(c); | |
6f71686e | 1019 | } else if (c == '\n') { |
64c0d71c | 1020 | error("invalid key (newline): %s", key); |
6f71686e JS |
1021 | free(store.key); |
1022 | ret = 1; | |
1023 | goto out_free; | |
d14f7764 LT |
1024 | } |
1025 | store.key[i] = c; | |
1026 | } | |
3dd94e3b | 1027 | store.key[i] = 0; |
10bea152 JS |
1028 | |
1029 | /* | |
6cbf973c | 1030 | * The lock serves a purpose in addition to locking: the new |
10bea152 JS |
1031 | * contents of .git/config will be written into it. |
1032 | */ | |
6cbf973c BS |
1033 | lock = xcalloc(sizeof(struct lock_file), 1); |
1034 | fd = hold_lock_file_for_update(lock, config_filename, 0); | |
1035 | if (fd < 0) { | |
6ffd567b | 1036 | error("could not lock config file %s: %s", config_filename, strerror(errno)); |
10bea152 | 1037 | free(store.key); |
dafc88b1 SH |
1038 | ret = -1; |
1039 | goto out_free; | |
10bea152 JS |
1040 | } |
1041 | ||
1042 | /* | |
1043 | * If .git/config does not exist yet, write a minimal version. | |
1044 | */ | |
88fb958b AR |
1045 | in_fd = open(config_filename, O_RDONLY); |
1046 | if ( in_fd < 0 ) { | |
10bea152 JS |
1047 | free(store.key); |
1048 | ||
88fb958b AR |
1049 | if ( ENOENT != errno ) { |
1050 | error("opening %s: %s", config_filename, | |
1051 | strerror(errno)); | |
dafc88b1 SH |
1052 | ret = 3; /* same as "invalid config file" */ |
1053 | goto out_free; | |
88fb958b | 1054 | } |
10bea152 JS |
1055 | /* if nothing to unset, error out */ |
1056 | if (value == NULL) { | |
dafc88b1 SH |
1057 | ret = 5; |
1058 | goto out_free; | |
10bea152 JS |
1059 | } |
1060 | ||
4b25d091 | 1061 | store.key = (char *)key; |
480c9e52 | 1062 | if (!store_write_section(fd, key) || |
93c1e079 JH |
1063 | !store_write_pair(fd, key, value)) |
1064 | goto write_err_out; | |
1065 | } else { | |
88fb958b | 1066 | struct stat st; |
4b25d091 | 1067 | char *contents; |
dc49cd76 SP |
1068 | size_t contents_sz, copy_begin, copy_end; |
1069 | int i, new_line = 0; | |
10bea152 JS |
1070 | |
1071 | if (value_regex == NULL) | |
1072 | store.value_regex = NULL; | |
1073 | else { | |
f98d863d JS |
1074 | if (value_regex[0] == '!') { |
1075 | store.do_not_match = 1; | |
1076 | value_regex++; | |
1077 | } else | |
1078 | store.do_not_match = 0; | |
1079 | ||
2d7320d0 | 1080 | store.value_regex = (regex_t*)xmalloc(sizeof(regex_t)); |
10bea152 JS |
1081 | if (regcomp(store.value_regex, value_regex, |
1082 | REG_EXTENDED)) { | |
64c0d71c | 1083 | error("invalid pattern: %s", value_regex); |
10bea152 | 1084 | free(store.value_regex); |
dafc88b1 SH |
1085 | ret = 6; |
1086 | goto out_free; | |
10bea152 JS |
1087 | } |
1088 | } | |
1089 | ||
4ddba79d | 1090 | store.offset[0] = 0; |
10bea152 JS |
1091 | store.state = START; |
1092 | store.seen = 0; | |
1093 | ||
1094 | /* | |
1095 | * After this, store.offset will contain the *end* offset | |
1096 | * of the last match, or remain at 0 if no match was found. | |
1097 | * As a side effect, we make sure to transform only a valid | |
1098 | * existing config file. | |
1099 | */ | |
ef90d6d4 | 1100 | if (git_config_from_file(store_aux, config_filename, NULL)) { |
64c0d71c | 1101 | error("invalid config file %s", config_filename); |
10bea152 JS |
1102 | free(store.key); |
1103 | if (store.value_regex != NULL) { | |
1104 | regfree(store.value_regex); | |
1105 | free(store.value_regex); | |
1106 | } | |
dafc88b1 SH |
1107 | ret = 3; |
1108 | goto out_free; | |
10bea152 JS |
1109 | } |
1110 | ||
1111 | free(store.key); | |
1112 | if (store.value_regex != NULL) { | |
1113 | regfree(store.value_regex); | |
1114 | free(store.value_regex); | |
1115 | } | |
1116 | ||
4ddba79d JS |
1117 | /* if nothing to unset, or too many matches, error out */ |
1118 | if ((store.seen == 0 && value == NULL) || | |
1119 | (store.seen > 1 && multi_replace == 0)) { | |
dafc88b1 SH |
1120 | ret = 5; |
1121 | goto out_free; | |
10bea152 JS |
1122 | } |
1123 | ||
88fb958b | 1124 | fstat(in_fd, &st); |
dc49cd76 SP |
1125 | contents_sz = xsize_t(st.st_size); |
1126 | contents = xmmap(NULL, contents_sz, PROT_READ, | |
10bea152 JS |
1127 | MAP_PRIVATE, in_fd, 0); |
1128 | close(in_fd); | |
1129 | ||
4ddba79d JS |
1130 | if (store.seen == 0) |
1131 | store.seen = 1; | |
1132 | ||
1133 | for (i = 0, copy_begin = 0; i < store.seen; i++) { | |
1134 | if (store.offset[i] == 0) { | |
dc49cd76 | 1135 | store.offset[i] = copy_end = contents_sz; |
4ddba79d JS |
1136 | } else if (store.state != KEY_SEEN) { |
1137 | copy_end = store.offset[i]; | |
10bea152 | 1138 | } else |
4ddba79d | 1139 | copy_end = find_beginning_of_line( |
dc49cd76 | 1140 | contents, contents_sz, |
4ddba79d JS |
1141 | store.offset[i]-2, &new_line); |
1142 | ||
02e5ba4a JK |
1143 | if (copy_end > 0 && contents[copy_end-1] != '\n') |
1144 | new_line = 1; | |
1145 | ||
4ddba79d JS |
1146 | /* write the first part of the config */ |
1147 | if (copy_end > copy_begin) { | |
93c1e079 JH |
1148 | if (write_in_full(fd, contents + copy_begin, |
1149 | copy_end - copy_begin) < | |
1150 | copy_end - copy_begin) | |
1151 | goto write_err_out; | |
1152 | if (new_line && | |
2b7ca830 | 1153 | write_str_in_full(fd, "\n") != 1) |
93c1e079 | 1154 | goto write_err_out; |
4ddba79d JS |
1155 | } |
1156 | copy_begin = store.offset[i]; | |
10bea152 JS |
1157 | } |
1158 | ||
10bea152 JS |
1159 | /* write the pair (value == NULL means unset) */ |
1160 | if (value != NULL) { | |
93c1e079 JH |
1161 | if (store.state == START) { |
1162 | if (!store_write_section(fd, key)) | |
1163 | goto write_err_out; | |
480c9e52 | 1164 | } |
93c1e079 JH |
1165 | if (!store_write_pair(fd, key, value)) |
1166 | goto write_err_out; | |
10bea152 JS |
1167 | } |
1168 | ||
1169 | /* write the rest of the config */ | |
dc49cd76 | 1170 | if (copy_begin < contents_sz) |
93c1e079 | 1171 | if (write_in_full(fd, contents + copy_begin, |
dc49cd76 SP |
1172 | contents_sz - copy_begin) < |
1173 | contents_sz - copy_begin) | |
93c1e079 | 1174 | goto write_err_out; |
10bea152 | 1175 | |
dc49cd76 | 1176 | munmap(contents, contents_sz); |
10bea152 JS |
1177 | } |
1178 | ||
4ed7cd3a | 1179 | if (commit_lock_file(lock) < 0) { |
64c0d71c | 1180 | error("could not commit config file %s", config_filename); |
dafc88b1 SH |
1181 | ret = 4; |
1182 | goto out_free; | |
10bea152 JS |
1183 | } |
1184 | ||
6cbf973c BS |
1185 | /* |
1186 | * lock is committed, so don't try to roll it back below. | |
1187 | * NOTE: Since lockfile.c keeps a linked list of all created | |
1188 | * lock_file structures, it isn't safe to free(lock). It's | |
1189 | * better to just leave it hanging around. | |
1190 | */ | |
1191 | lock = NULL; | |
dafc88b1 SH |
1192 | ret = 0; |
1193 | ||
1194 | out_free: | |
6cbf973c BS |
1195 | if (lock) |
1196 | rollback_lock_file(lock); | |
4cac42b1 | 1197 | free(config_filename); |
dafc88b1 | 1198 | return ret; |
93c1e079 JH |
1199 | |
1200 | write_err_out: | |
64c0d71c | 1201 | ret = write_error(lock->filename); |
93c1e079 JH |
1202 | goto out_free; |
1203 | ||
10bea152 JS |
1204 | } |
1205 | ||
118f8b24 PB |
1206 | static int section_name_match (const char *buf, const char *name) |
1207 | { | |
1208 | int i = 0, j = 0, dot = 0; | |
a4c0d463 AV |
1209 | if (buf[i] != '[') |
1210 | return 0; | |
1211 | for (i = 1; buf[i] && buf[i] != ']'; i++) { | |
118f8b24 PB |
1212 | if (!dot && isspace(buf[i])) { |
1213 | dot = 1; | |
1214 | if (name[j++] != '.') | |
1215 | break; | |
1216 | for (i++; isspace(buf[i]); i++) | |
1217 | ; /* do nothing */ | |
1218 | if (buf[i] != '"') | |
1219 | break; | |
1220 | continue; | |
1221 | } | |
1222 | if (buf[i] == '\\' && dot) | |
1223 | i++; | |
1224 | else if (buf[i] == '"' && dot) { | |
1225 | for (i++; isspace(buf[i]); i++) | |
1226 | ; /* do_nothing */ | |
1227 | break; | |
1228 | } | |
1229 | if (buf[i] != name[j++]) | |
1230 | break; | |
1231 | } | |
a4c0d463 AV |
1232 | if (buf[i] == ']' && name[j] == 0) { |
1233 | /* | |
1234 | * We match, now just find the right length offset by | |
1235 | * gobbling up any whitespace after it, as well | |
1236 | */ | |
1237 | i++; | |
1238 | for (; buf[i] && isspace(buf[i]); i++) | |
1239 | ; /* do nothing */ | |
1240 | return i; | |
1241 | } | |
1242 | return 0; | |
118f8b24 PB |
1243 | } |
1244 | ||
1245 | /* if new_name == NULL, the section is removed instead */ | |
0667fcfb JS |
1246 | int git_config_rename_section(const char *old_name, const char *new_name) |
1247 | { | |
118f8b24 | 1248 | int ret = 0, remove = 0; |
fc1905bb | 1249 | char *config_filename; |
0667fcfb JS |
1250 | struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1); |
1251 | int out_fd; | |
1252 | char buf[1024]; | |
1253 | ||
dc871831 DB |
1254 | if (config_exclusive_filename) |
1255 | config_filename = xstrdup(config_exclusive_filename); | |
1256 | else | |
a4f34cbb | 1257 | config_filename = git_pathdup("config"); |
0667fcfb | 1258 | out_fd = hold_lock_file_for_update(lock, config_filename, 0); |
fc1905bb | 1259 | if (out_fd < 0) { |
64c0d71c | 1260 | ret = error("could not lock config file %s", config_filename); |
fc1905bb JH |
1261 | goto out; |
1262 | } | |
0667fcfb | 1263 | |
fc1905bb | 1264 | if (!(config_file = fopen(config_filename, "rb"))) { |
01ebb9dc GB |
1265 | /* no config file means nothing to rename, no error */ |
1266 | goto unlock_and_out; | |
fc1905bb | 1267 | } |
0667fcfb JS |
1268 | |
1269 | while (fgets(buf, sizeof(buf), config_file)) { | |
1270 | int i; | |
480c9e52 | 1271 | int length; |
9a5abfc7 | 1272 | char *output = buf; |
0667fcfb JS |
1273 | for (i = 0; buf[i] && isspace(buf[i]); i++) |
1274 | ; /* do nothing */ | |
1275 | if (buf[i] == '[') { | |
1276 | /* it's a section */ | |
a4c0d463 AV |
1277 | int offset = section_name_match(&buf[i], old_name); |
1278 | if (offset > 0) { | |
118f8b24 PB |
1279 | ret++; |
1280 | if (new_name == NULL) { | |
1281 | remove = 1; | |
0667fcfb JS |
1282 | continue; |
1283 | } | |
0667fcfb | 1284 | store.baselen = strlen(new_name); |
480c9e52 | 1285 | if (!store_write_section(out_fd, new_name)) { |
64c0d71c | 1286 | ret = write_error(lock->filename); |
480c9e52 AW |
1287 | goto out; |
1288 | } | |
9a5abfc7 AV |
1289 | /* |
1290 | * We wrote out the new section, with | |
1291 | * a newline, now skip the old | |
1292 | * section's length | |
1293 | */ | |
1294 | output += offset + i; | |
1295 | if (strlen(output) > 0) { | |
1296 | /* | |
1297 | * More content means there's | |
1298 | * a declaration to put on the | |
1299 | * next line; indent with a | |
1300 | * tab | |
1301 | */ | |
1302 | output -= 1; | |
1303 | output[0] = '\t'; | |
1304 | } | |
0667fcfb | 1305 | } |
118f8b24 | 1306 | remove = 0; |
0667fcfb | 1307 | } |
118f8b24 PB |
1308 | if (remove) |
1309 | continue; | |
9a5abfc7 AV |
1310 | length = strlen(output); |
1311 | if (write_in_full(out_fd, output, length) != length) { | |
64c0d71c | 1312 | ret = write_error(lock->filename); |
480c9e52 AW |
1313 | goto out; |
1314 | } | |
0667fcfb | 1315 | } |
fc1905bb | 1316 | fclose(config_file); |
01ebb9dc | 1317 | unlock_and_out: |
4ed7cd3a | 1318 | if (commit_lock_file(lock) < 0) |
64c0d71c | 1319 | ret = error("could not commit config file %s", config_filename); |
fc1905bb JH |
1320 | out: |
1321 | free(config_filename); | |
0667fcfb JS |
1322 | return ret; |
1323 | } | |
40ea4ed9 JH |
1324 | |
1325 | /* | |
1326 | * Call this to report error for your variable that should not | |
1327 | * get a boolean value (i.e. "[my] var" means "true"). | |
1328 | */ | |
1329 | int config_error_nonbool(const char *var) | |
1330 | { | |
1331 | return error("Missing value for '%s'", var); | |
1332 | } |