repo-config: learn the flag "--no-local"
[git.git] / config.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  * Copyright (C) Johannes Schindelin, 2005
6  *
7  */
8 #include "cache.h"
9 #include <regex.h>
10
11 #define MAXNAME (256)
12
13 int git_ignore_local_config = 0;
14 static FILE *config_file;
15 static const char *config_file_name;
16 static int config_linenr;
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);
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                 }
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                 }
64                 if (!quote) {
65                         if (c == ';' || c == '#') {
66                                 comment = 1;
67                                 continue;
68                         }
69                 }
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;
89                         /* Some characters escape as themselves */
90                         case '\\': case '"':
91                                 break;
92                         /* Reject unknown escape sequences */
93                         default:
94                                 return NULL;
95                         }
96                         value[len++] = c;
97                         continue;
98                 }
99                 if (c == '"') {
100                         quote = 1-quote;
101                         continue;
102                 }
103                 value[len++] = c;
104         }
105 }
106
107 static int get_value(config_fn_t fn, char *name, unsigned int len)
108 {
109         int c;
110         char *value;
111
112         /* Get the full name */
113         for (;;) {
114                 c = get_next_char();
115                 if (c == EOF)
116                         break;
117                 if (!isalnum(c))
118                         break;
119                 name[len++] = tolower(c);
120                 if (len >= MAXNAME)
121                         return -1;
122         }
123         name[len] = 0;
124         while (c == ' ' || c == '\t')
125                 c = get_next_char();
126
127         value = NULL;
128         if (c != '\n') {
129                 if (c != '=')
130                         return -1;
131                 value = parse_value();
132                 if (!value)
133                         return -1;
134         }
135         return fn(name, value);
136 }
137
138 static int get_extended_base_var(char *name, int baselen, int c)
139 {
140         do {
141                 if (c == '\n')
142                         return -1;
143                 c = get_next_char();
144         } while (isspace(c));
145
146         /* We require the format to be '[base "extension"]' */
147         if (c != '"')
148                 return -1;
149         name[baselen++] = '.';
150
151         for (;;) {
152                 int c = get_next_char();
153                 if (c == '\n')
154                         return -1;
155                 if (c == '"')
156                         break;
157                 if (c == '\\') {
158                         c = get_next_char();
159                         if (c == '\n')
160                                 return -1;
161                 }
162                 name[baselen++] = c;
163                 if (baselen > MAXNAME / 2)
164                         return -1;
165         }
166
167         /* Final ']' */
168         if (get_next_char() != ']')
169                 return -1;
170         return baselen;
171 }
172
173 static int get_base_var(char *name)
174 {
175         int baselen = 0;
176
177         for (;;) {
178                 int c = get_next_char();
179                 if (c == EOF)
180                         return -1;
181                 if (c == ']')
182                         return baselen;
183                 if (isspace(c))
184                         return get_extended_base_var(name, baselen, c);
185                 if (!isalnum(c) && c != '.')
186                         return -1;
187                 if (baselen > MAXNAME / 2)
188                         return -1;
189                 name[baselen++] = tolower(c);
190         }
191 }
192
193 static int git_parse_file(config_fn_t fn)
194 {
195         int comment = 0;
196         int baselen = 0;
197         static char var[MAXNAME];
198
199         for (;;) {
200                 int c = get_next_char();
201                 if (c == '\n') {
202                         /* EOF? */
203                         if (!config_file)
204                                 return 0;
205                         comment = 0;
206                         continue;
207                 }
208                 if (comment || isspace(c))
209                         continue;
210                 if (c == '#' || c == ';') {
211                         comment = 1;
212                         continue;
213                 }
214                 if (c == '[') {
215                         baselen = get_base_var(var);
216                         if (baselen <= 0)
217                                 break;
218                         var[baselen++] = '.';
219                         var[baselen] = 0;
220                         continue;
221                 }
222                 if (!isalpha(c))
223                         break;
224                 var[baselen] = tolower(c);
225                 if (get_value(fn, var, baselen+1) < 0)
226                         break;
227         }
228         die("bad config file line %d in %s", config_linenr, config_file_name);
229 }
230
231 int git_config_int(const char *name, const char *value)
232 {
233         if (value && *value) {
234                 char *end;
235                 int val = strtol(value, &end, 0);
236                 if (!*end)
237                         return val;
238         }
239         die("bad config value for '%s' in %s", name, config_file_name);
240 }
241
242 int git_config_bool(const char *name, const char *value)
243 {
244         if (!value)
245                 return 1;
246         if (!*value)
247                 return 0;
248         if (!strcasecmp(value, "true"))
249                 return 1;
250         if (!strcasecmp(value, "false"))
251                 return 0;
252         return git_config_int(name, value) != 0;
253 }
254
255 int git_default_config(const char *var, const char *value)
256 {
257         /* This needs a better name */
258         if (!strcmp(var, "core.filemode")) {
259                 trust_executable_bit = git_config_bool(var, value);
260                 return 0;
261         }
262
263         if (!strcmp(var, "core.ignorestat")) {
264                 assume_unchanged = git_config_bool(var, value);
265                 return 0;
266         }
267
268         if (!strcmp(var, "core.prefersymlinkrefs")) {
269                 prefer_symlink_refs = git_config_bool(var, value);
270                 return 0;
271         }
272
273         if (!strcmp(var, "core.logallrefupdates")) {
274                 log_all_ref_updates = git_config_bool(var, value);
275                 return 0;
276         }
277
278         if (!strcmp(var, "core.warnambiguousrefs")) {
279                 warn_ambiguous_refs = git_config_bool(var, value);
280                 return 0;
281         }
282
283         if (!strcmp(var, "user.name")) {
284                 strncpy(git_default_name, value, sizeof(git_default_name));
285                 return 0;
286         }
287
288         if (!strcmp(var, "user.email")) {
289                 strncpy(git_default_email, value, sizeof(git_default_email));
290                 return 0;
291         }
292
293         if (!strcmp(var, "i18n.commitencoding")) {
294                 strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
295                 return 0;
296         }
297
298         /* Add other config variables here and to Documentation/config.txt. */
299         return 0;
300 }
301
302 int git_config_from_file(config_fn_t fn, const char *filename)
303 {
304         int ret;
305         FILE *f = fopen(filename, "r");
306
307         ret = -1;
308         if (f) {
309                 config_file = f;
310                 config_file_name = filename;
311                 config_linenr = 1;
312                 ret = git_parse_file(fn);
313                 fclose(f);
314                 config_file_name = NULL;
315         }
316         return ret;
317 }
318
319 int git_config(config_fn_t fn)
320 {
321         int ret = 0;
322         const char *home = getenv("HOME");
323
324         if (home) {
325                 ret = git_config_from_file(fn, mkpath("%s/.gitconfig", home));
326                 /* ignore if global config does not exist */
327                 if (ret < 0)
328                         ret = 0;
329         }
330
331         if (!git_ignore_local_config)
332                 ret += git_config_from_file(fn, git_path("config"));
333         return ret;
334 }
335
336 /*
337  * Find all the stuff for git_config_set() below.
338  */
339
340 #define MAX_MATCHES 512
341
342 static struct {
343         int baselen;
344         char* key;
345         int do_not_match;
346         regex_t* value_regex;
347         int multi_replace;
348         off_t offset[MAX_MATCHES];
349         enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
350         int seen;
351 } store;
352
353 static int matches(const char* key, const char* value)
354 {
355         return !strcmp(key, store.key) &&
356                 (store.value_regex == NULL ||
357                  (store.do_not_match ^
358                   !regexec(store.value_regex, value, 0, NULL, 0)));
359 }
360
361 static int store_aux(const char* key, const char* value)
362 {
363         switch (store.state) {
364         case KEY_SEEN:
365                 if (matches(key, value)) {
366                         if (store.seen == 1 && store.multi_replace == 0) {
367                                 fprintf(stderr,
368                                         "Warning: %s has multiple values\n",
369                                         key);
370                         } else if (store.seen >= MAX_MATCHES) {
371                                 fprintf(stderr, "Too many matches\n");
372                                 return 1;
373                         }
374
375                         store.offset[store.seen] = ftell(config_file);
376                         store.seen++;
377                 }
378                 break;
379         case SECTION_SEEN:
380                 if (strncmp(key, store.key, store.baselen+1)) {
381                         store.state = SECTION_END_SEEN;
382                         break;
383                 } else
384                         /* do not increment matches: this is no match */
385                         store.offset[store.seen] = ftell(config_file);
386                 /* fallthru */
387         case SECTION_END_SEEN:
388         case START:
389                 if (matches(key, value)) {
390                         store.offset[store.seen] = ftell(config_file);
391                         store.state = KEY_SEEN;
392                         store.seen++;
393                 } else {
394                         if (strrchr(key, '.') - key == store.baselen &&
395                               !strncmp(key, store.key, store.baselen)) {
396                                         store.state = SECTION_SEEN;
397                                         store.offset[store.seen] = ftell(config_file);
398                         }
399                 }
400         }
401         return 0;
402 }
403
404 static void store_write_section(int fd, const char* key)
405 {
406         const char *dot = strchr(key, '.');
407         int len1 = store.baselen, len2 = -1;
408
409         dot = strchr(key, '.');
410         if (dot) {
411                 int dotlen = dot - key;
412                 if (dotlen < len1) {
413                         len2 = len1 - dotlen - 1;
414                         len1 = dotlen;
415                 }
416         }
417
418         write(fd, "[", 1);
419         write(fd, key, len1);
420         if (len2 >= 0) {
421                 write(fd, " \"", 2);
422                 while (--len2 >= 0) {
423                         unsigned char c = *++dot;
424                         if (c == '"')
425                                 write(fd, "\\", 1);
426                         write(fd, &c, 1);
427                 }
428                 write(fd, "\"", 1);
429         }
430         write(fd, "]\n", 2);
431 }
432
433 static void store_write_pair(int fd, const char* key, const char* value)
434 {
435         int i;
436
437         write(fd, "\t", 1);
438         write(fd, key+store.baselen+1,
439                 strlen(key+store.baselen+1));
440         write(fd, " = ", 3);
441         for (i = 0; value[i]; i++)
442                 switch (value[i]) {
443                 case '\n': write(fd, "\\n", 2); break;
444                 case '\t': write(fd, "\\t", 2); break;
445                 case '"': case '\\': write(fd, "\\", 1);
446                 default: write(fd, value+i, 1);
447         }
448         write(fd, "\n", 1);
449 }
450
451 static int find_beginning_of_line(const char* contents, int size,
452         int offset_, int* found_bracket)
453 {
454         int equal_offset = size, bracket_offset = size;
455         int offset;
456
457         for (offset = offset_-2; offset > 0 
458                         && contents[offset] != '\n'; offset--)
459                 switch (contents[offset]) {
460                         case '=': equal_offset = offset; break;
461                         case ']': bracket_offset = offset; break;
462                 }
463         if (bracket_offset < equal_offset) {
464                 *found_bracket = 1;
465                 offset = bracket_offset+1;
466         } else
467                 offset++;
468
469         return offset;
470 }
471
472 int git_config_set(const char* key, const char* value)
473 {
474         return git_config_set_multivar(key, value, NULL, 0);
475 }
476
477 /*
478  * If value==NULL, unset in (remove from) config,
479  * if value_regex!=NULL, disregard key/value pairs where value does not match.
480  * if multi_replace==0, nothing, or only one matching key/value is replaced,
481  *     else all matching key/values (regardless how many) are removed,
482  *     before the new pair is written.
483  *
484  * Returns 0 on success.
485  *
486  * This function does this:
487  *
488  * - it locks the config file by creating ".git/config.lock"
489  *
490  * - it then parses the config using store_aux() as validator to find
491  *   the position on the key/value pair to replace. If it is to be unset,
492  *   it must be found exactly once.
493  *
494  * - the config file is mmap()ed and the part before the match (if any) is
495  *   written to the lock file, then the changed part and the rest.
496  *
497  * - the config file is removed and the lock file rename()d to it.
498  *
499  */
500 int git_config_set_multivar(const char* key, const char* value,
501         const char* value_regex, int multi_replace)
502 {
503         int i, dot;
504         int fd = -1, in_fd;
505         int ret;
506         char *config_filename, *lock_file;
507         const char* last_dot = strrchr(key, '.');
508
509         if (git_ignore_local_config) {
510                 const char *home = getenv("HOME");
511                 if (!home)
512                         die("No home?");
513                 config_filename = strdup(mkpath("%s/.gitconfig", home));
514                 lock_file = strdup(mkpath("%s/.gitconfig.lock", home));
515         } else {
516                 config_filename = strdup(git_path("config"));
517                 lock_file = strdup(git_path("config.lock"));
518         }
519
520         /*
521          * Since "key" actually contains the section name and the real
522          * key name separated by a dot, we have to know where the dot is.
523          */
524
525         if (last_dot == NULL) {
526                 fprintf(stderr, "key does not contain a section: %s\n", key);
527                 ret = 2;
528                 goto out_free;
529         }
530         store.baselen = last_dot - key;
531
532         store.multi_replace = multi_replace;
533
534         /*
535          * Validate the key and while at it, lower case it for matching.
536          */
537         store.key = (char*)malloc(strlen(key)+1);
538         dot = 0;
539         for (i = 0; key[i]; i++) {
540                 unsigned char c = key[i];
541                 if (c == '.')
542                         dot = 1;
543                 /* Leave the extended basename untouched.. */
544                 if (!dot || i > store.baselen) {
545                         if (!isalnum(c) || (i == store.baselen+1 && !isalpha(c))) {
546                                 fprintf(stderr, "invalid key: %s\n", key);
547                                 free(store.key);
548                                 ret = 1;
549                                 goto out_free;
550                         }
551                         c = tolower(c);
552                 }
553                 store.key[i] = c;
554         }
555         store.key[i] = 0;
556
557         /*
558          * The lock_file serves a purpose in addition to locking: the new
559          * contents of .git/config will be written into it.
560          */
561         fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
562         if (fd < 0 || adjust_shared_perm(lock_file)) {
563                 fprintf(stderr, "could not lock config file\n");
564                 free(store.key);
565                 ret = -1;
566                 goto out_free;
567         }
568
569         /*
570          * If .git/config does not exist yet, write a minimal version.
571          */
572         in_fd = open(config_filename, O_RDONLY);
573         if ( in_fd < 0 ) {
574                 free(store.key);
575
576                 if ( ENOENT != errno ) {
577                         error("opening %s: %s", config_filename,
578                               strerror(errno));
579                         ret = 3; /* same as "invalid config file" */
580                         goto out_free;
581                 }
582                 /* if nothing to unset, error out */
583                 if (value == NULL) {
584                         ret = 5;
585                         goto out_free;
586                 }
587
588                 store.key = (char*)key;
589                 store_write_section(fd, key);
590                 store_write_pair(fd, key, value);
591         } else{
592                 struct stat st;
593                 char* contents;
594                 int i, copy_begin, copy_end, new_line = 0;
595
596                 if (value_regex == NULL)
597                         store.value_regex = NULL;
598                 else {
599                         if (value_regex[0] == '!') {
600                                 store.do_not_match = 1;
601                                 value_regex++;
602                         } else
603                                 store.do_not_match = 0;
604
605                         store.value_regex = (regex_t*)malloc(sizeof(regex_t));
606                         if (regcomp(store.value_regex, value_regex,
607                                         REG_EXTENDED)) {
608                                 fprintf(stderr, "Invalid pattern: %s\n",
609                                         value_regex);
610                                 free(store.value_regex);
611                                 ret = 6;
612                                 goto out_free;
613                         }
614                 }
615
616                 store.offset[0] = 0;
617                 store.state = START;
618                 store.seen = 0;
619
620                 /*
621                  * After this, store.offset will contain the *end* offset
622                  * of the last match, or remain at 0 if no match was found.
623                  * As a side effect, we make sure to transform only a valid
624                  * existing config file.
625                  */
626                 if (git_config_from_file(store_aux, config_filename)) {
627                         fprintf(stderr, "invalid config file: %s\n",
628                                         config_filename);
629                         free(store.key);
630                         if (store.value_regex != NULL) {
631                                 regfree(store.value_regex);
632                                 free(store.value_regex);
633                         }
634                         ret = 3;
635                         goto out_free;
636                 }
637
638                 free(store.key);
639                 if (store.value_regex != NULL) {
640                         regfree(store.value_regex);
641                         free(store.value_regex);
642                 }
643
644                 /* if nothing to unset, or too many matches, error out */
645                 if ((store.seen == 0 && value == NULL) ||
646                                 (store.seen > 1 && multi_replace == 0)) {
647                         ret = 5;
648                         goto out_free;
649                 }
650
651                 fstat(in_fd, &st);
652                 contents = mmap(NULL, st.st_size, PROT_READ,
653                         MAP_PRIVATE, in_fd, 0);
654                 close(in_fd);
655
656                 if (store.seen == 0)
657                         store.seen = 1;
658
659                 for (i = 0, copy_begin = 0; i < store.seen; i++) {
660                         if (store.offset[i] == 0) {
661                                 store.offset[i] = copy_end = st.st_size;
662                         } else if (store.state != KEY_SEEN) {
663                                 copy_end = store.offset[i];
664                         } else
665                                 copy_end = find_beginning_of_line(
666                                         contents, st.st_size,
667                                         store.offset[i]-2, &new_line);
668
669                         /* write the first part of the config */
670                         if (copy_end > copy_begin) {
671                                 write(fd, contents + copy_begin,
672                                 copy_end - copy_begin);
673                                 if (new_line)
674                                         write(fd, "\n", 1);
675                         }
676                         copy_begin = store.offset[i];
677                 }
678
679                 /* write the pair (value == NULL means unset) */
680                 if (value != NULL) {
681                         if (store.state == START)
682                                 store_write_section(fd, key);
683                         store_write_pair(fd, key, value);
684                 }
685
686                 /* write the rest of the config */
687                 if (copy_begin < st.st_size)
688                         write(fd, contents + copy_begin,
689                                 st.st_size - copy_begin);
690
691                 munmap(contents, st.st_size);
692                 unlink(config_filename);
693         }
694
695         if (rename(lock_file, config_filename) < 0) {
696                 fprintf(stderr, "Could not rename the lock file?\n");
697                 ret = 4;
698                 goto out_free;
699         }
700
701         ret = 0;
702
703 out_free:
704         if (0 <= fd)
705                 close(fd);
706         if (config_filename)
707                 free(config_filename);
708         if (lock_file) {
709                 unlink(lock_file);
710                 free(lock_file);
711         }
712         return ret;
713 }
714
715