{"thread":{"id":"66979","subject":"[PATCH] unpack-trees: reject missing submodules early","startedAt":"2026-12-03T09:32:10Z","lastAt":"2026-12-03T09:32:10Z","messageCount":1,"participants":["zibuyu"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559954","messageId":"20261203093210.3341204-1-zibuyu423@gmail.com","threadId":"66979","inReplyTo":null,"subject":"[PATCH] unpack-trees: reject missing submodules early","fromName":"zibuyu","fromEmail":"zibuyu423@gmail.com","sentAt":"2026-12-03T09:32:10Z","receivedAt":"2026-12-03T09:32:10Z","isPatch":true,"sender":{"key":"zibuyu423@gmail.com","avatar":null},"body":"A recursive clone can lack a submodule that was removed before the\ncloned tip. Checking out an older commit with --recurse-submodules\nthen writes its .gitmodules file before discovering that the submodule\nrepository is missing. HEAD still points to the original commit, but\nthe failed checkout leaves changes in the worktree.\n\nCheck that each active gitlink scheduled for checkout has a local\nrepository before unpacking files. Use the incoming .gitmodules\nmapping when it will replace the current one. Keep the old mapping\navailable for removing submodules from the previous checkout.\n\nTell the user how to check out the commit without recursion and then\ninitialize its submodules. Exercise both gitdir layouts and a clone\nwhose active pathspec excludes the missing submodules.\n\nSigned-off-by: zibuyu <zibuyu423@gmail.com>\n---\n Documentation/git-checkout.adoc |  5 +++\n submodule-config.c              | 49 ++++++++++++++++++++++++++\n submodule-config.h              |  3 ++\n submodule.c                     | 61 +++++++++++++++++++++++++--------\n submodule.h                     |  2 ++\n t/t2013-checkout-submodule.sh   | 30 ++++++++++++++++\n unpack-trees.c                  |  4 +++\n 7 files changed, 140 insertions(+), 14 deletions(-)\n\ndiff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc\nindex a8b3b8c2e2..56bd69db0a 100644\n--- a/Documentation/git-checkout.adoc\n+++ b/Documentation/git-checkout.adoc\n@@ -308,6 +308,11 @@ include::diff-context-options.adoc[]\n \tis used, submodules working trees will not be updated.\n \tJust like linkgit:git-submodule[1], this will detach `HEAD` of the\n \tsubmodule.\n++\n+When switching to a commit that adds an active submodule, checkout needs\n+the submodule's repository to be available locally. If it is missing,\n+retry with `--no-recurse-submodules`, then run\n+`git submodule update --init --recursive` to populate the submodules.\n \n `--overlay`::\n `--no-overlay`::\ndiff --git a/submodule-config.c b/submodule-config.c\nindex 37c3be377b..a61899b5b7 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -15,6 +15,7 @@\n #include \"odb.h\"\n #include \"odb/source.h\"\n #include \"parse-options.h\"\n+#include \"read-cache.h\"\n #include \"thread-utils.h\"\n #include \"tree-walk.h\"\n #include \"url.h\"\n@@ -878,6 +879,54 @@ const struct submodule *submodule_from_path(struct repository *r,\n \treturn config_from(r, r->submodule_cache, treeish_name, path, lookup_path);\n }\n \n+int verify_submodules_available(struct index_state *istate)\n+{\n+\tstruct repository *repo = istate->repo;\n+\tstruct submodule_cache cache = { 0 };\n+\tconst struct object_id *gitmodules_oid = NULL;\n+\tconst struct object_id *worktree = null_oid(repo->hash_algo);\n+\tint pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));\n+\tint ret = 0;\n+\n+\tif (pos >= 0 && (istate->cache[pos]->ce_flags & CE_UPDATE))\n+\t\tgitmodules_oid = &istate->cache[pos]->oid;\n+\n+\tfor (unsigned int i = 0; i < istate->cache_nr; i++) {\n+\t\tconst struct cache_entry *ce = istate->cache[i];\n+\t\tconst struct submodule *sub;\n+\n+\t\tif (!(ce->ce_flags & CE_UPDATE) || !S_ISGITLINK(ce->ce_mode))\n+\t\t\tcontinue;\n+\t\t/*\n+\t\t * Read the incoming mapping without replacing the mapping\n+\t\t * that check_updates() still needs to remove old submodules.\n+\t\t */\n+\t\tif (gitmodules_oid && !cache.initialized) {\n+\t\t\tstruct parse_config_parameter data = {\n+\t\t\t\t.cache = &cache,\n+\t\t\t\t.gitmodules_oid = worktree,\n+\t\t\t\t.overwrite = 1,\n+\t\t\t};\n+\t\t\tsubmodule_cache_init(&cache);\n+\t\t\tret = git_config_from_blob_oid(parse_config, GITMODULES_FILE,\n+\t\t\t\t\t\t       repo, gitmodules_oid, &data,\n+\t\t\t\t\t\t       CONFIG_SCOPE_SUBMODULE);\n+\t\t\tif (ret < 0)\n+\t\t\t\tbreak;\n+\t\t}\n+\t\tif (gitmodules_oid)\n+\t\t\tsub = cache_lookup_path(&cache, worktree, ce->name);\n+\t\telse\n+\t\t\tsub = submodule_from_path(repo, worktree, ce->name);\n+\t\tif (sub && check_submodule_available(repo, sub)) {\n+\t\t\tret = -1;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\tsubmodule_cache_clear(&cache);\n+\treturn ret;\n+}\n+\n /**\n  * Used internally by submodules_of_tree(). Recurses into 'treeish_name'\n  * and appends submodule entries to 'out'. The submodule_cache expects\ndiff --git a/submodule-config.h b/submodule-config.h\nindex 755570d5d1..5351c7b4f3 100644\n--- a/submodule-config.h\n+++ b/submodule-config.h\n@@ -75,6 +75,9 @@ const struct submodule *submodule_from_path(struct repository *r,\n \t\t\t\t\t    const struct object_id *commit_or_tree,\n \t\t\t\t\t    const char *path);\n \n+/* Check active gitlinks before writing any pending worktree updates. */\n+int verify_submodules_available(struct index_state *istate);\n+\n /**\n  * Use these to free the internally cached values.\n  */\ndiff --git a/submodule.c b/submodule.c\nindex 6fcb606f7e..c1e4758544 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -236,17 +236,13 @@ int option_parse_recurse_submodules_worktree_updater(const struct option *opt,\n  * ie, the config looks like: \"[submodule] active\\n\".\n  * Since that is an invalid pathspec, we should inform the user.\n  */\n-int is_tree_submodule_active(struct repository *repo,\n-\t\t\t     const struct object_id *treeish_name,\n-\t\t\t     const char *path)\n+static int is_submodule_active_config(struct repository *repo,\n+\t\t\t\t      const struct submodule *module)\n {\n \tint ret = 0;\n \tchar *key = NULL;\n \tchar *value = NULL;\n \tconst struct string_list *sl;\n-\tconst struct submodule *module;\n-\n-\tmodule = submodule_from_path(repo, treeish_name, path);\n \n \t/* early return if there isn't a path->module mapping */\n \tif (!module)\n@@ -271,7 +267,8 @@ int is_tree_submodule_active(struct repository *repo,\n \t\t}\n \n \t\tparse_pathspec(&ps, 0, 0, NULL, args.v);\n-\t\tret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);\n+\t\tret = match_pathspec(repo->index, &ps, module->path, strlen(module->path),\n+\t\t\t\t     0, NULL, 1);\n \n \t\tstrvec_clear(&args);\n \t\tclear_pathspec(&ps);\n@@ -287,6 +284,14 @@ int is_tree_submodule_active(struct repository *repo,\n \treturn ret;\n }\n \n+int is_tree_submodule_active(struct repository *repo,\n+\t\t\t     const struct object_id *treeish_name,\n+\t\t\t     const char *path)\n+{\n+\treturn is_submodule_active_config(repo,\n+\t\t\t\t\t  submodule_from_path(repo, treeish_name, path));\n+}\n+\n int is_submodule_active(struct repository *repo, const char *path)\n {\n \treturn is_tree_submodule_active(repo, null_oid(the_hash_algo), path);\n@@ -2734,8 +2739,8 @@ int submodule_to_gitdir(struct repository *repo,\n \treturn ret;\n }\n \n-void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,\n-\t\t\t      const char *submodule_name)\n+static int submodule_name_to_gitdir_gently(struct strbuf *buf, struct repository *r,\n+\t\t\t\t\t   const char *submodule_name)\n {\n \tif (!r->repository_format_submodule_path_cfg) {\n \t\t/*\n@@ -2754,11 +2759,7 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,\n \t\tFREE_AND_NULL(key);\n \n \t\tif (ret)\n-\t\t\tdie(_(\"the 'submodule.%s.gitdir' config does not exist for module '%s'. \"\n-\t\t\t      \"Please ensure it is set, for example by running something like: \"\n-\t\t\t      \"'git config submodule.%s.gitdir .git/modules/%s'. For details \"\n-\t\t\t      \"see the extensions.submodulePathConfig documentation.\"),\n-\t\t\t    submodule_name, submodule_name, submodule_name, submodule_name);\n+\t\t\treturn -1;\n \n \t\tstrbuf_addstr(buf, gitdir);\n \t}\n@@ -2770,4 +2771,36 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,\n \t\tdie(_(\"refusing to create/use '%s' in another submodule's \"\n \t\t      \" git dir.\"), buf->buf);\n \t}\n+\treturn 0;\n+}\n+\n+void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,\n+\t\t\t      const char *submodule_name)\n+{\n+\tif (submodule_name_to_gitdir_gently(buf, r, submodule_name))\n+\t\tdie(_(\"the 'submodule.%s.gitdir' config does not exist for module '%s'. \"\n+\t\t      \"Please ensure it is set, for example by running something like: \"\n+\t\t      \"'git config submodule.%s.gitdir .git/modules/%s'. For details \"\n+\t\t      \"see the extensions.submodulePathConfig documentation.\"),\n+\t\t    submodule_name, submodule_name, submodule_name, submodule_name);\n+}\n+\n+int check_submodule_available(struct repository *repo, const struct submodule *sub)\n+{\n+\tstruct strbuf gitdir = STRBUF_INIT;\n+\tint err, ret = 0;\n+\n+\tif (!is_submodule_active_config(repo, sub) ||\n+\t    is_submodule_populated_gently(sub->path, &err))\n+\t\treturn 0;\n+\n+\tif (submodule_name_to_gitdir_gently(&gitdir, repo, sub->name) ||\n+\t    !is_git_directory(gitdir.buf)) {\n+\t\tret = error(_(\"submodule '%s' is not available locally\"), sub->path);\n+\t\tadvise_if_enabled(ADVICE_SUBMODULES_NOT_UPDATED,\n+\t\t\t\t  _(\"Retry without recursing into submodules, then run\\n\"\n+\t\t\t\t    \"'git submodule update --init --recursive'.\"));\n+\t}\n+\tstrbuf_release(&gitdir);\n+\treturn ret;\n }\ndiff --git a/submodule.h b/submodule.h\nindex b10e16e6c0..ae04a8b1db 100644\n--- a/submodule.h\n+++ b/submodule.h\n@@ -12,6 +12,7 @@ struct remote;\n struct repository;\n struct string_list;\n struct strbuf;\n+struct submodule;\n \n enum submodule_recurse_mode {\n \tRECURSE_SUBMODULES_ONLY = -5,\n@@ -69,6 +70,7 @@ int is_submodule_active(struct repository *repo, const char *path);\n  * Otherwise the return error code is the same as of resolve_gitdir_gently.\n  */\n int is_submodule_populated_gently(const char *path, int *return_error_code);\n+int check_submodule_available(struct repository *repo, const struct submodule *sub);\n void die_in_unpopulated_submodule(struct index_state *istate,\n \t\t\t\t  const char *prefix);\n void die_path_inside_submodule(struct index_state *istate,\ndiff --git a/t/t2013-checkout-submodule.sh b/t/t2013-checkout-submodule.sh\nindex b2bdd1fcb4..4fdf2a6306 100755\n--- a/t/t2013-checkout-submodule.sh\n+++ b/t/t2013-checkout-submodule.sh\n@@ -63,6 +63,36 @@ test_expect_success '\"checkout <submodule>\" honors submodule.*.ignore from .git/\n \ttest_must_be_empty actual\n '\n \n+for path_config in false true\n+do\n+\ttest_expect_success \"recursive checkout of a missing submodule (submodulePathConfig=$path_config)\" '\n+\t\tprolog &&\n+\t\tgit clone -c extensions.submodulePathConfig=$path_config \\\n+\t\t\t--recurse-submodules -b remove_sub1 submodule_update_repo fresh-$path_config &&\n+\t\t(\n+\t\t\tcd fresh-$path_config &&\n+\t\t\ttest_must_fail git checkout --recurse-submodules origin/add_sub1 2>../err &&\n+\t\t\ttest_cmp_rev HEAD origin/remove_sub1 &&\n+\t\t\tgit status --porcelain >../status &&\n+\t\t\ttest_must_be_empty ../status &&\n+\t\t\ttest_path_is_missing sub1 &&\n+\t\t\ttest_grep \"sub1.*not available locally\" ../err &&\n+\t\t\ttest_grep \"git submodule update --init --recursive\" ../err &&\n+\t\t\tgit checkout --no-recurse-submodules origin/add_sub1 &&\n+\t\t\tgit submodule update --init --recursive &&\n+\t\t\ttest_submodule_content sub1 HEAD\n+\t\t)\n+\t'\n+done\n+\n+test_expect_success 'recursive checkout skips missing submodules outside the active pathspec' '\n+\tprolog &&\n+\tgit clone --recurse-submodules=other -b remove_sub1 submodule_update_repo selective &&\n+\tgit -C selective checkout --recurse-submodules origin/add_sub1 &&\n+\ttest_dir_is_empty selective/sub1 &&\n+\ttest_dir_is_empty selective/uninitialized_sub\n+'\n+\n KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1\n test_submodule_switch_recursing_with_args \"checkout\"\n \ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex 1802809ad3..e42a8983ad 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -2075,6 +2075,10 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options\n \t\t}\n \t}\n \n+\tif (o->update && should_update_submodules() &&\n+\t    verify_submodules_available(&o->internal.result))\n+\t\tgoto return_failed;\n+\n \tret = check_updates(o, &o->internal.result) ? (-2) : 0;\n \tif (o->dst_index) {\n \t\tmove_index_extensions(&o->internal.result, o->src_index);\n-- \n2.56.1\n\n\n"}]}