Rename git-config-set to git-repo-config
[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 static FILE *config_file;
14 static int config_linenr;
15 static int get_next_char(void)
16 {
17         int c;
18         FILE *f;
19
20         c = '\n';
21         if ((f = config_file) != NULL) {
22                 c = fgetc(f);
23                 if (c == '\r') {
24                         /* DOS like systems */
25                         c = fgetc(f);
26                         if (c != '\n') {
27                                 ungetc(c, f);
28                                 c = '\r';
29                         }
30                 }
31                 if (c == '\n')
32                         config_linenr++;
33                 if (c == EOF) {
34                         config_file = NULL;
35                         c = '\n';
36                 }
37         }
38         return c;
39 }
40
41 static char *parse_value(void)
42 {
43         static char value[1024];
44         int quote = 0, comment = 0, len = 0, space = 0;
45
46         for (;;) {
47                 int c = get_next_char();
48                 if (len >= sizeof(value))
49                         return NULL;
50                 if (c == '\n') {
51                         if (quote)
52                                 return NULL;
53                         value[len] = 0;
54                         return value;
55                 }
56                 if (comment)
57                         continue;
58                 if (isspace(c) && !quote) {
59                         space = 1;
60                         continue;
61                 }
62                 if (space) {
63                         if (len)
64                                 value[len++] = ' ';
65                         space = 0;
66                 }
67                 if (c == '\\') {
68                         c = get_next_char();
69                         switch (c) {
70                         case '\n':
71                                 continue;
72                         case 't':
73                                 c = '\t';
74                                 break;
75                         case 'b':
76                                 c = '\b';
77                                 break;
78                         case 'n':
79                                 c = '\n';
80                                 break;
81                         /* Some characters escape as themselves */
82                         case '\\': case '"':
83                                 break;
84                         /* Reject unknown escape sequences */
85                         default:
86                                 return NULL;
87                         }
88                         value[len++] = c;
89                         continue;
90                 }
91                 if (c == '"') {
92                         quote = 1-quote;
93                         continue;
94                 }
95                 if (!quote) {
96                         if (c == ';' || c == '#') {
97                                 comment = 1;
98                                 continue;
99                         }
100                 }
101                 value[len++] = c;
102         }
103 }
104
105 static int get_value(config_fn_t fn, char *name, unsigned int len)
106 {
107         int c;
108         char *value;
109
110         /* Get the full name */
111         for (;;) {
112                 c = get_next_char();
113                 if (c == EOF)
114                         break;
115                 if (!isalnum(c))
116                         break;
117                 name[len++] = tolower(c);
118                 if (len >= MAXNAME)
119                         return -1;
120         }
121         name[len] = 0;
122         while (c == ' ' || c == '\t')
123                 c = get_next_char();
124
125         value = NULL;
126         if (c != '\n') {
127                 if (c != '=')
128                         return -1;
129                 value = parse_value();
130                 if (!value)
131                         return -1;
132         }
133         return fn(name, value);
134 }
135
136 static int get_base_var(char *name)
137 {
138         int baselen = 0;
139
140         for (;;) {
141                 int c = get_next_char();
142                 if (c == EOF)
143                         return -1;
144                 if (c == ']')
145                         return baselen;
146                 if (!isalnum(c) && c != '.')
147                         return -1;
148                 if (baselen > MAXNAME / 2)
149                         return -1;
150                 name[baselen++] = tolower(c);
151         }
152 }
153
154 static int git_parse_file(config_fn_t fn)
155 {
156         int comment = 0;
157         int baselen = 0;
158         static char var[MAXNAME];
159
160         for (;;) {
161                 int c = get_next_char();
162                 if (c == '\n') {
163                         /* EOF? */
164                         if (!config_file)
165                                 return 0;
166                         comment = 0;
167                         continue;
168                 }
169                 if (comment || isspace(c))
170                         continue;
171                 if (c == '#' || c == ';') {
172                         comment = 1;
173                         continue;
174                 }
175                 if (c == '[') {
176                         baselen = get_base_var(var);
177                         if (baselen <= 0)
178                                 break;
179                         var[baselen++] = '.';
180                         var[baselen] = 0;
181                         continue;
182                 }
183                 if (!isalpha(c))
184                         break;
185                 var[baselen] = tolower(c);
186                 if (get_value(fn, var, baselen+1) < 0)
187                         break;
188         }
189         die("bad config file line %d", config_linenr);
190 }
191
192 int git_config_int(const char *name, const char *value)
193 {
194         if (value && *value) {
195                 char *end;
196                 int val = strtol(value, &end, 0);
197                 if (!*end)
198                         return val;
199         }
200         die("bad config value for '%s'", name);
201 }
202
203 int git_config_bool(const char *name, const char *value)
204 {
205         if (!value)
206                 return 1;
207         if (!*value)
208                 return 0;
209         if (!strcasecmp(value, "true"))
210                 return 1;
211         if (!strcasecmp(value, "false"))
212                 return 0;
213         return git_config_int(name, value) != 0;
214 }
215
216 int git_default_config(const char *var, const char *value)
217 {
218         /* This needs a better name */
219         if (!strcmp(var, "core.filemode")) {
220                 trust_executable_bit = git_config_bool(var, value);
221                 return 0;
222         }
223
224         if (!strcmp(var, "core.symrefsonly")) {
225                 only_use_symrefs = git_config_bool(var, value);
226                 return 0;
227         }
228
229         if (!strcmp(var, "user.name")) {
230                 strncpy(git_default_name, value, sizeof(git_default_name));
231                 return 0;
232         }
233
234         if (!strcmp(var, "user.email")) {
235                 strncpy(git_default_email, value, sizeof(git_default_email));
236                 return 0;
237         }
238
239         /* Add other config variables here.. */
240         return 0;
241 }
242
243 int git_config(config_fn_t fn)
244 {
245         int ret;
246         FILE *f = fopen(git_path("config"), "r");
247
248         ret = -1;
249         if (f) {
250                 config_file = f;
251                 config_linenr = 1;
252                 ret = git_parse_file(fn);
253                 fclose(f);
254         }
255         return ret;
256 }
257
258 /*
259  * Find all the stuff for git_config_set() below.
260  */
261
262 #define MAX_MATCHES 512
263
264 static struct {
265         int baselen;
266         char* key;
267         int do_not_match;
268         regex_t* value_regex;
269         int multi_replace;
270         off_t offset[MAX_MATCHES];
271         enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
272         int seen;
273 } store;
274
275 static int matches(const char* key, const char* value)
276 {
277         return !strcmp(key, store.key) &&
278                 (store.value_regex == NULL ||
279                  (store.do_not_match ^
280                   !regexec(store.value_regex, value, 0, NULL, 0)));
281 }
282
283 static int store_aux(const char* key, const char* value)
284 {
285         switch (store.state) {
286         case KEY_SEEN:
287                 if (matches(key, value)) {
288                         if (store.seen == 1 && store.multi_replace == 0) {
289                                 fprintf(stderr,
290                                         "Warning: %s has multiple values\n",
291                                         key);
292                         } else if (store.seen >= MAX_MATCHES) {
293                                 fprintf(stderr, "Too many matches\n");
294                                 return 1;
295                         }
296
297                         store.offset[store.seen] = ftell(config_file);
298                         store.seen++;
299                 }
300                 break;
301         case SECTION_SEEN:
302                 if (strncmp(key, store.key, store.baselen+1)) {
303                         store.state = SECTION_END_SEEN;
304                         break;
305                 } else
306                         /* do not increment matches: this is no match */
307                         store.offset[store.seen] = ftell(config_file);
308                 /* fallthru */
309         case SECTION_END_SEEN:
310         case START:
311                 if (matches(key, value)) {
312                         store.offset[store.seen] = ftell(config_file);
313                         store.state = KEY_SEEN;
314                         store.seen++;
315                 } else if(!strncmp(key, store.key, store.baselen))
316                         store.state = SECTION_SEEN;
317         }
318         return 0;
319 }
320
321 static void store_write_section(int fd, const char* key)
322 {
323         write(fd, "[", 1);
324         write(fd, key, store.baselen);
325         write(fd, "]\n", 2);
326 }
327
328 static void store_write_pair(int fd, const char* key, const char* value)
329 {
330         int i;
331
332         write(fd, "\t", 1);
333         write(fd, key+store.baselen+1,
334                 strlen(key+store.baselen+1));
335         write(fd, " = ", 3);
336         for (i = 0; value[i]; i++)
337                 switch (value[i]) {
338                 case '\n': write(fd, "\\n", 2); break;
339                 case '\t': write(fd, "\\t", 2); break;
340                 case '"': case '\\': write(fd, "\\", 1);
341                 default: write(fd, value+i, 1);
342         }
343         write(fd, "\n", 1);
344 }
345
346 static int find_beginning_of_line(const char* contents, int size,
347         int offset_, int* found_bracket)
348 {
349         int equal_offset = size, bracket_offset = size;
350         int offset;
351
352         for (offset = offset_-2; offset > 0 
353                         && contents[offset] != '\n'; offset--)
354                 switch (contents[offset]) {
355                         case '=': equal_offset = offset; break;
356                         case ']': bracket_offset = offset; break;
357                 }
358         if (bracket_offset < equal_offset) {
359                 *found_bracket = 1;
360                 offset = bracket_offset+1;
361         } else
362                 offset++;
363
364         return offset;
365 }
366
367 int git_config_set(const char* key, const char* value)
368 {
369         return git_config_set_multivar(key, value, NULL, 0);
370 }
371
372 /*
373  * If value==NULL, unset in (remove from) config,
374  * if value_regex!=NULL, disregard key/value pairs where value does not match.
375  * if multi_replace==0, nothing, or only one matching key/value is replaced,
376  *     else all matching key/values (regardless how many) are removed,
377  *     before the new pair is written.
378  *
379  * Returns 0 on success.
380  *
381  * This function does this:
382  *
383  * - it locks the config file by creating ".git/config.lock"
384  *
385  * - it then parses the config using store_aux() as validator to find
386  *   the position on the key/value pair to replace. If it is to be unset,
387  *   it must be found exactly once.
388  *
389  * - the config file is mmap()ed and the part before the match (if any) is
390  *   written to the lock file, then the changed part and the rest.
391  *
392  * - the config file is removed and the lock file rename()d to it.
393  *
394  */
395 int git_config_set_multivar(const char* key, const char* value,
396         const char* value_regex, int multi_replace)
397 {
398         int i;
399         struct stat st;
400         int fd;
401         char* config_file = strdup(git_path("config"));
402         char* lock_file = strdup(git_path("config.lock"));
403         const char* last_dot = strrchr(key, '.');
404
405         /*
406          * Since "key" actually contains the section name and the real
407          * key name separated by a dot, we have to know where the dot is.
408          */
409
410         if (last_dot == NULL) { 
411                 fprintf(stderr, "key does not contain a section: %s\n", key);
412                 return 2;
413         }
414         store.baselen = last_dot - key;
415
416         store.multi_replace = multi_replace;
417
418         /*
419          * Validate the key and while at it, lower case it for matching.
420          */
421         store.key = (char*)malloc(strlen(key)+1);
422         for (i = 0; key[i]; i++)
423                 if (i != store.baselen &&
424                                 ((!isalnum(key[i]) && key[i] != '.') ||
425                                  (i == store.baselen+1 && !isalpha(key[i])))) {
426                         fprintf(stderr, "invalid key: %s\n", key);
427                         free(store.key);
428                         return 1;
429                 } else
430                         store.key[i] = tolower(key[i]);
431         store.key[i] = 0;
432
433         /*
434          * The lock_file serves a purpose in addition to locking: the new
435          * contents of .git/config will be written into it.
436          */
437         fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
438         if (fd < 0) {
439                 fprintf(stderr, "could not lock config file\n");
440                 free(store.key);
441                 return -1;
442         }
443
444         /*
445          * If .git/config does not exist yet, write a minimal version.
446          */
447         if (stat(config_file, &st)) {
448                 static const char contents[] =
449                         "#\n"
450                         "# This is the config file\n"
451                         "#\n"
452                         "\n";
453
454                 free(store.key);
455
456                 /* if nothing to unset, error out */
457                 if (value == NULL) {
458                         close(fd);
459                         unlink(lock_file);
460                         return 5;
461                 }
462
463                 store.key = (char*)key;
464
465                 write(fd, contents, sizeof(contents)-1);
466                 store_write_section(fd, key);
467                 store_write_pair(fd, key, value);
468         } else{
469                 int in_fd;
470                 char* contents;
471                 int i, copy_begin, copy_end, new_line = 0;
472
473                 if (value_regex == NULL)
474                         store.value_regex = NULL;
475                 else {
476                         if (value_regex[0] == '!') {
477                                 store.do_not_match = 1;
478                                 value_regex++;
479                         } else
480                                 store.do_not_match = 0;
481
482                         store.value_regex = (regex_t*)malloc(sizeof(regex_t));
483                         if (regcomp(store.value_regex, value_regex,
484                                         REG_EXTENDED)) {
485                                 fprintf(stderr, "Invalid pattern: %s",
486                                         value_regex);
487                                 free(store.value_regex);
488                                 return 6;
489                         }
490                 }
491
492                 store.offset[0] = 0;
493                 store.state = START;
494                 store.seen = 0;
495
496                 /*
497                  * After this, store.offset will contain the *end* offset
498                  * of the last match, or remain at 0 if no match was found.
499                  * As a side effect, we make sure to transform only a valid
500                  * existing config file.
501                  */
502                 if (git_config(store_aux)) {
503                         fprintf(stderr, "invalid config file\n");
504                         free(store.key);
505                         if (store.value_regex != NULL) {
506                                 regfree(store.value_regex);
507                                 free(store.value_regex);
508                         }
509                         return 3;
510                 }
511
512                 free(store.key);
513                 if (store.value_regex != NULL) {
514                         regfree(store.value_regex);
515                         free(store.value_regex);
516                 }
517
518                 /* if nothing to unset, or too many matches, error out */
519                 if ((store.seen == 0 && value == NULL) ||
520                                 (store.seen > 1 && multi_replace == 0)) {
521                         close(fd);
522                         unlink(lock_file);
523                         return 5;
524                 }
525
526                 in_fd = open(config_file, O_RDONLY, 0666);
527                 contents = mmap(NULL, st.st_size, PROT_READ,
528                         MAP_PRIVATE, in_fd, 0);
529                 close(in_fd);
530
531                 if (store.seen == 0)
532                         store.seen = 1;
533
534                 for (i = 0, copy_begin = 0; i < store.seen; i++) {
535                         if (store.offset[i] == 0) {
536                                 store.offset[i] = copy_end = st.st_size;
537                         } else if (store.state != KEY_SEEN) {
538                                 copy_end = store.offset[i];
539                         } else
540                                 copy_end = find_beginning_of_line(
541                                         contents, st.st_size,
542                                         store.offset[i]-2, &new_line);
543
544                         /* write the first part of the config */
545                         if (copy_end > copy_begin) {
546                                 write(fd, contents + copy_begin,
547                                 copy_end - copy_begin);
548                                 if (new_line)
549                                         write(fd, "\n", 1);
550                         }
551                         copy_begin = store.offset[i];
552                 }
553
554                 /* write the pair (value == NULL means unset) */
555                 if (value != NULL) {
556                         if (store.state == START)
557                                 store_write_section(fd, key);
558                         store_write_pair(fd, key, value);
559                 }
560
561                 /* write the rest of the config */
562                 if (copy_begin < st.st_size)
563                         write(fd, contents + copy_begin,
564                                 st.st_size - copy_begin);
565
566                 munmap(contents, st.st_size);
567                 unlink(config_file);
568         }
569
570         close(fd);
571
572         if (rename(lock_file, config_file) < 0) {
573                 fprintf(stderr, "Could not rename the lock file?\n");
574                 return 4;
575         }
576
577         return 0;
578 }
579
580