]>
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 | ||
236 | int git_config_int(const char *name, const char *value) | |
237 | { | |
238 | if (value && *value) { | |
239 | char *end; | |
240 | int val = strtol(value, &end, 0); | |
241 | if (!*end) | |
242 | return val; | |
d77a64d3 SP |
243 | if (!strcasecmp(end, "k")) |
244 | return val * 1024; | |
245 | if (!strcasecmp(end, "m")) | |
246 | return val * 1024 * 1024; | |
247 | if (!strcasecmp(end, "g")) | |
248 | return val * 1024 * 1024 * 1024; | |
17712991 | 249 | } |
4f629539 | 250 | die("bad config value for '%s' in %s", name, config_file_name); |
17712991 LT |
251 | } |
252 | ||
253 | int git_config_bool(const char *name, const char *value) | |
254 | { | |
255 | if (!value) | |
256 | return 1; | |
257 | if (!*value) | |
258 | return 0; | |
624314fd | 259 | if (!strcasecmp(value, "true") || !strcasecmp(value, "yes")) |
17712991 | 260 | return 1; |
624314fd | 261 | if (!strcasecmp(value, "false") || !strcasecmp(value, "no")) |
17712991 LT |
262 | return 0; |
263 | return git_config_int(name, value) != 0; | |
264 | } | |
265 | ||
266 | int git_default_config(const char *var, const char *value) | |
267 | { | |
268 | /* This needs a better name */ | |
269 | if (!strcmp(var, "core.filemode")) { | |
270 | trust_executable_bit = git_config_bool(var, value); | |
271 | return 0; | |
272 | } | |
273 | ||
78a8d641 JS |
274 | if (!strcmp(var, "core.symlinks")) { |
275 | has_symlinks = git_config_bool(var, value); | |
276 | return 0; | |
277 | } | |
278 | ||
7d1864ce JH |
279 | if (!strcmp(var, "core.bare")) { |
280 | is_bare_repository_cfg = git_config_bool(var, value); | |
281 | return 0; | |
282 | } | |
283 | ||
5f73076c JH |
284 | if (!strcmp(var, "core.ignorestat")) { |
285 | assume_unchanged = git_config_bool(var, value); | |
286 | return 0; | |
287 | } | |
288 | ||
e388c738 JH |
289 | if (!strcmp(var, "core.prefersymlinkrefs")) { |
290 | prefer_symlink_refs = git_config_bool(var, value); | |
f8348be3 JS |
291 | return 0; |
292 | } | |
293 | ||
6de08ae6 SP |
294 | if (!strcmp(var, "core.logallrefupdates")) { |
295 | log_all_ref_updates = git_config_bool(var, value); | |
296 | return 0; | |
297 | } | |
298 | ||
2f8acdb3 JH |
299 | if (!strcmp(var, "core.warnambiguousrefs")) { |
300 | warn_ambiguous_refs = git_config_bool(var, value); | |
301 | return 0; | |
302 | } | |
303 | ||
960ccca6 | 304 | if (!strcmp(var, "core.loosecompression")) { |
12f6c308 JBH |
305 | int level = git_config_int(var, value); |
306 | if (level == -1) | |
307 | level = Z_DEFAULT_COMPRESSION; | |
308 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
309 | die("bad zlib compression level %d", level); | |
310 | zlib_compression_level = level; | |
960ccca6 DH |
311 | zlib_compression_seen = 1; |
312 | return 0; | |
313 | } | |
314 | ||
315 | if (!strcmp(var, "core.compression")) { | |
316 | int level = git_config_int(var, value); | |
317 | if (level == -1) | |
318 | level = Z_DEFAULT_COMPRESSION; | |
319 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
320 | die("bad zlib compression level %d", level); | |
321 | core_compression_level = level; | |
322 | core_compression_seen = 1; | |
323 | if (!zlib_compression_seen) | |
324 | zlib_compression_level = level; | |
12f6c308 JBH |
325 | return 0; |
326 | } | |
327 | ||
60bb8b14 | 328 | if (!strcmp(var, "core.packedgitwindowsize")) { |
5faaf246 | 329 | int pgsz_x2 = getpagesize() * 2; |
60bb8b14 | 330 | packed_git_window_size = git_config_int(var, value); |
5faaf246 JH |
331 | |
332 | /* This value must be multiple of (pagesize * 2) */ | |
333 | packed_git_window_size /= pgsz_x2; | |
334 | if (packed_git_window_size < 1) | |
335 | packed_git_window_size = 1; | |
336 | packed_git_window_size *= pgsz_x2; | |
60bb8b14 SP |
337 | return 0; |
338 | } | |
339 | ||
77ccc5bb SP |
340 | if (!strcmp(var, "core.packedgitlimit")) { |
341 | packed_git_limit = git_config_int(var, value); | |
342 | return 0; | |
343 | } | |
344 | ||
18bdec11 SP |
345 | if (!strcmp(var, "core.deltabasecachelimit")) { |
346 | delta_base_cache_limit = git_config_int(var, value); | |
347 | return 0; | |
348 | } | |
349 | ||
6c510bee | 350 | if (!strcmp(var, "core.autocrlf")) { |
d7f46334 LT |
351 | if (value && !strcasecmp(value, "input")) { |
352 | auto_crlf = -1; | |
353 | return 0; | |
354 | } | |
6c510bee LT |
355 | auto_crlf = git_config_bool(var, value); |
356 | return 0; | |
357 | } | |
358 | ||
e1b10391 | 359 | if (!strcmp(var, "user.name")) { |
817151e6 | 360 | strlcpy(git_default_name, value, sizeof(git_default_name)); |
e1b10391 LT |
361 | return 0; |
362 | } | |
363 | ||
364 | if (!strcmp(var, "user.email")) { | |
817151e6 | 365 | strlcpy(git_default_email, value, sizeof(git_default_email)); |
e1b10391 LT |
366 | return 0; |
367 | } | |
368 | ||
4e72dcec | 369 | if (!strcmp(var, "i18n.commitencoding")) { |
dbb2b41a | 370 | git_commit_encoding = xstrdup(value); |
4e72dcec JH |
371 | return 0; |
372 | } | |
373 | ||
d2c11a38 | 374 | if (!strcmp(var, "i18n.logoutputencoding")) { |
dbb2b41a | 375 | git_log_output_encoding = xstrdup(value); |
d2c11a38 JH |
376 | return 0; |
377 | } | |
378 | ||
379 | ||
a159ca0c | 380 | if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { |
aa086eb8 ML |
381 | pager_use_color = git_config_bool(var,value); |
382 | return 0; | |
383 | } | |
384 | ||
1ab661dd | 385 | /* Add other config variables here and to Documentation/config.txt. */ |
17712991 LT |
386 | return 0; |
387 | } | |
388 | ||
4f629539 | 389 | int git_config_from_file(config_fn_t fn, const char *filename) |
17712991 LT |
390 | { |
391 | int ret; | |
4f629539 | 392 | FILE *f = fopen(filename, "r"); |
17712991 LT |
393 | |
394 | ret = -1; | |
395 | if (f) { | |
396 | config_file = f; | |
4f629539 | 397 | config_file_name = filename; |
17712991 LT |
398 | config_linenr = 1; |
399 | ret = git_parse_file(fn); | |
400 | fclose(f); | |
4f629539 | 401 | config_file_name = NULL; |
17712991 LT |
402 | } |
403 | return ret; | |
404 | } | |
10bea152 | 405 | |
4f629539 JH |
406 | int git_config(config_fn_t fn) |
407 | { | |
5f1a63e0 JS |
408 | int ret = 0; |
409 | char *repo_config = NULL; | |
410 | const char *home = NULL, *filename; | |
411 | ||
412 | /* $GIT_CONFIG makes git read _only_ the given config file, | |
413 | * $GIT_CONFIG_LOCAL will make it process it in addition to the | |
414 | * global config file, the same way it would the per-repository | |
415 | * config file otherwise. */ | |
d4ebc36c | 416 | filename = getenv(CONFIG_ENVIRONMENT); |
5f1a63e0 | 417 | if (!filename) { |
32043c9f JS |
418 | if (!access(ETC_GITCONFIG, R_OK)) |
419 | ret += git_config_from_file(fn, ETC_GITCONFIG); | |
5f1a63e0 | 420 | home = getenv("HOME"); |
d4ebc36c | 421 | filename = getenv(CONFIG_LOCAL_ENVIRONMENT); |
5f1a63e0 | 422 | if (!filename) |
9befac47 | 423 | filename = repo_config = xstrdup(git_path("config")); |
7f29f7a9 | 424 | } |
5f1a63e0 JS |
425 | |
426 | if (home) { | |
9befac47 | 427 | char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); |
e33d0611 | 428 | if (!access(user_config, R_OK)) |
5f1a63e0 JS |
429 | ret = git_config_from_file(fn, user_config); |
430 | free(user_config); | |
431 | } | |
432 | ||
433 | ret += git_config_from_file(fn, filename); | |
4cac42b1 | 434 | free(repo_config); |
5f1a63e0 | 435 | return ret; |
4f629539 JH |
436 | } |
437 | ||
10bea152 JS |
438 | /* |
439 | * Find all the stuff for git_config_set() below. | |
440 | */ | |
4ddba79d JS |
441 | |
442 | #define MAX_MATCHES 512 | |
443 | ||
10bea152 JS |
444 | static struct { |
445 | int baselen; | |
446 | char* key; | |
f98d863d | 447 | int do_not_match; |
10bea152 | 448 | regex_t* value_regex; |
4ddba79d | 449 | int multi_replace; |
dc49cd76 | 450 | size_t offset[MAX_MATCHES]; |
10bea152 JS |
451 | enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; |
452 | int seen; | |
453 | } store; | |
454 | ||
f98d863d JS |
455 | static int matches(const char* key, const char* value) |
456 | { | |
457 | return !strcmp(key, store.key) && | |
458 | (store.value_regex == NULL || | |
459 | (store.do_not_match ^ | |
460 | !regexec(store.value_regex, value, 0, NULL, 0))); | |
461 | } | |
462 | ||
10bea152 JS |
463 | static int store_aux(const char* key, const char* value) |
464 | { | |
ae9ee41d JH |
465 | const char *ep; |
466 | size_t section_len; | |
467 | ||
10bea152 JS |
468 | switch (store.state) { |
469 | case KEY_SEEN: | |
f98d863d | 470 | if (matches(key, value)) { |
4ddba79d | 471 | if (store.seen == 1 && store.multi_replace == 0) { |
10bea152 JS |
472 | fprintf(stderr, |
473 | "Warning: %s has multiple values\n", | |
474 | key); | |
4ddba79d JS |
475 | } else if (store.seen >= MAX_MATCHES) { |
476 | fprintf(stderr, "Too many matches\n"); | |
477 | return 1; | |
10bea152 | 478 | } |
4ddba79d JS |
479 | |
480 | store.offset[store.seen] = ftell(config_file); | |
10bea152 JS |
481 | store.seen++; |
482 | } | |
483 | break; | |
484 | case SECTION_SEEN: | |
ae9ee41d JH |
485 | /* |
486 | * What we are looking for is in store.key (both | |
487 | * section and var), and its section part is baselen | |
488 | * long. We found key (again, both section and var). | |
489 | * We would want to know if this key is in the same | |
490 | * section as what we are looking for. We already | |
491 | * know we are in the same section as what should | |
492 | * hold store.key. | |
493 | */ | |
494 | ep = strrchr(key, '.'); | |
495 | section_len = ep - key; | |
496 | ||
497 | if ((section_len != store.baselen) || | |
498 | memcmp(key, store.key, section_len+1)) { | |
10bea152 JS |
499 | store.state = SECTION_END_SEEN; |
500 | break; | |
ae9ee41d JH |
501 | } |
502 | ||
503 | /* | |
504 | * Do not increment matches: this is no match, but we | |
505 | * just made sure we are in the desired section. | |
506 | */ | |
507 | store.offset[store.seen] = ftell(config_file); | |
10bea152 JS |
508 | /* fallthru */ |
509 | case SECTION_END_SEEN: | |
510 | case START: | |
f98d863d | 511 | if (matches(key, value)) { |
4ddba79d | 512 | store.offset[store.seen] = ftell(config_file); |
10bea152 JS |
513 | store.state = KEY_SEEN; |
514 | store.seen++; | |
d14f7764 LT |
515 | } else { |
516 | if (strrchr(key, '.') - key == store.baselen && | |
bdf0ef08 | 517 | !strncmp(key, store.key, store.baselen)) { |
93ddef3e | 518 | store.state = SECTION_SEEN; |
bdf0ef08 | 519 | store.offset[store.seen] = ftell(config_file); |
d14f7764 | 520 | } |
bdf0ef08 | 521 | } |
10bea152 JS |
522 | } |
523 | return 0; | |
524 | } | |
525 | ||
480c9e52 AW |
526 | static int write_error() |
527 | { | |
528 | fprintf(stderr, "Failed to write new configuration file\n"); | |
529 | ||
530 | /* Same error code as "failed to rename". */ | |
531 | return 4; | |
532 | } | |
533 | ||
534 | static int store_write_section(int fd, const char* key) | |
10bea152 | 535 | { |
d14f7764 LT |
536 | const char *dot = strchr(key, '.'); |
537 | int len1 = store.baselen, len2 = -1; | |
538 | ||
539 | dot = strchr(key, '.'); | |
540 | if (dot) { | |
541 | int dotlen = dot - key; | |
542 | if (dotlen < len1) { | |
543 | len2 = len1 - dotlen - 1; | |
544 | len1 = dotlen; | |
545 | } | |
546 | } | |
547 | ||
480c9e52 AW |
548 | if (write_in_full(fd, "[", 1) != 1 || |
549 | write_in_full(fd, key, len1) != len1) | |
550 | return 0; | |
d14f7764 | 551 | if (len2 >= 0) { |
480c9e52 AW |
552 | if (write_in_full(fd, " \"", 2) != 2) |
553 | return 0; | |
d14f7764 LT |
554 | while (--len2 >= 0) { |
555 | unsigned char c = *++dot; | |
556 | if (c == '"') | |
480c9e52 AW |
557 | if (write_in_full(fd, "\\", 1) != 1) |
558 | return 0; | |
559 | if (write_in_full(fd, &c, 1) != 1) | |
560 | return 0; | |
d14f7764 | 561 | } |
480c9e52 AW |
562 | if (write_in_full(fd, "\"", 1) != 1) |
563 | return 0; | |
d14f7764 | 564 | } |
480c9e52 AW |
565 | if (write_in_full(fd, "]\n", 2) != 2) |
566 | return 0; | |
567 | ||
568 | return 1; | |
10bea152 JS |
569 | } |
570 | ||
480c9e52 | 571 | static int store_write_pair(int fd, const char* key, const char* value) |
10bea152 JS |
572 | { |
573 | int i; | |
480c9e52 | 574 | int length = strlen(key+store.baselen+1); |
cdd4fb15 BG |
575 | int quote = 0; |
576 | ||
577 | /* Check to see if the value needs to be quoted. */ | |
578 | if (value[0] == ' ') | |
579 | quote = 1; | |
580 | for (i = 0; value[i]; i++) | |
581 | if (value[i] == ';' || value[i] == '#') | |
582 | quote = 1; | |
583 | if (value[i-1] == ' ') | |
584 | quote = 1; | |
10bea152 | 585 | |
480c9e52 AW |
586 | if (write_in_full(fd, "\t", 1) != 1 || |
587 | write_in_full(fd, key+store.baselen+1, length) != length || | |
588 | write_in_full(fd, " = ", 3) != 3) | |
589 | return 0; | |
cdd4fb15 BG |
590 | if (quote && write_in_full(fd, "\"", 1) != 1) |
591 | return 0; | |
10bea152 JS |
592 | for (i = 0; value[i]; i++) |
593 | switch (value[i]) { | |
480c9e52 AW |
594 | case '\n': |
595 | if (write_in_full(fd, "\\n", 2) != 2) | |
596 | return 0; | |
597 | break; | |
598 | case '\t': | |
599 | if (write_in_full(fd, "\\t", 2) != 2) | |
600 | return 0; | |
601 | break; | |
602 | case '"': | |
603 | case '\\': | |
604 | if (write_in_full(fd, "\\", 1) != 1) | |
605 | return 0; | |
606 | default: | |
607 | if (write_in_full(fd, value+i, 1) != 1) | |
608 | return 0; | |
609 | break; | |
610 | } | |
cdd4fb15 BG |
611 | if (quote && write_in_full(fd, "\"", 1) != 1) |
612 | return 0; | |
480c9e52 AW |
613 | if (write_in_full(fd, "\n", 1) != 1) |
614 | return 0; | |
615 | return 1; | |
10bea152 JS |
616 | } |
617 | ||
dc49cd76 SP |
618 | static ssize_t find_beginning_of_line(const char* contents, size_t size, |
619 | size_t offset_, int* found_bracket) | |
4ddba79d | 620 | { |
dc49cd76 SP |
621 | size_t equal_offset = size, bracket_offset = size; |
622 | ssize_t offset; | |
4ddba79d JS |
623 | |
624 | for (offset = offset_-2; offset > 0 | |
625 | && contents[offset] != '\n'; offset--) | |
626 | switch (contents[offset]) { | |
627 | case '=': equal_offset = offset; break; | |
628 | case ']': bracket_offset = offset; break; | |
629 | } | |
630 | if (bracket_offset < equal_offset) { | |
631 | *found_bracket = 1; | |
632 | offset = bracket_offset+1; | |
633 | } else | |
634 | offset++; | |
635 | ||
636 | return offset; | |
637 | } | |
638 | ||
10bea152 JS |
639 | int git_config_set(const char* key, const char* value) |
640 | { | |
4ddba79d | 641 | return git_config_set_multivar(key, value, NULL, 0); |
10bea152 JS |
642 | } |
643 | ||
644 | /* | |
645 | * If value==NULL, unset in (remove from) config, | |
646 | * if value_regex!=NULL, disregard key/value pairs where value does not match. | |
4ddba79d JS |
647 | * if multi_replace==0, nothing, or only one matching key/value is replaced, |
648 | * else all matching key/values (regardless how many) are removed, | |
649 | * before the new pair is written. | |
10bea152 JS |
650 | * |
651 | * Returns 0 on success. | |
652 | * | |
653 | * This function does this: | |
654 | * | |
655 | * - it locks the config file by creating ".git/config.lock" | |
656 | * | |
657 | * - it then parses the config using store_aux() as validator to find | |
658 | * the position on the key/value pair to replace. If it is to be unset, | |
659 | * it must be found exactly once. | |
660 | * | |
661 | * - the config file is mmap()ed and the part before the match (if any) is | |
662 | * written to the lock file, then the changed part and the rest. | |
663 | * | |
664 | * - the config file is removed and the lock file rename()d to it. | |
665 | * | |
666 | */ | |
667 | int git_config_set_multivar(const char* key, const char* value, | |
4ddba79d | 668 | const char* value_regex, int multi_replace) |
10bea152 | 669 | { |
d14f7764 | 670 | int i, dot; |
f8ba655e | 671 | int fd = -1, in_fd; |
dafc88b1 | 672 | int ret; |
9c3796fc JS |
673 | char* config_filename; |
674 | char* lock_file; | |
b17e659d | 675 | const char* last_dot = strrchr(key, '.'); |
4ddba79d | 676 | |
d4ebc36c | 677 | config_filename = getenv(CONFIG_ENVIRONMENT); |
9c3796fc | 678 | if (!config_filename) { |
d4ebc36c | 679 | config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT); |
9c3796fc JS |
680 | if (!config_filename) |
681 | config_filename = git_path("config"); | |
682 | } | |
9befac47 SP |
683 | config_filename = xstrdup(config_filename); |
684 | lock_file = xstrdup(mkpath("%s.lock", config_filename)); | |
9c3796fc | 685 | |
10bea152 JS |
686 | /* |
687 | * Since "key" actually contains the section name and the real | |
688 | * key name separated by a dot, we have to know where the dot is. | |
689 | */ | |
b17e659d | 690 | |
dafc88b1 | 691 | if (last_dot == NULL) { |
10bea152 | 692 | fprintf(stderr, "key does not contain a section: %s\n", key); |
dafc88b1 SH |
693 | ret = 2; |
694 | goto out_free; | |
10bea152 | 695 | } |
b17e659d JS |
696 | store.baselen = last_dot - key; |
697 | ||
698 | store.multi_replace = multi_replace; | |
10bea152 JS |
699 | |
700 | /* | |
701 | * Validate the key and while at it, lower case it for matching. | |
702 | */ | |
2d7320d0 | 703 | store.key = xmalloc(strlen(key) + 1); |
d14f7764 LT |
704 | dot = 0; |
705 | for (i = 0; key[i]; i++) { | |
706 | unsigned char c = key[i]; | |
707 | if (c == '.') | |
708 | dot = 1; | |
709 | /* Leave the extended basename untouched.. */ | |
710 | if (!dot || i > store.baselen) { | |
38c5afa8 | 711 | if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) { |
d14f7764 LT |
712 | fprintf(stderr, "invalid key: %s\n", key); |
713 | free(store.key); | |
714 | ret = 1; | |
715 | goto out_free; | |
716 | } | |
717 | c = tolower(c); | |
6f71686e JS |
718 | } else if (c == '\n') { |
719 | fprintf(stderr, "invalid key (newline): %s\n", key); | |
720 | free(store.key); | |
721 | ret = 1; | |
722 | goto out_free; | |
d14f7764 LT |
723 | } |
724 | store.key[i] = c; | |
725 | } | |
3dd94e3b | 726 | store.key[i] = 0; |
10bea152 JS |
727 | |
728 | /* | |
729 | * The lock_file serves a purpose in addition to locking: the new | |
730 | * contents of .git/config will be written into it. | |
731 | */ | |
732 | fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666); | |
138086a7 | 733 | if (fd < 0 || adjust_shared_perm(lock_file)) { |
10bea152 JS |
734 | fprintf(stderr, "could not lock config file\n"); |
735 | free(store.key); | |
dafc88b1 SH |
736 | ret = -1; |
737 | goto out_free; | |
10bea152 JS |
738 | } |
739 | ||
740 | /* | |
741 | * If .git/config does not exist yet, write a minimal version. | |
742 | */ | |
88fb958b AR |
743 | in_fd = open(config_filename, O_RDONLY); |
744 | if ( in_fd < 0 ) { | |
10bea152 JS |
745 | free(store.key); |
746 | ||
88fb958b AR |
747 | if ( ENOENT != errno ) { |
748 | error("opening %s: %s", config_filename, | |
749 | strerror(errno)); | |
dafc88b1 SH |
750 | ret = 3; /* same as "invalid config file" */ |
751 | goto out_free; | |
88fb958b | 752 | } |
10bea152 JS |
753 | /* if nothing to unset, error out */ |
754 | if (value == NULL) { | |
dafc88b1 SH |
755 | ret = 5; |
756 | goto out_free; | |
10bea152 JS |
757 | } |
758 | ||
759 | store.key = (char*)key; | |
480c9e52 | 760 | if (!store_write_section(fd, key) || |
93c1e079 JH |
761 | !store_write_pair(fd, key, value)) |
762 | goto write_err_out; | |
763 | } else { | |
88fb958b | 764 | struct stat st; |
10bea152 | 765 | char* contents; |
dc49cd76 SP |
766 | size_t contents_sz, copy_begin, copy_end; |
767 | int i, new_line = 0; | |
10bea152 JS |
768 | |
769 | if (value_regex == NULL) | |
770 | store.value_regex = NULL; | |
771 | else { | |
f98d863d JS |
772 | if (value_regex[0] == '!') { |
773 | store.do_not_match = 1; | |
774 | value_regex++; | |
775 | } else | |
776 | store.do_not_match = 0; | |
777 | ||
2d7320d0 | 778 | store.value_regex = (regex_t*)xmalloc(sizeof(regex_t)); |
10bea152 JS |
779 | if (regcomp(store.value_regex, value_regex, |
780 | REG_EXTENDED)) { | |
7246ed43 | 781 | fprintf(stderr, "Invalid pattern: %s\n", |
10bea152 JS |
782 | value_regex); |
783 | free(store.value_regex); | |
dafc88b1 SH |
784 | ret = 6; |
785 | goto out_free; | |
10bea152 JS |
786 | } |
787 | } | |
788 | ||
4ddba79d | 789 | store.offset[0] = 0; |
10bea152 JS |
790 | store.state = START; |
791 | store.seen = 0; | |
792 | ||
793 | /* | |
794 | * After this, store.offset will contain the *end* offset | |
795 | * of the last match, or remain at 0 if no match was found. | |
796 | * As a side effect, we make sure to transform only a valid | |
797 | * existing config file. | |
798 | */ | |
9c3796fc | 799 | if (git_config_from_file(store_aux, config_filename)) { |
10bea152 JS |
800 | fprintf(stderr, "invalid config file\n"); |
801 | free(store.key); | |
802 | if (store.value_regex != NULL) { | |
803 | regfree(store.value_regex); | |
804 | free(store.value_regex); | |
805 | } | |
dafc88b1 SH |
806 | ret = 3; |
807 | goto out_free; | |
10bea152 JS |
808 | } |
809 | ||
810 | free(store.key); | |
811 | if (store.value_regex != NULL) { | |
812 | regfree(store.value_regex); | |
813 | free(store.value_regex); | |
814 | } | |
815 | ||
4ddba79d JS |
816 | /* if nothing to unset, or too many matches, error out */ |
817 | if ((store.seen == 0 && value == NULL) || | |
818 | (store.seen > 1 && multi_replace == 0)) { | |
dafc88b1 SH |
819 | ret = 5; |
820 | goto out_free; | |
10bea152 JS |
821 | } |
822 | ||
88fb958b | 823 | fstat(in_fd, &st); |
dc49cd76 SP |
824 | contents_sz = xsize_t(st.st_size); |
825 | contents = xmmap(NULL, contents_sz, PROT_READ, | |
10bea152 JS |
826 | MAP_PRIVATE, in_fd, 0); |
827 | close(in_fd); | |
828 | ||
4ddba79d JS |
829 | if (store.seen == 0) |
830 | store.seen = 1; | |
831 | ||
832 | for (i = 0, copy_begin = 0; i < store.seen; i++) { | |
833 | if (store.offset[i] == 0) { | |
dc49cd76 | 834 | store.offset[i] = copy_end = contents_sz; |
4ddba79d JS |
835 | } else if (store.state != KEY_SEEN) { |
836 | copy_end = store.offset[i]; | |
10bea152 | 837 | } else |
4ddba79d | 838 | copy_end = find_beginning_of_line( |
dc49cd76 | 839 | contents, contents_sz, |
4ddba79d JS |
840 | store.offset[i]-2, &new_line); |
841 | ||
842 | /* write the first part of the config */ | |
843 | if (copy_end > copy_begin) { | |
93c1e079 JH |
844 | if (write_in_full(fd, contents + copy_begin, |
845 | copy_end - copy_begin) < | |
846 | copy_end - copy_begin) | |
847 | goto write_err_out; | |
848 | if (new_line && | |
849 | write_in_full(fd, "\n", 1) != 1) | |
850 | goto write_err_out; | |
4ddba79d JS |
851 | } |
852 | copy_begin = store.offset[i]; | |
10bea152 JS |
853 | } |
854 | ||
10bea152 JS |
855 | /* write the pair (value == NULL means unset) */ |
856 | if (value != NULL) { | |
93c1e079 JH |
857 | if (store.state == START) { |
858 | if (!store_write_section(fd, key)) | |
859 | goto write_err_out; | |
480c9e52 | 860 | } |
93c1e079 JH |
861 | if (!store_write_pair(fd, key, value)) |
862 | goto write_err_out; | |
10bea152 JS |
863 | } |
864 | ||
865 | /* write the rest of the config */ | |
dc49cd76 | 866 | if (copy_begin < contents_sz) |
93c1e079 | 867 | if (write_in_full(fd, contents + copy_begin, |
dc49cd76 SP |
868 | contents_sz - copy_begin) < |
869 | contents_sz - copy_begin) | |
93c1e079 | 870 | goto write_err_out; |
10bea152 | 871 | |
dc49cd76 | 872 | munmap(contents, contents_sz); |
3a2f2bb3 | 873 | unlink(config_filename); |
10bea152 JS |
874 | } |
875 | ||
3a2f2bb3 | 876 | if (rename(lock_file, config_filename) < 0) { |
10bea152 | 877 | fprintf(stderr, "Could not rename the lock file?\n"); |
dafc88b1 SH |
878 | ret = 4; |
879 | goto out_free; | |
10bea152 JS |
880 | } |
881 | ||
dafc88b1 SH |
882 | ret = 0; |
883 | ||
884 | out_free: | |
f8ba655e JH |
885 | if (0 <= fd) |
886 | close(fd); | |
4cac42b1 | 887 | free(config_filename); |
f8ba655e JH |
888 | if (lock_file) { |
889 | unlink(lock_file); | |
dafc88b1 | 890 | free(lock_file); |
f8ba655e | 891 | } |
dafc88b1 | 892 | return ret; |
93c1e079 JH |
893 | |
894 | write_err_out: | |
895 | ret = write_error(); | |
896 | goto out_free; | |
897 | ||
10bea152 JS |
898 | } |
899 | ||
118f8b24 PB |
900 | static int section_name_match (const char *buf, const char *name) |
901 | { | |
902 | int i = 0, j = 0, dot = 0; | |
903 | for (; buf[i] && buf[i] != ']'; i++) { | |
904 | if (!dot && isspace(buf[i])) { | |
905 | dot = 1; | |
906 | if (name[j++] != '.') | |
907 | break; | |
908 | for (i++; isspace(buf[i]); i++) | |
909 | ; /* do nothing */ | |
910 | if (buf[i] != '"') | |
911 | break; | |
912 | continue; | |
913 | } | |
914 | if (buf[i] == '\\' && dot) | |
915 | i++; | |
916 | else if (buf[i] == '"' && dot) { | |
917 | for (i++; isspace(buf[i]); i++) | |
918 | ; /* do_nothing */ | |
919 | break; | |
920 | } | |
921 | if (buf[i] != name[j++]) | |
922 | break; | |
923 | } | |
924 | return (buf[i] == ']' && name[j] == 0); | |
925 | } | |
926 | ||
927 | /* if new_name == NULL, the section is removed instead */ | |
0667fcfb JS |
928 | int git_config_rename_section(const char *old_name, const char *new_name) |
929 | { | |
118f8b24 | 930 | int ret = 0, remove = 0; |
fc1905bb | 931 | char *config_filename; |
0667fcfb JS |
932 | struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1); |
933 | int out_fd; | |
934 | char buf[1024]; | |
935 | ||
d4ebc36c | 936 | config_filename = getenv(CONFIG_ENVIRONMENT); |
0667fcfb | 937 | if (!config_filename) { |
d4ebc36c | 938 | config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT); |
0667fcfb JS |
939 | if (!config_filename) |
940 | config_filename = git_path("config"); | |
941 | } | |
942 | config_filename = xstrdup(config_filename); | |
943 | out_fd = hold_lock_file_for_update(lock, config_filename, 0); | |
fc1905bb JH |
944 | if (out_fd < 0) { |
945 | ret = error("Could not lock config file!"); | |
946 | goto out; | |
947 | } | |
0667fcfb | 948 | |
fc1905bb | 949 | if (!(config_file = fopen(config_filename, "rb"))) { |
01ebb9dc GB |
950 | /* no config file means nothing to rename, no error */ |
951 | goto unlock_and_out; | |
fc1905bb | 952 | } |
0667fcfb JS |
953 | |
954 | while (fgets(buf, sizeof(buf), config_file)) { | |
955 | int i; | |
480c9e52 | 956 | int length; |
0667fcfb JS |
957 | for (i = 0; buf[i] && isspace(buf[i]); i++) |
958 | ; /* do nothing */ | |
959 | if (buf[i] == '[') { | |
960 | /* it's a section */ | |
118f8b24 PB |
961 | if (section_name_match (&buf[i+1], old_name)) { |
962 | ret++; | |
963 | if (new_name == NULL) { | |
964 | remove = 1; | |
0667fcfb JS |
965 | continue; |
966 | } | |
0667fcfb | 967 | store.baselen = strlen(new_name); |
480c9e52 AW |
968 | if (!store_write_section(out_fd, new_name)) { |
969 | ret = write_error(); | |
970 | goto out; | |
971 | } | |
0667fcfb JS |
972 | continue; |
973 | } | |
118f8b24 | 974 | remove = 0; |
0667fcfb | 975 | } |
118f8b24 PB |
976 | if (remove) |
977 | continue; | |
480c9e52 AW |
978 | length = strlen(buf); |
979 | if (write_in_full(out_fd, buf, length) != length) { | |
980 | ret = write_error(); | |
981 | goto out; | |
982 | } | |
0667fcfb | 983 | } |
fc1905bb | 984 | fclose(config_file); |
01ebb9dc | 985 | unlock_and_out: |
0667fcfb | 986 | if (close(out_fd) || commit_lock_file(lock) < 0) |
480c9e52 | 987 | ret = error("Cannot commit config file!"); |
fc1905bb JH |
988 | out: |
989 | free(config_filename); | |
0667fcfb JS |
990 | return ret; |
991 | } | |
4ddba79d | 992 |