submodule: refactor `submodule_to_gitdir()` to accept a repo

The `submodule_to_gitdir()` function implicitly uses `the_repository` to resolve submodule paths. Refactor the function to instead accept a repo as parameter to remove the dependency on global state. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 7, 2025 at 12:03 UTC f9467895d884908d5588fc920997b2e53dfb3302
5 files changed +11 -9
builtin/submodule--helper.c
+1 -1
@@ -1301,7 +1301,7 @@ static void sync_submodule(const char *path, const char *prefix,
1301 remote_key = xstrfmt("remote.%s.url", default_remote);
1302 free(default_remote);
1303
1304 - submodule_to_gitdir(&sb, path);
1304 + submodule_to_gitdir(the_repository, &sb, path);
1305 strbuf_addstr(&sb, "/config");
1306
1307 if (git_config_set_in_file_gently(sb.buf, remote_key, NULL, sub_origin_url))
path.c
+1 -1
@@ -567,7 +567,7 @@ static int do_submodule_path(struct strbuf *buf, const char *path,
567 struct strbuf git_submodule_dir = STRBUF_INIT;
568 int ret;
569
570 - ret = submodule_to_gitdir(&git_submodule_dir, path);
570 + ret = submodule_to_gitdir(the_repository, &git_submodule_dir, path);
571 if (ret)
572 goto cleanup;
573
refs.c
+1 -1
@@ -2146,7 +2146,7 @@ struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
2146 if (!is_nonbare_repository_dir(&submodule_sb))
2147 goto done;
2148
2149 - if (submodule_to_gitdir(&submodule_sb, submodule))
2149 + if (submodule_to_gitdir(repo, &submodule_sb, submodule))
2150 goto done;
2151
2152 subrepo = xmalloc(sizeof(*subrepo));
submodule.c
+6 -5
@@ -536,7 +536,8 @@ static struct repository *open_submodule(const char *path)
536 struct strbuf sb = STRBUF_INIT;
537 struct repository *out = xmalloc(sizeof(*out));
538
539 - if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
539 + if (submodule_to_gitdir(the_repository, &sb, path) ||
540 + repo_init(out, sb.buf, NULL)) {
541 strbuf_release(&sb);
542 free(out);
543 return NULL;
@@ -2572,7 +2573,8 @@ int get_superproject_working_tree(struct strbuf *buf)
2573 * Put the gitdir for a submodule (given relative to the main
2574 * repository worktree) into `buf`, or return -1 on error.
2575 */
2575 -int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2576 +int submodule_to_gitdir(struct repository *repo,
2577 + struct strbuf *buf, const char *submodule)
2578 {
2579 const struct submodule *sub;
2580 const char *git_dir;
@@ -2592,14 +2594,13 @@ int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2594 strbuf_addstr(buf, git_dir);
2595 }
2596 if (!is_git_directory(buf->buf)) {
2595 - sub = submodule_from_path(the_repository, null_oid(),
2596 - submodule);
2597 + sub = submodule_from_path(repo, null_oid(), submodule);
2598 if (!sub) {
2599 ret = -1;
2600 goto cleanup;
2601 }
2602 strbuf_reset(buf);
2602 - submodule_name_to_gitdir(buf, the_repository, sub->name);
2603 + submodule_name_to_gitdir(buf, repo, sub->name);
2604 }
2605
2606 cleanup:
submodule.h
+2 -1
@@ -136,7 +136,8 @@ int push_unpushed_submodules(struct repository *r,
136 * path of that submodule in 'buf'. Return -1 on error or when the
137 * submodule is not initialized.
138 */
139 -int submodule_to_gitdir(struct strbuf *buf, const char *submodule);
139 +int submodule_to_gitdir(struct repository *repo,
140 + struct strbuf *buf, const char *submodule);
141
142 /*
143 * Given a submodule name, create a path to where the submodule's gitdir lives