git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/2] Add for_each_remote() function, and extend remote_find_tracking()

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jul 10, 2007, 17:48 UTC
Message-ID
<Pine.LNX.4.64.0707101848050.4047@racer.site>
In-Reply-To
<7vy7hosv7v.fsf@assigned-by-dhcp.cox.net>
The function for_each_remote() does exactly what the name suggests.

The function remote_find_tracking() was extended to be able to search remote refs for a given local ref. You have to set the parameter "reverse" to true for that behavior.

Both changes are required for the next step: simplification of git-branch's --track functionality.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	Thanks, Daniel and Junio, for your suggestions.
 remote.c    |   60 ++++++++++++++++++++++++++++++++++++++++++++--------------
 remote.h    |    5 +++-
 send-pack.c |    4 +-
 3 files changed, 51 insertions(+), 18 deletions(-)
diff --git a/remote.c b/remote.c
index 09c4279..bb774d0 100644
--- a/remote.c
+++ b/remote.c
@@ -279,6 +279,25 @@ struct remote *remote_get(const char *name)
 	return ret;
 }
 
+int for_each_remote(each_remote_fn fn, void *priv)
+{
+	int i, result = 0;
+	read_config();
+	for (i = 0; i < allocated_remotes && !result; i++) {
+		struct remote *r = remotes[i];
+		if (!r)
+			continue;
+		if (!r->fetch)
+			r->fetch = parse_ref_spec(r->fetch_refspec_nr,
+					r->fetch_refspec);
+		if (!r->push)
+			r->push = parse_ref_spec(r->push_refspec_nr,
+					r->push_refspec);
+		result = fn(r, priv);
+	}
+	return result;
+}
+
 int remote_has_uri(struct remote *remote, const char *uri)
 {
 	int i;
@@ -291,32 +310,43 @@ int remote_has_uri(struct remote *remote, const char *uri)
 
 int remote_find_tracking(struct remote *remote, struct refspec *refspec)
 {
+	int find_src = refspec->src == NULL;
+	char *needle, **result;
 	int i;
+
+	if (find_src) {
+		if (refspec->dst == NULL)
+			return error("find_tracking: need either src or dst");
+		needle = refspec->dst;
+		result = &refspec->src;
+	} else {
+		needle = refspec->src;
+		result = &refspec->dst;
+	}
+
 	for (i = 0; i < remote->fetch_refspec_nr; i++) {
 		struct refspec *fetch = &remote->fetch[i];
+		const char *key = find_src ? fetch->dst : fetch->src;
+		const char *value = find_src ? fetch->src : fetch->dst;
 		if (!fetch->dst)
 			continue;
 		if (fetch->pattern) {
-			if (!prefixcmp(refspec->src, fetch->src)) {
-				refspec->dst =
-					xmalloc(strlen(fetch->dst) +
-						strlen(refspec->src) -
-						strlen(fetch->src) + 1);
-				strcpy(refspec->dst, fetch->dst);
-				strcpy(refspec->dst + strlen(fetch->dst),
-				       refspec->src + strlen(fetch->src));
-				refspec->force = fetch->force;
-				return 0;
-			}
-		} else {
-			if (!strcmp(refspec->src, fetch->src)) {
-				refspec->dst = xstrdup(fetch->dst);
+			if (!prefixcmp(needle, key)) {
+				*result = xmalloc(strlen(value) +
+						  strlen(needle) -
+						  strlen(key) + 1);
+				strcpy(*result, value);
+				strcpy(*result + strlen(value),
+				       needle + strlen(key));
 				refspec->force = fetch->force;
 				return 0;
 			}
+		} else if (!strcmp(needle, key)) {
+			*result = xstrdup(value);
+			refspec->force = fetch->force;
+			return 0;
 		}
 	}
-	refspec->dst = NULL;
 	return -1;
 }
 
diff --git a/remote.h b/remote.h
index 080b7da..17b8b5b 100644
--- a/remote.h
+++ b/remote.h
@@ -20,13 +20,16 @@ struct remote {
 
 struct remote *remote_get(const char *name);
 
+typedef int each_remote_fn(struct remote *remote, void *priv);
+int for_each_remote(each_remote_fn fn, void *priv);
+
 int remote_has_uri(struct remote *remote, const char *uri);
 
 struct refspec {
 	unsigned force : 1;
 	unsigned pattern : 1;
 
-	const char *src;
+	char *src;
 	char *dst;
 };
 
diff --git a/send-pack.c b/send-pack.c
index fecbda9..9fc8a81 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -305,8 +305,8 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
 		if (remote) {
 			struct refspec rs;
 			rs.src = ref->name;
-			remote_find_tracking(remote, &rs);
-			if (rs.dst) {
+			rs.dst = NULL;
+			if (!remote_find_tracking(remote, &rs)) {
 				struct ref_lock *lock;
 				fprintf(stderr, " Also local %s\n", rs.dst);
 				if (will_delete_ref) {
-- 
1.5.3.rc0.2783.gf3f7
Previous: Daniel BarkalowNext: Junio C Hamano
Message 17 of 23 in “git-branch: default to --track”
  1. git-branch: default to --trackJohannes Schindelin, Jul 6, 2007
  2. Junio C HamanoJul 8, 2007
  3. branch.autosetupmerge: allow boolean values, or "all"Johannes Schindelin, Jul 8, 2007
  4. Junio C HamanoJul 8, 2007
  5. Johannes SchindelinJul 8, 2007
  6. Paolo BonziniJul 9, 2007
  7. Junio C HamanoJul 9, 2007
  8. branch --track: code cleanup and saner handling of local branchesJohannes Schindelin, Jul 9, 2007
  9. Junio C HamanoJul 9, 2007
  10. Johannes SchindelinJul 9, 2007
  11. Junio C HamanoJul 9, 2007
  12. 1/2 Add for_each_remote() function, and extend remote_find_tracking()Johannes Schindelin, Jul 10, 2007
  13. Daniel BarkalowJul 10, 2007
  14. Johannes SchindelinJul 10, 2007
  15. Junio C HamanoJul 10, 2007
  16. Daniel BarkalowJul 10, 2007
  17. 1/2 Add for_each_remote() function, and extend remote_find_tracking()Johannes Schindelin, Jul 10, 2007
  18. Junio C HamanoJul 10, 2007
  19. Johannes SchindelinJul 10, 2007
  20. Daniel BarkalowJul 10, 2007
  21. 2/2 branch --track: code cleanup and saner handling of local branchesJohannes Schindelin, Jul 10, 2007
  22. 2/2 branch --track: code cleanup and saner handling of local branchesJohannes Schindelin, Jul 10, 2007
  23. Johannes SchindelinJul 9, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.