]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/config.c
Merge branch 'jk/save-getenv-result'
[thirdparty/git.git] / builtin / config.c
index 752d0c65d76ca94498ce413f79efde208399b3e5..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;
@@ -123,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")),
@@ -161,7 +164,8 @@ 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) {
+static void check_argc(int argc, int min, int max)
+{
        if (argc >= min && argc <= max)
                return;
        if (min == max)
@@ -602,6 +606,7 @@ 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_builtin_config();
@@ -645,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);