diff mbox series

[RFC,3/4] submodule: cache superproject gitdir during absorbgitdirs

Message ID 20210611225428.1208973-4-emilyshaffer@google.com (mailing list archive)
State Superseded
Headers show
Series cache parent project's gitdir in submodules | expand

Commit Message

Emily Shaffer June 11, 2021, 10:54 p.m. UTC
Already during 'git submodule add' we cache a pointer to the
superproject's gitdir. However, this doesn't help brand-new
submodules created with 'git init' and later absorbed with 'git
submodule absorbgitdir'. Let's start adding that pointer during 'git
submodule absorbgitdir' too.

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
---
 submodule.c                        | 10 ++++++++++
 t/t7412-submodule-absorbgitdirs.sh |  1 +
 2 files changed, 11 insertions(+)

Comments

Junio C Hamano June 14, 2021, 6:18 a.m. UTC | #1
Emily Shaffer <emilyshaffer@google.com> writes:

> +	test . -ef "$(git -C sub1 config submodule.superprojectGitDir)" &&

Unfortunately "test f1 -ef f2", "test f1 -nt f2", etc. are not part
of POSIX.

When in doubt, check

  https://pubs.opengroup.org/onlinepubs/9699919799/utilities/toc.html

Thanks.
diff mbox series

Patch

diff --git a/submodule.c b/submodule.c
index 9767ba9893..09dfc4ee38 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2064,6 +2064,7 @@  static void relocate_single_git_dir_into_superproject(const char *path)
 	char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
 	char *new_git_dir;
 	const struct submodule *sub;
+	struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT;
 
 	if (submodule_uses_worktrees(path))
 		die(_("relocate_gitdir for submodule '%s' with "
@@ -2095,6 +2096,15 @@  static void relocate_single_git_dir_into_superproject(const char *path)
 
 	relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
 
+	/* cache pointer to superproject's gitdir */
+	/* NEEDSWORK: this may differ if experimental.worktreeConfig is enabled */
+	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+	git_config_set_in_file(config_path.buf, "submodule.superprojectGitdir",
+			       relative_path(get_super_prefix_or_empty(),
+					     path, &sb));
+
+	strbuf_release(&config_path);
+	strbuf_release(&sb);
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
diff --git a/t/t7412-submodule-absorbgitdirs.sh b/t/t7412-submodule-absorbgitdirs.sh
index 1cfa150768..70fc282937 100755
--- a/t/t7412-submodule-absorbgitdirs.sh
+++ b/t/t7412-submodule-absorbgitdirs.sh
@@ -29,6 +29,7 @@  test_expect_success 'absorb the git dir' '
 	test -d .git/modules/sub1 &&
 	git status >actual.1 &&
 	git -C sub1 rev-parse HEAD >actual.2 &&
+	test . -ef "$(git -C sub1 config submodule.superprojectGitDir)" &&
 	test_cmp expect.1 actual.1 &&
 	test_cmp expect.2 actual.2
 '