submodule: always validate gitdirs inside submodule_name_to_gitdir

Move the ad-hoc validation checks sprinkled across the source tree, after calling submodule_name_to_gitdir() into the function proper, which now always validates the gitdir before returning it. This simplifies the API and helps to: 1. Avoid redundant validation calls after submodule_name_to_gitdir(). 2. Avoid the risk of callers forgetting to validate. 3. Ensure gitdir paths provided by users via configs are always valid (config gitdir paths are added in a subsequent commit). The validation function can still be called as many times as needed outside submodule_name_to_gitdir(), for example we keep two calls which are still required, to avoid parallel clone races by re-running the validation in builtin/submodule-helper.c. Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adrian Ratiu committed Jan 12, 2026 at 20:46 UTC 05a1cdb5255b2b438e66bdaa91b7b2ce75fbe71b
2 files changed +4 -12
builtin/submodule--helper.c
-4
@@ -1703,10 +1703,6 @@ static int clone_submodule(const struct module_clone_data *clone_data,
1703 clone_data_path = to_free = xstrfmt("%s/%s", repo_get_work_tree(the_repository),
1704 clone_data->path);
1705
1706 - if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1707 - die(_("refusing to create/use '%s' in another submodule's "
1708 - "git dir"), sm_gitdir);
1709 -
1706 if (!file_exists(sm_gitdir)) {
1707 if (clone_data->require_init && !stat(clone_data_path, &st) &&
1708 !is_empty_dir(clone_data_path))
submodule.c
+4 -8
@@ -2172,11 +2172,6 @@ int submodule_move_head(const char *path, const char *super_prefix,
2172 struct strbuf gitdir = STRBUF_INIT;
2173 submodule_name_to_gitdir(&gitdir, the_repository,
2174 sub->name);
2175 - if (validate_submodule_git_dir(gitdir.buf,
2176 - sub->name) < 0)
2177 - die(_("refusing to create/use '%s' in another "
2178 - "submodule's git dir"),
2179 - gitdir.buf);
2175 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2176 strbuf_release(&gitdir);
2177
@@ -2355,9 +2350,6 @@ static void relocate_single_git_dir_into_superproject(const char *path,
2350 die(_("could not lookup name for submodule '%s'"), path);
2351
2352 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2358 - if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
2359 - die(_("refusing to move '%s' into an existing git dir"),
2360 - real_old_git_dir);
2353 if (safe_create_leading_directories_const(the_repository, new_gitdir.buf) < 0)
2354 die(_("could not create directory '%s'"), new_gitdir.buf);
2355 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
@@ -2606,4 +2598,8 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2598 */
2599 repo_git_path_append(r, buf, "modules/");
2600 strbuf_addstr(buf, submodule_name);
2601 +
2602 + if (validate_submodule_git_dir(buf->buf, submodule_name) < 0)
2603 + die(_("refusing to create/use '%s' in another submodule's "
2604 + "git dir"), buf->buf);
2605 }