git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Junio C Hamano <gitster@pobox.com>
Cc: Paolo Bonzini <bonzini@gnu.org>,
	git@vger.kernel.org, Daniel Barkalow <barkalow@iabervon.org>
Subject: [PATCH 1/2] Add for_each_remote() function, and extend remote_find_tracking()
Date: Tue, 10 Jul 2007 04:02:02 +0100 (BST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0707100401070.4131@racer.site> (raw)
In-Reply-To: <7vzm25tex6.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>
---

	You're right. I completely missed that functionality. Well, a
	few tweaks were needed. If this clashes too seriously with
	Daniel's work, I will gladly redo it after his changes are
	in "next".

 remote.c    |   42 ++++++++++++++++++++++++++++++++----------
 remote.h    |    7 ++++++-
 send-pack.c |    3 +--
 3 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/remote.c b/remote.c
index cf98a44..21adb0d 100644
--- a/remote.c
+++ b/remote.c
@@ -279,6 +279,26 @@ 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; 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);
+		if ((result = fn(r, priv)))
+			break;
+	}
+	return result;
+}
+
 int remote_has_uri(struct remote *remote, const char *uri)
 {
 	int i;
@@ -289,34 +309,36 @@ int remote_has_uri(struct remote *remote, const char *uri)
 	return 0;
 }
 
-int remote_find_tracking(struct remote *remote, struct refspec *refspec)
+int remote_find_tracking(struct remote *remote, struct refspec *refspec,
+		int reverse)
 {
 	int i;
 	for (i = 0; i < remote->fetch_refspec_nr; i++) {
 		struct refspec *fetch = &remote->fetch[i];
+		const char *src = reverse ? fetch->dst : fetch->src;
+		const char *dst = reverse ? fetch->src : fetch->dst;
 		if (!fetch->dst)
 			continue;
 		if (fetch->pattern) {
-			if (!prefixcmp(refspec->src, fetch->src)) {
+			if (!prefixcmp(refspec->src, src)) {
 				refspec->dst =
-					xmalloc(strlen(fetch->dst) +
+					xmalloc(strlen(dst) +
 						strlen(refspec->src) -
-						strlen(fetch->src) + 1);
-				strcpy(refspec->dst, fetch->dst);
-				strcpy(refspec->dst + strlen(fetch->dst),
-				       refspec->src + strlen(fetch->src));
+						strlen(src) + 1);
+				strcpy(refspec->dst, dst);
+				strcpy(refspec->dst + strlen(dst),
+				       refspec->src + strlen(src));
 				refspec->force = fetch->force;
 				return 0;
 			}
 		} else {
-			if (!strcmp(refspec->src, fetch->src)) {
-				refspec->dst = xstrdup(fetch->dst);
+			if (!strcmp(refspec->src, src)) {
+				refspec->dst = xstrdup(dst);
 				refspec->force = fetch->force;
 				return 0;
 			}
 		}
 	}
-	refspec->dst = NULL;
 	return -1;
 }
 
diff --git a/remote.h b/remote.h
index 01dbcef..9ab7eb6 100644
--- a/remote.h
+++ b/remote.h
@@ -20,6 +20,9 @@ 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 {
@@ -35,7 +38,9 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
 
 /*
  * For the given remote, reads the refspec's src and sets the other fields.
+ * If reverse is 1, the given src is the local ref, and we want the remote.
  */
-int remote_find_tracking(struct remote *remote, struct refspec *refspec);
+int remote_find_tracking(struct remote *remote, struct refspec *refspec,
+	int reverse);
 
 #endif
diff --git a/send-pack.c b/send-pack.c
index fecbda9..9fdd7b4 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -305,8 +305,7 @@ 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) {
+			if (!remote_find_tracking(remote, &rs, 0)) {
 				struct ref_lock *lock;
 				fprintf(stderr, " Also local %s\n", rs.dst);
 				if (will_delete_ref) {
-- 
1.5.3.rc0.2769.gd9be2

  reply	other threads:[~2007-07-10  3:09 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-06 21:54 [RFC/PATCH] git-branch: default to --track Johannes Schindelin
2007-07-08  8:59 ` Junio C Hamano
2007-07-08 12:41   ` [PATCH] branch.autosetupmerge: allow boolean values, or "all" Johannes Schindelin
2007-07-08 18:41     ` Junio C Hamano
2007-07-08 19:15       ` Johannes Schindelin
2007-07-09  1:59       ` Paolo Bonzini
2007-07-09  2:27         ` Junio C Hamano
2007-07-09 11:35           ` [PATCH] branch --track: code cleanup and saner handling of local branches Johannes Schindelin
2007-07-09 21:05             ` Junio C Hamano
2007-07-09 21:05               ` Johannes Schindelin
2007-07-09 22:01                 ` Junio C Hamano
2007-07-10  3:02                   ` Johannes Schindelin [this message]
2007-07-10  3:55                     ` [PATCH 1/2] Add for_each_remote() function, and extend remote_find_tracking() Daniel Barkalow
2007-07-10 14:11                       ` Johannes Schindelin
2007-07-10  5:07                     ` Junio C Hamano
2007-07-10  5:23                       ` Daniel Barkalow
2007-07-10 17:48                       ` [PATCH v2 " Johannes Schindelin
2007-07-10 18:38                         ` Junio C Hamano
2007-07-10 19:28                           ` Johannes Schindelin
2007-07-10 21:09                           ` Daniel Barkalow
2007-07-10 17:50                       ` [PATCH v2 2/2] branch --track: code cleanup and saner handling of local branches Johannes Schindelin
2007-07-10  3:05                   ` [PATCH " Johannes Schindelin
2007-07-09 11:28         ` [PATCH] branch.autosetupmerge: allow boolean values, or "all" Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0707100401070.4131@racer.site \
    --to=johannes.schindelin@gmx.de \
    --cc=barkalow@iabervon.org \
    --cc=bonzini@gnu.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).