]>
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 LT |
8 | #include "cache.h" |
9 | ||
10 | #define MAXNAME (256) | |
11 | ||
12 | static FILE *config_file; | |
0dccc7dc | 13 | static const char *config_file_name; |
17712991 | 14 | static int config_linenr; |
960ccca6 DH |
15 | static int zlib_compression_seen; |
16 | ||
17712991 LT |
17 | static int get_next_char(void) |
18 | { | |
19 | int c; | |
20 | FILE *f; | |
21 | ||
22 | c = '\n'; | |
23 | if ((f = config_file) != NULL) { | |
24 | c = fgetc(f); | |
db2c075d JH |
25 | if (c == '\r') { |
26 | /* DOS like systems */ | |
27 | c = fgetc(f); | |
28 | if (c != '\n') { | |
29 | ungetc(c, f); | |
30 | c = '\r'; | |
31 | } | |
32 | } | |
17712991 LT |
33 | if (c == '\n') |
34 | config_linenr++; | |
35 | if (c == EOF) { | |
36 | config_file = NULL; | |
37 | c = '\n'; | |
38 | } | |
39 | } | |
40 | return c; | |
41 | } | |
42 | ||
43 | static char *parse_value(void) | |
44 | { | |
45 | static char value[1024]; | |
46 | int quote = 0, comment = 0, len = 0, space = 0; | |
47 | ||
48 | for (;;) { | |
49 | int c = get_next_char(); | |
50 | if (len >= sizeof(value)) | |
51 | return NULL; | |
52 | if (c == '\n') { | |
53 | if (quote) | |
54 | return NULL; | |
55 | value[len] = 0; | |
56 | return value; | |
57 | } | |
58 | if (comment) | |
59 | continue; | |
60 | if (isspace(c) && !quote) { | |
61 | space = 1; | |
62 | continue; | |
63 | } | |
7ebdba61 JS |
64 | if (!quote) { |
65 | if (c == ';' || c == '#') { | |
66 | comment = 1; | |
67 | continue; | |
68 | } | |
69 | } | |
17712991 LT |
70 | if (space) { |
71 | if (len) | |
72 | value[len++] = ' '; | |
73 | space = 0; | |
74 | } | |
75 | if (c == '\\') { | |
76 | c = get_next_char(); | |
77 | switch (c) { | |
78 | case '\n': | |
79 | continue; | |
80 | case 't': | |
81 | c = '\t'; | |
82 | break; | |
83 | case 'b': | |
84 | c = '\b'; | |
85 | break; | |
86 | case 'n': | |
87 | c = '\n'; | |
88 | break; | |
5cbb401d LT |
89 | /* Some characters escape as themselves */ |
90 | case '\\': case '"': | |
91 | break; | |
92 | /* Reject unknown escape sequences */ | |
93 | default: | |
94 | return NULL; | |
17712991 LT |
95 | } |
96 | value[len++] = c; | |
97 | continue; | |
98 | } | |
99 | if (c == '"') { | |
100 | quote = 1-quote; | |
101 | continue; | |
102 | } | |
17712991 LT |
103 | value[len++] = c; |
104 | } | |
105 | } | |
106 | ||
38c5afa8 LT |
107 | static inline int iskeychar(int c) |
108 | { | |
109 | return isalnum(c) || c == '-'; | |
110 | } | |
111 | ||
17712991 LT |
112 | static int get_value(config_fn_t fn, char *name, unsigned int len) |
113 | { | |
114 | int c; | |
115 | char *value; | |
116 | ||
117 | /* Get the full name */ | |
118 | for (;;) { | |
119 | c = get_next_char(); | |
120 | if (c == EOF) | |
121 | break; | |
38c5afa8 | 122 | if (!iskeychar(c)) |
17712991 LT |
123 | break; |
124 | name[len++] = tolower(c); | |
125 | if (len >= MAXNAME) | |
126 | return -1; | |
127 | } | |
128 | name[len] = 0; | |
129 | while (c == ' ' || c == '\t') | |
130 | c = get_next_char(); | |
131 | ||
132 | value = NULL; | |
133 | if (c != '\n') { | |
134 | if (c != '=') | |
135 | return -1; | |
136 | value = parse_value(); | |
137 | if (!value) | |
138 | return -1; | |
139 | } | |
140 | return fn(name, value); | |
141 | } | |
142 | ||
d14f7764 LT |
143 | static int get_extended_base_var(char *name, int baselen, int c) |
144 | { | |
145 | do { | |
146 | if (c == '\n') | |
147 | return -1; | |
148 | c = get_next_char(); | |
149 | } while (isspace(c)); | |
150 | ||
151 | /* We require the format to be '[base "extension"]' */ | |
152 | if (c != '"') | |
153 | return -1; | |
154 | name[baselen++] = '.'; | |
155 | ||
156 | for (;;) { | |
157 | int c = get_next_char(); | |
158 | if (c == '\n') | |
159 | return -1; | |
160 | if (c == '"') | |
161 | break; | |
162 | if (c == '\\') { | |
163 | c = get_next_char(); | |
164 | if (c == '\n') | |
165 | return -1; | |
166 | } | |
167 | name[baselen++] = c; | |
168 | if (baselen > MAXNAME / 2) | |
169 | return -1; | |
170 | } | |
171 | ||
172 | /* Final ']' */ | |
173 | if (get_next_char() != ']') | |
174 | return -1; | |
175 | return baselen; | |
176 | } | |
177 | ||
17712991 LT |
178 | static int get_base_var(char *name) |
179 | { | |
180 | int baselen = 0; | |
181 | ||
182 | for (;;) { | |
183 | int c = get_next_char(); | |
184 | if (c == EOF) | |
185 | return -1; | |
186 | if (c == ']') | |
187 | return baselen; | |
d14f7764 LT |
188 | if (isspace(c)) |
189 | return get_extended_base_var(name, baselen, c); | |
38c5afa8 | 190 | if (!iskeychar(c) && c != '.') |
17712991 LT |
191 | return -1; |
192 | if (baselen > MAXNAME / 2) | |
193 | return -1; | |
194 | name[baselen++] = tolower(c); | |
195 | } | |
196 | } | |
197 | ||
198 | static int git_parse_file(config_fn_t fn) | |
199 | { | |
200 | int comment = 0; | |
201 | int baselen = 0; | |
202 | static char var[MAXNAME]; | |
203 | ||
204 | for (;;) { | |
205 | int c = get_next_char(); | |
206 | if (c == '\n') { | |
207 | /* EOF? */ | |
208 | if (!config_file) | |
209 | return 0; | |
210 | comment = 0; | |
211 | continue; | |
212 | } | |
213 | if (comment || isspace(c)) | |
214 | continue; | |
215 | if (c == '#' || c == ';') { | |
216 | comment = 1; | |
217 | continue; | |
218 | } | |
219 | if (c == '[') { | |
220 | baselen = get_base_var(var); | |
221 | if (baselen <= 0) | |
222 | break; | |
223 | var[baselen++] = '.'; | |
224 | var[baselen] = 0; | |
225 | continue; | |
226 | } | |
227 | if (!isalpha(c)) | |
228 | break; | |
128af9d1 | 229 | var[baselen] = tolower(c); |
17712991 LT |
230 | if (get_value(fn, var, baselen+1) < 0) |
231 | break; | |
232 | } | |
4f629539 | 233 | die("bad config file line %d in %s", config_linenr, config_file_name); |
17712991 LT |
234 | } |
235 | ||
0b87b6e0 BD |
236 | static unsigned long get_unit_factor(const char *end) |
237 | { | |
238 | if (!*end) | |
239 | return 1; | |
240 | else if (!strcasecmp(end, "k")) | |
241 | return 1024; | |
242 | else if (!strcasecmp(end, "m")) | |
243 | return 1024 * 1024; | |
244 | else if (!strcasecmp(end, "g")) | |
245 | return 1024 * 1024 * 1024; | |
246 | die("unknown unit: '%s'", end); | |
247 | } | |
248 | ||
249 | int git_parse_long(const char *value, long *ret) | |
250 | { | |
251 | if (value && *value) { | |
252 | char *end; | |
253 | long val = strtol(value, &end, 0); | |
254 | *ret = val * get_unit_factor(end); | |
255 | return 1; | |
256 | } | |
257 | return 0; | |
258 | } | |
259 | ||
260 | int git_parse_ulong(const char *value, unsigned long *ret) | |
17712991 LT |
261 | { |
262 | if (value && *value) { | |
263 | char *end; | |
0b87b6e0 BD |
264 | unsigned long val = strtoul(value, &end, 0); |
265 | *ret = val * get_unit_factor(end); | |
266 | return 1; | |
17712991 | 267 | } |
0b87b6e0 BD |
268 | return 0; |
269 | } | |
270 | ||
271 | int git_config_int(const char *name, const char *value) | |
272 | { | |
273 | long ret; | |
274 | if (!git_parse_long(value, &ret)) | |
275 | die("bad config value for '%s' in %s", name, config_file_name); | |
276 | return ret; | |
277 | } | |
278 | ||
279 | unsigned long git_config_ulong(const char *name, const char *value) | |
280 | { | |
281 | unsigned long ret; | |
282 | if (!git_parse_ulong(value, &ret)) | |
283 | die("bad config value for '%s' in %s", name, config_file_name); | |
284 | return ret; | |
17712991 LT |
285 | } |
286 | ||
287 | int git_config_bool(const char *name, const char *value) | |
288 | { | |
289 | if (!value) | |
290 | return 1; | |
291 | if (!*value) | |
292 | return 0; | |
624314fd | 293 | if (!strcasecmp(value, "true") || !strcasecmp(value, "yes")) |
17712991 | 294 | return 1; |
624314fd | 295 | if (!strcasecmp(value, "false") || !strcasecmp(value, "no")) |
17712991 LT |
296 | return 0; |
297 | return git_config_int(name, value) != 0; | |
298 | } | |
299 | ||
300 | int git_default_config(const char *var, const char *value) | |
301 | { | |
302 | /* This needs a better name */ | |
303 | if (!strcmp(var, "core.filemode")) { | |
304 | trust_executable_bit = git_config_bool(var, value); | |
305 | return 0; | |
306 | } | |
307 | ||
9378c161 JH |
308 | if (!strcmp(var, "core.quotepath")) { |
309 | quote_path_fully = git_config_bool(var, value); | |
310 | return 0; | |
311 | } | |
312 | ||
78a8d641 JS |
313 | if (!strcmp(var, "core.symlinks")) { |
314 | has_symlinks = git_config_bool(var, value); | |
315 | return 0; | |
316 | } | |
317 | ||
7d1864ce JH |
318 | if (!strcmp(var, "core.bare")) { |
319 | is_bare_repository_cfg = git_config_bool(var, value); | |
320 | return 0; | |
321 | } | |
322 | ||
5f73076c JH |
323 | if (!strcmp(var, "core.ignorestat")) { |
324 | assume_unchanged = git_config_bool(var, value); | |
325 | return 0; | |
326 | } | |
327 | ||
e388c738 JH |
328 | if (!strcmp(var, "core.prefersymlinkrefs")) { |
329 | prefer_symlink_refs = git_config_bool(var, value); | |
f8348be3 JS |
330 | return 0; |
331 | } | |
332 | ||
6de08ae6 SP |
333 | if (!strcmp(var, "core.logallrefupdates")) { |
334 | log_all_ref_updates = git_config_bool(var, value); | |
335 | return 0; | |
336 | } | |
337 | ||
2f8acdb3 JH |
338 | if (!strcmp(var, "core.warnambiguousrefs")) { |
339 | warn_ambiguous_refs = git_config_bool(var, value); | |
340 | return 0; | |
341 | } | |
342 | ||
960ccca6 | 343 | if (!strcmp(var, "core.loosecompression")) { |
12f6c308 JBH |
344 | int level = git_config_int(var, value); |
345 | if (level == -1) | |
346 | level = Z_DEFAULT_COMPRESSION; | |
347 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
348 | die("bad zlib compression level %d", level); | |
349 | zlib_compression_level = level; | |
960ccca6 DH |
350 | zlib_compression_seen = 1; |
351 | return 0; | |
352 | } | |
353 | ||
354 | if (!strcmp(var, "core.compression")) { | |
355 | int level = git_config_int(var, value); | |
356 | if (level == -1) | |
357 | level = Z_DEFAULT_COMPRESSION; | |
358 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
359 | die("bad zlib compression level %d", level); | |
360 | core_compression_level = level; | |
361 | core_compression_seen = 1; | |
362 | if (!zlib_compression_seen) | |
363 | zlib_compression_level = level; | |
12f6c308 JBH |
364 | return 0; |
365 | } | |
366 | ||
60bb8b14 | 367 | if (!strcmp(var, "core.packedgitwindowsize")) { |
5faaf246 | 368 | int pgsz_x2 = getpagesize() * 2; |
60bb8b14 | 369 | packed_git_window_size = git_config_int(var, value); |
5faaf246 JH |
370 | |
371 | /* This value must be multiple of (pagesize * 2) */ | |
372 | packed_git_window_size /= pgsz_x2; | |
373 | if (packed_git_window_size < 1) | |
374 | packed_git_window_size = 1; | |
375 | packed_git_window_size *= pgsz_x2; | |
60bb8b14 SP |
376 | return 0; |
377 | } | |
378 | ||
77ccc5bb SP |
379 | if (!strcmp(var, "core.packedgitlimit")) { |
380 | packed_git_limit = git_config_int(var, value); | |
381 | return 0; | |
382 | } | |
383 | ||
18bdec11 SP |
384 | if (!strcmp(var, "core.deltabasecachelimit")) { |
385 | delta_base_cache_limit = git_config_int(var, value); | |
386 | return 0; | |
387 | } | |
388 | ||
6c510bee | 389 | if (!strcmp(var, "core.autocrlf")) { |
d7f46334 LT |
390 | if (value && !strcasecmp(value, "input")) { |
391 | auto_crlf = -1; | |
392 | return 0; | |
393 | } | |
6c510bee LT |
394 | auto_crlf = git_config_bool(var, value); |
395 | return 0; | |
396 | } | |
397 | ||
e1b10391 | 398 | if (!strcmp(var, "user.name")) { |
817151e6 | 399 | strlcpy(git_default_name, value, sizeof(git_default_name)); |
e1b10391 LT |
400 | return 0; |
401 | } | |
402 | ||
403 | if (!strcmp(var, "user.email")) { | |
817151e6 | 404 | strlcpy(git_default_email, value, sizeof(git_default_email)); |
e1b10391 LT |
405 | return 0; |
406 | } | |
407 | ||
4e72dcec | 408 | if (!strcmp(var, "i18n.commitencoding")) { |
dbb2b41a | 409 | git_commit_encoding = xstrdup(value); |
4e72dcec JH |
410 | return 0; |
411 | } | |
412 | ||
d2c11a38 | 413 | if (!strcmp(var, "i18n.logoutputencoding")) { |
dbb2b41a | 414 | git_log_output_encoding = xstrdup(value); |
d2c11a38 JH |
415 | return 0; |
416 | } | |
417 | ||
418 | ||
a159ca0c | 419 | if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { |
aa086eb8 ML |
420 | pager_use_color = git_config_bool(var,value); |
421 | return 0; | |
422 | } | |
423 | ||
54adf370 BG |
424 | if (!strcmp(var, "core.pager")) { |
425 | pager_program = xstrdup(value); | |
426 | return 0; | |
427 | } | |
428 | ||
4d87b9c5 JS |
429 | if (!strcmp(var, "core.editor")) { |
430 | editor_program = xstrdup(value); | |
431 | return 0; | |
432 | } | |
433 | ||
1ab661dd | 434 | /* Add other config variables here and to Documentation/config.txt. */ |
17712991 LT |
435 | return 0; |
436 | } | |
437 | ||
4f629539 | 438 | int git_config_from_file(config_fn_t fn, const char *filename) |
17712991 LT |
439 | { |
440 | int ret; | |
4f629539 | 441 | FILE *f = fopen(filename, "r"); |
17712991 LT |
442 | |
443 | ret = -1; | |
444 | if (f) { | |
445 | config_file = f; | |
4f629539 | 446 | config_file_name = filename; |
17712991 LT |
447 | config_linenr = 1; |
448 | ret = git_parse_file(fn); | |
449 | fclose(f); | |
4f629539 | 450 | config_file_name = NULL; |
17712991 LT |
451 | } |
452 | return ret; | |
453 | } | |
10bea152 | 454 | |
4f629539 JH |
455 | int git_config(config_fn_t fn) |
456 | { | |
5f1a63e0 JS |
457 | int ret = 0; |
458 | char *repo_config = NULL; | |
459 | const char *home = NULL, *filename; | |
460 | ||
461 | /* $GIT_CONFIG makes git read _only_ the given config file, | |
462 | * $GIT_CONFIG_LOCAL will make it process it in addition to the | |
463 | * global config file, the same way it would the per-repository | |
464 | * config file otherwise. */ | |
d4ebc36c | 465 | filename = getenv(CONFIG_ENVIRONMENT); |
5f1a63e0 | 466 | if (!filename) { |
32043c9f JS |
467 | if (!access(ETC_GITCONFIG, R_OK)) |
468 | ret += git_config_from_file(fn, ETC_GITCONFIG); | |
5f1a63e0 | 469 | home = getenv("HOME"); |
d4ebc36c | 470 | filename = getenv(CONFIG_LOCAL_ENVIRONMENT); |
5f1a63e0 | 471 | if (!filename) |
9befac47 | 472 | filename = repo_config = xstrdup(git_path("config")); |
7f29f7a9 | 473 | } |
5f1a63e0 JS |
474 | |
475 | if (home) { | |
9befac47 | 476 | char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); |
e33d0611 | 477 | if (!access(user_config, R_OK)) |
5f1a63e0 JS |
478 | ret = git_config_from_file(fn, user_config); |
479 | free(user_config); | |
480 | } | |
481 | ||
482 | ret += git_config_from_file(fn, filename); | |
4cac42b1 | 483 | free(repo_config); |
5f1a63e0 | 484 | return ret; |
4f629539 JH |
485 | } |
486 | ||
10bea152 JS |
487 | /* |
488 | * Find all the stuff for git_config_set() below. | |
489 | */ | |
4ddba79d JS |
490 | |
491 | #define MAX_MATCHES 512 | |
492 | ||
10bea152 JS |
493 | static struct { |
494 | int baselen; | |
495 | char* key; | |
f98d863d | 496 | int do_not_match; |
10bea152 | 497 | regex_t* value_regex; |
4ddba79d | 498 | int multi_replace; |
dc49cd76 | 499 | size_t offset[MAX_MATCHES]; |
10bea152 JS |
500 | enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; |
501 | int seen; | |
502 | } store; | |
503 | ||
f98d863d JS |
504 | static int matches(const char* key, const char* value) |
505 | { | |
506 | return !strcmp(key, store.key) && | |
507 | (store.value_regex == NULL || | |
508 | (store.do_not_match ^ | |
509 | !regexec(store.value_regex, value, 0, NULL, 0))); | |
510 | } | |
511 | ||
10bea152 JS |
512 | static int store_aux(const char* key, const char* value) |
513 | { | |
ae9ee41d JH |
514 | const char *ep; |
515 | size_t section_len; | |
516 | ||
10bea152 JS |
517 | switch (store.state) { |
518 | case KEY_SEEN: | |
f98d863d | 519 | if (matches(key, value)) { |
4ddba79d | 520 | if (store.seen == 1 && store.multi_replace == 0) { |
10bea152 JS |
521 | fprintf(stderr, |
522 | "Warning: %s has multiple values\n", | |
523 | key); | |
4ddba79d JS |
524 | } else if (store.seen >= MAX_MATCHES) { |
525 | fprintf(stderr, "Too many matches\n"); | |
526 | return 1; | |
10bea152 | 527 | } |
4ddba79d JS |
528 | |
529 | store.offset[store.seen] = ftell(config_file); | |
10bea152 JS |
530 | store.seen++; |
531 | } | |
532 | break; | |
533 | case SECTION_SEEN: | |
ae9ee41d JH |
534 | /* |
535 | * What we are looking for is in store.key (both | |
536 | * section and var), and its section part is baselen | |
537 | * long. We found key (again, both section and var). | |
538 | * We would want to know if this key is in the same | |
539 | * section as what we are looking for. We already | |
540 | * know we are in the same section as what should | |
541 | * hold store.key. | |
542 | */ | |
543 | ep = strrchr(key, '.'); | |
544 | section_len = ep - key; | |
545 | ||
546 | if ((section_len != store.baselen) || | |
547 | memcmp(key, store.key, section_len+1)) { | |
10bea152 JS |
548 | store.state = SECTION_END_SEEN; |
549 | break; | |
ae9ee41d JH |
550 | } |
551 | ||
552 | /* | |
553 | * Do not increment matches: this is no match, but we | |
554 | * just made sure we are in the desired section. | |
555 | */ | |
556 | store.offset[store.seen] = ftell(config_file); | |
10bea152 JS |
557 | /* fallthru */ |
558 | case SECTION_END_SEEN: | |
559 | case START: | |
f98d863d | 560 | if (matches(key, value)) { |
4ddba79d | 561 | store.offset[store.seen] = ftell(config_file); |
10bea152 JS |
562 | store.state = KEY_SEEN; |
563 | store.seen++; | |
d14f7764 LT |
564 | } else { |
565 | if (strrchr(key, '.') - key == store.baselen && | |
bdf0ef08 | 566 | !strncmp(key, store.key, store.baselen)) { |
93ddef3e | 567 | store.state = SECTION_SEEN; |
bdf0ef08 | 568 | store.offset[store.seen] = ftell(config_file); |
d14f7764 | 569 | } |
bdf0ef08 | 570 | } |
10bea152 JS |
571 | } |
572 | return 0; | |
573 | } | |
574 | ||
b79d18c9 | 575 | static int write_error(void) |
480c9e52 AW |
576 | { |
577 | fprintf(stderr, "Failed to write new configuration file\n"); | |
578 | ||
579 | /* Same error code as "failed to rename". */ | |
580 | return 4; | |
581 | } | |
582 | ||
583 | static int store_write_section(int fd, const char* key) | |
10bea152 | 584 | { |
d14f7764 LT |
585 | const char *dot = strchr(key, '.'); |
586 | int len1 = store.baselen, len2 = -1; | |
587 | ||
588 | dot = strchr(key, '.'); | |
589 | if (dot) { | |
590 | int dotlen = dot - key; | |
591 | if (dotlen < len1) { | |
592 | len2 = len1 - dotlen - 1; | |
593 | len1 = dotlen; | |
594 | } | |
595 | } | |
596 | ||
480c9e52 AW |
597 | if (write_in_full(fd, "[", 1) != 1 || |
598 | write_in_full(fd, key, len1) != len1) | |
599 | return 0; | |
d14f7764 | 600 | if (len2 >= 0) { |
480c9e52 AW |
601 | if (write_in_full(fd, " \"", 2) != 2) |
602 | return 0; | |
d14f7764 LT |
603 | while (--len2 >= 0) { |
604 | unsigned char c = *++dot; | |
605 | if (c == '"') | |
480c9e52 AW |
606 | if (write_in_full(fd, "\\", 1) != 1) |
607 | return 0; | |
608 | if (write_in_full(fd, &c, 1) != 1) | |
609 | return 0; | |
d14f7764 | 610 | } |
480c9e52 AW |
611 | if (write_in_full(fd, "\"", 1) != 1) |
612 | return 0; | |
d14f7764 | 613 | } |
480c9e52 AW |
614 | if (write_in_full(fd, "]\n", 2) != 2) |
615 | return 0; | |
616 | ||
617 | return 1; | |
10bea152 JS |
618 | } |
619 | ||
480c9e52 | 620 | static int store_write_pair(int fd, const char* key, const char* value) |
10bea152 JS |
621 | { |
622 | int i; | |
480c9e52 | 623 | int length = strlen(key+store.baselen+1); |
cdd4fb15 BG |
624 | int quote = 0; |
625 | ||
626 | /* Check to see if the value needs to be quoted. */ | |
627 | if (value[0] == ' ') | |
628 | quote = 1; | |
629 | for (i = 0; value[i]; i++) | |
630 | if (value[i] == ';' || value[i] == '#') | |
631 | quote = 1; | |
632 | if (value[i-1] == ' ') | |
633 | quote = 1; | |
10bea152 | 634 | |
480c9e52 AW |
635 | if (write_in_full(fd, "\t", 1) != 1 || |
636 | write_in_full(fd, key+store.baselen+1, length) != length || | |
637 | write_in_full(fd, " = ", 3) != 3) | |
638 | return 0; | |
cdd4fb15 BG |
639 | if (quote && write_in_full(fd, "\"", 1) != 1) |
640 | return 0; | |
10bea152 JS |
641 | for (i = 0; value[i]; i++) |
642 | switch (value[i]) { | |
480c9e52 AW |
643 | case '\n': |
644 | if (write_in_full(fd, "\\n", 2) != 2) | |
645 | return 0; | |
646 | break; | |
647 | case '\t': | |
648 | if (write_in_full(fd, "\\t", 2) != 2) | |
649 | return 0; | |
650 | break; | |
651 | case '"': | |
652 | case '\\': | |
653 | if (write_in_full(fd, "\\", 1) != 1) | |
654 | return 0; | |
655 | default: | |
656 | if (write_in_full(fd, value+i, 1) != 1) | |
657 | return 0; | |
658 | break; | |
659 | } | |
cdd4fb15 BG |
660 | if (quote && write_in_full(fd, "\"", 1) != 1) |
661 | return 0; | |
480c9e52 AW |
662 | if (write_in_full(fd, "\n", 1) != 1) |
663 | return 0; | |
664 | return 1; | |
10bea152 JS |
665 | } |
666 | ||
dc49cd76 SP |
667 | static ssize_t find_beginning_of_line(const char* contents, size_t size, |
668 | size_t offset_, int* found_bracket) | |
4ddba79d | 669 | { |
dc49cd76 SP |
670 | size_t equal_offset = size, bracket_offset = size; |
671 | ssize_t offset; | |
4ddba79d | 672 | |
a6080a0a | 673 | for (offset = offset_-2; offset > 0 |
4ddba79d JS |
674 | && contents[offset] != '\n'; offset--) |
675 | switch (contents[offset]) { | |
676 | case '=': equal_offset = offset; break; | |
677 | case ']': bracket_offset = offset; break; | |
678 | } | |
679 | if (bracket_offset < equal_offset) { | |
680 | *found_bracket = 1; | |
681 | offset = bracket_offset+1; | |
682 | } else | |
683 | offset++; | |
684 | ||
685 | return offset; | |
686 | } | |
687 | ||
10bea152 JS |
688 | int git_config_set(const char* key, const char* value) |
689 | { | |
4ddba79d | 690 | return git_config_set_multivar(key, value, NULL, 0); |
10bea152 JS |
691 | } |
692 | ||
693 | /* | |
694 | * If value==NULL, unset in (remove from) config, | |
695 | * if value_regex!=NULL, disregard key/value pairs where value does not match. | |
4ddba79d JS |
696 | * if multi_replace==0, nothing, or only one matching key/value is replaced, |
697 | * else all matching key/values (regardless how many) are removed, | |
698 | * before the new pair is written. | |
10bea152 JS |
699 | * |
700 | * Returns 0 on success. | |
701 | * | |
702 | * This function does this: | |
703 | * | |
704 | * - it locks the config file by creating ".git/config.lock" | |
705 | * | |
706 | * - it then parses the config using store_aux() as validator to find | |
707 | * the position on the key/value pair to replace. If it is to be unset, | |
708 | * it must be found exactly once. | |
709 | * | |
710 | * - the config file is mmap()ed and the part before the match (if any) is | |
711 | * written to the lock file, then the changed part and the rest. | |
712 | * | |
713 | * - the config file is removed and the lock file rename()d to it. | |
714 | * | |
715 | */ | |
716 | int git_config_set_multivar(const char* key, const char* value, | |
4ddba79d | 717 | const char* value_regex, int multi_replace) |
10bea152 | 718 | { |
d14f7764 | 719 | int i, dot; |
f8ba655e | 720 | int fd = -1, in_fd; |
dafc88b1 | 721 | int ret; |
9c3796fc | 722 | char* config_filename; |
6cbf973c | 723 | struct lock_file *lock = NULL; |
b17e659d | 724 | const char* last_dot = strrchr(key, '.'); |
4ddba79d | 725 | |
d4ebc36c | 726 | config_filename = getenv(CONFIG_ENVIRONMENT); |
9c3796fc | 727 | if (!config_filename) { |
d4ebc36c | 728 | config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT); |
9c3796fc JS |
729 | if (!config_filename) |
730 | config_filename = git_path("config"); | |
731 | } | |
9befac47 | 732 | config_filename = xstrdup(config_filename); |
9c3796fc | 733 | |
10bea152 JS |
734 | /* |
735 | * Since "key" actually contains the section name and the real | |
736 | * key name separated by a dot, we have to know where the dot is. | |
737 | */ | |
b17e659d | 738 | |
dafc88b1 | 739 | if (last_dot == NULL) { |
10bea152 | 740 | fprintf(stderr, "key does not contain a section: %s\n", key); |
dafc88b1 SH |
741 | ret = 2; |
742 | goto out_free; | |
10bea152 | 743 | } |
b17e659d JS |
744 | store.baselen = last_dot - key; |
745 | ||
746 | store.multi_replace = multi_replace; | |
10bea152 JS |
747 | |
748 | /* | |
749 | * Validate the key and while at it, lower case it for matching. | |
750 | */ | |
2d7320d0 | 751 | store.key = xmalloc(strlen(key) + 1); |
d14f7764 LT |
752 | dot = 0; |
753 | for (i = 0; key[i]; i++) { | |
754 | unsigned char c = key[i]; | |
755 | if (c == '.') | |
756 | dot = 1; | |
757 | /* Leave the extended basename untouched.. */ | |
758 | if (!dot || i > store.baselen) { | |
38c5afa8 | 759 | if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) { |
d14f7764 LT |
760 | fprintf(stderr, "invalid key: %s\n", key); |
761 | free(store.key); | |
762 | ret = 1; | |
763 | goto out_free; | |
764 | } | |
765 | c = tolower(c); | |
6f71686e JS |
766 | } else if (c == '\n') { |
767 | fprintf(stderr, "invalid key (newline): %s\n", key); | |
768 | free(store.key); | |
769 | ret = 1; | |
770 | goto out_free; | |
d14f7764 LT |
771 | } |
772 | store.key[i] = c; | |
773 | } | |
3dd94e3b | 774 | store.key[i] = 0; |
10bea152 JS |
775 | |
776 | /* | |
6cbf973c | 777 | * The lock serves a purpose in addition to locking: the new |
10bea152 JS |
778 | * contents of .git/config will be written into it. |
779 | */ | |
6cbf973c BS |
780 | lock = xcalloc(sizeof(struct lock_file), 1); |
781 | fd = hold_lock_file_for_update(lock, config_filename, 0); | |
782 | if (fd < 0) { | |
10bea152 JS |
783 | fprintf(stderr, "could not lock config file\n"); |
784 | free(store.key); | |
dafc88b1 SH |
785 | ret = -1; |
786 | goto out_free; | |
10bea152 JS |
787 | } |
788 | ||
789 | /* | |
790 | * If .git/config does not exist yet, write a minimal version. | |
791 | */ | |
88fb958b AR |
792 | in_fd = open(config_filename, O_RDONLY); |
793 | if ( in_fd < 0 ) { | |
10bea152 JS |
794 | free(store.key); |
795 | ||
88fb958b AR |
796 | if ( ENOENT != errno ) { |
797 | error("opening %s: %s", config_filename, | |
798 | strerror(errno)); | |
dafc88b1 SH |
799 | ret = 3; /* same as "invalid config file" */ |
800 | goto out_free; | |
88fb958b | 801 | } |
10bea152 JS |
802 | /* if nothing to unset, error out */ |
803 | if (value == NULL) { | |
dafc88b1 SH |
804 | ret = 5; |
805 | goto out_free; | |
10bea152 JS |
806 | } |
807 | ||
808 | store.key = (char*)key; | |
480c9e52 | 809 | if (!store_write_section(fd, key) || |
93c1e079 JH |
810 | !store_write_pair(fd, key, value)) |
811 | goto write_err_out; | |
812 | } else { | |
88fb958b | 813 | struct stat st; |
10bea152 | 814 | char* contents; |
dc49cd76 SP |
815 | size_t contents_sz, copy_begin, copy_end; |
816 | int i, new_line = 0; | |
10bea152 JS |
817 | |
818 | if (value_regex == NULL) | |
819 | store.value_regex = NULL; | |
820 | else { | |
f98d863d JS |
821 | if (value_regex[0] == '!') { |
822 | store.do_not_match = 1; | |
823 | value_regex++; | |
824 | } else | |
825 | store.do_not_match = 0; | |
826 | ||
2d7320d0 | 827 | store.value_regex = (regex_t*)xmalloc(sizeof(regex_t)); |
10bea152 JS |
828 | if (regcomp(store.value_regex, value_regex, |
829 | REG_EXTENDED)) { | |
7246ed43 | 830 | fprintf(stderr, "Invalid pattern: %s\n", |
10bea152 JS |
831 | value_regex); |
832 | free(store.value_regex); | |
dafc88b1 SH |
833 | ret = 6; |
834 | goto out_free; | |
10bea152 JS |
835 | } |
836 | } | |
837 | ||
4ddba79d | 838 | store.offset[0] = 0; |
10bea152 JS |
839 | store.state = START; |
840 | store.seen = 0; | |
841 | ||
842 | /* | |
843 | * After this, store.offset will contain the *end* offset | |
844 | * of the last match, or remain at 0 if no match was found. | |
845 | * As a side effect, we make sure to transform only a valid | |
846 | * existing config file. | |
847 | */ | |
9c3796fc | 848 | if (git_config_from_file(store_aux, config_filename)) { |
10bea152 JS |
849 | fprintf(stderr, "invalid config file\n"); |
850 | free(store.key); | |
851 | if (store.value_regex != NULL) { | |
852 | regfree(store.value_regex); | |
853 | free(store.value_regex); | |
854 | } | |
dafc88b1 SH |
855 | ret = 3; |
856 | goto out_free; | |
10bea152 JS |
857 | } |
858 | ||
859 | free(store.key); | |
860 | if (store.value_regex != NULL) { | |
861 | regfree(store.value_regex); | |
862 | free(store.value_regex); | |
863 | } | |
864 | ||
4ddba79d JS |
865 | /* if nothing to unset, or too many matches, error out */ |
866 | if ((store.seen == 0 && value == NULL) || | |
867 | (store.seen > 1 && multi_replace == 0)) { | |
dafc88b1 SH |
868 | ret = 5; |
869 | goto out_free; | |
10bea152 JS |
870 | } |
871 | ||
88fb958b | 872 | fstat(in_fd, &st); |
dc49cd76 SP |
873 | contents_sz = xsize_t(st.st_size); |
874 | contents = xmmap(NULL, contents_sz, PROT_READ, | |
10bea152 JS |
875 | MAP_PRIVATE, in_fd, 0); |
876 | close(in_fd); | |
877 | ||
4ddba79d JS |
878 | if (store.seen == 0) |
879 | store.seen = 1; | |
880 | ||
881 | for (i = 0, copy_begin = 0; i < store.seen; i++) { | |
882 | if (store.offset[i] == 0) { | |
dc49cd76 | 883 | store.offset[i] = copy_end = contents_sz; |
4ddba79d JS |
884 | } else if (store.state != KEY_SEEN) { |
885 | copy_end = store.offset[i]; | |
10bea152 | 886 | } else |
4ddba79d | 887 | copy_end = find_beginning_of_line( |
dc49cd76 | 888 | contents, contents_sz, |
4ddba79d JS |
889 | store.offset[i]-2, &new_line); |
890 | ||
891 | /* write the first part of the config */ | |
892 | if (copy_end > copy_begin) { | |
93c1e079 JH |
893 | if (write_in_full(fd, contents + copy_begin, |
894 | copy_end - copy_begin) < | |
895 | copy_end - copy_begin) | |
896 | goto write_err_out; | |
897 | if (new_line && | |
898 | write_in_full(fd, "\n", 1) != 1) | |
899 | goto write_err_out; | |
4ddba79d JS |
900 | } |
901 | copy_begin = store.offset[i]; | |
10bea152 JS |
902 | } |
903 | ||
10bea152 JS |
904 | /* write the pair (value == NULL means unset) */ |
905 | if (value != NULL) { | |
93c1e079 JH |
906 | if (store.state == START) { |
907 | if (!store_write_section(fd, key)) | |
908 | goto write_err_out; | |
480c9e52 | 909 | } |
93c1e079 JH |
910 | if (!store_write_pair(fd, key, value)) |
911 | goto write_err_out; | |
10bea152 JS |
912 | } |
913 | ||
914 | /* write the rest of the config */ | |
dc49cd76 | 915 | if (copy_begin < contents_sz) |
93c1e079 | 916 | if (write_in_full(fd, contents + copy_begin, |
dc49cd76 SP |
917 | contents_sz - copy_begin) < |
918 | contents_sz - copy_begin) | |
93c1e079 | 919 | goto write_err_out; |
10bea152 | 920 | |
dc49cd76 | 921 | munmap(contents, contents_sz); |
10bea152 JS |
922 | } |
923 | ||
6cbf973c BS |
924 | if (close(fd) || commit_lock_file(lock) < 0) { |
925 | fprintf(stderr, "Cannot commit config file!\n"); | |
dafc88b1 SH |
926 | ret = 4; |
927 | goto out_free; | |
10bea152 JS |
928 | } |
929 | ||
6cbf973c BS |
930 | /* fd is closed, so don't try to close it below. */ |
931 | fd = -1; | |
932 | /* | |
933 | * lock is committed, so don't try to roll it back below. | |
934 | * NOTE: Since lockfile.c keeps a linked list of all created | |
935 | * lock_file structures, it isn't safe to free(lock). It's | |
936 | * better to just leave it hanging around. | |
937 | */ | |
938 | lock = NULL; | |
dafc88b1 SH |
939 | ret = 0; |
940 | ||
941 | out_free: | |
f8ba655e JH |
942 | if (0 <= fd) |
943 | close(fd); | |
6cbf973c BS |
944 | if (lock) |
945 | rollback_lock_file(lock); | |
4cac42b1 | 946 | free(config_filename); |
dafc88b1 | 947 | return ret; |
93c1e079 JH |
948 | |
949 | write_err_out: | |
950 | ret = write_error(); | |
951 | goto out_free; | |
952 | ||
10bea152 JS |
953 | } |
954 | ||
118f8b24 PB |
955 | static int section_name_match (const char *buf, const char *name) |
956 | { | |
957 | int i = 0, j = 0, dot = 0; | |
958 | for (; buf[i] && buf[i] != ']'; i++) { | |
959 | if (!dot && isspace(buf[i])) { | |
960 | dot = 1; | |
961 | if (name[j++] != '.') | |
962 | break; | |
963 | for (i++; isspace(buf[i]); i++) | |
964 | ; /* do nothing */ | |
965 | if (buf[i] != '"') | |
966 | break; | |
967 | continue; | |
968 | } | |
969 | if (buf[i] == '\\' && dot) | |
970 | i++; | |
971 | else if (buf[i] == '"' && dot) { | |
972 | for (i++; isspace(buf[i]); i++) | |
973 | ; /* do_nothing */ | |
974 | break; | |
975 | } | |
976 | if (buf[i] != name[j++]) | |
977 | break; | |
978 | } | |
979 | return (buf[i] == ']' && name[j] == 0); | |
980 | } | |
981 | ||
982 | /* if new_name == NULL, the section is removed instead */ | |
0667fcfb JS |
983 | int git_config_rename_section(const char *old_name, const char *new_name) |
984 | { | |
118f8b24 | 985 | int ret = 0, remove = 0; |
fc1905bb | 986 | char *config_filename; |
0667fcfb JS |
987 | struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1); |
988 | int out_fd; | |
989 | char buf[1024]; | |
990 | ||
d4ebc36c | 991 | config_filename = getenv(CONFIG_ENVIRONMENT); |
0667fcfb | 992 | if (!config_filename) { |
d4ebc36c | 993 | config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT); |
0667fcfb JS |
994 | if (!config_filename) |
995 | config_filename = git_path("config"); | |
996 | } | |
997 | config_filename = xstrdup(config_filename); | |
998 | out_fd = hold_lock_file_for_update(lock, config_filename, 0); | |
fc1905bb JH |
999 | if (out_fd < 0) { |
1000 | ret = error("Could not lock config file!"); | |
1001 | goto out; | |
1002 | } | |
0667fcfb | 1003 | |
fc1905bb | 1004 | if (!(config_file = fopen(config_filename, "rb"))) { |
01ebb9dc GB |
1005 | /* no config file means nothing to rename, no error */ |
1006 | goto unlock_and_out; | |
fc1905bb | 1007 | } |
0667fcfb JS |
1008 | |
1009 | while (fgets(buf, sizeof(buf), config_file)) { | |
1010 | int i; | |
480c9e52 | 1011 | int length; |
0667fcfb JS |
1012 | for (i = 0; buf[i] && isspace(buf[i]); i++) |
1013 | ; /* do nothing */ | |
1014 | if (buf[i] == '[') { | |
1015 | /* it's a section */ | |
118f8b24 PB |
1016 | if (section_name_match (&buf[i+1], old_name)) { |
1017 | ret++; | |
1018 | if (new_name == NULL) { | |
1019 | remove = 1; | |
0667fcfb JS |
1020 | continue; |
1021 | } | |
0667fcfb | 1022 | store.baselen = strlen(new_name); |
480c9e52 AW |
1023 | if (!store_write_section(out_fd, new_name)) { |
1024 | ret = write_error(); | |
1025 | goto out; | |
1026 | } | |
0667fcfb JS |
1027 | continue; |
1028 | } | |
118f8b24 | 1029 | remove = 0; |
0667fcfb | 1030 | } |
118f8b24 PB |
1031 | if (remove) |
1032 | continue; | |
480c9e52 AW |
1033 | length = strlen(buf); |
1034 | if (write_in_full(out_fd, buf, length) != length) { | |
1035 | ret = write_error(); | |
1036 | goto out; | |
1037 | } | |
0667fcfb | 1038 | } |
fc1905bb | 1039 | fclose(config_file); |
01ebb9dc | 1040 | unlock_and_out: |
0667fcfb | 1041 | if (close(out_fd) || commit_lock_file(lock) < 0) |
480c9e52 | 1042 | ret = error("Cannot commit config file!"); |
fc1905bb JH |
1043 | out: |
1044 | free(config_filename); | |
0667fcfb JS |
1045 | return ret; |
1046 | } |