diff mbox series

[03/10] submodule add: support multiple worktrees

Message ID 20190116103159.9305-4-pclouds@gmail.com (mailing list archive)
State New, archived
Headers show
Series Support using submodules with worktrees | expand

Commit Message

Duy Nguyen Jan. 16, 2019, 10:31 a.m. UTC
The entire submodule section in the superproject will be per-worktree
and written to $GIT_DIR/config.worktree.

The behavior when you only have one worktree (and not enabled
extensions.worktreeConfig) is the same as before, everything is still
written in $GIT_DIR/config

'submodule-helper config --check-writable' also checks if it's
supported worktree configuration.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/submodule--helper.c    | 17 +++++++++++++-
 git-submodule.sh               |  8 +++----
 t/t2405-worktree-submodules.sh | 42 ++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 6 deletions(-)
 create mode 100755 t/t2405-worktree-submodules.sh

Comments

Stefan Beller Jan. 16, 2019, 10:27 p.m. UTC | #1
> --- /dev/null
> +++ b/t/t2405-worktree-submodules.sh
> @@ -0,0 +1,42 @@
> +#!/bin/sh
> +
> +test_description='multiple worktrees as superprojects'
> +
> +. ./test-lib.sh
> +
> +test_expect_success 'set up submodule source' '
> +       test_create_repo submodsrc &&
> +       (
> +               cd submodsrc &&
> +               test_commit one
> +       ) &&

Just like git itself, test_commit now supports the -C <dir>
argument, so you could replace the whole subshell with

    test_commit -C submodsrc one &&


> +       test_commit initial &&
> +       git worktree add -b secondary secondary HEAD &&

Could we have multiple things not named the same?
(i.e. have the the branch and worktree be spelled differently)
That way it is less confusing and we'd catch errors of swapping
these two in the implementation (unlikely, but...)

> +       git config extensions.worktreeConfig true
> +'
> +
> +test_expect_success 'add submodules' '
> +       SRC="$(pwd)/submodsrc" &&
> +       git submodule add "$SRC" sub1 &&

you can also use relative paths in submodules:

    git submodule add ./submodsrc sub1 &&

if you want to.

> +       git commit -m sub1 &&
> +       git -C secondary submodule add "$SRC" sub2 &&

Oh never mind, by having the absolute path we
add the submodule with the same setting.
(When using relative path we'd have to use ../submodusrc
here, but would that matter?)

> +       git -C secondary commit -m sub2 &&
> +
> +       git config --get-regexp "submodule.*" | sort >actual1 &&
> +       cat >expected1 <<-EOF &&
> +       submodule.sub1.active true
> +       submodule.sub1.url $(pwd)/submodsrc
> +       EOF
> +       test_cmp expected1 actual1 &&
> +       test -d .git/modules/sub1 &&
> +
> +       git -C secondary config --get-regexp "submodule.*" | sort >actual2 &&
> +       cat >expected2 <<-EOF &&
> +       submodule.sub2.active true
> +       submodule.sub2.url $(pwd)/submodsrc
> +       EOF
> +       test_cmp expected2 actual2 &&
> +       test -d .git/worktrees/secondary/modules/sub2

This section is very brittle. For example the way how submodules
are active changed not so long ago, and might change again, which
seems unrelated to the thing tested here?
diff mbox series

Patch

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 232bfaac7f..7b328ec060 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -18,6 +18,7 @@ 
 #include "diffcore.h"
 #include "diff.h"
 #include "object-store.h"
+#include "worktree.h"
 
 #define OPT_QUIET (1 << 0)
 #define OPT_CACHED (1 << 1)
@@ -27,6 +28,18 @@ 
 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
 				  void *cb_data);
 
+static void check_multi_worktree_support(void)
+{
+	char *worktree_config = get_worktree_config(the_repository);
+	if (!worktree_config)
+		die(_("submodules cannot be used with multiple "
+		      "working trees unless the config\n"
+		      "extension worktreeConfig is enabled. "
+		      "Please read \"CONFIGURATION FILE\"\n"
+		      "section in \"git help worktree\" for details"));
+	free(worktree_config);
+}
+
 static char *get_default_remote(void)
 {
 	char *dest = NULL, *ret;
@@ -2162,8 +2175,10 @@  static int module_config(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, module_config_options,
 			     git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
 
-	if (argc == 1 && command == CHECK_WRITEABLE)
+	if (argc == 1 && command == CHECK_WRITEABLE) {
+		check_multi_worktree_support();
 		return is_writing_gitmodules_ok() ? 0 : -1;
+	}
 
 	/* Equivalent to ACTION_GET in builtin/config.c */
 	if (argc == 2)
diff --git a/git-submodule.sh b/git-submodule.sh
index 5e608f8bad..695939eff9 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -288,7 +288,7 @@  or you are unsure what this means choose another name with the '--name' option."
 			esac
 		) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 	fi
-	git config submodule."$sm_name".url "$realrepo"
+	git config --worktree submodule."$sm_name".url "$realrepo"
 
 	git add --no-warn-embedded-repo $force "$sm_path" ||
 	die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
@@ -302,18 +302,16 @@  or you are unsure what this means choose another name with the '--name' option."
 	git add --force .gitmodules ||
 	die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
 
-	# NEEDSWORK: In a multi-working-tree world, this needs to be
-	# set in the per-worktree config.
 	if git config --get submodule.active >/dev/null
 	then
 		# If the submodule being adding isn't already covered by the
 		# current configured pathspec, set the submodule's active flag
 		if ! git submodule--helper is-active "$sm_path"
 		then
-			git config submodule."$sm_name".active "true"
+			git config --worktree submodule."$sm_name".active "true"
 		fi
 	else
-		git config submodule."$sm_name".active "true"
+		git config --worktree submodule."$sm_name".active "true"
 	fi
 }
 
diff --git a/t/t2405-worktree-submodules.sh b/t/t2405-worktree-submodules.sh
new file mode 100755
index 0000000000..3ee5380e88
--- /dev/null
+++ b/t/t2405-worktree-submodules.sh
@@ -0,0 +1,42 @@ 
+#!/bin/sh
+
+test_description='multiple worktrees as superprojects'
+
+. ./test-lib.sh
+
+test_expect_success 'set up submodule source' '
+	test_create_repo submodsrc &&
+	(
+		cd submodsrc &&
+		test_commit one
+	) &&
+	test_commit initial &&
+	git worktree add -b secondary secondary HEAD &&
+	git config extensions.worktreeConfig true
+'
+
+test_expect_success 'add submodules' '
+	SRC="$(pwd)/submodsrc" &&
+	git submodule add "$SRC" sub1 &&
+	git commit -m sub1 &&
+	git -C secondary submodule add "$SRC" sub2 &&
+	git -C secondary commit -m sub2 &&
+
+	git config --get-regexp "submodule.*" | sort >actual1 &&
+	cat >expected1 <<-EOF &&
+	submodule.sub1.active true
+	submodule.sub1.url $(pwd)/submodsrc
+	EOF
+	test_cmp expected1 actual1 &&
+	test -d .git/modules/sub1 &&
+
+	git -C secondary config --get-regexp "submodule.*" | sort >actual2 &&
+	cat >expected2 <<-EOF &&
+	submodule.sub2.active true
+	submodule.sub2.url $(pwd)/submodsrc
+	EOF
+	test_cmp expected2 actual2 &&
+	test -d .git/worktrees/secondary/modules/sub2
+'
+
+test_done