From: Adrian Ratiu Date: Sat, 20 Dec 2025 10:15:19 +0000 (+0200) Subject: submodule: always validate gitdirs inside submodule_name_to_gitdir X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a9d503f816d23576f24d99820c15dd1bf89b892e;p=thirdparty%2Fgit.git 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 Signed-off-by: Junio C Hamano --- diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 2873b2780e..fc10ace5a8 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1703,10 +1703,6 @@ static int clone_submodule(const struct module_clone_data *clone_data, clone_data_path = to_free = xstrfmt("%s/%s", repo_get_work_tree(the_repository), clone_data->path); - if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) - die(_("refusing to create/use '%s' in another submodule's " - "git dir"), sm_gitdir); - if (!file_exists(sm_gitdir)) { if (clone_data->require_init && !stat(clone_data_path, &st) && !is_empty_dir(clone_data_path)) diff --git a/submodule.c b/submodule.c index 35c55155f7..d937911fbc 100644 --- a/submodule.c +++ b/submodule.c @@ -2172,11 +2172,6 @@ int submodule_move_head(const char *path, const char *super_prefix, struct strbuf gitdir = STRBUF_INIT; submodule_name_to_gitdir(&gitdir, the_repository, sub->name); - if (validate_submodule_git_dir(gitdir.buf, - sub->name) < 0) - die(_("refusing to create/use '%s' in another " - "submodule's git dir"), - gitdir.buf); connect_work_tree_and_git_dir(path, gitdir.buf, 0); strbuf_release(&gitdir); @@ -2355,9 +2350,6 @@ static void relocate_single_git_dir_into_superproject(const char *path, die(_("could not lookup name for submodule '%s'"), path); submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name); - if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0) - die(_("refusing to move '%s' into an existing git dir"), - real_old_git_dir); if (safe_create_leading_directories_const(the_repository, new_gitdir.buf) < 0) die(_("could not create directory '%s'"), new_gitdir.buf); 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, */ repo_git_path_append(r, buf, "modules/"); strbuf_addstr(buf, submodule_name); + + if (validate_submodule_git_dir(buf->buf, submodule_name) < 0) + die(_("refusing to create/use '%s' in another submodule's " + "git dir"), buf->buf); }