path: root/builtin/config.c
diff options
authorNguyễn Thái Ngọc Duy <>2018-10-21 14:02:28 (GMT)
committerJunio C Hamano <>2018-10-22 04:17:04 (GMT)
commit58b284a2e9123588eedc8c5ee17e8b069d9454f8 (patch)
treee2398ea0280482d84ed02866f20c228df2e4f38b /builtin/config.c
parenta5db0b77b916014e732155d116575bdffbcbe79b (diff)
worktree: add per-worktree config files
A new repo extension is added, worktreeConfig. When it is present: - Repository config reading by default includes $GIT_DIR/config _and_ $GIT_DIR/config.worktree. "config" file remains shared in multiple worktree setup. - The special treatment for core.bare and core.worktree, to stay effective only in main worktree, is gone. These config settings are supposed to be in config.worktree. This extension is most useful in multiple worktree setup because you now have an option to store per-worktree config (which is either .git/config.worktree for main worktree, or .git/worktrees/xx/config.worktree for linked ones). This extension can be used in single worktree mode, even though it's pretty much useless (but this can happen after you remove all linked worktrees and move back to single worktree). "git config" reads from both "config" and "config.worktree" by default (i.e. without either --user, --file...) when this extension is present. Default writes still go to "config", not "config.worktree". A new option --worktree is added for that (*). Since a new repo extension is introduced, existing git binaries should refuse to access to the repo (both from main and linked worktrees). So they will not misread the config file (i.e. skip the config.worktree part). They may still accidentally write to the config file anyway if they use with "git config --file <path>". This design places a bet on the assumption that the majority of config variables are shared so it is the default mode. A safer move would be default writes go to per-worktree file, so that accidental changes are isolated. (*) "git config --worktree" points back to "config" file when this extension is not present and there is only one worktree so that it works in any both single and multiple worktree setups. Signed-off-by: Nguyễn Thái Ngọc Duy <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/config.c')
1 files changed, 18 insertions, 1 deletions
diff --git a/builtin/config.c b/builtin/config.c
index 97b58c4..84385ef 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -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")),
@@ -602,6 +605,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
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"));
@@ -645,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);