threads / patch / 66570

patchcheckout: use repository parameter in unique_tracking_name

Subject: [Outreachy PATCH] checkout: use repository parameter in unique_tracking_name

## tl;dr

14 messages between Oct 17, 2026 and Nov 4, 2026. Diffs are folded; open one to read it.

replies: 13people: 4as markdown or json

Okhuomon Ajayi· Oct 17, 2026, 22:53 UTC · lore

The function uses the_repository to read configuration and look up the object ID of a tracking reference.

Pass the repository to unique_tracking_name() and through tracking_name_data. Callers pass the_repository for now.

This removes the need for USE_THE_REPOSITORY_VARIABLE in checkout.c.
Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 11 ++++++-----
 checkout.h         |  4 +++-
 4 files changed, 15 insertions(+), 9 deletions(-)
Show changes to 4 files +15 −9

builtin/checkout.c, builtin/worktree.c, checkout.c, checkout.h

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2d575a4f57..bad49368b9 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1389,7 +1389,8 @@ static char *parse_remote_branch(const char *arg,
 	int num_matches = 0;
 	struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-	char *remote = unique_tracking_name(arg, rev, &num_matches,
+	char *remote = unique_tracking_name(the_repository, arg, rev,
+					    &num_matches,
 					    &matched_remote_names);
 
 	if (remote && could_be_checkout_paths) {
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..203456bd23 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -802,7 +802,8 @@ static char *dwim_branch(const struct add_opts *opts, const char *path, char **n
 		int num_matches = 0;
 		struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-		remote = unique_tracking_name(*new_branch, &oid, &num_matches,
+		remote = unique_tracking_name(the_repository, *new_branch,
+					      &oid, &num_matches,
 					      &matched_remote_names);
 		if (!remote && num_matches > 1) {
 			if (!opts->quiet &&
@@ -938,7 +939,8 @@ static int add(int ac, const char **av, const char *prefix,
 			int num_matches = 0;
 			struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-			remote = unique_tracking_name(branch, &oid, &num_matches,
+			remote = unique_tracking_name(the_repository, branch,
+						      &oid, &num_matches,
 						      &matched_remote_names);
 			if (remote) {
 				new_branch = branch;
diff --git a/checkout.c b/checkout.c
index a0d0229435..f0fb40d48e 100644
--- a/checkout.c
+++ b/checkout.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "object-name.h"
 #include "remote.h"
@@ -11,6 +9,7 @@
 #include "string-list.h"
 
 struct tracking_name_data {
+	struct repository *repo;
 	/* const */ char *src_ref;
 	char *dst_ref;
 	struct object_id *dst_oid;
@@ -30,7 +29,7 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	memset(&query, 0, sizeof(struct refspec_item));
 	query.src = cb->src_ref;
 	if (remote_find_tracking(remote, &query) ||
-	    repo_get_oid(the_repository, query.dst, cb->dst_oid)) {
+	    repo_get_oid(cb->repo, query.dst, cb->dst_oid)) {
 		free(query.dst);
 		return 0;
 	}
@@ -51,17 +50,19 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	return 0;
 }
 
-char *unique_tracking_name(const char *name, struct object_id *oid,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name, struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names)
 {
 	struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
 	const char *default_remote = NULL;
 
-	if (!repo_config_get_string_tmp(the_repository,
+	if (!repo_config_get_string_tmp(repo,
 					"checkout.defaultremote",
 					&default_remote))
 		cb_data.default_remote = default_remote;
+	cb_data.repo = repo;
 	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
 	cb_data.dst_oid = oid;
 	cb_data.remote_names = dwim_remote_names;
diff --git a/checkout.h b/checkout.h
index 0b185a0fc9..25fe7dfb7c 100644
--- a/checkout.h
+++ b/checkout.h
@@ -3,6 +3,7 @@
 
 #include "hash.h"
 
+struct repository;
 struct string_list;
 
 /*
@@ -10,7 +11,8 @@ struct string_list;
  * tracking branch.  Return the name of the remote if such a branch
  * exists, NULL otherwise.
  */
-char *unique_tracking_name(const char *name,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name,
 			   struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names);
-- 
2.54.0
Usman Akinyemi· Oct 22, 2026, 09:41 UTC · re: Okhuomon Ajayi · lore

Re: [Outreachy PATCH] checkout: use repository parameter in unique_tracking_name

>
> The function uses the_repository to read configuration and look up

Which function ? You have left the reader of the commit message to wonder what function ?

> the object ID of a tracking reference.
>
> Pass the repository to unique_tracking_name() and through
> tracking_name_data. Callers pass the_repository for now.

You could be more descriptive more about the patches, someone should be able to go through the commit and sort get the whole idea of the patch, what was present, What changes and why ?

Otherwise, the changes look good to me.
>
> This removes the need for USE_THE_REPOSITORY_VARIABLE in checkout.c.
>
Thanks.
Okhuomon Ajayi· Oct 22, 2026, 10:26 UTC · re: Usman Akinyemi · lore

Re: [Outreachy PATCH] checkout: use repository parameter in unique_tracking_name

On Thu, Oct 22, 2026 at 10:41 AM Usman Akinyemi <usmanakinyemi202@gmail.com> wrote:

Show 6 quoted lines
> > Pass the repository to unique_tracking_name() and through
> > tracking_name_data. Callers pass the_repository for now.
> You could be more descriptive more about the patches, someone should be able to
> go through the commit and sort get the whole idea of the patch, what
> was present,
> What changes and why ?

Thanks for the review. I will name unique_tracking_name() in the message and explain what it used before, what changed, and why, so the commit can be understood on its own. I will send v2 with the updated message.

Okhuomon Ajayi· Oct 22, 2026, 10:56 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v2] checkout: pass the repository to unique_tracking_name()

unique_tracking_name() uses the_repository to read the "checkout.defaultRemote" configuration and look up the object ID of a remote-tracking branch.

Pass the repository to unique_tracking_name() and through tracking_name_data so check_tracking_name() can use it for the object lookup. Update the three callers to pass the_repository.

This removes the need for USE_THE_REPOSITORY_VARIABLE in checkout.c. for_each_remote() still uses the global repository; leave that for a separate change.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
Changes since v1:
- Rewrote the commit message to name the function and say what was
  there, what changed and why, as suggested by Usman. No code changes.
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 11 ++++++-----
 checkout.h         |  4 +++-
 4 files changed, 15 insertions(+), 9 deletions(-)
Show changes to 4 files +15 −9

builtin/checkout.c, builtin/worktree.c, checkout.c, checkout.h

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2d575a4f57..bad49368b9 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1389,7 +1389,8 @@ static char *parse_remote_branch(const char *arg,
 	int num_matches = 0;
 	struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-	char *remote = unique_tracking_name(arg, rev, &num_matches,
+	char *remote = unique_tracking_name(the_repository, arg, rev,
+					    &num_matches,
 					    &matched_remote_names);
 
 	if (remote && could_be_checkout_paths) {
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..203456bd23 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -802,7 +802,8 @@ static char *dwim_branch(const struct add_opts *opts, const char *path, char **n
 		int num_matches = 0;
 		struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-		remote = unique_tracking_name(*new_branch, &oid, &num_matches,
+		remote = unique_tracking_name(the_repository, *new_branch,
+					      &oid, &num_matches,
 					      &matched_remote_names);
 		if (!remote && num_matches > 1) {
 			if (!opts->quiet &&
@@ -938,7 +939,8 @@ static int add(int ac, const char **av, const char *prefix,
 			int num_matches = 0;
 			struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-			remote = unique_tracking_name(branch, &oid, &num_matches,
+			remote = unique_tracking_name(the_repository, branch,
+						      &oid, &num_matches,
 						      &matched_remote_names);
 			if (remote) {
 				new_branch = branch;
diff --git a/checkout.c b/checkout.c
index a0d0229435..f0fb40d48e 100644
--- a/checkout.c
+++ b/checkout.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "object-name.h"
 #include "remote.h"
@@ -11,6 +9,7 @@
 #include "string-list.h"
 
 struct tracking_name_data {
+	struct repository *repo;
 	/* const */ char *src_ref;
 	char *dst_ref;
 	struct object_id *dst_oid;
@@ -30,7 +29,7 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	memset(&query, 0, sizeof(struct refspec_item));
 	query.src = cb->src_ref;
 	if (remote_find_tracking(remote, &query) ||
-	    repo_get_oid(the_repository, query.dst, cb->dst_oid)) {
+	    repo_get_oid(cb->repo, query.dst, cb->dst_oid)) {
 		free(query.dst);
 		return 0;
 	}
@@ -51,17 +50,19 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	return 0;
 }
 
-char *unique_tracking_name(const char *name, struct object_id *oid,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name, struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names)
 {
 	struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
 	const char *default_remote = NULL;
 
-	if (!repo_config_get_string_tmp(the_repository,
+	if (!repo_config_get_string_tmp(repo,
 					"checkout.defaultremote",
 					&default_remote))
 		cb_data.default_remote = default_remote;
+	cb_data.repo = repo;
 	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
 	cb_data.dst_oid = oid;
 	cb_data.remote_names = dwim_remote_names;
diff --git a/checkout.h b/checkout.h
index 0b185a0fc9..25fe7dfb7c 100644
--- a/checkout.h
+++ b/checkout.h
@@ -3,6 +3,7 @@
 
 #include "hash.h"
 
+struct repository;
 struct string_list;
 
 /*
@@ -10,7 +11,8 @@ struct string_list;
  * tracking branch.  Return the name of the remote if such a branch
  * exists, NULL otherwise.
  */
-char *unique_tracking_name(const char *name,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name,
 			   struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names);

base-commit: c51408889c3b372814bc20b9431c3b74d0d268eb
-- 
2.56.0
Junio C Hamano· Oct 22, 2026, 18:51 UTC · re: Okhuomon Ajayi · lore

Re: [Outreachy PATCH v2] checkout: pass the repository to unique_tracking_name()

Okhuomon Ajayi <okhuomonajayi54@gmail.com> writes:
Show 12 quoted lines
> unique_tracking_name() uses the_repository to read the
> "checkout.defaultRemote" configuration and look up the object
> ID of a remote-tracking branch.
>
> Pass the repository to unique_tracking_name() and through
> tracking_name_data so check_tracking_name() can use it for
> the object lookup. Update the three callers to pass
> the_repository.
>
> This removes the need for USE_THE_REPOSITORY_VARIABLE in
> checkout.c. for_each_remote() still uses the global repository;
> leave that for a separate change.

The mention of for_each_remote() feels weirdly abrupt. Many other things still rely on the the_repository global variable, but there must be a reason why for_each_remote() is singled out in the context of this patch. What is it?

Replace the last sentence with something like:
    The callers still cannot call unique_tracking_name() with an
    arbitrary repository parameter other than the_repository, until
    the call in unique_tracking_name() to for_each_remote(), which
    only works with the_repository, is updated to take an arbitrary
    repository instance.
perhaps?

And explained that way, it becomes dubious if we chose a good place to stop. The updated unique_tracking_name() gives an illusion that callers now can pass an arbitrary repository to it and expect it to work correctly with the given repository, but it does not.

So, I dunno.
Show 123 quoted lines
> Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
> ---
> Changes since v1:
> - Rewrote the commit message to name the function and say what was
>   there, what changed and why, as suggested by Usman. No code changes.
>
>  builtin/checkout.c |  3 ++-
>  builtin/worktree.c |  6 ++++--
>  checkout.c         | 11 ++++++-----
>  checkout.h         |  4 +++-
>  4 files changed, 15 insertions(+), 9 deletions(-)
>
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index 2d575a4f57..bad49368b9 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -1389,7 +1389,8 @@ static char *parse_remote_branch(const char *arg,
>  	int num_matches = 0;
>  	struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
>  
> -	char *remote = unique_tracking_name(arg, rev, &num_matches,
> +	char *remote = unique_tracking_name(the_repository, arg, rev,
> +					    &num_matches,
>  					    &matched_remote_names);
>  
>  	if (remote && could_be_checkout_paths) {
> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index 77ecd0f71f..203456bd23 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -802,7 +802,8 @@ static char *dwim_branch(const struct add_opts *opts, const char *path, char **n
>  		int num_matches = 0;
>  		struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
>  
> -		remote = unique_tracking_name(*new_branch, &oid, &num_matches,
> +		remote = unique_tracking_name(the_repository, *new_branch,
> +					      &oid, &num_matches,
>  					      &matched_remote_names);
>  		if (!remote && num_matches > 1) {
>  			if (!opts->quiet &&
> @@ -938,7 +939,8 @@ static int add(int ac, const char **av, const char *prefix,
>  			int num_matches = 0;
>  			struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
>  
> -			remote = unique_tracking_name(branch, &oid, &num_matches,
> +			remote = unique_tracking_name(the_repository, branch,
> +						      &oid, &num_matches,
>  						      &matched_remote_names);
>  			if (remote) {
>  				new_branch = branch;
> diff --git a/checkout.c b/checkout.c
> index a0d0229435..f0fb40d48e 100644
> --- a/checkout.c
> +++ b/checkout.c
> @@ -1,5 +1,3 @@
> -#define USE_THE_REPOSITORY_VARIABLE
> -
>  #include "git-compat-util.h"
>  #include "object-name.h"
>  #include "remote.h"
> @@ -11,6 +9,7 @@
>  #include "string-list.h"
>  
>  struct tracking_name_data {
> +	struct repository *repo;
>  	/* const */ char *src_ref;
>  	char *dst_ref;
>  	struct object_id *dst_oid;
> @@ -30,7 +29,7 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
>  	memset(&query, 0, sizeof(struct refspec_item));
>  	query.src = cb->src_ref;
>  	if (remote_find_tracking(remote, &query) ||
> -	    repo_get_oid(the_repository, query.dst, cb->dst_oid)) {
> +	    repo_get_oid(cb->repo, query.dst, cb->dst_oid)) {
>  		free(query.dst);
>  		return 0;
>  	}
> @@ -51,17 +50,19 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
>  	return 0;
>  }
>  
> -char *unique_tracking_name(const char *name, struct object_id *oid,
> +char *unique_tracking_name(struct repository *repo,
> +			   const char *name, struct object_id *oid,
>  			   int *dwim_remotes_matched,
>  			   struct string_list *dwim_remote_names)
>  {
>  	struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
>  	const char *default_remote = NULL;
>  
> -	if (!repo_config_get_string_tmp(the_repository,
> +	if (!repo_config_get_string_tmp(repo,
>  					"checkout.defaultremote",
>  					&default_remote))
>  		cb_data.default_remote = default_remote;
> +	cb_data.repo = repo;
>  	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
>  	cb_data.dst_oid = oid;
>  	cb_data.remote_names = dwim_remote_names;
> diff --git a/checkout.h b/checkout.h
> index 0b185a0fc9..25fe7dfb7c 100644
> --- a/checkout.h
> +++ b/checkout.h
> @@ -3,6 +3,7 @@
>  
>  #include "hash.h"
>  
> +struct repository;
>  struct string_list;
>  
>  /*
> @@ -10,7 +11,8 @@ struct string_list;
>   * tracking branch.  Return the name of the remote if such a branch
>   * exists, NULL otherwise.
>   */
> -char *unique_tracking_name(const char *name,
> +char *unique_tracking_name(struct repository *repo,
> +			   const char *name,
>  			   struct object_id *oid,
>  			   int *dwim_remotes_matched,
>  			   struct string_list *dwim_remote_names);
>
> base-commit: c51408889c3b372814bc20b9431c3b74d0d268eb
Junio C Hamano· Oct 22, 2026, 19:30 UTC · re: Junio C Hamano · lore

Re: [Outreachy PATCH v2] checkout: pass the repository to unique_tracking_name()

Junio C Hamano <gitster@pobox.com> writes:
Show 16 quoted lines
> Replace the last sentence with something like:
>
>     The callers still cannot call unique_tracking_name() with an
>     arbitrary repository parameter other than the_repository, until
>     the call in unique_tracking_name() to for_each_remote(), which
>     only works with the_repository, is updated to take an arbitrary
>     repository instance.
>
> perhaps?
>
> And explained that way, it becomes dubious if we chose a good place
> to stop.  The updated unique_tracking_name() gives an illusion that
> callers now can pass an arbitrary repository to it and expect it to
> work correctly with the given repository, but it does not.
>
> So, I dunno.

I think we need to improve the situation by doing something like the attached patch. We already have similar provisions in environment.h (to clarify that have_git_dir() and friends only work with 'the_repository'), hash.h ('the_hash_algo'), hex.h (get_hash_hex(), get_oid_hex(), etc.), path.h (GIT_PATH_FUNC() macro) and repository.h ('the_repository' itself).

As we pass a 'struct repository' pointer to more functions, making sure they do not call functions that still work only with 'the_repository' will become more important.

I suspect that in remote.h, there may be more symbols that need to be hidden when 'USE_THE_REPOSITORY_VARIABLE' is not defined, to ensure functions written without the macro can truly work with an arbitrary repository instance. The attached patch is just to illustrate the direction.

This is totally outside your topic and meant for more senior developers who help polish the foundation of the codebase.

Thanks.
 remote.h | 2 ++
 1 file changed, 2 insertions(+)
Show changes to diff +2 −0
diff --git i/remote.h w/remote.h
index bfa5218c6e..eb7d4481eb 100644
--- i/remote.h
+++ w/remote.h
@@ -145,8 +145,10 @@ int remote_is_configured(struct remote *remote, int in_repo);
 
 typedef int each_remote_fn(struct remote *remote, void *priv);
 
+#ifdef USE_THE_REPOSITORY_VARIABLE
 /* iterate through struct remotes */
 int for_each_remote(each_remote_fn fn, void *priv);
+#endif
 
 int remote_has_url(struct remote *remote, const char *url);
 struct strvec *push_url_of_remote(struct remote *remote);
Okhuomon Ajayi· Oct 22, 2026, 21:23 UTC · re: Junio C Hamano · lore

Re: [Outreachy PATCH v2] checkout: pass the repository to unique_tracking_name()

On Thu, Oct 22, 2026 at 7:51 PM Junio C Hamano <gitster@pobox.com> wrote:
> The mention of for_each_remote() feels weirdly abrupt.  Many other
> things still rely on the the_repository global variable, but
> there must be a reason why for_each_remote() is singled out in the
> context of this patch.  What is it?
Thanks for the review.

You are right. I mentioned for_each_remote() because unique_tracking_name() calls it to go through the remotes, and it still reads them from the_repository. So the new repository parameter is not used everywhere yet, and a caller passing a different repository would not get the right result.

I will look into this more closely before sending anything else.

Thanks, Okhuomon

Okhuomon Ajayi· Oct 23, 2026, 21:15 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v3 0/2] checkout: make unique_tracking_name() use the given repository

unique_tracking_name() takes a repository since v2, but it still went through the remotes with for_each_remote(), which only works with the_repository. As Junio pointed out, that gave the illusion that callers could pass any repository.

Patch 1 adds repo_for_each_remote(), which takes a repository, and keeps for_each_remote() as a wrapper that passes the_repository. Patch 2 is v2 with unique_tracking_name() using repo_for_each_remote(), so everything it reads comes from the given repository.

Changes since v2:
- New patch 1 adding repo_for_each_remote().
- unique_tracking_name() now uses it instead of for_each_remote().
- Dropped the sentence about for_each_remote() from the commit
  message of patch 2.
Okhuomon Ajayi (2):
  remote: add repo_for_each_remote()
  checkout: pass the repository to unique_tracking_name()
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 13 +++++++------
 checkout.h         |  4 +++-
 remote.c           | 14 ++++++++++----
 remote.h           |  2 ++
 6 files changed, 28 insertions(+), 14 deletions(-)
base-commit: c51408889c3b372814bc20b9431c3b74d0d268eb
-- 
2.56.0
Okhuomon Ajayi· Oct 23, 2026, 21:15 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v3 1/2] remote: add repo_for_each_remote()

for_each_remote() always reads the remotes of the_repository.

Add repo_for_each_remote(), which takes a repository, and turn for_each_remote() into a wrapper that passes the_repository. Callers that are given a repository, such as unique_tracking_name() in the next patch, can then use the remotes of that repository.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 remote.c | 14 ++++++++++----
 remote.h |  2 ++
 2 files changed, 12 insertions(+), 4 deletions(-)
Show changes to 2 files +12 −4

remote.c, remote.h

diff --git a/remote.c b/remote.c
index 71170f36a9..d18f39d19a 100644
--- a/remote.c
+++ b/remote.c
@@ -869,14 +869,15 @@ int remote_is_configured(struct remote *remote, int in_repo)
 	return !!remote->origin;
 }
 
-int for_each_remote(each_remote_fn fn, void *priv)
+int repo_for_each_remote(struct repository *repo,
+			 each_remote_fn fn, void *priv)
 {
 	int i, result = 0;
-	read_config(the_repository, 0);
-	for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
+	read_config(repo, 0);
+	for (i = 0; i < repo->remote_state->remotes_nr && !result;
 	     i++) {
 		struct remote *remote =
-			the_repository->remote_state->remotes[i];
+			repo->remote_state->remotes[i];
 		if (!remote)
 			continue;
 		result = fn(remote, priv);
@@ -884,6 +885,11 @@ int for_each_remote(each_remote_fn fn, void *priv)
 	return result;
 }
 
+int for_each_remote(each_remote_fn fn, void *priv)
+{
+	return repo_for_each_remote(the_repository, fn, priv);
+}
+
 static void handle_duplicate(struct ref *ref1, struct ref *ref2)
 {
 	if (strcmp(ref1->name, ref2->name)) {
diff --git a/remote.h b/remote.h
index cca02033b9..2a0bf416d9 100644
--- a/remote.h
+++ b/remote.h
@@ -140,6 +140,8 @@ int remote_is_configured(struct remote *remote, int in_repo);
 typedef int each_remote_fn(struct remote *remote, void *priv);
 
 /* iterate through struct remotes */
+int repo_for_each_remote(struct repository *repo,
+			 each_remote_fn fn, void *priv);
 int for_each_remote(each_remote_fn fn, void *priv);
 
 int remote_has_url(struct remote *remote, const char *url);
-- 
2.56.0
Christian Couder· Nov 3, 2026, 12:41 UTC · re: Okhuomon Ajayi · lore

Re: [Outreachy PATCH v3 1/2] remote: add repo_for_each_remote()

On Fri, Oct 23, 2026 at 11:15 PM Okhuomon Ajayi <okhuomonajayi54@gmail.com> wrote:

Show 13 quoted lines
> -int for_each_remote(each_remote_fn fn, void *priv)
> +int repo_for_each_remote(struct repository *repo,
> +                        each_remote_fn fn, void *priv)
>  {
>         int i, result = 0;
> -       read_config(the_repository, 0);
> -       for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
> +       read_config(repo, 0);
> +       for (i = 0; i < repo->remote_state->remotes_nr && !result;
>              i++) {
>                 struct remote *remote =
> -                       the_repository->remote_state->remotes[i];
> +                       repo->remote_state->remotes[i];

It looks like moving from `the_repository` to `repo` could be a good opportunity to unwrap some lines. For example something like:

        int result = 0;
        read_config(repo, 0);
        for (int i = 0; i < repo->remote_state->remotes_nr && !result; i++) {
                struct remote *remote = repo->remote_state->remotes[i];
keeps lines under our 80 characters limit and is a bit nicer to read.
Thanks.
>                 if (!remote)
>                         continue;
>                 result = fn(remote, priv);
Okhuomon Ajayi· Oct 23, 2026, 21:15 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v3 2/2] checkout: pass the repository to unique_tracking_name()

unique_tracking_name() uses the_repository to read the "checkout.defaultRemote" configuration and look up the object ID of a remote-tracking branch.

Pass the repository to unique_tracking_name() and through tracking_name_data so check_tracking_name() can use it for the object lookup, and use repo_for_each_remote() so the remotes are also read from that repository. Update the three callers to pass the_repository.

This removes the need for USE_THE_REPOSITORY_VARIABLE in checkout.c.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 13 +++++++------
 checkout.h         |  4 +++-
 4 files changed, 16 insertions(+), 10 deletions(-)
Show changes to 4 files +16 −10

builtin/checkout.c, builtin/worktree.c, checkout.c, checkout.h

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2d575a4f57..bad49368b9 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1389,7 +1389,8 @@ static char *parse_remote_branch(const char *arg,
 	int num_matches = 0;
 	struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-	char *remote = unique_tracking_name(arg, rev, &num_matches,
+	char *remote = unique_tracking_name(the_repository, arg, rev,
+					    &num_matches,
 					    &matched_remote_names);
 
 	if (remote && could_be_checkout_paths) {
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..203456bd23 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -802,7 +802,8 @@ static char *dwim_branch(const struct add_opts *opts, const char *path, char **n
 		int num_matches = 0;
 		struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-		remote = unique_tracking_name(*new_branch, &oid, &num_matches,
+		remote = unique_tracking_name(the_repository, *new_branch,
+					      &oid, &num_matches,
 					      &matched_remote_names);
 		if (!remote && num_matches > 1) {
 			if (!opts->quiet &&
@@ -938,7 +939,8 @@ static int add(int ac, const char **av, const char *prefix,
 			int num_matches = 0;
 			struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-			remote = unique_tracking_name(branch, &oid, &num_matches,
+			remote = unique_tracking_name(the_repository, branch,
+						      &oid, &num_matches,
 						      &matched_remote_names);
 			if (remote) {
 				new_branch = branch;
diff --git a/checkout.c b/checkout.c
index a0d0229435..8a4812df8d 100644
--- a/checkout.c
+++ b/checkout.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "object-name.h"
 #include "remote.h"
@@ -11,6 +9,7 @@
 #include "string-list.h"
 
 struct tracking_name_data {
+	struct repository *repo;
 	/* const */ char *src_ref;
 	char *dst_ref;
 	struct object_id *dst_oid;
@@ -30,7 +29,7 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	memset(&query, 0, sizeof(struct refspec_item));
 	query.src = cb->src_ref;
 	if (remote_find_tracking(remote, &query) ||
-	    repo_get_oid(the_repository, query.dst, cb->dst_oid)) {
+	    repo_get_oid(cb->repo, query.dst, cb->dst_oid)) {
 		free(query.dst);
 		return 0;
 	}
@@ -51,21 +50,23 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	return 0;
 }
 
-char *unique_tracking_name(const char *name, struct object_id *oid,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name, struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names)
 {
 	struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
 	const char *default_remote = NULL;
 
-	if (!repo_config_get_string_tmp(the_repository,
+	if (!repo_config_get_string_tmp(repo,
 					"checkout.defaultremote",
 					&default_remote))
 		cb_data.default_remote = default_remote;
+	cb_data.repo = repo;
 	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
 	cb_data.dst_oid = oid;
 	cb_data.remote_names = dwim_remote_names;
-	for_each_remote(check_tracking_name, &cb_data);
+	repo_for_each_remote(repo, check_tracking_name, &cb_data);
 	if (dwim_remotes_matched)
 		*dwim_remotes_matched = cb_data.num_matches;
 	free(cb_data.src_ref);
diff --git a/checkout.h b/checkout.h
index 0b185a0fc9..25fe7dfb7c 100644
--- a/checkout.h
+++ b/checkout.h
@@ -3,6 +3,7 @@
 
 #include "hash.h"
 
+struct repository;
 struct string_list;
 
 /*
@@ -10,7 +11,8 @@ struct string_list;
  * tracking branch.  Return the name of the remote if such a branch
  * exists, NULL otherwise.
  */
-char *unique_tracking_name(const char *name,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name,
 			   struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names);
-- 
2.56.0
Okhuomon Ajayi· Nov 4, 2026, 09:05 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v4 0/2] checkout: make unique_tracking_name() use the given repository

Changes since v3:
- In patch 1, unwrap the lines in repo_for_each_remote() that are
  now short enough after the switch from the_repository to repo, as
  Christian suggested. Patch 2 is unchanged.
Thanks, Christian, for the review.
Okhuomon Ajayi (2):
  remote: add repo_for_each_remote()
  checkout: pass the repository to unique_tracking_name()
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 13 +++++++------
 checkout.h         |  4 +++-
 remote.c           | 18 +++++++++++-------
 remote.h           |  2 ++
 6 files changed, 29 insertions(+), 17 deletions(-)
base-commit: c51408889c3b372814bc20b9431c3b74d0d268eb
-- 
2.56.0
Okhuomon Ajayi· Nov 4, 2026, 09:05 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v4 1/2] remote: add repo_for_each_remote()

for_each_remote() always reads the remotes of the_repository.

Add repo_for_each_remote(), which takes a repository, and turn for_each_remote() into a wrapper that passes the_repository. Callers that are given a repository, such as unique_tracking_name() in the next patch, can then use the remotes of that repository.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 remote.c | 18 +++++++++++-------
 remote.h |  2 ++
 2 files changed, 13 insertions(+), 7 deletions(-)
Show changes to 2 files +13 −7

remote.c, remote.h

diff --git a/remote.c b/remote.c
index 71170f36a9..f070ad5a01 100644
--- a/remote.c
+++ b/remote.c
@@ -869,14 +869,13 @@ int remote_is_configured(struct remote *remote, int in_repo)
 	return !!remote->origin;
 }
 
-int for_each_remote(each_remote_fn fn, void *priv)
+int repo_for_each_remote(struct repository *repo,
+			 each_remote_fn fn, void *priv)
 {
-	int i, result = 0;
-	read_config(the_repository, 0);
-	for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
-	     i++) {
-		struct remote *remote =
-			the_repository->remote_state->remotes[i];
+	int result = 0;
+	read_config(repo, 0);
+	for (int i = 0; i < repo->remote_state->remotes_nr && !result; i++) {
+		struct remote *remote = repo->remote_state->remotes[i];
 		if (!remote)
 			continue;
 		result = fn(remote, priv);
@@ -884,6 +883,11 @@ int for_each_remote(each_remote_fn fn, void *priv)
 	return result;
 }
 
+int for_each_remote(each_remote_fn fn, void *priv)
+{
+	return repo_for_each_remote(the_repository, fn, priv);
+}
+
 static void handle_duplicate(struct ref *ref1, struct ref *ref2)
 {
 	if (strcmp(ref1->name, ref2->name)) {
diff --git a/remote.h b/remote.h
index cca02033b9..2a0bf416d9 100644
--- a/remote.h
+++ b/remote.h
@@ -140,6 +140,8 @@ int remote_is_configured(struct remote *remote, int in_repo);
 typedef int each_remote_fn(struct remote *remote, void *priv);
 
 /* iterate through struct remotes */
+int repo_for_each_remote(struct repository *repo,
+			 each_remote_fn fn, void *priv);
 int for_each_remote(each_remote_fn fn, void *priv);
 
 int remote_has_url(struct remote *remote, const char *url);
-- 
2.56.0
Okhuomon Ajayi· Nov 4, 2026, 09:05 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v4 2/2] checkout: pass the repository to unique_tracking_name()

unique_tracking_name() uses the_repository to read the "checkout.defaultRemote" configuration and look up the object ID of a remote-tracking branch.

Pass the repository to unique_tracking_name() and through tracking_name_data so check_tracking_name() can use it for the object lookup, and use repo_for_each_remote() so the remotes are also read from that repository. Update the three callers to pass the_repository.

This removes the need for USE_THE_REPOSITORY_VARIABLE in checkout.c.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 13 +++++++------
 checkout.h         |  4 +++-
 4 files changed, 16 insertions(+), 10 deletions(-)
Show changes to 4 files +16 −10

builtin/checkout.c, builtin/worktree.c, checkout.c, checkout.h

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2d575a4f57..bad49368b9 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1389,7 +1389,8 @@ static char *parse_remote_branch(const char *arg,
 	int num_matches = 0;
 	struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-	char *remote = unique_tracking_name(arg, rev, &num_matches,
+	char *remote = unique_tracking_name(the_repository, arg, rev,
+					    &num_matches,
 					    &matched_remote_names);
 
 	if (remote && could_be_checkout_paths) {
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 77ecd0f71f..203456bd23 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -802,7 +802,8 @@ static char *dwim_branch(const struct add_opts *opts, const char *path, char **n
 		int num_matches = 0;
 		struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-		remote = unique_tracking_name(*new_branch, &oid, &num_matches,
+		remote = unique_tracking_name(the_repository, *new_branch,
+					      &oid, &num_matches,
 					      &matched_remote_names);
 		if (!remote && num_matches > 1) {
 			if (!opts->quiet &&
@@ -938,7 +939,8 @@ static int add(int ac, const char **av, const char *prefix,
 			int num_matches = 0;
 			struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
 
-			remote = unique_tracking_name(branch, &oid, &num_matches,
+			remote = unique_tracking_name(the_repository, branch,
+						      &oid, &num_matches,
 						      &matched_remote_names);
 			if (remote) {
 				new_branch = branch;
diff --git a/checkout.c b/checkout.c
index a0d0229435..8a4812df8d 100644
--- a/checkout.c
+++ b/checkout.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "object-name.h"
 #include "remote.h"
@@ -11,6 +9,7 @@
 #include "string-list.h"
 
 struct tracking_name_data {
+	struct repository *repo;
 	/* const */ char *src_ref;
 	char *dst_ref;
 	struct object_id *dst_oid;
@@ -30,7 +29,7 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	memset(&query, 0, sizeof(struct refspec_item));
 	query.src = cb->src_ref;
 	if (remote_find_tracking(remote, &query) ||
-	    repo_get_oid(the_repository, query.dst, cb->dst_oid)) {
+	    repo_get_oid(cb->repo, query.dst, cb->dst_oid)) {
 		free(query.dst);
 		return 0;
 	}
@@ -51,21 +50,23 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	return 0;
 }
 
-char *unique_tracking_name(const char *name, struct object_id *oid,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name, struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names)
 {
 	struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
 	const char *default_remote = NULL;
 
-	if (!repo_config_get_string_tmp(the_repository,
+	if (!repo_config_get_string_tmp(repo,
 					"checkout.defaultremote",
 					&default_remote))
 		cb_data.default_remote = default_remote;
+	cb_data.repo = repo;
 	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
 	cb_data.dst_oid = oid;
 	cb_data.remote_names = dwim_remote_names;
-	for_each_remote(check_tracking_name, &cb_data);
+	repo_for_each_remote(repo, check_tracking_name, &cb_data);
 	if (dwim_remotes_matched)
 		*dwim_remotes_matched = cb_data.num_matches;
 	free(cb_data.src_ref);
diff --git a/checkout.h b/checkout.h
index 0b185a0fc9..25fe7dfb7c 100644
--- a/checkout.h
+++ b/checkout.h
@@ -3,6 +3,7 @@
 
 #include "hash.h"
 
+struct repository;
 struct string_list;
 
 /*
@@ -10,7 +11,8 @@ struct string_list;
  * tracking branch.  Return the name of the remote if such a branch
  * exists, NULL otherwise.
  */
-char *unique_tracking_name(const char *name,
+char *unique_tracking_name(struct repository *repo,
+			   const char *name,
 			   struct object_id *oid,
 			   int *dwim_remotes_matched,
 			   struct string_list *dwim_remote_names);
-- 
2.56.0

← back to recent threads