Git development
 help / color / mirror / Atom feed
* [PATCH] worktree: allow sharing root files with linked worktrees
@ 2026-11-26 18:25 Bhushan Gaikwad via GitGitGadget
  2026-11-30 16:08 ` D. Ben Knoble
  0 siblings, 1 reply; 2+ messages in thread
From: Bhushan Gaikwad via GitGitGadget @ 2026-11-26 18:25 UTC (permalink / raw)
  To: git; +Cc: Bhushan Gaikwad

From: Bhushan Gaikwad <bhushan@alfredscholar.com>

Ignored configuration files such as .env often need to remain identical
across linked worktrees. Today users must copy them manually or install a
post-checkout hook, which is easy to forget and can create divergent local
configuration.

Add a multi-valued worktree.sharedFile setting. For each configured
root-level regular file, worktree add creates an absolute symbolic link
from the new worktree to the primary worktree before running the
post-checkout hook.

Reject nested paths and source symlinks, avoid replacing checkout files,
and fail cleanly when symbolic links are unavailable.

Signed-off-by: Bhushan Gaikwad <bhushan@alfredscholar.com>
---
    worktree: allow sharing root files with linked worktrees

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2245%2Fbhushan%2Fworktree-link-shared-files-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2245/bhushan/worktree-link-shared-files-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2245

 Documentation/config/worktree.adoc | 13 ++++
 builtin/worktree.c                 | 78 ++++++++++++++++++++++++
 t/t2400-worktree-add.sh            | 95 ++++++++++++++++++++++++++++++
 3 files changed, 186 insertions(+)

diff --git a/Documentation/config/worktree.adoc b/Documentation/config/worktree.adoc
index 0930183b91..ddd423caea 100644
--- a/Documentation/config/worktree.adoc
+++ b/Documentation/config/worktree.adoc
@@ -18,3 +18,16 @@
 Note that setting `worktree.useRelativePaths` to "`true`" implies enabling the
 `extensions.relativeWorktrees` config (see linkgit:git-config[1]),
 thus making it incompatible with older versions of Git.
+
+`worktree.sharedFile`::
+	Specify a file at the root of the primary worktree to create as a
+	symbolic link in each new linked worktree. This is useful for ignored
+	configuration files which should be shared by all worktrees. Paths
+	containing directory separators are rejected. The variable may be
+	specified more than once to share multiple files.
++
+The source must be a regular file, not a symbolic link. Missing or non-regular
+sources are skipped with a warning. Git does not overwrite a path populated by
+checkout, and warns instead. Shared files are linked before the `post-checkout`
+hook runs. The symbolic link uses the absolute source path. Worktree creation
+fails when symbolic links are disabled.
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..23af5f0beb 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -131,6 +131,7 @@ static int verbose;
 static int guess_remote;
 static int use_relative_paths;
 static timestamp_t expire;
+static struct string_list shared_files = STRING_LIST_INIT_DUP;
 
 static int git_worktree_config(const char *var, const char *value,
 			       const struct config_context *ctx, void *cb)
@@ -141,6 +142,14 @@ static int git_worktree_config(const char *var, const char *value,
 	} else if (!strcmp(var, "worktree.userelativepaths")) {
 		use_relative_paths = git_config_bool(var, value);
 		return 0;
+	} else if (!strcmp(var, "worktree.sharedfile")) {
+		if (!value || is_absolute_path(value) ||
+		    strchr(value, '/') || strchr(value, '\\') ||
+		    !verify_path(value, S_IFREG))
+			return error(_("invalid value for worktree.sharedFile: %s"),
+				     value ? value : "");
+		string_list_append(&shared_files, value);
+		return 0;
 	}
 
 	return git_default_config(var, value, ctx, cb);
@@ -455,6 +464,73 @@ static void setup_alternate_ref_dir(struct worktree *wt, const char *wt_git_path
 	strbuf_release(&sb);
 }
 
+static int link_shared_files(const char *path)
+{
+	struct worktree **worktrees;
+	struct string_list_item *item;
+	struct strbuf source = STRBUF_INIT;
+	struct strbuf destination = STRBUF_INIT;
+	struct strbuf primary = STRBUF_INIT;
+	struct strbuf target = STRBUF_INIT;
+	int ret = 0;
+
+	if (!shared_files.nr)
+		return 0;
+	if (!repo_has_symlinks(the_repository))
+		return error(_("worktree.sharedFile requires symbolic link support"));
+
+	worktrees = get_worktrees_without_reading_head(the_repository);
+	if (worktrees[0]->is_bare) {
+		warning(_("not linking shared files: primary worktree is bare"));
+		goto done;
+	}
+
+	strbuf_realpath(&primary, worktrees[0]->path, 1);
+	strbuf_realpath(&target, path, 1);
+
+	for_each_string_list_item(item, &shared_files) {
+		struct stat st;
+
+		strbuf_reset(&source);
+		strbuf_addf(&source, "%s/%s", primary.buf, item->string);
+		if (lstat(source.buf, &st)) {
+			warning_errno(_("not linking shared file '%s'"), source.buf);
+			continue;
+		}
+		if (!S_ISREG(st.st_mode)) {
+			warning(_("not linking shared file '%s': not a regular file"),
+				source.buf);
+			continue;
+		}
+
+		strbuf_reset(&destination);
+		strbuf_addf(&destination, "%s/%s", target.buf, item->string);
+		if (!lstat(destination.buf, &st)) {
+			warning(_("not linking shared file '%s': destination already exists"),
+				item->string);
+			continue;
+		} else if (errno != ENOENT) {
+			ret = error_errno(_("could not inspect shared file destination '%s'"),
+					  destination.buf);
+			goto done;
+		}
+
+		if (symlink(source.buf, destination.buf)) {
+			ret = error_errno(_("could not link shared file '%s'"),
+					  destination.buf);
+			goto done;
+		}
+	}
+
+done:
+	free_worktrees(worktrees);
+	strbuf_release(&source);
+	strbuf_release(&destination);
+	strbuf_release(&primary);
+	strbuf_release(&target);
+	return ret;
+}
+
 static int add_worktree(const char *path, const char *refname,
 			const struct add_opts *opts)
 {
@@ -591,6 +667,8 @@ static int add_worktree(const char *path, const char *refname,
 	if (opts->checkout &&
 	    (ret = checkout_worktree(opts, &child_env)))
 		goto done;
+	if ((ret = link_shared_files(path)))
+		goto done;
 
 	is_junk = 0;
 	FREE_AND_NULL(junk_work_tree);
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index bdcca97633..0aa426bbac 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -1288,4 +1288,99 @@ test_expect_success 'relative worktree sets extension config' '
 	test_cmp_config -C repo true extensions.relativeworktrees
 '
 
+test_expect_success SYMLINKS 'worktree.sharedFile links files from the primary worktree' '
+	test_when_finished "git worktree remove --force shared-file-wt" &&
+	echo shared-secret >.env &&
+	echo local-secret >.env.local &&
+	test_config worktree.sharedFile .env &&
+	git config --add worktree.sharedFile .env.local &&
+	git worktree add --detach shared-file-wt &&
+	test -L shared-file-wt/.env &&
+	test -L shared-file-wt/.env.local &&
+	test "$(readlink shared-file-wt/.env)" = "$(pwd)/.env" &&
+	test_cmp .env shared-file-wt/.env &&
+	test_cmp .env.local shared-file-wt/.env.local
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile uses the primary worktree as source' '
+	test_when_finished "git worktree remove --force shared-source-wt" &&
+	test_when_finished "git worktree remove --force existing-shared-source" &&
+	git worktree add --detach existing-shared-source &&
+	echo primary >.env &&
+	echo linked >existing-shared-source/.env &&
+	test_config worktree.sharedFile .env &&
+	git -C existing-shared-source worktree add --detach ../shared-source-wt &&
+	echo primary >expect &&
+	test_cmp expect shared-source-wt/.env
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile is available to post-checkout hook' '
+	test_when_finished "git worktree remove --force shared-hook-wt" &&
+	test_when_finished "rm -rf .git/hooks" &&
+	echo hook-secret >.env &&
+	test_config worktree.sharedFile .env &&
+	mkdir .git/hooks &&
+	test_hook post-checkout <<-\EOF &&
+	cat .env >hook-env
+	EOF
+	git worktree add --detach shared-hook-wt &&
+	test_cmp .env shared-hook-wt/hook-env
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile rejects paths outside primary worktree' '
+	test_config worktree.sharedFile ../.env &&
+	test_must_fail git worktree add --detach invalid-shared-file 2>err &&
+	test_grep "invalid value for worktree.sharedFile: ../.env" err &&
+	test_path_is_missing invalid-shared-file
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile rejects nested paths' '
+	test_config worktree.sharedFile nested/.env &&
+	test_must_fail git worktree add --detach nested-shared-file 2>err &&
+	test_grep "invalid value for worktree.sharedFile: nested/.env" err &&
+	test_path_is_missing nested-shared-file
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile does not follow source symlinks' '
+	test_when_finished "git worktree remove --force symlink-source-wt" &&
+	test_when_finished "rm -f ../outside-shared-file shared-symlink" &&
+	echo outside >../outside-shared-file &&
+	ln -s ../outside-shared-file shared-symlink &&
+	test_config worktree.sharedFile shared-symlink &&
+	git worktree add --detach symlink-source-wt 2>err &&
+	test_grep "not a regular file" err &&
+	test_path_is_missing symlink-source-wt/shared-symlink
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile requires symlink support' '
+	echo shared-secret >.env &&
+	test_config worktree.sharedFile .env &&
+	test_config core.symlinks false &&
+	test_must_fail git worktree add --detach no-symlink-wt 2>err &&
+	test_grep "worktree.sharedFile requires symbolic link support" err &&
+	test_path_is_missing no-symlink-wt
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile works with --no-checkout' '
+	test_when_finished "git worktree remove --force no-checkout-shared-wt" &&
+	echo shared-secret >.env &&
+	test_config worktree.sharedFile .env &&
+	git worktree add --detach --no-checkout no-checkout-shared-wt &&
+	test -L no-checkout-shared-wt/.env &&
+	test_cmp .env no-checkout-shared-wt/.env
+'
+
+test_expect_success SYMLINKS 'worktree.sharedFile does not replace checkout files' '
+	test_when_finished "git worktree remove --force tracked-shared-file" &&
+	echo tracked >shared &&
+	git add shared &&
+	git commit -m shared-file &&
+	echo primary-only >shared &&
+	test_config worktree.sharedFile shared &&
+	git worktree add --detach tracked-shared-file 2>err &&
+	test_grep "not linking shared file.*already exists" err &&
+	test_path_is_file tracked-shared-file/shared &&
+	test ! -L tracked-shared-file/shared
+'
+
 test_done

base-commit: c46c1e37724f0478939de636ab8ea5a89086d532
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] worktree: allow sharing root files with linked worktrees
  2026-11-26 18:25 [PATCH] worktree: allow sharing root files with linked worktrees Bhushan Gaikwad via GitGitGadget
@ 2026-11-30 16:08 ` D. Ben Knoble
  0 siblings, 0 replies; 2+ messages in thread
From: D. Ben Knoble @ 2026-11-30 16:08 UTC (permalink / raw)
  To: Bhushan Gaikwad via GitGitGadget; +Cc: git, Bhushan Gaikwad

See also https://lore.kernel.org/git/CAA8YNQAsyUe275DLPRnB4dym5aq9ZyemA39u+HJciKzat5tNQA@mail.gmail.com/,
perhaps?

On Thu, Nov 26, 2026 at 1:25 PM Bhushan Gaikwad via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Bhushan Gaikwad <bhushan@alfredscholar.com>
>
> Ignored configuration files such as .env often need to remain identical
> across linked worktrees. Today users must copy them manually or install a
> post-checkout hook, which is easy to forget and can create divergent local
> configuration.
>
> Add a multi-valued worktree.sharedFile setting. For each configured
> root-level regular file, worktree add creates an absolute symbolic link
> from the new worktree to the primary worktree before running the
> post-checkout hook.
>
> Reject nested paths and source symlinks, avoid replacing checkout files,
> and fail cleanly when symbolic links are unavailable.

The limitations and design choices seem odd to me:

• On what platforms do we not support symlinks? Wikipedia suggests
Windows has equivalents, but our mingw compatibility layer says they
are disabled unless MSYS contains winsymlinks:nativestrict, and of
course I don't know what other platforms do to customize the build.
• Why reject nested paths? A project at work has api/.env{,.example}
which might benefit.
• Why use absolute symlinks? I suppose that might be annoying when a
directory might have a different absolute path on different machines
(due to NFS or something)? Is it a security thing, something about
relative symlinks becoming a nice target for some kind of exploit?

I wonder if instead we could just copy the paths, which lets them
diverge (in case a worktree wants differing settings!), though at risk
of forgetting the divergence in a long-lived worktree?

I am somewhat negative on making Git do this automatically,
personally, especially since the example ".env" is typically ignored
(i.e., not managed by Git at all). But that's just my opinion, so if
the design is fleshed out and others are positive, ok.

-- 
D. Ben Knoble

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 16:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 18:25 [PATCH] worktree: allow sharing root files with linked worktrees Bhushan Gaikwad via GitGitGadget
2026-11-30 16:08 ` D. Ben Knoble

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox