Skip to contentSkip to conversation

checkout: use repository parameter in unique_tracking_name

PATCH In review Okhuomon Ajayi 4 participants 17 messages Oct 17 – Nov 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkout: use repository parameter in unique_tracking_name

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.

---
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 11 ++++++-----
 checkout.h         |  4 +++-
 4 files changed, 15 insertions(+), 9 deletions(-)
builtin/checkout.c +2 -1
index 2d575a4f57..bad49368b9 100644
@@ -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) {
builtin/worktree.c +4 -2
index 77ecd0f71f..203456bd23 100644
@@ -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;
checkout.c +6 -5
index a0d0229435..f0fb40d48e 100644
@@ -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;
checkout.h +3 -1
index 0b185a0fc9..25fe7dfb7c 100644
@@ -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);
Signature · 1 line
2.54.0
#1 lore raw
Quoted 2 lines

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 ?

Quoted 4 lines

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.

Quoted 3 lines

This removes the need for USE_THE_REPOSITORY_VARIABLE in checkout.c.

Thanks.

#2 lore raw
Quoted 6 lines · Usman Akinyemi
Quoted 2 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.

#3 lore raw

PATCH v2 1/1 checkout: pass the repository to unique_tracking_name()

In reply to Okhuomon Ajayi

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.

---
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(-)
builtin/checkout.c +2 -1
index 2d575a4f57..bad49368b9 100644
@@ -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) {
builtin/worktree.c +4 -2
index 77ecd0f71f..203456bd23 100644
@@ -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;
checkout.c +6 -5
index a0d0229435..f0fb40d48e 100644
@@ -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;
checkout.h +3 -1
index 0b185a0fc9..25fe7dfb7c 100644
@@ -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

Signature · 1 line
2.56.0
#4 lore raw

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

Quoted 4 lines · Okhuomon Ajayi

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

Quoted 8 lines

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.

Quoted 123 lines

---
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(-)
builtin/checkout.c +2 -1
index 2d575a4f57..bad49368b9 100644
@@ -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) {
builtin/worktree.c +4 -2
index 77ecd0f71f..203456bd23 100644
@@ -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;
checkout.c +6 -5
index a0d0229435..f0fb40d48e 100644
@@ -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;
checkout.h +3 -1
index 0b185a0fc9..25fe7dfb7c 100644
@@ -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

#5 lore raw

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

Quoted 8 lines · Junio C Hamano

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.

Quoted 8 lines

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(+)
remote.h +2 -0
index bfa5218c6e..eb7d4481eb 100644
@@ -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);
#6 lore raw

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

In reply to Junio C Hamano
Quoted 4 lines · Junio C Hamano

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

#7 lore raw

v3 cover checkout: make unique_tracking_name() use the given repository

In reply to Okhuomon Ajayi

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

Signature · 1 line
2.56.0
#8 lore raw

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.

---
 remote.c | 14 ++++++++++----
 remote.h |  2 ++
 2 files changed, 12 insertions(+), 4 deletions(-)
remote.c +10 -4
index 71170f36a9..d18f39d19a 100644
@@ -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)) {
remote.h +2 -0
index cca02033b9..2a0bf416d9 100644
@@ -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);
Signature · 1 line
2.56.0
#9 lore raw

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

remote.c int remote_is_configured(struct remote *remote, int in_repo) · line 880

Quoted 13 lines · Okhuomon Ajayi
remote.c +5 -4
-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.

Quoted 3 lines
                if (!remote)
                        continue;
                result = fn(remote, priv);
#10 lore raw

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

In reply to Okhuomon Ajayi

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.

---
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 13 +++++++------
 checkout.h         |  4 +++-
 4 files changed, 16 insertions(+), 10 deletions(-)
builtin/checkout.c +2 -1
index 2d575a4f57..bad49368b9 100644
@@ -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) {
builtin/worktree.c +4 -2
index 77ecd0f71f..203456bd23 100644
@@ -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;
checkout.c +7 -6
index a0d0229435..8a4812df8d 100644
@@ -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);
checkout.h +3 -1
index 0b185a0fc9..25fe7dfb7c 100644
@@ -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);
Signature · 1 line
2.56.0
#11 lore raw

v4 cover checkout: make unique_tracking_name() use the given repository

In reply to Okhuomon Ajayi

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

Signature · 1 line
2.56.0
#12 lore raw

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.

---
 remote.c | 18 +++++++++++-------
 remote.h |  2 ++
 2 files changed, 13 insertions(+), 7 deletions(-)
remote.c +11 -7
index 71170f36a9..f070ad5a01 100644
@@ -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)) {
remote.h +2 -0
index cca02033b9..2a0bf416d9 100644
@@ -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);
Signature · 1 line
2.56.0
#13 lore raw

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

In reply to Okhuomon Ajayi

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.

---
 builtin/checkout.c |  3 ++-
 builtin/worktree.c |  6 ++++--
 checkout.c         | 13 +++++++------
 checkout.h         |  4 +++-
 4 files changed, 16 insertions(+), 10 deletions(-)
builtin/checkout.c +2 -1
index 2d575a4f57..bad49368b9 100644
@@ -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) {
builtin/worktree.c +4 -2
index 77ecd0f71f..203456bd23 100644
@@ -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;
checkout.c +7 -6
index a0d0229435..8a4812df8d 100644
@@ -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);
checkout.h +3 -1
index 0b185a0fc9..25fe7dfb7c 100644
@@ -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);
Signature · 1 line
2.56.0
#14 lore raw

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

In reply to Okhuomon Ajayi

Hi Junio, Christian,

Gentle ping on this series. It's in 'seen' marked "Needs review". Is there anything you'd like me to change?

Thanks,
Okhuomon

#15 lore raw

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

In reply to Okhuomon Ajayi
Quoted 5 lines · Okhuomon Ajayi

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.

This v4 looks good to me too.

Thanks!

#16 lore raw

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

Quoted 11 lines · Christian Couder
Quoted 5 lines · Okhuomon Ajayi

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.

This v4 looks good to me too.

Thanks!

Thanks for writing and reviewing.