Signed-off-by: Stefan Beller <sbel...@google.com>
---
 builtin/submodule--helper.c      | 24 ++++++++++++++++++++++--
 t/t5526-fetch-submodules.sh      |  2 +-
 t/t7406-submodule-update.sh      |  8 ++++++++
 t/t7410-submodule-checkout-to.sh |  2 +-
 4 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 36de64902ec..3aa385bce5c 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1515,6 +1515,7 @@ struct update_clone_data {
        struct object_id oid;
        unsigned just_cloned;
        unsigned retried;
+       unsigned cleanup_url;
 };
 
 struct submodule_update_clone {
@@ -1590,7 +1591,7 @@ static int prepare_to_clone_next_submodule(const struct 
cache_entry *ce,
        struct strbuf displaypath_sb = STRBUF_INIT;
        struct strbuf sb = STRBUF_INIT;
        const char *displaypath = NULL;
-       int needs_cloning = 0;
+       int needs_cloning = 0, active;
 
        if (ce_stage(ce)) {
                if (suc->recursive_prefix)
@@ -1632,7 +1633,8 @@ static int prepare_to_clone_next_submodule(const struct 
cache_entry *ce,
        }
 
        /* Check if the submodule has been initialized. */
-       if (!is_submodule_active(the_repository, ce->name)) {
+       active = is_submodule_active(the_repository, ce->name);
+       if (!active) {
                next_submodule_warn_missing(suc, out, displaypath);
                goto cleanup;
        }
@@ -1653,6 +1655,8 @@ static int prepare_to_clone_next_submodule(const struct 
cache_entry *ce,
        suc->update_clone[suc->update_clone_nr].sub = sub;
        suc->update_clone[suc->update_clone_nr].retried = 0;
        suc->update_clone[suc->update_clone_nr].ce = ce;
+       suc->update_clone[suc->update_clone_nr].cleanup_url =
+               (active != SUBMODULE_ACTIVE_VIA_URL);
        suc->update_clone_nr++;
 
        if (!needs_cloning)
@@ -1801,6 +1805,22 @@ static int git_update_clone_config(const char *var, 
const char *value,
 
 static void update_submodule(struct update_clone_data *ucd)
 {
+       if (ucd->cleanup_url) {
+               struct strbuf cfg = STRBUF_INIT;
+               struct strbuf submodule_url = STRBUF_INIT;
+               int r;
+
+               strbuf_addf(&submodule_url, "submodule.%s.url", ucd->sub->name);
+               strbuf_repo_git_path(&cfg, the_repository, "config");
+
+               r = git_config_set_in_file_gently(cfg.buf, submodule_url.buf, 
NULL);
+               if (r && r != CONFIG_NOTHING_SET)
+                       die(_("failed to remove '%s'"), submodule_url.buf);
+
+               strbuf_release(&cfg);
+               strbuf_release(&submodule_url);
+       }
+
        fprintf(stdout, "dummy %s %d\t%s\n",
                oid_to_hex(&ucd->oid),
                ucd->just_cloned,
diff --git a/t/t5526-fetch-submodules.sh b/t/t5526-fetch-submodules.sh
index 0f730d77815..cd1bd131b59 100755
--- a/t/t5526-fetch-submodules.sh
+++ b/t/t5526-fetch-submodules.sh
@@ -508,7 +508,7 @@ test_expect_success "'fetch.recurseSubmodules=on-demand' 
works also without .git
                git config -f .gitmodules submodule.fake.path fake &&
                git config -f .gitmodules submodule.fake.url fakeurl &&
                git add .gitmodules &&
-               git config --unset submodule.submodule.url &&
+               test_might_fail git config --unset submodule.submodule.url &&
                git fetch >../actual.out 2>../actual.err &&
                # cleanup
                git config --unset fetch.recurseSubmodules &&
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index f604ef7a729..f581fea28e0 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -84,6 +84,14 @@ test_expect_success 'submodule update detaching the HEAD ' '
        )
 '
 
+test_expect_success 'active submodule leaves no URL config in superproject' '
+       # relies on previous test
+       (
+               cd super &&
+               test_must_fail git config -f .git/config submodule.submodule.url
+       )
+'
+
 test_expect_success 'submodule update from subdirectory' '
        (cd super/submodule &&
         git reset --hard HEAD~1
diff --git a/t/t7410-submodule-checkout-to.sh b/t/t7410-submodule-checkout-to.sh
index f1b492ebc46..683e957934b 100755
--- a/t/t7410-submodule-checkout-to.sh
+++ b/t/t7410-submodule-checkout-to.sh
@@ -55,7 +55,7 @@ test_expect_failure 'can see submodule diffs just after 
checkout' '
 test_expect_success 'checkout main and initialize independent clones' '
        mkdir fully_cloned_submodule &&
        git -C clone/main worktree add "$base_path/fully_cloned_submodule/main" 
"$rev1_hash_main" &&
-       git -C fully_cloned_submodule/main submodule update
+       git -C fully_cloned_submodule/main submodule update --init
 '
 
 test_expect_success 'can see submodule diffs after independent cloning' '
-- 
2.18.0.265.g16de1b435c9.dirty

Reply via email to