2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
13 static FILE *config_file;
14 static int config_linenr;
15 static int get_next_char(void)
21 if ((f = config_file) != NULL) {
24 /* DOS like systems */
41 static char *parse_value(void)
43 static char value[1024];
44 int quote = 0, comment = 0, len = 0, space = 0;
47 int c = get_next_char();
48 if (len >= sizeof(value))
58 if (isspace(c) && !quote) {
81 /* Some characters escape as themselves */
84 /* Reject unknown escape sequences */
96 if (c == ';' || c == '#') {
105 static int get_value(config_fn_t fn, char *name, unsigned int len)
110 /* Get the full name */
117 name[len++] = tolower(c);
122 while (c == ' ' || c == '\t')
129 value = parse_value();
133 return fn(name, value);
136 static int get_base_var(char *name)
141 int c = get_next_char();
148 if (baselen > MAXNAME / 2)
150 name[baselen++] = tolower(c);
154 static int git_parse_file(config_fn_t fn)
158 static char var[MAXNAME];
161 int c = get_next_char();
169 if (comment || isspace(c))
171 if (c == '#' || c == ';') {
176 baselen = get_base_var(var);
179 var[baselen++] = '.';
185 var[baselen] = tolower(c);
186 if (get_value(fn, var, baselen+1) < 0)
189 die("bad config file line %d", config_linenr);
192 int git_config_int(const char *name, const char *value)
194 if (value && *value) {
196 int val = strtol(value, &end, 0);
200 die("bad config value for '%s'", name);
203 int git_config_bool(const char *name, const char *value)
209 if (!strcasecmp(value, "true"))
211 if (!strcasecmp(value, "false"))
213 return git_config_int(name, value) != 0;
216 int git_default_config(const char *var, const char *value)
218 /* This needs a better name */
219 if (!strcmp(var, "core.filemode")) {
220 trust_executable_bit = git_config_bool(var, value);
224 if (!strcmp(var, "core.symrefsonly")) {
225 only_use_symrefs = git_config_bool(var, value);
229 if (!strcmp(var, "user.name")) {
230 strncpy(git_default_name, value, sizeof(git_default_name));
234 if (!strcmp(var, "user.email")) {
235 strncpy(git_default_email, value, sizeof(git_default_email));
239 if (!strcmp(var, "diff.renamelimit")) {
240 diff_rename_limit_default = git_config_int(var, value);
244 /* Add other config variables here.. */
248 int git_config(config_fn_t fn)
251 FILE *f = fopen(git_path("config"), "r");
257 ret = git_parse_file(fn);
264 * Find all the stuff for git_config_set() below.
269 regex_t* value_regex;
271 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
275 static int store_aux(const char* key, const char* value)
277 switch (store.state) {
279 if (!strcmp(key, store.key) &&
280 (store.value_regex == NULL ||
281 !regexec(store.value_regex, value, 0, NULL, 0))) {
282 if (store.seen == 1) {
284 "Warning: %s has multiple values\n",
287 store.offset = ftell(config_file);
292 if (strncmp(key, store.key, store.baselen+1)) {
293 store.state = SECTION_END_SEEN;
296 store.offset = ftell(config_file);
298 case SECTION_END_SEEN:
300 if (!strcmp(key, store.key) &&
301 (store.value_regex == NULL ||
302 !regexec(store.value_regex, value, 0, NULL, 0))) {
303 store.offset = ftell(config_file);
304 store.state = KEY_SEEN;
306 } else if(!strncmp(key, store.key, store.baselen))
307 store.state = SECTION_SEEN;
312 static void store_write_section(int fd, const char* key)
315 write(fd, key, store.baselen);
319 static void store_write_pair(int fd, const char* key, const char* value)
324 write(fd, key+store.baselen+1,
325 strlen(key+store.baselen+1));
327 for (i = 0; value[i]; i++)
329 case '\n': write(fd, "\\n", 2); break;
330 case '\t': write(fd, "\\t", 2); break;
331 case '"': case '\\': write(fd, "\\", 1);
332 default: write(fd, value+i, 1);
337 int git_config_set(const char* key, const char* value)
339 return git_config_set_multivar(key, value, NULL);
343 * If value==NULL, unset in (remove from) config,
344 * if value_regex!=NULL, disregard key/value pairs where value does not match.
346 * Returns 0 on success.
348 * This function does this:
350 * - it locks the config file by creating ".git/config.lock"
352 * - it then parses the config using store_aux() as validator to find
353 * the position on the key/value pair to replace. If it is to be unset,
354 * it must be found exactly once.
356 * - the config file is mmap()ed and the part before the match (if any) is
357 * written to the lock file, then the changed part and the rest.
359 * - the config file is removed and the lock file rename()d to it.
362 int git_config_set_multivar(const char* key, const char* value,
363 const char* value_regex)
368 char* config_file = strdup(git_path("config"));
369 char* lock_file = strdup(git_path("config.lock"));
372 * Since "key" actually contains the section name and the real
373 * key name separated by a dot, we have to know where the dot is.
375 for (store.baselen = 0;
376 key[store.baselen] != '.' && key[store.baselen];
378 if (!key[store.baselen] || !key[store.baselen+1]) {
379 fprintf(stderr, "key does not contain a section: %s\n", key);
384 * Validate the key and while at it, lower case it for matching.
386 store.key = (char*)malloc(strlen(key)+1);
387 for (i = 0; key[i]; i++)
388 if (i != store.baselen && (!isalnum(key[i]) ||
389 (i == store.baselen+1 && !isalpha(key[i])))) {
390 fprintf(stderr, "invalid key: %s\n", key);
394 store.key[i] = tolower(key[i]);
397 * The lock_file serves a purpose in addition to locking: the new
398 * contents of .git/config will be written into it.
400 fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
402 fprintf(stderr, "could not lock config file\n");
408 * If .git/config does not exist yet, write a minimal version.
410 if (stat(config_file, &st)) {
411 static const char contents[] =
413 "# This is the config file\n"
419 /* if nothing to unset, error out */
426 store.key = (char*)key;
428 write(fd, contents, sizeof(contents)-1);
429 store_write_section(fd, key);
430 store_write_pair(fd, key, value);
434 int offset, new_line = 0;
436 if (value_regex == NULL)
437 store.value_regex = NULL;
439 store.value_regex = (regex_t*)malloc(sizeof(regex_t));
440 if (regcomp(store.value_regex, value_regex,
442 fprintf(stderr, "Invalid pattern: %s",
444 free(store.value_regex);
454 * After this, store.offset will contain the *end* offset
455 * of the last match, or remain at 0 if no match was found.
456 * As a side effect, we make sure to transform only a valid
457 * existing config file.
459 if (git_config(store_aux)) {
460 fprintf(stderr, "invalid config file\n");
462 if (store.value_regex != NULL) {
463 regfree(store.value_regex);
464 free(store.value_regex);
470 if (store.value_regex != NULL) {
471 regfree(store.value_regex);
472 free(store.value_regex);
475 /* if nothing to unset, error out */
476 if (store.seen == 0 && value == NULL) {
482 store.key = (char*)key;
484 in_fd = open(config_file, O_RDONLY, 0666);
485 contents = mmap(NULL, st.st_size, PROT_READ,
486 MAP_PRIVATE, in_fd, 0);
489 if (store.offset == 0) {
490 store.offset = offset = st.st_size;
491 } else if (store.state != KEY_SEEN) {
492 offset = store.offset;
494 int equal_offset = st.st_size,
495 bracket_offset = st.st_size;
497 if (value == NULL && store.seen > 1) {
498 fprintf(stderr, "Cannot remove multivar (%s has %d values\n", key, store.seen);
503 for (offset = store.offset-2; offset > 0
504 && contents[offset] != '\n'; offset--)
505 switch (contents[offset]) {
506 case '=': equal_offset = offset; break;
507 case ']': bracket_offset = offset; break;
509 if (bracket_offset < equal_offset) {
511 offset = bracket_offset+1;
516 /* write the first part of the config */
517 write(fd, contents, offset);
521 /* write the pair (value == NULL means unset) */
523 if (store.state == START)
524 store_write_section(fd, key);
525 store_write_pair(fd, key, value);
528 /* write the rest of the config */
529 if (store.offset < st.st_size)
530 write(fd, contents + store.offset,
531 st.st_size - store.offset);
533 munmap(contents, st.st_size);
539 if (rename(lock_file, config_file) < 0) {
540 fprintf(stderr, "Could not rename the lock file?\n");