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