submodule--helper: set alternateLocation for cloned submodules

In 31224cbdc7 (clone: recursive and reference option triggers submodule alternates, 2016-08-17) a mechanism was added to have submodules referenced. It did not address _nested_ submodules, however. This patch makes all not just the root repository, but also all submodules (recursively) have submodule.alternateLocation and submodule.alternateErrorStrategy configured, making Git search for possible alternates for nested submodules as well. As submodule's alternate target does not end in .git/objects (rather .git/modules/qqqqqq/objects), this alternate target path restriction for in add_possible_reference_from_superproject relates from "*.git/objects" to just */objects". New tests have been added to t7408-submodule-reference. Signed-off-by: Vitaly _Vi Shukela <vi0oss@gmail.com> Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Vitaly "_Vi" Shukela committed Dec 8, 2016 at 04:38 UTC bf03b790471d57d1bf2a6efca6e0c640c9f37d2e
2 files changed +83 -2
builtin/submodule--helper.c
+17 -2
@@ -498,9 +498,9 @@ static int add_possible_reference_from_superproject(
498
499 /*
500 * If the alternate object store is another repository, try the
501 - * standard layout with .git/modules/<name>/objects
501 + * standard layout with .git/(modules/<name>)+/objects
502 */
503 - if (ends_with(alt->path, ".git/objects")) {
503 + if (ends_with(alt->path, "/objects")) {
504 char *sm_alternate;
505 struct strbuf sb = STRBUF_INIT;
506 struct strbuf err = STRBUF_INIT;
@@ -583,6 +583,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
583 struct strbuf rel_path = STRBUF_INIT;
584 struct strbuf sb = STRBUF_INIT;
585 struct string_list reference = STRING_LIST_INIT_NODUP;
586 + char *sm_alternate = NULL, *error_strategy = NULL;
587
588 struct option module_clone_options[] = {
589 OPT_STRING(0, "prefix", &prefix,
@@ -672,6 +673,20 @@ static int module_clone(int argc, const char **argv, const char *prefix)
673 die(_("could not get submodule directory for '%s'"), path);
674 git_config_set_in_file(p, "core.worktree",
675 relative_path(path, sm_gitdir, &rel_path));
676 +
677 + /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
678 + git_config_get_string("submodule.alternateLocation", &sm_alternate);
679 + if (sm_alternate)
680 + git_config_set_in_file(p, "submodule.alternateLocation",
681 + sm_alternate);
682 + git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
683 + if (error_strategy)
684 + git_config_set_in_file(p, "submodule.alternateErrorStrategy",
685 + error_strategy);
686 +
687 + free(sm_alternate);
688 + free(error_strategy);
689 +
690 strbuf_release(&sb);
691 strbuf_release(&rel_path);
692 free(sm_gitdir);
t/t7408-submodule-reference.sh
+66
@@ -125,4 +125,70 @@ test_expect_success 'ignoring missing submodule alternates passes clone and subm
125 )
126 '
127
128 +test_expect_success 'preparing second superproject with a nested submodule plus partial clone' '
129 + test_create_repo supersuper &&
130 + (
131 + cd supersuper &&
132 + echo "I am super super." >file &&
133 + git add file &&
134 + git commit -m B-super-super-initial
135 + git submodule add "file://$base_dir/super" subwithsub &&
136 + git commit -m B-super-super-added &&
137 + git submodule update --init --recursive &&
138 + git repack -ad
139 + ) &&
140 + git clone supersuper supersuper2 &&
141 + (
142 + cd supersuper2 &&
143 + git submodule update --init
144 + )
145 +'
146 +
147 +# At this point there are three root-level positories: A, B, super and super2
148 +
149 +test_expect_success 'nested submodule alternate in works and is actually used' '
150 + test_when_finished "rm -rf supersuper-clone" &&
151 + git clone --recursive --reference supersuper supersuper supersuper-clone &&
152 + (
153 + cd supersuper-clone &&
154 + # test superproject has alternates setup correctly
155 + test_alternate_is_used .git/objects/info/alternates . &&
156 + # immediate submodule has alternate:
157 + test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
158 + # nested submodule also has alternate:
159 + test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub
160 + )
161 +'
162 +
163 +check_that_two_of_three_alternates_are_used() {
164 + test_alternate_is_used .git/objects/info/alternates . &&
165 + # immediate submodule has alternate:
166 + test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
167 + # but nested submodule has no alternate:
168 + test_must_fail test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub
169 +}
170 +
171 +
172 +test_expect_success 'missing nested submodule alternate fails clone and submodule update' '
173 + test_when_finished "rm -rf supersuper-clone" &&
174 + test_must_fail git clone --recursive --reference supersuper2 supersuper2 supersuper-clone &&
175 + (
176 + cd supersuper-clone &&
177 + check_that_two_of_three_alternates_are_used &&
178 + # update of the submodule fails
179 + test_must_fail git submodule update --init --recursive
180 + )
181 +'
182 +
183 +test_expect_success 'missing nested submodule alternate in --reference-if-able mode' '
184 + test_when_finished "rm -rf supersuper-clone" &&
185 + git clone --recursive --reference-if-able supersuper2 supersuper2 supersuper-clone &&
186 + (
187 + cd supersuper-clone &&
188 + check_that_two_of_three_alternates_are_used &&
189 + # update of the submodule succeeds
190 + git submodule update --init --recursive
191 + )
192 +'
193 +
194 test_done