Merge branch 'rs/receive-pack-use-labs'
[gitweb.git] / builtin / config.c
index 5677c942b6936f3332b1b4ed12541f45c4228dfb..8cc2604069cd008297081988ac807412ba9901a7 100644 (file)
@@ -296,7 +296,8 @@ static int git_get_color_config(const char *var, const char *value, void *cb)
        if (!strcmp(var, get_color_slot)) {
                if (!value)
                        config_error_nonbool(var);
-               color_parse(value, var, parsed_color);
+               if (color_parse(value, parsed_color) < 0)
+                       return -1;
                get_color_found = 1;
        }
        return 0;
@@ -309,8 +310,10 @@ static void get_color(const char *def_color)
        git_config_with_options(git_get_color_config, NULL,
                                &given_config_source, respect_includes);
 
-       if (!get_color_found && def_color)
-               color_parse(def_color, "command line", parsed_color);
+       if (!get_color_found && def_color) {
+               if (color_parse(def_color, parsed_color) < 0)
+                       die(_("unable to parse default color value"));
+       }
 
        fputs(parsed_color, stdout);
 }
@@ -395,19 +398,6 @@ static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
        return 0;
 }
 
-static char *dup_downcase(const char *string)
-{
-       char *result;
-       size_t len, i;
-
-       len = strlen(string);
-       result = xmalloc(len + 1);
-       for (i = 0; i < len; i++)
-               result[i] = tolower(string[i]);
-       result[i] = '\0';
-       return result;
-}
-
 static int get_urlmatch(const char *var, const char *url)
 {
        char *section_tail;
@@ -422,7 +412,7 @@ static int get_urlmatch(const char *var, const char *url)
        if (!url_normalize(url, &config.url))
                die("%s", config.url.err);
 
-       config.section = dup_downcase(var);
+       config.section = xstrdup_tolower(var);
        section_tail = strchr(config.section, '.');
        if (section_tail) {
                *section_tail = '\0';
@@ -458,6 +448,20 @@ static int get_urlmatch(const char *var, const char *url)
        return 0;
 }
 
+static char *default_user_config(void)
+{
+       struct strbuf buf = STRBUF_INIT;
+       strbuf_addf(&buf,
+                   _("# This is Git's per-user configuration file.\n"
+                     "[core]\n"
+                     "# Please adapt and uncomment the following lines:\n"
+                     "#        user = %s\n"
+                     "#        email = %s\n"),
+                   ident_default_name(),
+                   ident_default_email());
+       return strbuf_detach(&buf, NULL);
+}
+
 int cmd_config(int argc, const char **argv, const char *prefix)
 {
        int nongit = !startup_info->have_repository;
@@ -564,6 +568,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                }
        }
        else if (actions == ACTION_EDIT) {
+               const char *config_file = given_config_source.file ?
+                       given_config_source.file : git_path("config");
                check_argc(argc, 0, 0);
                if (!given_config_source.file && nongit)
                        die("not in a git directory");
@@ -572,9 +578,18 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                if (given_config_source.blob)
                        die("editing blobs is not supported");
                git_config(git_default_config, NULL);
-               launch_editor(given_config_source.file ?
-                             given_config_source.file : git_path("config"),
-                             NULL, NULL);
+               if (use_global_config) {
+                       int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
+                       if (fd) {
+                               char *content = default_user_config();
+                               write_str_in_full(fd, content);
+                               free(content);
+                               close(fd);
+                       }
+                       else if (errno != EEXIST)
+                               die_errno(_("cannot create configuration file %s"), config_file);
+               }
+               launch_editor(config_file, NULL, NULL);
        }
        else if (actions == ACTION_SET) {
                int ret;
@@ -599,7 +614,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
                return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], value, "^$", 0);
+                                                      argv[0], value,
+                                                      CONFIG_REGEX_NONE, 0);
        }
        else if (actions == ACTION_REPLACE_ALL) {
                check_write();