Code

Merge branch 'na/strtoimax' into maint
[git.git] / config.c
index 43aa456be1468b5e8be9e4a18675a5ca4cdf70e7..5ea101fb251d27eadac20c665a7f01fb210c20d1 100644 (file)
--- a/config.c
+++ b/config.c
 
 #define MAXNAME (256)
 
-static FILE *config_file;
-static const char *config_file_name;
-static int config_linenr;
-static int config_file_eof;
+typedef struct config_file {
+       struct config_file *prev;
+       FILE *f;
+       const char *name;
+       int linenr;
+       int eof;
+       struct strbuf value;
+       char var[MAXNAME];
+} config_file;
+
+static config_file *cf;
+
 static int zlib_compression_seen;
 
 const char *config_exclusive_filename = NULL;
@@ -39,13 +47,13 @@ void git_config_push_parameter(const char *text)
        strbuf_release(&env);
 }
 
-static int git_config_parse_parameter(const char *text,
-                                     config_fn_t fn, void *data)
+int git_config_parse_parameter(const char *text,
+                              config_fn_t fn, void *data)
 {
-       struct strbuf tmp = STRBUF_INIT;
        struct strbuf **pair;
-       strbuf_addstr(&tmp, text);
-       pair = strbuf_split(&tmp, '=');
+       pair = strbuf_split_str(text, '=', 2);
+       if (!pair[0])
+               return error("bogus config parameter: %s", text);
        if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
                strbuf_setlen(pair[0], pair[0]->len - 1);
        strbuf_trim(pair[0]);
@@ -99,7 +107,7 @@ static int get_next_char(void)
        FILE *f;
 
        c = '\n';
-       if ((f = config_file) != NULL) {
+       if (cf && ((f = cf->f) != NULL)) {
                c = fgetc(f);
                if (c == '\r') {
                        /* DOS like systems */
@@ -110,9 +118,9 @@ static int get_next_char(void)
                        }
                }
                if (c == '\n')
-                       config_linenr++;
+                       cf->linenr++;
                if (c == EOF) {
-                       config_file_eof = 1;
+                       cf->eof = 1;
                        c = '\n';
                }
        }
@@ -121,21 +129,20 @@ static int get_next_char(void)
 
 static char *parse_value(void)
 {
-       static struct strbuf value = STRBUF_INIT;
        int quote = 0, comment = 0, space = 0;
 
-       strbuf_reset(&value);
+       strbuf_reset(&cf->value);
        for (;;) {
                int c = get_next_char();
                if (c == '\n') {
                        if (quote)
                                return NULL;
-                       return value.buf;
+                       return cf->value.buf;
                }
                if (comment)
                        continue;
                if (isspace(c) && !quote) {
-                       if (value.len)
+                       if (cf->value.len)
                                space++;
                        continue;
                }
@@ -146,7 +153,7 @@ static char *parse_value(void)
                        }
                }
                for (; space; space--)
-                       strbuf_addch(&value, ' ');
+                       strbuf_addch(&cf->value, ' ');
                if (c == '\\') {
                        c = get_next_char();
                        switch (c) {
@@ -168,14 +175,14 @@ static char *parse_value(void)
                        default:
                                return NULL;
                        }
-                       strbuf_addch(&value, c);
+                       strbuf_addch(&cf->value, c);
                        continue;
                }
                if (c == '"') {
                        quote = 1-quote;
                        continue;
                }
-               strbuf_addch(&value, c);
+               strbuf_addch(&cf->value, c);
        }
 }
 
@@ -192,7 +199,7 @@ static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
        /* Get the full name */
        for (;;) {
                c = get_next_char();
-               if (config_file_eof)
+               if (cf->eof)
                        break;
                if (!iskeychar(c))
                        break;
@@ -256,7 +263,7 @@ static int get_base_var(char *name)
 
        for (;;) {
                int c = get_next_char();
-               if (config_file_eof)
+               if (cf->eof)
                        return -1;
                if (c == ']')
                        return baselen;
@@ -274,7 +281,7 @@ static int git_parse_file(config_fn_t fn, void *data)
 {
        int comment = 0;
        int baselen = 0;
-       static char var[MAXNAME];
+       char *var = cf->var;
 
        /* U+FEFF Byte Order Mark in UTF8 */
        static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
@@ -298,7 +305,7 @@ static int git_parse_file(config_fn_t fn, void *data)
                        }
                }
                if (c == '\n') {
-                       if (config_file_eof)
+                       if (cf->eof)
                                return 0;
                        comment = 0;
                        continue;
@@ -323,7 +330,7 @@ static int git_parse_file(config_fn_t fn, void *data)
                if (get_value(fn, data, var, baselen+1) < 0)
                        break;
        }
-       die("bad config file line %d in %s", config_linenr, config_file_name);
+       die("bad config file line %d in %s", cf->linenr, cf->name);
 }
 
 static int parse_unit_factor(const char *end, uintmax_t *val)
@@ -396,8 +403,8 @@ int git_parse_ulong(const char *value, unsigned long *ret)
 
 static void die_bad_config(const char *name)
 {
-       if (config_file_name)
-               die("bad config value for '%s' in %s", name, config_file_name);
+       if (cf && cf->name)
+               die("bad config value for '%s' in %s", name, cf->name);
        die("bad config value for '%s'", name);
 }
 
@@ -506,6 +513,9 @@ static int git_default_core_config(const char *var, const char *value)
                return 0;
        }
 
+       if (!strcmp(var, "core.attributesfile"))
+               return git_config_pathname(&git_attributes_file, var, value);
+
        if (!strcmp(var, "core.bare")) {
                is_bare_repository_cfg = git_config_bool(var, value);
                return 0;
@@ -590,6 +600,9 @@ static int git_default_core_config(const char *var, const char *value)
                return 0;
        }
 
+       if (!strcmp(var, "core.logpackaccess"))
+               return git_config_string(&log_pack_access, var, value);
+
        if (!strcmp(var, "core.autocrlf")) {
                if (value && !strcasecmp(value, "input")) {
                        if (core_eol == EOL_CRLF)
@@ -816,13 +829,24 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
 
        ret = -1;
        if (f) {
-               config_file = f;
-               config_file_name = filename;
-               config_linenr = 1;
-               config_file_eof = 0;
+               config_file top;
+
+               /* push config-file parsing state stack */
+               top.prev = cf;
+               top.f = f;
+               top.name = filename;
+               top.linenr = 1;
+               top.eof = 0;
+               strbuf_init(&top.value, 1024);
+               cf = &top;
+
                ret = git_parse_file(fn, data);
+
+               /* pop config-file parsing state stack */
+               strbuf_release(&top.value);
+               cf = top.prev;
+
                fclose(f);
-               config_file_name = NULL;
        }
        return ret;
 }
@@ -862,12 +886,12 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
 
        home = getenv("HOME");
        if (home) {
-               char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
+               char buf[PATH_MAX];
+               char *user_config = mksnpath(buf, sizeof(buf), "%s/.gitconfig", home);
                if (!access(user_config, R_OK)) {
                        ret += git_config_from_file(fn, user_config, data);
                        found += 1;
                }
-               free(user_config);
        }
 
        if (repo_config && !access(repo_config, R_OK)) {
@@ -877,7 +901,7 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
 
        switch (git_config_from_parameters(fn, data)) {
        case -1: /* error */
-               ret--;
+               die("unable to parse command-line config");
                break;
        case 0: /* found nothing */
                break;
@@ -930,6 +954,7 @@ static int store_aux(const char *key, const char *value, void *cb)
 {
        const char *ep;
        size_t section_len;
+       FILE *f = cf->f;
 
        switch (store.state) {
        case KEY_SEEN:
@@ -941,7 +966,7 @@ static int store_aux(const char *key, const char *value, void *cb)
                                return 1;
                        }
 
-                       store.offset[store.seen] = ftell(config_file);
+                       store.offset[store.seen] = ftell(f);
                        store.seen++;
                }
                break;
@@ -968,19 +993,19 @@ static int store_aux(const char *key, const char *value, void *cb)
                 * Do not increment matches: this is no match, but we
                 * just made sure we are in the desired section.
                 */
-               store.offset[store.seen] = ftell(config_file);
+               store.offset[store.seen] = ftell(f);
                /* fallthru */
        case SECTION_END_SEEN:
        case START:
                if (matches(key, value)) {
-                       store.offset[store.seen] = ftell(config_file);
+                       store.offset[store.seen] = ftell(f);
                        store.state = KEY_SEEN;
                        store.seen++;
                } else {
                        if (strrchr(key, '.') - key == store.baselen &&
                              !strncmp(key, store.key, store.baselen)) {
                                        store.state = SECTION_SEEN;
-                                       store.offset[store.seen] = ftell(config_file);
+                                       store.offset[store.seen] = ftell(f);
                        }
                }
        }
@@ -1094,6 +1119,12 @@ contline:
        return offset;
 }
 
+int git_config_set_in_file(const char *config_filename,
+                       const char *key, const char *value)
+{
+       return git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
+}
+
 int git_config_set(const char *key, const char *value)
 {
        return git_config_set_multivar(key, value, NULL, 0);
@@ -1191,19 +1222,14 @@ out_free_ret_1:
  * - the config file is removed and the lock file rename()d to it.
  *
  */
-int git_config_set_multivar(const char *key, const char *value,
-       const char *value_regex, int multi_replace)
+int git_config_set_multivar_in_file(const char *config_filename,
+                               const char *key, const char *value,
+                               const char *value_regex, int multi_replace)
 {
        int fd = -1, in_fd;
        int ret;
-       char *config_filename;
        struct lock_file *lock = NULL;
 
-       if (config_exclusive_filename)
-               config_filename = xstrdup(config_exclusive_filename);
-       else
-               config_filename = git_pathdup("config");
-
        /* parse-key returns negative; flip the sign to feed exit(3) */
        ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
        if (ret)
@@ -1380,7 +1406,6 @@ int git_config_set_multivar(const char *key, const char *value,
 out_free:
        if (lock)
                rollback_lock_file(lock);
-       free(config_filename);
        return ret;
 
 write_err_out:
@@ -1389,6 +1414,24 @@ write_err_out:
 
 }
 
+int git_config_set_multivar(const char *key, const char *value,
+                       const char *value_regex, int multi_replace)
+{
+       const char *config_filename;
+       char *buf = NULL;
+       int ret;
+
+       if (config_exclusive_filename)
+               config_filename = config_exclusive_filename;
+       else
+               config_filename = buf = git_pathdup("config");
+
+       ret = git_config_set_multivar_in_file(config_filename, key, value,
+                                       value_regex, multi_replace);
+       free(buf);
+       return ret;
+}
+
 static int section_name_match (const char *buf, const char *name)
 {
        int i = 0, j = 0, dot = 0;
@@ -1436,6 +1479,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
        struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
        int out_fd;
        char buf[1024];
+       FILE *config_file;
 
        if (config_exclusive_filename)
                config_filename = xstrdup(config_exclusive_filename);
@@ -1500,10 +1544,10 @@ int git_config_rename_section(const char *old_name, const char *new_name)
                }
        }
        fclose(config_file);
- unlock_and_out:
+unlock_and_out:
        if (commit_lock_file(lock) < 0)
                ret = error("could not commit config file %s", config_filename);
- out:
+out:
        free(config_filename);
        return ret;
 }