am -3: do not let failed merge from completing the error codepath
[gitweb.git] / builtin / config.c
index 05cf6915541600781b2078b3dbf2e620f1d6c5ed..71acc4414352e8d097447993937db96a1cc6efaa 100644 (file)
@@ -5,7 +5,7 @@
 #include "urlmatch.h"
 
 static const char *const builtin_config_usage[] = {
-       N_("git config [options]"),
+       N_("git config [<options>]"),
        NULL
 };
 
@@ -13,6 +13,7 @@ static char *key;
 static regex_t *key_regexp;
 static regex_t *regexp;
 static int show_keys;
+static int omit_values;
 static int use_key_regexp;
 static int do_all;
 static int do_not_match;
@@ -69,8 +70,8 @@ static struct option builtin_config_options[] = {
        OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
        OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
        OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
-       OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
-       OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
+       OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
+       OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
        OPT_GROUP(N_("Type")),
        OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
        OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
@@ -78,6 +79,7 @@ static struct option builtin_config_options[] = {
        OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
        OPT_GROUP(N_("Other")),
        OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
+       OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
        OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
        OPT_END(),
 };
@@ -91,7 +93,7 @@ static void check_argc(int argc, int min, int max) {
 
 static int show_all_config(const char *key_, const char *value_, void *cb)
 {
-       if (value_)
+       if (!omit_values && value_)
                printf("%s%c%s%c", key_, delim, value_, term);
        else
                printf("%s%c", key_, term);
@@ -106,48 +108,40 @@ struct strbuf_list {
 
 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 {
-       int must_free_vptr = 0;
-       int must_print_delim = 0;
-       char value[256];
-       const char *vptr = value;
-
-       strbuf_init(buf, 0);
-
-       if (show_keys) {
+       if (show_keys)
                strbuf_addstr(buf, key_);
-               must_print_delim = 1;
-       }
-       if (types == TYPE_INT)
-               sprintf(value, "%"PRId64,
-                       git_config_int64(key_, value_ ? value_ : ""));
-       else if (types == TYPE_BOOL)
-               vptr = git_config_bool(key_, value_) ? "true" : "false";
-       else if (types == TYPE_BOOL_OR_INT) {
-               int is_bool, v;
-               v = git_config_bool_or_int(key_, value_, &is_bool);
-               if (is_bool)
-                       vptr = v ? "true" : "false";
-               else
-                       sprintf(value, "%d", v);
-       } else if (types == TYPE_PATH) {
-               if (git_config_pathname(&vptr, key_, value_) < 0)
-                       return -1;
-               must_free_vptr = 1;
-       } else if (value_) {
-               vptr = value_;
-       } else {
-               /* Just show the key name */
-               vptr = "";
-               must_print_delim = 0;
-       }
+       if (!omit_values) {
+               if (show_keys)
+                       strbuf_addch(buf, key_delim);
 
-       if (must_print_delim)
-               strbuf_addch(buf, key_delim);
-       strbuf_addstr(buf, vptr);
+               if (types == TYPE_INT)
+                       strbuf_addf(buf, "%"PRId64,
+                                   git_config_int64(key_, value_ ? value_ : ""));
+               else if (types == TYPE_BOOL)
+                       strbuf_addstr(buf, git_config_bool(key_, value_) ?
+                                     "true" : "false");
+               else if (types == TYPE_BOOL_OR_INT) {
+                       int is_bool, v;
+                       v = git_config_bool_or_int(key_, value_, &is_bool);
+                       if (is_bool)
+                               strbuf_addstr(buf, v ? "true" : "false");
+                       else
+                               strbuf_addf(buf, "%d", v);
+               } else if (types == TYPE_PATH) {
+                       const char *v;
+                       if (git_config_pathname(&v, key_, value_) < 0)
+                               return -1;
+                       strbuf_addstr(buf, v);
+                       free((char *)v);
+               } else if (value_) {
+                       strbuf_addstr(buf, value_);
+               } else {
+                       /* Just show the key name; back out delimiter */
+                       if (show_keys)
+                               strbuf_setlen(buf, buf->len - 1);
+               }
+       }
        strbuf_addch(buf, term);
-
-       if (must_free_vptr)
-               free((char *)vptr);
        return 0;
 }
 
@@ -164,6 +158,7 @@ static int collect_config(const char *key_, const char *value_, void *cb)
                return 0;
 
        ALLOC_GROW(values->items, values->nr + 1, values->alloc);
+       strbuf_init(&values->items[values->nr], 0);
 
        return format_config(&values->items[values->nr++], key_, value_);
 }
@@ -193,7 +188,7 @@ static int get_value(const char *key_, const char *regex_)
 
                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
                if (regcomp(key_regexp, key, REG_EXTENDED)) {
-                       fprintf(stderr, "Invalid key pattern: %s\n", key_);
+                       error("invalid key pattern: %s", key_);
                        free(key_regexp);
                        key_regexp = NULL;
                        ret = CONFIG_INVALID_PATTERN;
@@ -214,7 +209,7 @@ static int get_value(const char *key_, const char *regex_)
 
                regexp = (regex_t*)xmalloc(sizeof(regex_t));
                if (regcomp(regexp, regex_, REG_EXTENDED)) {
-                       fprintf(stderr, "Invalid pattern: %s\n", regex_);
+                       error("invalid pattern: %s", regex_);
                        free(regexp);
                        regexp = NULL;
                        ret = CONFIG_INVALID_PATTERN;
@@ -296,21 +291,25 @@ 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;
 }
 
-static void get_color(const char *def_color)
+static void get_color(const char *var, const char *def_color)
 {
+       get_color_slot = var;
        get_color_found = 0;
        parsed_color[0] = '\0';
        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);
 }
@@ -330,8 +329,9 @@ static int git_get_colorbool_config(const char *var, const char *value,
        return 0;
 }
 
-static int get_colorbool(int print)
+static int get_colorbool(const char *var, int print)
 {
+       get_colorbool_slot = var;
        get_colorbool_found = -1;
        get_diff_color_found = -1;
        get_color_ui_found = -1;
@@ -395,19 +395,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 +409,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';
@@ -438,14 +425,11 @@ static int get_urlmatch(const char *var, const char *url)
 
        for_each_string_list_item(item, &values) {
                struct urlmatch_current_candidate_value *matched = item->util;
-               struct strbuf key = STRBUF_INIT;
                struct strbuf buf = STRBUF_INIT;
 
-               strbuf_addstr(&key, item->string);
-               format_config(&buf, key.buf,
+               format_config(&buf, item->string,
                              matched->value_is_null ? NULL : matched->value.buf);
                fwrite(buf.buf, 1, buf.len, stdout);
-               strbuf_release(&key);
                strbuf_release(&buf);
 
                strbuf_release(&matched->value);
@@ -496,10 +480,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
        }
 
        if (use_global_config) {
-               char *user_config = NULL;
-               char *xdg_config = NULL;
-
-               home_config_paths(&user_config, &xdg_config, "config");
+               char *user_config = expand_user_path("~/.gitconfig");
+               char *xdg_config = xdg_config_home("config");
 
                if (!user_config)
                        /*
@@ -542,12 +524,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                usage_with_options(builtin_config_usage, builtin_config_options);
        }
 
-       if (get_color_slot)
-           actions |= ACTION_GET_COLOR;
-       if (get_colorbool_slot)
-           actions |= ACTION_GET_COLORBOOL;
-
-       if ((get_color_slot || get_colorbool_slot) && types) {
+       if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
                error("--get-color and variable type are incoherent");
                usage_with_options(builtin_config_usage, builtin_config_options);
        }
@@ -564,7 +541,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                default:
                        usage_with_options(builtin_config_usage, builtin_config_options);
                }
-
+       if (omit_values &&
+           !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
+               error("--name-only is only applicable to --list or --get-regexp");
+               usage_with_options(builtin_config_usage, builtin_config_options);
+       }
        if (actions == ACTION_LIST) {
                check_argc(argc, 0, 0);
                if (git_config_with_options(show_all_config, NULL,
@@ -578,8 +559,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");
+               char *config_file;
+
                check_argc(argc, 0, 0);
                if (!given_config_source.file && nongit)
                        die("not in a git directory");
@@ -588,6 +569,8 @@ 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);
+               config_file = xstrdup(given_config_source.file ?
+                                     given_config_source.file : git_path("config"));
                if (use_global_config) {
                        int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
                        if (fd) {
@@ -600,6 +583,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                                die_errno(_("cannot create configuration file %s"), config_file);
                }
                launch_editor(config_file, NULL, NULL);
+               free(config_file);
        }
        else if (actions == ACTION_SET) {
                int ret;
@@ -624,7 +608,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();
@@ -692,12 +677,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                        die("No such section!");
        }
        else if (actions == ACTION_GET_COLOR) {
-               get_color(argv[0]);
+               check_argc(argc, 1, 2);
+               get_color(argv[0], argv[1]);
        }
        else if (actions == ACTION_GET_COLORBOOL) {
-               if (argc == 1)
-                       color_stdout_is_tty = git_config_bool("command line", argv[0]);
-               return get_colorbool(argc != 0);
+               check_argc(argc, 1, 2);
+               if (argc == 2)
+                       color_stdout_is_tty = git_config_bool("command line", argv[1]);
+               return get_colorbool(argv[0], argc == 2);
        }
 
        return 0;