git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 3/6] Add RefSpec.expandFromDestination for reverse mappings
Date: Thu, 12 Feb 2009 15:54:37 -0800	[thread overview]
Message-ID: <1234482880-1316-4-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1234482880-1316-3-git-send-email-spearce@spearce.org>

This makes it easy to loop through the destination's refs and see
if any match up to a source name which doesn't actually exist in
the set of available source refs.  In such cases, we may want to
delete the destination ref.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../spearce/jgit/transport/RefSpecTestCase.java    |   22 ++++++
 .../src/org/spearce/jgit/transport/RefSpec.java    |   73 ++++++++++++++------
 2 files changed, 75 insertions(+), 20 deletions(-)

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RefSpecTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RefSpecTestCase.java
index 341b4a4..11e7cdb 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RefSpecTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RefSpecTestCase.java
@@ -232,4 +232,26 @@ public void testSetSourceDestination() {
 		assertEquals("HEAD", a.toString());
 		assertEquals("refs/heads/*:refs/remotes/origin/*", b.toString());
 	}
+
+	public void testExpandFromDestination_NonWildcard() {
+		final String src = "refs/heads/master";
+		final String dst = "refs/remotes/origin/master";
+		final RefSpec a = new RefSpec(src + ":" + dst);
+		final RefSpec r = a.expandFromDestination(dst);
+		assertSame(a, r);
+		assertFalse(r.isWildcard());
+		assertEquals(src, r.getSource());
+		assertEquals(dst, r.getDestination());
+	}
+
+	public void testExpandFromDestination_Wildcard() {
+		final String src = "refs/heads/master";
+		final String dst = "refs/remotes/origin/master";
+		final RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
+		final RefSpec r = a.expandFromDestination(dst);
+		assertNotSame(a, r);
+		assertFalse(r.isWildcard());
+		assertEquals(src, r.getSource());
+		assertEquals(dst, r.getDestination());
+	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/RefSpec.java b/org.spearce.jgit/src/org/spearce/jgit/transport/RefSpec.java
index 521110b..bc6ea3a 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/RefSpec.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/RefSpec.java
@@ -135,24 +135,6 @@ else if (isWildcard(srcName) || isWildcard(dstName))
 		}
 	}
 
-	/**
-	 * Expand a wildcard specification.
-	 * 
-	 * @param p
-	 *            the wildcard specification we should base ourselves on.
-	 * @param name
-	 *            actual name that matched the source of <code>p</code>.
-	 */
-	protected RefSpec(final RefSpec p, final String name) {
-		final String pdst = p.getDestination();
-		if (p.getSource() == null || pdst == null)
-			throw new IllegalArgumentException("Cannot expand from " + p);
-		force = p.isForceUpdate();
-		srcName = name;
-		dstName = pdst.substring(0, pdst.length() - 1)
-				+ name.substring(p.getSource().length() - 1);
-	}
-
 	private RefSpec(final RefSpec p) {
 		force = p.isForceUpdate();
 		wildcard = p.isWildcard();
@@ -349,7 +331,16 @@ public boolean matchDestination(final Ref r) {
 	 *         wildcard.
 	 */
 	public RefSpec expandFromSource(final String r) {
-		return isWildcard() ? new RefSpec(this, r) : this;
+		return isWildcard() ? new RefSpec(this).expandFromSourceImp(r) : this;
+	}
+
+	private RefSpec expandFromSourceImp(final String name) {
+		final String psrc = srcName, pdst = dstName;
+		wildcard = false;
+		srcName = name;
+		dstName = pdst.substring(0, pdst.length() - 1)
+				+ name.substring(psrc.length() - 1);
+		return this;
 	}
 
 	/**
@@ -366,7 +357,49 @@ public RefSpec expandFromSource(final String r) {
 	 *         wildcard.
 	 */
 	public RefSpec expandFromSource(final Ref r) {
-		return isWildcard() ? new RefSpec(this, r.getName()) : this;
+		return expandFromSource(r.getName());
+	}
+
+	/**
+	 * Expand this specification to exactly match a ref name.
+	 * <p>
+	 * Callers must first verify the passed ref name matches this specification,
+	 * otherwise expansion results may be unpredictable.
+	 * 
+	 * @param r
+	 *            a ref name that matched our destination specification. Could
+	 *            be a wildcard also.
+	 * @return a new specification expanded from provided ref name. Result
+	 *         specification is wildcard if and only if provided ref name is
+	 *         wildcard.
+	 */
+	public RefSpec expandFromDestination(final String r) {
+		return isWildcard() ? new RefSpec(this).expandFromDstImp(r) : this;
+	}
+
+	private RefSpec expandFromDstImp(final String name) {
+		final String psrc = srcName, pdst = dstName;
+		wildcard = false;
+		srcName = psrc.substring(0, psrc.length() - 1)
+				+ name.substring(pdst.length() - 1);
+		dstName = name;
+		return this;
+	}
+
+	/**
+	 * Expand this specification to exactly match a ref.
+	 * <p>
+	 * Callers must first verify the passed ref matches this specification,
+	 * otherwise expansion results may be unpredictable.
+	 * 
+	 * @param r
+	 *            a ref that matched our destination specification.
+	 * @return a new specification expanded from provided ref name. Result
+	 *         specification is wildcard if and only if provided ref name is
+	 *         wildcard.
+	 */
+	public RefSpec expandFromDestination(final Ref r) {
+		return expandFromDestination(r.getName());
 	}
 
 	private boolean match(final String refName, final String s) {
-- 
1.6.2.rc0.226.gf08f

  reply	other threads:[~2009-02-12 23:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-12 23:54 [JGIT PATCH 0/6] Add prune of stale tracking branches to fetch Shawn O. Pearce
2009-02-12 23:54 ` [JGIT PATCH 1/6] Fix RefUpdate.delete to update the result status Shawn O. Pearce
2009-02-12 23:54   ` [JGIT PATCH 2/6] Add setBoolean, setInt to RepositoryConfig Shawn O. Pearce
2009-02-12 23:54     ` Shawn O. Pearce [this message]
2009-02-12 23:54       ` [JGIT PATCH 4/6] Add the remote.name.mirror flag to RemoteConfig Shawn O. Pearce
2009-02-12 23:54         ` [JGIT PATCH 5/6] Don't pass TagOpt to FetchProcess, get it from the Transport Shawn O. Pearce
2009-02-12 23:54           ` [JGIT PATCH 6/6] Teach fetch to prune stale tracking branches Shawn O. Pearce
2009-02-13  0:37 ` [JGIT PATCH 0/6] Add prune of stale tracking branches to fetch Junio C Hamano
2009-02-13  0:42   ` Shawn O. Pearce
2009-02-13  0:48     ` Junio C Hamano

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=1234482880-1316-4-git-send-email-spearce@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@dewire.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).