{"thread":{"id":"44680","subject":"[PATCH] submodule--helper: set alternateLocation for cloned submodules","startedAt":"2016-12-12T02:46:26Z","lastAt":"2016-12-12T02:46:26Z","messageCount":1,"participants":["vi0oss@gmail.com"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"307484","messageId":"20161212024555.27868-1-vi0oss@gmail.com","threadId":"44680","inReplyTo":null,"subject":"[PATCH] submodule--helper: set alternateLocation for cloned submodules","fromName":"","fromEmail":"vi0oss@gmail.com","sentAt":"2016-12-12T02:45:55Z","receivedAt":"2016-12-12T02:46:26Z","isPatch":true,"sender":{"key":"vi0oss@gmail.com","avatar":"https://avatars.githubusercontent.com/u/173219?v=4"},"body":"From: \"Vitaly \\\"_Vi\\\" Shukela\" <vi0oss@gmail.com>\n\nIn 31224cbdc7 (clone: recursive and reference option triggers\nsubmodule alternates, 2016-08-17) a mechanism was added to\nhave submodules referenced.  It did not address _nested_\nsubmodules, however.\n\nThis patch makes all not just the root repository, but also\nall submodules (recursively) have submodule.alternateLocation\nand submodule.alternateErrorStrategy configured, making Git\nsearch for possible alternates for nested submodules as well.\n\nAs submodule's alternate target does not end in .git/objects\n(rather .git/modules/qqqqqq/objects), this alternate target\npath restriction for in add_possible_reference_from_superproject\nrelates from \"*.git/objects\" to just */objects\".\n\nNew tests have been added to t7408-submodule-reference.\n\nSigned-off-by: Vitaly _Vi Shukela <vi0oss@gmail.com>\n---\n\nNotes:\n    Reviewed by Stefan Beller <sbeller@google.com>\n    \n    For \"maint\" branch.\n\n builtin/submodule--helper.c    | 19 ++++++++++--\n t/t7408-submodule-reference.sh | 66 ++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 83 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex 4beeda5..92fd676 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -498,9 +498,9 @@ static int add_possible_reference_from_superproject(\n \n \t/*\n \t * If the alternate object store is another repository, try the\n-\t * standard layout with .git/modules/<name>/objects\n+\t * standard layout with .git/(modules/<name>)+/objects\n \t */\n-\tif (ends_with(alt->path, \".git/objects\")) {\n+\tif (ends_with(alt->path, \"/objects\")) {\n \t\tchar *sm_alternate;\n \t\tstruct strbuf sb = STRBUF_INIT;\n \t\tstruct strbuf err = STRBUF_INIT;\n@@ -583,6 +583,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)\n \tstruct strbuf rel_path = STRBUF_INIT;\n \tstruct strbuf sb = STRBUF_INIT;\n \tstruct string_list reference = STRING_LIST_INIT_NODUP;\n+\tchar *sm_alternate = NULL, *error_strategy = NULL;\n \n \tstruct option module_clone_options[] = {\n \t\tOPT_STRING(0, \"prefix\", &prefix,\n@@ -672,6 +673,20 @@ static int module_clone(int argc, const char **argv, const char *prefix)\n \t\tdie(_(\"could not get submodule directory for '%s'\"), path);\n \tgit_config_set_in_file(p, \"core.worktree\",\n \t\t\t       relative_path(path, sm_gitdir, &rel_path));\n+\n+\t/* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */\n+\tgit_config_get_string(\"submodule.alternateLocation\", &sm_alternate);\n+\tif (sm_alternate)\n+\t\tgit_config_set_in_file(p, \"submodule.alternateLocation\",\n+\t\t\t\t\t   sm_alternate);\n+\tgit_config_get_string(\"submodule.alternateErrorStrategy\", &error_strategy);\n+\tif (error_strategy)\n+\t\tgit_config_set_in_file(p, \"submodule.alternateErrorStrategy\",\n+\t\t\t\t\t   error_strategy);\n+\n+\tfree(sm_alternate);\n+\tfree(error_strategy);\n+\n \tstrbuf_release(&sb);\n \tstrbuf_release(&rel_path);\n \tfree(sm_gitdir);\ndiff --git a/t/t7408-submodule-reference.sh b/t/t7408-submodule-reference.sh\nindex 1c1e289..e159fc5 100755\n--- a/t/t7408-submodule-reference.sh\n+++ b/t/t7408-submodule-reference.sh\n@@ -125,4 +125,70 @@ test_expect_success 'ignoring missing submodule alternates passes clone and subm\n \t)\n '\n \n+test_expect_success 'preparing second superproject with a nested submodule plus partial clone' '\n+\ttest_create_repo supersuper &&\n+\t(\n+\t\tcd supersuper &&\n+\t\techo \"I am super super.\" >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m B-super-super-initial\n+\t\tgit submodule add \"file://$base_dir/super\" subwithsub &&\n+\t\tgit commit -m B-super-super-added &&\n+\t\tgit submodule update --init --recursive &&\n+\t\tgit repack -ad\n+\t) &&\n+\tgit clone supersuper supersuper2 &&\n+\t(\n+\t\tcd supersuper2 &&\n+\t\tgit submodule update --init\n+\t)\n+'\n+\n+# At this point there are three root-level positories: A, B, super and super2\n+\n+test_expect_success 'nested submodule alternate in works and is actually used' '\n+\ttest_when_finished \"rm -rf supersuper-clone\" &&\n+\tgit clone --recursive --reference supersuper supersuper supersuper-clone &&\n+\t(\n+\t\tcd supersuper-clone &&\n+\t\t# test superproject has alternates setup correctly\n+\t\ttest_alternate_is_used .git/objects/info/alternates . &&\n+\t\t# immediate submodule has alternate:\n+\t\ttest_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&\n+\t\t# nested submodule also has alternate:\n+\t\ttest_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub\n+\t)\n+'\n+\n+check_that_two_of_three_alternates_are_used() {\n+\ttest_alternate_is_used .git/objects/info/alternates . &&\n+\t# immediate submodule has alternate:\n+\ttest_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&\n+\t# but nested submodule has no alternate:\n+\ttest_must_fail test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub\n+}\n+\n+\n+test_expect_success 'missing nested submodule alternate fails clone and submodule update' '\n+\ttest_when_finished \"rm -rf supersuper-clone\" &&\n+\ttest_must_fail git clone --recursive --reference supersuper2 supersuper2 supersuper-clone &&\n+\t(\n+\t\tcd supersuper-clone &&\n+\t\tcheck_that_two_of_three_alternates_are_used &&\n+\t\t# update of the submodule fails\n+\t\ttest_must_fail git submodule update --init --recursive\n+\t)\n+'\n+\n+test_expect_success 'missing nested submodule alternate in --reference-if-able mode' '\n+\ttest_when_finished \"rm -rf supersuper-clone\" &&\n+\tgit clone --recursive --reference-if-able supersuper2 supersuper2 supersuper-clone &&\n+\t(\n+\t\tcd supersuper-clone &&\n+\t\tcheck_that_two_of_three_alternates_are_used &&\n+\t\t# update of the submodule succeeds\n+\t\tgit submodule update --init --recursive\n+\t)\n+'\n+\n test_done\n-- \n2.10.2\n\n"}]}