tag: fix formatting
[gitweb.git] / builtin / config.c
index a09ead5bc8f0f0b3e17463094167cc7a15f77d7e..98d65bc0ad4fd4bee8bd2755b9d7444d9862eae8 100644 (file)
@@ -5,6 +5,7 @@
 #include "parse-options.h"
 #include "urlmatch.h"
 #include "quote.h"
+#include "worktree.h"
 
 static const char *const builtin_config_usage[] = {
        N_("git config [<options>]"),
@@ -24,6 +25,7 @@ static char key_delim = ' ';
 static char term = '\n';
 
 static int use_global_config, use_system_config, use_local_config;
+static int use_worktree_config;
 static struct git_config_source given_config_source;
 static int actions, type;
 static char *default_value;
@@ -67,7 +69,7 @@ static int show_origin;
        { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
        PARSE_OPT_NONEG, option_parse_type, (i) }
 
-static struct option builtin_config_options[];
+static NORETURN void usage_builtin_config(void);
 
 static int option_parse_type(const struct option *opt, const char *arg,
                             int unset)
@@ -110,9 +112,8 @@ static int option_parse_type(const struct option *opt, const char *arg,
                 * --int' and '--type=bool
                 * --type=int'.
                 */
-               error("only one type at a time");
-               usage_with_options(builtin_config_usage,
-                       builtin_config_options);
+               error(_("only one type at a time"));
+               usage_builtin_config();
        }
        *to_type = new_type;
 
@@ -124,6 +125,7 @@ static struct option builtin_config_options[] = {
        OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
        OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
        OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
+       OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
        OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
        OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
        OPT_GROUP(N_("Action")),
@@ -157,15 +159,21 @@ static struct option builtin_config_options[] = {
        OPT_END(),
 };
 
-static void check_argc(int argc, int min, int max) {
+static NORETURN void usage_builtin_config(void)
+{
+       usage_with_options(builtin_config_usage, builtin_config_options);
+}
+
+static void check_argc(int argc, int min, int max)
+{
        if (argc >= min && argc <= max)
                return;
        if (min == max)
-               error("wrong number of arguments, should be %d", min);
+               error(_("wrong number of arguments, should be %d"), min);
        else
-               error("wrong number of arguments, should be from %d to %d",
+               error(_("wrong number of arguments, should be from %d to %d"),
                      min, max);
-       usage_with_options(builtin_config_usage, builtin_config_options);
+       usage_builtin_config();
 }
 
 static void show_config_origin(struct strbuf *buf)
@@ -297,7 +305,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_);
+                       error(_("invalid key pattern: %s"), key_);
                        FREE_AND_NULL(key_regexp);
                        ret = CONFIG_INVALID_PATTERN;
                        goto free_strings;
@@ -317,7 +325,7 @@ 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_);
+                       error(_("invalid pattern: %s"), regex_);
                        FREE_AND_NULL(regexp);
                        ret = CONFIG_INVALID_PATTERN;
                        goto free_strings;
@@ -390,7 +398,7 @@ static char *normalize_value(const char *key, const char *value)
        if (type == TYPE_COLOR) {
                char v[COLOR_MAXLEN];
                if (git_config_color(v, key, value))
-                       die("cannot parse color '%s'", value);
+                       die(_("cannot parse color '%s'"), value);
 
                /*
                 * The contents of `v` now contain an ANSI escape
@@ -485,13 +493,13 @@ 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");
+               die(_("not in a git directory"));
 
        if (given_config_source.use_stdin)
-               die("writing to stdin is not supported");
+               die(_("writing to stdin is not supported"));
 
        if (given_config_source.blob)
-               die("writing config blobs is not supported");
+               die(_("writing config blobs is not supported"));
 }
 
 struct urlmatch_current_candidate_value {
@@ -591,16 +599,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)
        int nongit = !startup_info->have_repository;
        char *value;
 
-       given_config_source.file = getenv(CONFIG_ENVIRONMENT);
+       given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
 
        argc = parse_options(argc, argv, prefix, builtin_config_options,
                             builtin_config_usage,
                             PARSE_OPT_STOP_AT_NON_OPTION);
 
        if (use_global_config + use_system_config + use_local_config +
+           use_worktree_config +
            !!given_config_source.file + !!given_config_source.blob > 1) {
-               error("only one config file at a time");
-               usage_with_options(builtin_config_usage, builtin_config_options);
+               error(_("only one config file at a time"));
+               usage_builtin_config();
        }
 
        if (use_local_config && nongit)
@@ -626,7 +635,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                         * location; error out even if XDG_CONFIG_HOME
                         * is set and points at a sane location.
                         */
-                       die("$HOME not set");
+                       die(_("$HOME not set"));
 
                if (access_or_warn(user_config, R_OK, 0) &&
                    xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
@@ -641,7 +650,20 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                given_config_source.file = git_etc_gitconfig();
        else if (use_local_config)
                given_config_source.file = git_pathdup("config");
-       else if (given_config_source.file) {
+       else if (use_worktree_config) {
+               struct worktree **worktrees = get_worktrees(0);
+               if (repository_format_worktree_config)
+                       given_config_source.file = git_pathdup("config.worktree");
+               else if (worktrees[0] && worktrees[1])
+                       die(_("--worktree cannot be used with multiple "
+                             "working trees unless the config\n"
+                             "extension worktreeConfig is enabled. "
+                             "Please read \"CONFIGURATION FILE\"\n"
+                             "section in \"git help worktree\" for details"));
+               else
+                       given_config_source.file = git_pathdup("config");
+               free_worktrees(worktrees);
+       } else if (given_config_source.file) {
                if (!is_absolute_path(given_config_source.file) && prefix)
                        given_config_source.file =
                                prefix_filename(prefix, given_config_source.file);
@@ -663,13 +685,13 @@ int cmd_config(int argc, const char **argv, const char *prefix)
        }
 
        if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
-               error("--get-color and variable type are incoherent");
-               usage_with_options(builtin_config_usage, builtin_config_options);
+               error(_("--get-color and variable type are incoherent"));
+               usage_builtin_config();
        }
 
        if (HAS_MULTI_BITS(actions)) {
-               error("only one action at a time");
-               usage_with_options(builtin_config_usage, builtin_config_options);
+               error(_("only one action at a time"));
+               usage_builtin_config();
        }
        if (actions == 0)
                switch (argc) {
@@ -677,25 +699,24 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                case 2: actions = ACTION_SET; break;
                case 3: actions = ACTION_SET_ALL; break;
                default:
-                       usage_with_options(builtin_config_usage, builtin_config_options);
+                       usage_builtin_config();
                }
        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);
+               error(_("--name-only is only applicable to --list or --get-regexp"));
+               usage_builtin_config();
        }
 
        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);
+               error(_("--show-origin is only applicable to --get, --get-all, "
+                       "--get-regexp, and --list"));
+               usage_builtin_config();
        }
 
        if (default_value && !(actions & ACTION_GET)) {
-               error("--default is only applicable to --get");
-               usage_with_options(builtin_config_usage,
-                       builtin_config_options);
+               error(_("--default is only applicable to --get"));
+               usage_builtin_config();
        }
 
        if (actions & PAGING_ACTIONS)
@@ -707,10 +728,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                                        &given_config_source,
                                        &config_options) < 0) {
                        if (given_config_source.file)
-                               die_errno("unable to read config file '%s'",
+                               die_errno(_("unable to read config file '%s'"),
                                          given_config_source.file);
                        else
-                               die("error processing config file(s)");
+                               die(_("error processing config file(s)"));
                }
        }
        else if (actions == ACTION_EDIT) {
@@ -718,11 +739,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 
                check_argc(argc, 0, 0);
                if (!given_config_source.file && nongit)
-                       die("not in a git directory");
+                       die(_("not in a git directory"));
                if (given_config_source.use_stdin)
-                       die("editing stdin is not supported");
+                       die(_("editing stdin is not supported"));
                if (given_config_source.blob)
-                       die("editing blobs is not supported");
+                       die(_("editing blobs is not supported"));
                git_config(git_default_config, NULL);
                config_file = given_config_source.file ?
                                xstrdup(given_config_source.file) :
@@ -823,7 +844,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                if (ret < 0)
                        return ret;
                if (ret == 0)
-                       die("no such section: %s", argv[0]);
+                       die(_("no such section: %s"), argv[0]);
        }
        else if (actions == ACTION_REMOVE_SECTION) {
                int ret;
@@ -834,7 +855,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                if (ret < 0)
                        return ret;
                if (ret == 0)
-                       die("no such section: %s", argv[0]);
+                       die(_("no such section: %s"), argv[0]);
        }
        else if (actions == ACTION_GET_COLOR) {
                check_argc(argc, 1, 2);