Code

symbolit-ref: fix resolve_ref conversion.
[git.git] / config.c
index 2ae6153e5ee29ca88f52241d1ece79069d8c5875..e8f0caf7cf674ff44b23e0182420988e9672cc69 100644 (file)
--- a/config.c
+++ b/config.c
@@ -244,9 +244,9 @@ int git_config_bool(const char *name, const char *value)
                return 1;
        if (!*value)
                return 0;
-       if (!strcasecmp(value, "true"))
+       if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
                return 1;
-       if (!strcasecmp(value, "false"))
+       if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
                return 0;
        return git_config_int(name, value) != 0;
 }
@@ -279,18 +279,38 @@ int git_default_config(const char *var, const char *value)
                return 0;
        }
 
+       if (!strcmp(var, "core.legacyheaders")) {
+               use_legacy_headers = git_config_bool(var, value);
+               return 0;
+       }
+
+       if (!strcmp(var, "core.compression")) {
+               int level = git_config_int(var, value);
+               if (level == -1)
+                       level = Z_DEFAULT_COMPRESSION;
+               else if (level < 0 || level > Z_BEST_COMPRESSION)
+                       die("bad zlib compression level %d", level);
+               zlib_compression_level = level;
+               return 0;
+       }
+
        if (!strcmp(var, "user.name")) {
-               strncpy(git_default_name, value, sizeof(git_default_name));
+               strlcpy(git_default_name, value, sizeof(git_default_name));
                return 0;
        }
 
        if (!strcmp(var, "user.email")) {
-               strncpy(git_default_email, value, sizeof(git_default_email));
+               strlcpy(git_default_email, value, sizeof(git_default_email));
                return 0;
        }
 
        if (!strcmp(var, "i18n.commitencoding")) {
-               strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
+               strlcpy(git_commit_encoding, value, sizeof(git_commit_encoding));
+               return 0;
+       }
+
+       if (!strcmp(var, "pager.color")) {
+               pager_use_color = git_config_bool(var,value);
                return 0;
        }
 
@@ -317,7 +337,32 @@ int git_config_from_file(config_fn_t fn, const char *filename)
 
 int git_config(config_fn_t fn)
 {
-       return git_config_from_file(fn, git_path("config"));
+       int ret = 0;
+       char *repo_config = NULL;
+       const char *home = NULL, *filename;
+
+       /* $GIT_CONFIG makes git read _only_ the given config file,
+        * $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");
+       if (!filename) {
+               home = getenv("HOME");
+               filename = getenv("GIT_CONFIG_LOCAL");
+               if (!filename)
+                       filename = repo_config = xstrdup(git_path("config"));
+       }
+
+       if (home) {
+               char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
+               if (!access(user_config, R_OK))
+                       ret = git_config_from_file(fn, user_config);
+               free(user_config);
+       }
+
+       ret += git_config_from_file(fn, filename);
+       free(repo_config);
+       return ret;
 }
 
 /*
@@ -490,10 +535,19 @@ int git_config_set_multivar(const char* key, const char* value,
        int i, dot;
        int fd = -1, in_fd;
        int ret;
-       char* config_filename = strdup(git_path("config"));
-       char* lock_file = strdup(git_path("config.lock"));
+       char* config_filename;
+       char* lock_file;
        const char* last_dot = strrchr(key, '.');
 
+       config_filename = getenv("GIT_CONFIG");
+       if (!config_filename) {
+               config_filename = getenv("GIT_CONFIG_LOCAL");
+               if (!config_filename)
+                       config_filename  = git_path("config");
+       }
+       config_filename = xstrdup(config_filename);
+       lock_file = xstrdup(mkpath("%s.lock", config_filename));
+
        /*
         * Since "key" actually contains the section name and the real
         * key name separated by a dot, we have to know where the dot is.
@@ -511,7 +565,7 @@ int git_config_set_multivar(const char* key, const char* value,
        /*
         * Validate the key and while at it, lower case it for matching.
         */
-       store.key = (char*)malloc(strlen(key)+1);
+       store.key = xmalloc(strlen(key) + 1);
        dot = 0;
        for (i = 0; key[i]; i++) {
                unsigned char c = key[i];
@@ -536,7 +590,7 @@ int git_config_set_multivar(const char* key, const char* value,
         * contents of .git/config will be written into it.
         */
        fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
-       if (fd < 0) {
+       if (fd < 0 || adjust_shared_perm(lock_file)) {
                fprintf(stderr, "could not lock config file\n");
                free(store.key);
                ret = -1;
@@ -579,7 +633,7 @@ int git_config_set_multivar(const char* key, const char* value,
                        } else
                                store.do_not_match = 0;
 
-                       store.value_regex = (regex_t*)malloc(sizeof(regex_t));
+                       store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
                        if (regcomp(store.value_regex, value_regex,
                                        REG_EXTENDED)) {
                                fprintf(stderr, "Invalid pattern: %s\n",
@@ -600,7 +654,7 @@ int git_config_set_multivar(const char* key, const char* value,
                 * As a side effect, we make sure to transform only a valid
                 * existing config file.
                 */
-               if (git_config(store_aux)) {
+               if (git_config_from_file(store_aux, config_filename)) {
                        fprintf(stderr, "invalid config file\n");
                        free(store.key);
                        if (store.value_regex != NULL) {
@@ -679,8 +733,7 @@ int git_config_set_multivar(const char* key, const char* value,
 out_free:
        if (0 <= fd)
                close(fd);
-       if (config_filename)
-               free(config_filename);
+       free(config_filename);
        if (lock_file) {
                unlink(lock_file);
                free(lock_file);