Merge branch 'rs/t4062-obsd' into next
[gitweb.git] / builtin / config.c
index 71acc4414352e8d097447993937db96a1cc6efaa..70ff231e9c5bce550e7af20ddfe91b8b68ee32ae 100644 (file)
@@ -1,8 +1,10 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "color.h"
 #include "parse-options.h"
 #include "urlmatch.h"
+#include "quote.h"
 
 static const char *const builtin_config_usage[] = {
        N_("git config [<options>]"),
@@ -24,9 +26,10 @@ static char term = '\n';
 static int use_global_config, use_system_config, use_local_config;
 static struct git_config_source given_config_source;
 static int actions, types;
-static const char *get_color_slot, *get_colorbool_slot;
 static int end_null;
-static int respect_includes = -1;
+static int respect_includes_opt = -1;
+static struct config_options config_options;
+static int show_origin;
 
 #define ACTION_GET (1<<0)
 #define ACTION_GET_ALL (1<<1)
@@ -80,7 +83,8 @@ static struct option builtin_config_options[] = {
        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_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
+       OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
        OPT_END(),
 };
 
@@ -91,8 +95,28 @@ static void check_argc(int argc, int min, int max) {
        usage_with_options(builtin_config_usage, builtin_config_options);
 }
 
+static void show_config_origin(struct strbuf *buf)
+{
+       const char term = end_null ? '\0' : '\t';
+
+       strbuf_addstr(buf, current_config_origin_type());
+       strbuf_addch(buf, ':');
+       if (end_null)
+               strbuf_addstr(buf, current_config_name());
+       else
+               quote_c_style(current_config_name(), buf, NULL, 0);
+       strbuf_addch(buf, term);
+}
+
 static int show_all_config(const char *key_, const char *value_, void *cb)
 {
+       if (show_origin) {
+               struct strbuf buf = STRBUF_INIT;
+               show_config_origin(&buf);
+               /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
+               fwrite(buf.buf, 1, buf.len, stdout);
+               strbuf_release(&buf);
+       }
        if (!omit_values && value_)
                printf("%s%c%s%c", key_, delim, value_, term);
        else
@@ -108,6 +132,8 @@ struct strbuf_list {
 
 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 {
+       if (show_origin)
+               show_config_origin(buf);
        if (show_keys)
                strbuf_addstr(buf, key_);
        if (!omit_values) {
@@ -189,8 +215,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)) {
                        error("invalid key pattern: %s", key_);
-                       free(key_regexp);
-                       key_regexp = NULL;
+                       FREE_AND_NULL(key_regexp);
                        ret = CONFIG_INVALID_PATTERN;
                        goto free_strings;
                }
@@ -210,15 +235,14 @@ static int get_value(const char *key_, const char *regex_)
                regexp = (regex_t*)xmalloc(sizeof(regex_t));
                if (regcomp(regexp, regex_, REG_EXTENDED)) {
                        error("invalid pattern: %s", regex_);
-                       free(regexp);
-                       regexp = NULL;
+                       FREE_AND_NULL(regexp);
                        ret = CONFIG_INVALID_PATTERN;
                        goto free_strings;
                }
        }
 
-       git_config_with_options(collect_config, &values,
-                               &given_config_source, respect_includes);
+       config_with_options(collect_config, &values,
+                           &given_config_source, &config_options);
 
        ret = !values.nr;
 
@@ -246,8 +270,6 @@ static int get_value(const char *key_, const char *regex_)
 
 static char *normalize_value(const char *key, const char *value)
 {
-       char *normalized;
-
        if (!value)
                return NULL;
 
@@ -258,27 +280,21 @@ static char *normalize_value(const char *key, const char *value)
                 * "~/foobar/" in the config file, and to expand the ~
                 * when retrieving the value.
                 */
-               normalized = xstrdup(value);
-       else {
-               normalized = xmalloc(64);
-               if (types == TYPE_INT) {
-                       int64_t v = git_config_int64(key, value);
-                       sprintf(normalized, "%"PRId64, v);
-               }
-               else if (types == TYPE_BOOL)
-                       sprintf(normalized, "%s",
-                               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)
-                               sprintf(normalized, "%d", v);
-                       else
-                               sprintf(normalized, "%s", v ? "true" : "false");
-               }
+               return xstrdup(value);
+       if (types == TYPE_INT)
+               return xstrfmt("%"PRId64, git_config_int64(key, value));
+       if (types == TYPE_BOOL)
+               return xstrdup(git_config_bool(key, value) ?  "true" : "false");
+       if (types == TYPE_BOOL_OR_INT) {
+               int is_bool, v;
+               v = git_config_bool_or_int(key, value, &is_bool);
+               if (!is_bool)
+                       return xstrfmt("%d", v);
+               else
+                       return xstrdup(v ? "true" : "false");
        }
 
-       return normalized;
+       die("BUG: cannot normalize type %d", types);
 }
 
 static int get_color_found;
@@ -303,8 +319,8 @@ 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);
+       config_with_options(git_get_color_config, NULL,
+                           &given_config_source, &config_options);
 
        if (!get_color_found && def_color) {
                if (color_parse(def_color, parsed_color) < 0)
@@ -335,8 +351,8 @@ static int get_colorbool(const char *var, int print)
        get_colorbool_found = -1;
        get_diff_color_found = -1;
        get_color_ui_found = -1;
-       git_config_with_options(git_get_colorbool_config, NULL,
-                               &given_config_source, respect_includes);
+       config_with_options(git_get_colorbool_config, NULL,
+                           &given_config_source, &config_options);
 
        if (get_colorbool_found < 0) {
                if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -360,6 +376,9 @@ static int get_colorbool(const char *var, int print)
 
 static void check_write(void)
 {
+       if (!given_config_source.file && !startup_info->have_repository)
+               die("not in a git directory");
+
        if (given_config_source.use_stdin)
                die("writing to stdin is not supported");
 
@@ -397,6 +416,7 @@ static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
 
 static int get_urlmatch(const char *var, const char *url)
 {
+       int ret;
        char *section_tail;
        struct string_list_item *item;
        struct urlmatch_config config = { STRING_LIST_INIT_DUP };
@@ -420,8 +440,10 @@ static int get_urlmatch(const char *var, const char *url)
                show_keys = 1;
        }
 
-       git_config_with_options(urlmatch_config_entry, &config,
-                               &given_config_source, respect_includes);
+       config_with_options(urlmatch_config_entry, &config,
+                           &given_config_source, &config_options);
+
+       ret = !values.nr;
 
        for_each_string_list_item(item, &values) {
                struct urlmatch_current_candidate_value *matched = item->util;
@@ -439,7 +461,7 @@ static int get_urlmatch(const char *var, const char *url)
        free(config.url.url);
 
        free((void *)config.section);
-       return 0;
+       return ret;
 }
 
 static char *default_user_config(void)
@@ -473,6 +495,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                usage_with_options(builtin_config_usage, builtin_config_options);
        }
 
+       if (use_local_config && nongit)
+               die(_("--local can only be used inside a git repository"));
+
        if (given_config_source.file &&
                        !strcmp(given_config_source.file, "-")) {
                given_config_source.file = NULL;
@@ -480,7 +505,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
        }
 
        if (use_global_config) {
-               char *user_config = expand_user_path("~/.gitconfig");
+               char *user_config = expand_user_path("~/.gitconfig", 0);
                char *xdg_config = xdg_config_home("config");
 
                if (!user_config)
@@ -505,13 +530,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)
        else if (given_config_source.file) {
                if (!is_absolute_path(given_config_source.file) && prefix)
                        given_config_source.file =
-                               xstrdup(prefix_filename(prefix,
-                                                       strlen(prefix),
-                                                       given_config_source.file));
+                               prefix_filename(prefix, given_config_source.file);
        }
 
-       if (respect_includes == -1)
-               respect_includes = !given_config_source.file;
+       if (respect_includes_opt == -1)
+               config_options.respect_includes = !given_config_source.file;
+       else
+               config_options.respect_includes = respect_includes_opt;
+       if (!nongit) {
+               config_options.commondir = get_git_common_dir();
+               config_options.git_dir = get_git_dir();
+       }
 
        if (end_null) {
                term = '\0';
@@ -546,11 +575,19 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                error("--name-only is only applicable to --list or --get-regexp");
                usage_with_options(builtin_config_usage, builtin_config_options);
        }
+
+       if (show_origin && !(actions &
+               (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
+               error("--show-origin is only applicable to --get, --get-all, "
+                         "--get-regexp, and --list.");
+               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,
-                                           &given_config_source,
-                                           respect_includes) < 0) {
+               if (config_with_options(show_all_config, NULL,
+                                       &given_config_source,
+                                       &config_options) < 0) {
                        if (given_config_source.file)
                                die_errno("unable to read config file '%s'",
                                          given_config_source.file);
@@ -569,11 +606,12 @@ 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"));
+               config_file = given_config_source.file ?
+                               xstrdup(given_config_source.file) :
+                               git_pathdup("config");
                if (use_global_config) {
                        int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
-                       if (fd) {
+                       if (fd >= 0) {
                                char *content = default_user_config();
                                write_str_in_full(fd, content);
                                free(content);
@@ -590,33 +628,33 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_write();
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
-               ret = git_config_set_in_file(given_config_source.file, argv[0], value);
+               ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
                if (ret == CONFIG_NOTHING_SET)
-                       error("cannot overwrite multiple values with a single value\n"
-                       "       Use a regexp, --add or --replace-all to change %s.", argv[0]);
+                       error(_("cannot overwrite multiple values with a single value\n"
+                       "       Use a regexp, --add or --replace-all to change %s."), argv[0]);
                return ret;
        }
        else if (actions == ACTION_SET_ALL) {
                check_write();
                check_argc(argc, 2, 3);
                value = normalize_value(argv[0], argv[1]);
-               return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], value, argv[2], 0);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], value, argv[2], 0);
        }
        else if (actions == ACTION_ADD) {
                check_write();
                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,
-                                                      CONFIG_REGEX_NONE, 0);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], value,
+                                                             CONFIG_REGEX_NONE, 0);
        }
        else if (actions == ACTION_REPLACE_ALL) {
                check_write();
                check_argc(argc, 2, 3);
                value = normalize_value(argv[0], argv[1]);
-               return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], value, argv[2], 1);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], value, argv[2], 1);
        }
        else if (actions == ACTION_GET) {
                check_argc(argc, 1, 2);
@@ -642,17 +680,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                check_write();
                check_argc(argc, 1, 2);
                if (argc == 2)
-                       return git_config_set_multivar_in_file(given_config_source.file,
-                                                              argv[0], NULL, argv[1], 0);
+                       return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                                     argv[0], NULL, argv[1], 0);
                else
-                       return git_config_set_in_file(given_config_source.file,
-                                                     argv[0], NULL);
+                       return git_config_set_in_file_gently(given_config_source.file,
+                                                            argv[0], NULL);
        }
        else if (actions == ACTION_UNSET_ALL) {
                check_write();
                check_argc(argc, 1, 2);
-               return git_config_set_multivar_in_file(given_config_source.file,
-                                                      argv[0], NULL, argv[1], 1);
+               return git_config_set_multivar_in_file_gently(given_config_source.file,
+                                                             argv[0], NULL, argv[1], 1);
        }
        else if (actions == ACTION_RENAME_SECTION) {
                int ret;