# [PATCH] clone: pass work tree to repack child of --dissociate

1 messages from 2026-12-02 to 2026-12-02. Participants: Daniel Fox via GitGitGadget.
Thread: https://gitlist.dev/t/66977

## Daniel Fox via GitGitGadget, 2026-12-02 20:45

Subject: [PATCH] clone: pass work tree to repack child of --dissociate
Message-ID: <pull.2346.git.1796244340074.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.2346.git.1796244340074.gitgitgadget%40gmail.com

```
From: Daniel Fox <danudey@me.com>

After "git clone --dissociate" copies the borrowed objects,
dissociate_from_references() runs "git repack -a -d" in a child
process. init_db() exports GIT_DIR, so the child inherits it.
GIT_WORK_TREE is not exported. With GIT_DIR set and no
GIT_WORK_TREE, the child treats its current directory as the top of
the work tree. That directory is the one clone was run from, which
is usually the parent of the new work tree.

Repack itself does not care about the work tree, but it reads the
index. When core.fsmonitor is enabled, reading the index can start
the fsmonitor daemon implicitly. That daemon then watches the wrong
directory and never sees the cookie files that clients create in
the real work tree. As a result, every fsmonitor query in the new
clone waits for the cookie timeout.

When we have a work tree, export it to the child as GIT_WORK_TREE.

Signed-off-by: Daniel Fox <danudey@me.com>
---
    clone: pass work tree to repack child of --dissociate

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2346%2Fdanudey%2Ffsmonitor-clone-dissociate-worktree-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2346/danudey/fsmonitor-clone-dissociate-worktree-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2346

 builtin/clone.c              | 11 +++++++++++
 t/t7527-builtin-fsmonitor.sh | 24 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/builtin/clone.c b/builtin/clone.c
index ac40250d25..f10f95e017 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -851,6 +851,17 @@ static void dissociate_from_references(void)
 
 		cmd.git_cmd = 1;
 		cmd.no_stdin = 1;
+		/*
+		 * We export GIT_DIR but not GIT_WORK_TREE, so without this
+		 * the child would treat our cwd (the parent of the new
+		 * work tree) as its work tree.  That matters because
+		 * "repack" reads the index, which can auto-start the
+		 * fsmonitor daemon, and it would then watch the wrong
+		 * directory.
+		 */
+		if (the_repository->worktree)
+			strvec_pushf(&cmd.env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT,
+				     the_repository->worktree);
 		strvec_pushl(&cmd.args, "repack", "-a", "-d", NULL);
 		if (run_command(&cmd))
 			die(_("cannot repack to clean up"));
diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh
index 86195770e9..a49511945b 100755
--- a/t/t7527-builtin-fsmonitor.sh
+++ b/t/t7527-builtin-fsmonitor.sh
@@ -304,6 +304,30 @@ test_expect_success MINGW,SHORTNAMES 'implicit daemon stop (rename GIT~2)' '
 	verify_implicit_shutdown test_implicit_1s2
 '
 
+test_expect_success 'clone --dissociate starts daemon on the new work tree' '
+	test_when_finished "stop_daemon_delete_repo dissociate/clone; rm -rf dissociate dissociate.trace" &&
+
+	git init dissociate/src &&
+	test_commit -C dissociate/src file &&
+	git clone dissociate/src dissociate/ref &&
+
+	# "--dissociate" runs "git repack" in a child process, which
+	# reads the index and so implicitly starts the daemon.  That
+	# daemon must watch the new work tree, not the directory clone
+	# was run from; otherwise it never sees its own cookie files
+	# and every query waits for the cookie timeout.
+	(
+		cd dissociate &&
+		GIT_TRACE_FSMONITOR="$TRASH_DIRECTORY/dissociate.trace" &&
+		export GIT_TRACE_FSMONITOR &&
+		git -c core.fsmonitor=true clone --reference ref --dissociate \
+			"file://$TRASH_DIRECTORY/dissociate/src" clone &&
+		git -C clone fsmonitor--daemon status &&
+		test-tool -C clone fsmonitor-client query --token 0 >/dev/null
+	) &&
+	test_grep ! "cookie_wait timed out" dissociate.trace
+'
+
 test_expect_success 'cannot start multiple daemons' '
 	test_when_finished "stop_daemon_delete_repo test_multiple" &&
 

base-commit: 03b41523c576f26ad34085378717a0c1b287470f
-- 
gitgitgadget


```
