From: zibuyu Date: Thu, 03 Dec 2026 09:32:10 GMT Subject: [PATCH] unpack-trees: reject missing submodules early Message-ID: <20261203093210.3341204-1-zibuyu423@gmail.com> A recursive clone can lack a submodule that was removed before the cloned tip. Checking out an older commit with --recurse-submodules then writes its .gitmodules file before discovering that the submodule repository is missing. HEAD still points to the original commit, but the failed checkout leaves changes in the worktree. Check that each active gitlink scheduled for checkout has a local repository before unpacking files. Use the incoming .gitmodules mapping when it will replace the current one. Keep the old mapping available for removing submodules from the previous checkout. Tell the user how to check out the commit without recursion and then initialize its submodules. Exercise both gitdir layouts and a clone whose active pathspec excludes the missing submodules. Signed-off-by: zibuyu --- Documentation/git-checkout.adoc | 5 +++ submodule-config.c | 49 ++++++++++++++++++++++++++ submodule-config.h | 3 ++ submodule.c | 61 +++++++++++++++++++++++++-------- submodule.h | 2 ++ t/t2013-checkout-submodule.sh | 30 ++++++++++++++++ unpack-trees.c | 4 +++ 7 files changed, 140 insertions(+), 14 deletions(-) diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc index a8b3b8c2e2..56bd69db0a 100644 --- a/Documentation/git-checkout.adoc +++ b/Documentation/git-checkout.adoc @@ -308,6 +308,11 @@ include::diff-context-options.adoc[] is used, submodules working trees will not be updated. Just like linkgit:git-submodule[1], this will detach `HEAD` of the submodule. ++ +When switching to a commit that adds an active submodule, checkout needs +the submodule's repository to be available locally. If it is missing, +retry with `--no-recurse-submodules`, then run +`git submodule update --init --recursive` to populate the submodules. `--overlay`:: `--no-overlay`:: diff --git a/submodule-config.c b/submodule-config.c index 37c3be377b..a61899b5b7 100644 --- a/submodule-config.c +++ b/submodule-config.c @@ -15,6 +15,7 @@ #include "odb.h" #include "odb/source.h" #include "parse-options.h" +#include "read-cache.h" #include "thread-utils.h" #include "tree-walk.h" #include "url.h" @@ -878,6 +879,54 @@ const struct submodule *submodule_from_path(struct repository *r, return config_from(r, r->submodule_cache, treeish_name, path, lookup_path); } +int verify_submodules_available(struct index_state *istate) +{ + struct repository *repo = istate->repo; + struct submodule_cache cache = { 0 }; + const struct object_id *gitmodules_oid = NULL; + const struct object_id *worktree = null_oid(repo->hash_algo); + int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE)); + int ret = 0; + + if (pos >= 0 && (istate->cache[pos]->ce_flags & CE_UPDATE)) + gitmodules_oid = &istate->cache[pos]->oid; + + for (unsigned int i = 0; i < istate->cache_nr; i++) { + const struct cache_entry *ce = istate->cache[i]; + const struct submodule *sub; + + if (!(ce->ce_flags & CE_UPDATE) || !S_ISGITLINK(ce->ce_mode)) + continue; + /* + * Read the incoming mapping without replacing the mapping + * that check_updates() still needs to remove old submodules. + */ + if (gitmodules_oid && !cache.initialized) { + struct parse_config_parameter data = { + .cache = &cache, + .gitmodules_oid = worktree, + .overwrite = 1, + }; + submodule_cache_init(&cache); + ret = git_config_from_blob_oid(parse_config, GITMODULES_FILE, + repo, gitmodules_oid, &data, + CONFIG_SCOPE_SUBMODULE); + if (ret < 0) + break; + } + if (gitmodules_oid) + sub = cache_lookup_path(&cache, worktree, ce->name); + else + sub = submodule_from_path(repo, worktree, ce->name); + if (sub && check_submodule_available(repo, sub)) { + ret = -1; + break; + } + } + submodule_cache_clear(&cache); + return ret; +} + /** * Used internally by submodules_of_tree(). Recurses into 'treeish_name' * and appends submodule entries to 'out'. The submodule_cache expects diff --git a/submodule-config.h b/submodule-config.h index 755570d5d1..5351c7b4f3 100644 --- a/submodule-config.h +++ b/submodule-config.h @@ -75,6 +75,9 @@ const struct submodule *submodule_from_path(struct repository *r, const struct object_id *commit_or_tree, const char *path); +/* Check active gitlinks before writing any pending worktree updates. */ +int verify_submodules_available(struct index_state *istate); + /** * Use these to free the internally cached values. */ diff --git a/submodule.c b/submodule.c index 6fcb606f7e..c1e4758544 100644 --- a/submodule.c +++ b/submodule.c @@ -236,17 +236,13 @@ int option_parse_recurse_submodules_worktree_updater(const struct option *opt, * ie, the config looks like: "[submodule] active\n". * Since that is an invalid pathspec, we should inform the user. */ -int is_tree_submodule_active(struct repository *repo, - const struct object_id *treeish_name, - const char *path) +static int is_submodule_active_config(struct repository *repo, + const struct submodule *module) { int ret = 0; char *key = NULL; char *value = NULL; const struct string_list *sl; - const struct submodule *module; - - module = submodule_from_path(repo, treeish_name, path); /* early return if there isn't a path->module mapping */ if (!module) @@ -271,7 +267,8 @@ int is_tree_submodule_active(struct repository *repo, } parse_pathspec(&ps, 0, 0, NULL, args.v); - ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1); + ret = match_pathspec(repo->index, &ps, module->path, strlen(module->path), + 0, NULL, 1); strvec_clear(&args); clear_pathspec(&ps); @@ -287,6 +284,14 @@ int is_tree_submodule_active(struct repository *repo, return ret; } +int is_tree_submodule_active(struct repository *repo, + const struct object_id *treeish_name, + const char *path) +{ + return is_submodule_active_config(repo, + submodule_from_path(repo, treeish_name, path)); +} + int is_submodule_active(struct repository *repo, const char *path) { return is_tree_submodule_active(repo, null_oid(the_hash_algo), path); @@ -2734,8 +2739,8 @@ int submodule_to_gitdir(struct repository *repo, return ret; } -void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, - const char *submodule_name) +static int submodule_name_to_gitdir_gently(struct strbuf *buf, struct repository *r, + const char *submodule_name) { if (!r->repository_format_submodule_path_cfg) { /* @@ -2754,11 +2759,7 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, FREE_AND_NULL(key); if (ret) - die(_("the 'submodule.%s.gitdir' config does not exist for module '%s'. " - "Please ensure it is set, for example by running something like: " - "'git config submodule.%s.gitdir .git/modules/%s'. For details " - "see the extensions.submodulePathConfig documentation."), - submodule_name, submodule_name, submodule_name, submodule_name); + return -1; strbuf_addstr(buf, gitdir); } @@ -2770,4 +2771,36 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, die(_("refusing to create/use '%s' in another submodule's " " git dir."), buf->buf); } + return 0; +} + +void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, + const char *submodule_name) +{ + if (submodule_name_to_gitdir_gently(buf, r, submodule_name)) + die(_("the 'submodule.%s.gitdir' config does not exist for module '%s'. " + "Please ensure it is set, for example by running something like: " + "'git config submodule.%s.gitdir .git/modules/%s'. For details " + "see the extensions.submodulePathConfig documentation."), + submodule_name, submodule_name, submodule_name, submodule_name); +} + +int check_submodule_available(struct repository *repo, const struct submodule *sub) +{ + struct strbuf gitdir = STRBUF_INIT; + int err, ret = 0; + + if (!is_submodule_active_config(repo, sub) || + is_submodule_populated_gently(sub->path, &err)) + return 0; + + if (submodule_name_to_gitdir_gently(&gitdir, repo, sub->name) || + !is_git_directory(gitdir.buf)) { + ret = error(_("submodule '%s' is not available locally"), sub->path); + advise_if_enabled(ADVICE_SUBMODULES_NOT_UPDATED, + _("Retry without recursing into submodules, then run\n" + "'git submodule update --init --recursive'.")); + } + strbuf_release(&gitdir); + return ret; } diff --git a/submodule.h b/submodule.h index b10e16e6c0..ae04a8b1db 100644 --- a/submodule.h +++ b/submodule.h @@ -12,6 +12,7 @@ struct remote; struct repository; struct string_list; struct strbuf; +struct submodule; enum submodule_recurse_mode { RECURSE_SUBMODULES_ONLY = -5, @@ -69,6 +70,7 @@ int is_submodule_active(struct repository *repo, const char *path); * Otherwise the return error code is the same as of resolve_gitdir_gently. */ int is_submodule_populated_gently(const char *path, int *return_error_code); +int check_submodule_available(struct repository *repo, const struct submodule *sub); void die_in_unpopulated_submodule(struct index_state *istate, const char *prefix); void die_path_inside_submodule(struct index_state *istate, diff --git a/t/t2013-checkout-submodule.sh b/t/t2013-checkout-submodule.sh index b2bdd1fcb4..4fdf2a6306 100755 --- a/t/t2013-checkout-submodule.sh +++ b/t/t2013-checkout-submodule.sh @@ -63,6 +63,36 @@ test_expect_success '"checkout " honors submodule.*.ignore from .git/ test_must_be_empty actual ' +for path_config in false true +do + test_expect_success "recursive checkout of a missing submodule (submodulePathConfig=$path_config)" ' + prolog && + git clone -c extensions.submodulePathConfig=$path_config \ + --recurse-submodules -b remove_sub1 submodule_update_repo fresh-$path_config && + ( + cd fresh-$path_config && + test_must_fail git checkout --recurse-submodules origin/add_sub1 2>../err && + test_cmp_rev HEAD origin/remove_sub1 && + git status --porcelain >../status && + test_must_be_empty ../status && + test_path_is_missing sub1 && + test_grep "sub1.*not available locally" ../err && + test_grep "git submodule update --init --recursive" ../err && + git checkout --no-recurse-submodules origin/add_sub1 && + git submodule update --init --recursive && + test_submodule_content sub1 HEAD + ) + ' +done + +test_expect_success 'recursive checkout skips missing submodules outside the active pathspec' ' + prolog && + git clone --recurse-submodules=other -b remove_sub1 submodule_update_repo selective && + git -C selective checkout --recurse-submodules origin/add_sub1 && + test_dir_is_empty selective/sub1 && + test_dir_is_empty selective/uninitialized_sub +' + KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 test_submodule_switch_recursing_with_args "checkout" diff --git a/unpack-trees.c b/unpack-trees.c index 1802809ad3..e42a8983ad 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -2075,6 +2075,10 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options } } + if (o->update && should_update_submodules() && + verify_submodules_available(&o->internal.result)) + goto return_failed; + ret = check_updates(o, &o->internal.result) ? (-2) : 0; if (o->dst_index) { move_index_extensions(&o->internal.result, o->src_index); -- 2.56.1