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

[PATCH] Fix core.worktree being used when GIT_DIR is not set

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 27, 2009, 13:28 UTC
Message-ID
<1261920513-25189-1-git-send-email-pclouds@gmail.com>
In-Reply-To
<200912071115.48085.robin.rosenberg.lists@dewire.com>
According to config.txt:
> core.worktree::
>         Set the path to the working tree.  The value will not be
>         used in combination with repositories found automatically in
>         a .git directory (i.e. $GIT_DIR is not set).

This behavior was changed after e90fdc3 (Clean up work-tree handling - 2007-08-01) and 9459aa7 (Do check_repository_format() early (re-fix) - 2007-12-05). If core.worktree is set, even if git_dir automatically found (and git_work_tree_cfg set), git_work_tree_cfg will be reset to core.worktree. This makes core.worktree effective even if GIT_DIR is not set, in contrast to config.txt.

This patch makes sure it only checks for core.worktree if GIT_DIR is set.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 setup.c             |   19 +++++++++++++------
 t/t1501-worktree.sh |    4 ++++
 2 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/setup.c b/setup.c
index 2cf0f19..35b7915 100644
--- a/setup.c
+++ b/setup.c
@@ -283,6 +283,18 @@ const char *read_gitfile_gently(const char *path)
 	return path;
 }
 
+static int check_repository_work_tree(const char *var, const char *value, void *cb)
+{
+	if (strcmp(var, "core.worktree") == 0) {
+		if (!value)
+			return config_error_nonbool(var);
+		free(git_work_tree_cfg);
+		git_work_tree_cfg = xstrdup(value);
+		inside_work_tree = -1;
+	}
+	return 0;
+}
+
 /*
  * We cannot decide in this function whether we are in the work tree or
  * not, since the config can only be read _after_ this function was called.
@@ -320,6 +332,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 			if (!work_tree_env) {
 				retval = set_work_tree(gitdirenv);
 				/* config may override worktree */
+				git_config(check_repository_work_tree, NULL);
 				if (check_repository_format_gently(nongit_ok))
 					return NULL;
 				return retval;
@@ -474,12 +487,6 @@ int check_repository_format_version(const char *var, const char *value, void *cb
 		is_bare_repository_cfg = git_config_bool(var, value);
 		if (is_bare_repository_cfg == 1)
 			inside_work_tree = -1;
-	} else if (strcmp(var, "core.worktree") == 0) {
-		if (!value)
-			return config_error_nonbool(var);
-		free(git_work_tree_cfg);
-		git_work_tree_cfg = xstrdup(value);
-		inside_work_tree = -1;
 	}
 	return 0;
 }
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 74e6443..9756f35 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -30,6 +30,10 @@ test_rev_parse() {
 
 EMPTY_TREE=$(git write-tree)
 mkdir -p work/sub/dir || exit 1
+
+git config core.worktree work
+test_rev_parse 'core.worktree without GIT_DIR' false false true ''
+
 mv .git repo.git || exit 1
 
 say "core.worktree = relative path"
-- 
1.6.5.2.216.g9c1ec
Previous: Nguyen Thai Ngoc DuyNext: Junio C Hamano
Message 3 of 11 in “core.worktree bug”
  1. Robin RosenbergDec 7, 2009
  2. Nguyen Thai Ngoc DuyDec 15, 2009
  3. Fix core.worktree being used when GIT_DIR is not setNguyễn Thái Ngọc Duy, Dec 27, 2009
  4. Junio C HamanoDec 27, 2009
  5. Robin RosenbergDec 28, 2009
  6. Nguyen Thai Ngoc DuyDec 28, 2009
  7. Documentation: always respect core.worktree if setNguyễn Thái Ngọc Duy, Dec 28, 2009
  8. Junio C HamanoDec 28, 2009
  9. Documentation: always respect core.worktree if setNguyễn Thái Ngọc Duy, Dec 29, 2009
  10. Junio C HamanoDec 29, 2009
  11. Robin RosenbergDec 29, 2009

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.