stash: convert branch to builtin
[gitweb.git] / builtin / config.c
index 5761a2c4ac25d3f90938f9830d4c9d7eebaa9a92..84385ef165195e7c65be54e9463cb49578bec2bb 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)
@@ -111,8 +113,7 @@ static int option_parse_type(const struct option *opt, const char *arg,
                 * --type=int'.
                 */
                error(_("only one type at a time"));
-               usage_with_options(builtin_config_usage,
-                       builtin_config_options);
+               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,6 +159,11 @@ static struct option builtin_config_options[] = {
        OPT_END(),
 };
 
+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;
@@ -165,7 +172,7 @@ static void check_argc(int argc, int min, int max) {
        else
                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)
@@ -598,9 +605,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                             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);
+               usage_builtin_config();
        }
 
        if (use_local_config && nongit)
@@ -641,7 +649,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);
@@ -664,12 +685,12 @@ 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);
+               usage_builtin_config();
        }
 
        if (HAS_MULTI_BITS(actions)) {
                error(_("only one action at a time"));
-               usage_with_options(builtin_config_usage, builtin_config_options);
+               usage_builtin_config();
        }
        if (actions == 0)
                switch (argc) {
@@ -677,25 +698,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);
+               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);
+               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);
+               usage_builtin_config();
        }
 
        if (actions & PAGING_ACTIONS)