Commit 8c0f59ed authored by Duy Nguyen's avatar Duy Nguyen

submodule add: support multiple worktrees

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

If you have multiple worktrees without extensions.worktreeConfig(),
you'll face this message:

fatal: submodules cannot be used with multiple working trees unless the
config extension worktreeConfig is enabled. Please read "CONFIGURATION
FILE" section in "git help worktree" for details
parent 790bacd8
......@@ -19,6 +19,7 @@
#include "diffcore.h"
#include "diff.h"
#include "object-store.h"
#include "worktree.h"
#define OPT_QUIET (1 << 0)
#define OPT_CACHED (1 << 1)
......@@ -28,6 +29,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;
......@@ -2148,7 +2161,8 @@ static int module_config(int argc, const char **argv, const char *prefix)
{
enum {
CHECK_WRITEABLE = 1,
DO_UNSET = 2
CHECK_WORKTREE_SETUP,
DO_UNSET
} command = 0;
struct option module_config_options[] = {
......@@ -2158,6 +2172,9 @@ static int module_config(int argc, const char **argv, const char *prefix)
OPT_CMDMODE(0, "unset", &command,
N_("unset the config in the .gitmodules file"),
DO_UNSET),
OPT_CMDMODE(0, "check-worktree-setup", &command,
N_("check if the current working tree setup is supported"),
CHECK_WORKTREE_SETUP),
OPT_END()
};
const char *const git_submodule_helper_usage[] = {
......@@ -2170,8 +2187,14 @@ 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) {
return is_writing_gitmodules_ok() ? 0 : -1;
}
if (argc == 1 && command == CHECK_WORKTREE_SETUP) {
check_multi_worktree_support();
return 0;
}
/* Equivalent to ACTION_GET in builtin/config.c */
if (argc == 2 && command != DO_UNSET)
......
......@@ -166,6 +166,8 @@ cmd_add()
shift
done
git submodule--helper config --check-worktree-setup || exit 1
if ! git submodule--helper config --check-writeable >/dev/null 2>&1
then
die "$(eval_gettext "please make sure that the .gitmodules file is in the working tree")"
......@@ -290,7 +292,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'")"
......@@ -304,18 +306,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
}
......
#!/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_expect_success 'add submodules on unsupported setup' '
test_when_finished git config extensions.worktreeConfig true &&
test_unconfig extensions.worktreeConfig &&
SRC="$(pwd)/submodsrc" &&
test_must_fail git submodule add "$SRC" sub2
'
test_done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment