submodule--helper: use submodule_name_to_gitdir in add_submodule

While testing submodule gitdir path encoding, I noticed submodule--helper is still using a hardcoded modules gitdir path leading to test failures. Call the submodule_name_to_gitdir() helper instead, which was invented exactly for this purpose and is already used by all the other locations which work on gitdirs. Also narrow the scope of the submod_gitdir_path variable which is not used anymore in the updated "else" branch. 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 b60f7d890dee571069f9c3c6d44ecaed5c34fa39
1 file changed +7 -6
builtin/submodule--helper.c
+7 -6
@@ -3187,13 +3187,13 @@ static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3187
3188 static int add_submodule(const struct add_data *add_data)
3189 {
3190 - char *submod_gitdir_path;
3190 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3191 struct string_list reference = STRING_LIST_INIT_NODUP;
3192 int ret = -1;
3193
3194 /* perhaps the path already exists and is already a git repo, else clone it */
3195 if (is_directory(add_data->sm_path)) {
3196 + char *submod_gitdir_path;
3197 struct strbuf sm_path = STRBUF_INIT;
3198 strbuf_addstr(&sm_path, add_data->sm_path);
3199 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
@@ -3207,10 +3207,11 @@ static int add_submodule(const struct add_data *add_data)
3207 free(submod_gitdir_path);
3208 } else {
3209 struct child_process cp = CHILD_PROCESS_INIT;
3210 + struct strbuf submod_gitdir = STRBUF_INIT;
3211
3211 - submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3212 + submodule_name_to_gitdir(&submod_gitdir, the_repository, add_data->sm_name);
3213
3213 - if (is_directory(submod_gitdir_path)) {
3214 + if (is_directory(submod_gitdir.buf)) {
3215 if (!add_data->force) {
3216 struct strbuf msg = STRBUF_INIT;
3217 char *die_msg;
@@ -3219,8 +3220,8 @@ static int add_submodule(const struct add_data *add_data)
3220 "locally with remote(s):\n"),
3221 add_data->sm_name);
3222
3222 - append_fetch_remotes(&msg, submod_gitdir_path);
3223 - free(submod_gitdir_path);
3223 + append_fetch_remotes(&msg, submod_gitdir.buf);
3224 + strbuf_release(&submod_gitdir);
3225
3226 strbuf_addf(&msg, _("If you want to reuse this local git "
3227 "directory instead of cloning again from\n"
@@ -3238,7 +3239,7 @@ static int add_submodule(const struct add_data *add_data)
3239 "submodule '%s'\n"), add_data->sm_name);
3240 }
3241 }
3241 - free(submod_gitdir_path);
3242 + strbuf_release(&submod_gitdir);
3243
3244 clone_data.prefix = add_data->prefix;
3245 clone_data.path = add_data->sm_path;