]> git.ipfire.org Git - thirdparty/git.git/blobdiff - config.c
Use preprocessor constants for environment variable names.
[thirdparty/git.git] / config.c
index e8f0caf7cf674ff44b23e0182420988e9672cc69..6a4224e98cf51f8ced2b8e942231012b42aaeef7 100644 (file)
--- a/config.c
+++ b/config.c
@@ -103,6 +103,11 @@ static char *parse_value(void)
        }
 }
 
+static inline int iskeychar(int c)
+{
+       return isalnum(c) || c == '-';
+}
+
 static int get_value(config_fn_t fn, char *name, unsigned int len)
 {
        int c;
@@ -113,7 +118,7 @@ static int get_value(config_fn_t fn, char *name, unsigned int len)
                c = get_next_char();
                if (c == EOF)
                        break;
-               if (!isalnum(c))
+               if (!iskeychar(c))
                        break;
                name[len++] = tolower(c);
                if (len >= MAXNAME)
@@ -181,7 +186,7 @@ static int get_base_var(char *name)
                        return baselen;
                if (isspace(c))
                        return get_extended_base_var(name, baselen, c);
-               if (!isalnum(c) && c != '.')
+               if (!iskeychar(c) && c != '.')
                        return -1;
                if (baselen > MAXNAME / 2)
                        return -1;
@@ -309,7 +314,7 @@ int git_default_config(const char *var, const char *value)
                return 0;
        }
 
-       if (!strcmp(var, "pager.color")) {
+       if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
                pager_use_color = git_config_bool(var,value);
                return 0;
        }
@@ -345,10 +350,10 @@ int git_config(config_fn_t fn)
         * $GIT_CONFIG_LOCAL will make it process it in addition to the
         * global config file, the same way it would the per-repository
         * config file otherwise. */
-       filename = getenv("GIT_CONFIG");
+       filename = getenv(CONFIG_ENVIRONMENT);
        if (!filename) {
                home = getenv("HOME");
-               filename = getenv("GIT_CONFIG_LOCAL");
+               filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
                if (!filename)
                        filename = repo_config = xstrdup(git_path("config"));
        }
@@ -539,9 +544,9 @@ int git_config_set_multivar(const char* key, const char* value,
        char* lock_file;
        const char* last_dot = strrchr(key, '.');
 
-       config_filename = getenv("GIT_CONFIG");
+       config_filename = getenv(CONFIG_ENVIRONMENT);
        if (!config_filename) {
-               config_filename = getenv("GIT_CONFIG_LOCAL");
+               config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
                if (!config_filename)
                        config_filename  = git_path("config");
        }
@@ -573,7 +578,7 @@ int git_config_set_multivar(const char* key, const char* value,
                        dot = 1;
                /* Leave the extended basename untouched.. */
                if (!dot || i > store.baselen) {
-                       if (!isalnum(c) || (i == store.baselen+1 && !isalpha(c))) {
+                       if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
                                fprintf(stderr, "invalid key: %s\n", key);
                                free(store.key);
                                ret = 1;
@@ -741,4 +746,68 @@ out_free:
        return ret;
 }
 
+int git_config_rename_section(const char *old_name, const char *new_name)
+{
+       int ret = 0;
+       const char *config_filename;
+       struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
+       int out_fd;
+       char buf[1024];
+
+       config_filename = getenv(CONFIG_ENVIRONMENT);
+       if (!config_filename) {
+               config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
+               if (!config_filename)
+                       config_filename  = git_path("config");
+       }
+       config_filename = xstrdup(config_filename);
+       out_fd = hold_lock_file_for_update(lock, config_filename, 0);
+       if (out_fd < 0)
+               return error("Could not lock config file!");
+
+       if (!(config_file = fopen(config_filename, "rb")))
+               return error("Could not open config file!");
+
+       while (fgets(buf, sizeof(buf), config_file)) {
+               int i;
+               for (i = 0; buf[i] && isspace(buf[i]); i++)
+                       ; /* do nothing */
+               if (buf[i] == '[') {
+                       /* it's a section */
+                       int j = 0, dot = 0;
+                       for (i++; buf[i] && buf[i] != ']'; i++) {
+                               if (!dot && isspace(buf[i])) {
+                                       dot = 1;
+                                       if (old_name[j++] != '.')
+                                               break;
+                                       for (i++; isspace(buf[i]); i++)
+                                               ; /* do nothing */
+                                       if (buf[i] != '"')
+                                               break;
+                                       continue;
+                               }
+                               if (buf[i] == '\\' && dot)
+                                       i++;
+                               else if (buf[i] == '"' && dot) {
+                                       for (i++; isspace(buf[i]); i++)
+                                               ; /* do_nothing */
+                                       break;
+                               }
+                               if (buf[i] != old_name[j++])
+                                       break;
+                       }
+                       if (buf[i] == ']') {
+                               /* old_name matches */
+                               ret++;
+                               store.baselen = strlen(new_name);
+                               store_write_section(out_fd, new_name);
+                               continue;
+                       }
+               }
+               write(out_fd, buf, strlen(buf));
+       }
+       if (close(out_fd) || commit_lock_file(lock) < 0)
+               return error("Cannot commit config file!");
+       return ret;
+}