remote: remove the_repository from some functions

The remotes_remote_get_1 (and its caller, remotes_remote_get, have an implicit dependency on the_repository due to calling read_branches_file() and read_remotes_file(), both of which use the_repository. The branch_get() function calls set_merge() which has an implicit dependency on the_repository as well. Because of this use of the_repository, the helper functions cannot be used in code paths which operate on other repositories. A future refactor of the submodule--helper will want to make use of some of these functions. Refactor to break the dependency by passing struct repository *repo instead of struct remote_state *remote_state in a few places. The public callers and many other helper functions still depend on the_repository. A repo-aware function will be exposed in a following change for git submodule--helper. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed Jun 23, 2025 at 16:11 UTC f8542961da88bee31f7e0da21fd8d2792d62f888
1 file changed +28 -30
remote.c
+28 -30
@@ -334,11 +334,10 @@ static void warn_about_deprecated_remote_type(const char *type,
334 type, remote->name, remote->name, remote->name);
335 }
336
337 -static void read_remotes_file(struct remote_state *remote_state,
338 - struct remote *remote)
337 +static void read_remotes_file(struct repository *repo, struct remote *remote)
338 {
339 struct strbuf buf = STRBUF_INIT;
341 - FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf,
340 + FILE *f = fopen_or_warn(repo_git_path_append(repo, &buf,
341 "remotes/%s", remote->name), "r");
342
343 if (!f)
@@ -354,7 +353,7 @@ static void read_remotes_file(struct remote_state *remote_state,
353 strbuf_rtrim(&buf);
354
355 if (skip_prefix(buf.buf, "URL:", &v))
357 - add_url_alias(remote_state, remote,
356 + add_url_alias(repo->remote_state, remote,
357 skip_spaces(v));
358 else if (skip_prefix(buf.buf, "Push:", &v))
359 refspec_append(&remote->push, skip_spaces(v));
@@ -367,12 +366,11 @@ out:
366 strbuf_release(&buf);
367 }
368
370 -static void read_branches_file(struct remote_state *remote_state,
371 - struct remote *remote)
369 +static void read_branches_file(struct repository *repo, struct remote *remote)
370 {
371 char *frag, *to_free = NULL;
372 struct strbuf buf = STRBUF_INIT;
375 - FILE *f = fopen_or_warn(repo_git_path_append(the_repository, &buf,
373 + FILE *f = fopen_or_warn(repo_git_path_append(repo, &buf,
374 "branches/%s", remote->name), "r");
375
376 if (!f)
@@ -399,9 +397,9 @@ static void read_branches_file(struct remote_state *remote_state,
397 if (frag)
398 *(frag++) = '\0';
399 else
402 - frag = to_free = repo_default_branch_name(the_repository, 0);
400 + frag = to_free = repo_default_branch_name(repo, 0);
401
404 - add_url_alias(remote_state, remote, buf.buf);
402 + add_url_alias(repo->remote_state, remote, buf.buf);
403 refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
404 frag, remote->name);
405
@@ -698,7 +696,7 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
696 branch, explicit);
697 }
698
701 -static struct remote *remotes_remote_get(struct remote_state *remote_state,
699 +static struct remote *remotes_remote_get(struct repository *repo,
700 const char *name);
701
702 char *remote_ref_for_branch(struct branch *branch, int for_push)
@@ -717,7 +715,7 @@ char *remote_ref_for_branch(struct branch *branch, int for_push)
715 the_repository->remote_state, branch,
716 NULL);
717 struct remote *remote = remotes_remote_get(
720 - the_repository->remote_state, remote_name);
718 + the_repository, remote_name);
719
720 if (remote && remote->push.nr &&
721 (dst = apply_refspecs(&remote->push,
@@ -774,10 +772,11 @@ loop_cleanup:
772 }
773
774 static struct remote *
777 -remotes_remote_get_1(struct remote_state *remote_state, const char *name,
775 +remotes_remote_get_1(struct repository *repo, const char *name,
776 const char *(*get_default)(struct remote_state *,
777 struct branch *, int *))
778 {
779 + struct remote_state *remote_state = repo->remote_state;
780 struct remote *ret;
781 int name_given = 0;
782
@@ -791,9 +790,9 @@ remotes_remote_get_1(struct remote_state *remote_state, const char *name,
790 #ifndef WITH_BREAKING_CHANGES
791 if (valid_remote_nick(name) && have_git_dir()) {
792 if (!valid_remote(ret))
794 - read_remotes_file(remote_state, ret);
793 + read_remotes_file(repo, ret);
794 if (!valid_remote(ret))
796 - read_branches_file(remote_state, ret);
795 + read_branches_file(repo, ret);
796 }
797 #endif /* WITH_BREAKING_CHANGES */
798 if (name_given && !valid_remote(ret))
@@ -807,35 +806,33 @@ remotes_remote_get_1(struct remote_state *remote_state, const char *name,
806 }
807
808 static inline struct remote *
810 -remotes_remote_get(struct remote_state *remote_state, const char *name)
809 +remotes_remote_get(struct repository *repo, const char *name)
810 {
812 - return remotes_remote_get_1(remote_state, name,
813 - remotes_remote_for_branch);
811 + return remotes_remote_get_1(repo, name, remotes_remote_for_branch);
812 }
813
814 struct remote *remote_get(const char *name)
815 {
816 read_config(the_repository, 0);
819 - return remotes_remote_get(the_repository->remote_state, name);
817 + return remotes_remote_get(the_repository, name);
818 }
819
820 struct remote *remote_get_early(const char *name)
821 {
822 read_config(the_repository, 1);
825 - return remotes_remote_get(the_repository->remote_state, name);
823 + return remotes_remote_get(the_repository, name);
824 }
825
826 static inline struct remote *
829 -remotes_pushremote_get(struct remote_state *remote_state, const char *name)
827 +remotes_pushremote_get(struct repository *repo, const char *name)
828 {
831 - return remotes_remote_get_1(remote_state, name,
832 - remotes_pushremote_for_branch);
829 + return remotes_remote_get_1(repo, name, remotes_pushremote_for_branch);
830 }
831
832 struct remote *pushremote_get(const char *name)
833 {
834 read_config(the_repository, 0);
838 - return remotes_pushremote_get(the_repository->remote_state, name);
835 + return remotes_pushremote_get(the_repository, name);
836 }
837
838 int remote_is_configured(struct remote *remote, int in_repo)
@@ -1739,7 +1736,7 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1736 }
1737 }
1738
1742 -static void set_merge(struct remote_state *remote_state, struct branch *ret)
1739 +static void set_merge(struct repository *repo, struct branch *ret)
1740 {
1741 struct remote *remote;
1742 char *ref;
@@ -1760,13 +1757,13 @@ static void set_merge(struct remote_state *remote_state, struct branch *ret)
1757 }
1758 ret->set_merge = 1;
1759
1763 - remote = remotes_remote_get(remote_state, ret->remote_name);
1760 + remote = remotes_remote_get(repo, ret->remote_name);
1761
1762 for (i = 0; i < ret->merge_nr; i++) {
1763 if (!remote_find_tracking(remote, ret->merge[i]) ||
1764 strcmp(ret->remote_name, "."))
1765 continue;
1769 - if (repo_dwim_ref(the_repository, ret->merge[i]->src,
1766 + if (repo_dwim_ref(repo, ret->merge[i]->src,
1767 strlen(ret->merge[i]->src), &oid, &ref,
1768 0) == 1)
1769 ret->merge[i]->dst = ref;
@@ -1785,7 +1782,7 @@ struct branch *branch_get(const char *name)
1782 else
1783 ret = make_branch(the_repository->remote_state, name,
1784 strlen(name));
1788 - set_merge(the_repository->remote_state, ret);
1785 + set_merge(the_repository, ret);
1786 return ret;
1787 }
1788
@@ -1856,13 +1853,14 @@ static const char *tracking_for_push_dest(struct remote *remote,
1853 return ret;
1854 }
1855
1859 -static const char *branch_get_push_1(struct remote_state *remote_state,
1856 +static const char *branch_get_push_1(struct repository *repo,
1857 struct branch *branch, struct strbuf *err)
1858 {
1859 + struct remote_state *remote_state = repo->remote_state;
1860 struct remote *remote;
1861
1862 remote = remotes_remote_get(
1865 - remote_state,
1863 + repo,
1864 remotes_pushremote_for_branch(remote_state, branch, NULL));
1865 if (!remote)
1866 return error_buf(err,
@@ -1929,7 +1927,7 @@ const char *branch_get_push(struct branch *branch, struct strbuf *err)
1927
1928 if (!branch->push_tracking_ref)
1929 branch->push_tracking_ref = branch_get_push_1(
1932 - the_repository->remote_state, branch, err);
1930 + the_repository, branch, err);
1931 return branch->push_tracking_ref;
1932 }
1933