git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] config.c: new config namespace worktree.* stored in $GIT_DIR/config.worktree

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 8, 2015, 13:16 UTC
Message-ID
<1423401394-13675-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1423401394-13675-1-git-send-email-pclouds@gmail.com>

On the surface, worktree.* variables are just like any other variables. You can read or modify them with `git config` command, or config_* API. Underneath, worktree.* are always stored in $GIT_DIR/config.worktree instead of $GIT_DIR/config (and never in $HOME/.gitconfig or /etc/gitconfig)

The reason for this split is, as the name implies, for worktree-specific configuration. When the same repo is attached to more than one worktree, we can't use $GIT_DIR/config to store worktree specific stuff because it's shared across worktrees.

With this, both git-new-workdir and nd/multiple-work-trees will work. git-new-workdir creates a symlink to $GIT_DIR/config in the new worktree, but not $GIT_DIR/config.worktree. That file will be created new when worktree.* are written in the new worktree. `git checkout --to` can remap the path $GIT_DIR/config.worktree to make it worktree-specific.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt |  4 +++-
 builtin/config.c         |  8 ++++++++
 config.c                 | 38 ++++++++++++++++++++++++++++++++++++++
 t/t1300-repo-config.sh   | 31 +++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 1 deletion(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 04e2a71..26e4e07 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -2,11 +2,13 @@ CONFIGURATION FILE
 ------------------
 
 The Git configuration file contains a number of variables that affect
-the Git commands' behavior. The `.git/config` file in each repository
+the Git commands' behavior. The `.git/config` and `.git/config.worktree`
+files in each repository
 is used to store the configuration for that repository, and
 `$HOME/.gitconfig` is used to store a per-user configuration as
 fallback values for the `.git/config` file. The file `/etc/gitconfig`
 can be used to store a system-wide default configuration.
+worktree.* variables can only be contained in `.git/config.worktree`
 
 The configuration variables are used by both the Git plumbing
 and the porcelains. The variables are divided into sections, wherein
diff --git a/builtin/config.c b/builtin/config.c
index 15a7bea..d9333cd 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -552,6 +552,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 			usage_with_options(builtin_config_usage, builtin_config_options);
 		}
 
+	/*
+	 * For set operations, --local could be either config or
+	 * config.worktree. Let config.c determine the path based on
+	 * config keys.
+	 */
+	if (use_local_config && actions != ACTION_LIST)
+		given_config_source.file = NULL;
+
 	if (actions == ACTION_LIST) {
 		check_argc(argc, 0, 0);
 		if (git_config_with_options(show_all_config, NULL,
diff --git a/config.c b/config.c
index 752e2e2..3d46192 100644
--- a/config.c
+++ b/config.c
@@ -12,6 +12,7 @@
 #include "quote.h"
 #include "hashmap.h"
 #include "string-list.h"
+#include "dir.h"
 
 struct config_source {
 	struct config_source *prev;
@@ -1177,6 +1178,15 @@ int git_config_system(void)
 	return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
 }
 
+static int config_worktree_filter(const char *var, const char *value, void *data)
+{
+	struct config_include_data *inc = data;
+
+	if (!starts_with(var, "worktree."))
+		return error("$GIT_DIR/config.worktree can only contain worktree.*");
+	return inc->fn(var, value, inc->data);
+}
+
 int git_config_early(config_fn_t fn, void *data, const char *repo_config)
 {
 	int ret = 0, found = 0;
@@ -1202,8 +1212,19 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
 	}
 
 	if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
+		char *wt_config;
 		ret += git_config_from_file(fn, repo_config, data);
 		found += 1;
+		wt_config = git_pathdup("config.worktree");
+		if (!access_or_die(wt_config, R_OK, 0)) {
+			struct config_include_data inc = CONFIG_INCLUDE_INIT;
+			inc.fn = fn;
+			inc.data = data;
+			ret += git_config_from_file(config_worktree_filter,
+						    wt_config, &inc);
+			found += 1;
+		}
+		free(wt_config);
 	}
 
 	switch (git_config_from_parameters(fn, data)) {
@@ -1942,6 +1963,23 @@ int git_config_set_multivar_in_file(const char *config_filename,
 
 	store.multi_replace = multi_replace;
 
+	if (starts_with(key, "worktree.")) {
+		if (!config_filename)
+			config_filename = filename_buf = git_pathdup("config.worktree");
+		/* cheap trick, but should work 90% of time */
+		else if (!ends_with(config_filename, ".worktree"))
+			die("worktree.* can only be stored in %s",
+			    git_path("config.worktree"));
+		else {
+			struct strbuf sb = STRBUF_INIT;
+			strbuf_addstr(&sb, real_path(config_filename));
+			if (strcmp_icase(sb.buf,
+					 real_path(git_path("config.worktree"))))
+				die("worktree.* can only be stored in %s",
+				    git_path("config.worktree"));
+			strbuf_release(&sb);
+		}
+	}
 	if (!config_filename)
 		config_filename = filename_buf = git_pathdup("config");
 
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 938fc8b..58a5009 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -1179,4 +1179,35 @@ test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
 	  "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
 '
 
+test_expect_success 'setting worktree.foo goes to config.worktree' '
+	test_when_finished "rm .git/config.worktree" &&
+	git config worktree.foo bar &&
+	cat >expect <<\EOF &&
+[worktree]
+	foo = bar
+EOF
+	test_cmp expect .git/config.worktree &&
+	test "`git config worktree.foo`" = bar
+'
+
+test_expect_success 'setting --local worktree.foo goes to config.worktree' '
+	test_when_finished "rm .git/config.worktree" &&
+	git config --local worktree.fooo barr &&
+	cat >expect <<\EOF &&
+[worktree]
+	fooo = barr
+EOF
+	test_cmp expect .git/config.worktree &&
+	test "`git config worktree.fooo`" = barr
+'
+
+test_expect_success 'setting worktree.foo outside config.worktree' '
+	test_must_fail git config --global worktree.foo bar 2>err &&
+	grep config.worktree err &&
+	test_must_fail git config --system worktree.foo bar 2>err &&
+	grep config.worktree err &&
+	test_must_fail git config --file foo.worktree worktree.foo bar 2>err &&
+	grep config.worktree err
+'
+
 test_done
-- 
2.3.0.rc1.137.g477eb31
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 2 of 22 in “worktree.* config keys and submodule and multiple worktrees”
  1. 0/3 worktree.* config keys and submodule and multiple worktreesNguyễn Thái Ngọc Duy, Feb 8, 2015
  2. 1/3 config.c: new config namespace worktree.* stored in $GIT_DIR/config.worktreeNguyễn Thái Ngọc Duy, Feb 8, 2015
  3. 2/3 setup: add worktree.path to shadow core.worktreeNguyễn Thái Ngọc Duy, Feb 8, 2015
  4. 3/3 submodule: use worktree.path instead of core.worktreeNguyễn Thái Ngọc Duy, Feb 8, 2015
  5. Jens LehmannFeb 8, 2015
  6. Jens LehmannFeb 8, 2015
  7. Duy NguyenFeb 9, 2015
  8. per-repository and per-worktree config variablesMax Kirillov, Mar 18, 2015
  9. Duy NguyenMar 24, 2015
  10. config.c: split some variables to $GIT_DIR/config.worktreeNguyễn Thái Ngọc Duy, Mar 26, 2015
  11. Max KirillovMar 26, 2015
  12. Duy NguyenMar 29, 2015
  13. Max KirillovMar 30, 2015
  14. config.c: split some variables to $GIT_DIR/config.worktreeNguyễn Thái Ngọc Duy, Mar 31, 2015
  15. Duy NguyenMar 31, 2015
  16. Max KirillovApr 1, 2015
  17. Duy NguyenApr 3, 2015
  18. Max KirillovApr 13, 2015
  19. Duy NguyenApr 18, 2015
  20. Max KirillovApr 20, 2015
  21. Duy NguyenApr 20, 2015
  22. Jens LehmannMar 25, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.