[EGIT PATCH 25/31] Create ListRemoteOperation for listing remote repo branches

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This code is a common task, so it's now bundled in this operation
with simple progress monitor information.

Signed-off-by: Marek Zawirski <marek.zawirski@xxxxxxxxx>
---
 .../src/org/spearce/egit/core/CoreText.java        |    3 +
 .../src/org/spearce/egit/core/coretext.properties  |    2 +
 .../spearce/egit/core/op/ListRemoteOperation.java  |  104 ++++++++++++++++++++
 3 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.egit.core/src/org/spearce/egit/core/op/ListRemoteOperation.java

diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/CoreText.java b/org.spearce.egit.core/src/org/spearce/egit/core/CoreText.java
index 8fbda4a..5974a5f 100644
--- a/org.spearce.egit.core/src/org/spearce/egit/core/CoreText.java
+++ b/org.spearce.egit.core/src/org/spearce/egit/core/CoreText.java
@@ -95,6 +95,9 @@ public class CoreText extends NLS {
 	/** */
 	public static String CloneOperation_title;
 
+	/** */
+	public static String ListRemoteOperation_title;
+
 	static {
 		final Class c = CoreText.class;
 		initializeMessages(c.getPackage().getName() + ".coretext", c);
diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/coretext.properties b/org.spearce.egit.core/src/org/spearce/egit/core/coretext.properties
index 3b3c229..c412161 100644
--- a/org.spearce.egit.core/src/org/spearce/egit/core/coretext.properties
+++ b/org.spearce.egit.core/src/org/spearce/egit/core/coretext.properties
@@ -55,3 +55,5 @@ CheckpointJob_writingTrees=modified trees
 CheckpointJob_failed=Failed to write modified objects.
 
 CloneOperation_title=Cloning from {0}
+
+ListRemoteOperation_title=Getting remote branches information
diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/op/ListRemoteOperation.java b/org.spearce.egit.core/src/org/spearce/egit/core/op/ListRemoteOperation.java
new file mode 100644
index 0000000..d3f777a
--- /dev/null
+++ b/org.spearce.egit.core/src/org/spearce/egit/core/op/ListRemoteOperation.java
@@ -0,0 +1,104 @@
+/*******************************************************************************
+ * Copyright (C) 2008, Marek Zawirski <marek.zawirski@xxxxxxxxx>
+ * Copyright (C) 2008, Shawn O. Pearce <spearce@xxxxxxxxxxx>
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * See LICENSE for the full license text, also available.
+ *******************************************************************************/
+package org.spearce.egit.core.op;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collection;
+import java.util.Map;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.spearce.egit.core.CoreText;
+import org.spearce.jgit.errors.NotSupportedException;
+import org.spearce.jgit.errors.TransportException;
+import org.spearce.jgit.lib.Ref;
+import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.transport.Connection;
+import org.spearce.jgit.transport.Transport;
+import org.spearce.jgit.transport.URIish;
+
+/**
+ * Operation of listing remote repository advertised refs.
+ */
+public class ListRemoteOperation implements IRunnableWithProgress {
+	private final Repository localDb;
+
+	private final URIish uri;
+
+	private Map<String, Ref> remoteRefsMap;
+
+	/**
+	 * Create listing operation for specified local repository (needed by
+	 * transport) and remote repository URI.
+	 * 
+	 * @param localDb
+	 *            local repository (needed for transport) where fetch would
+	 *            occur.
+	 * @param uri
+	 *            URI of remote repository to list.
+	 */
+	public ListRemoteOperation(final Repository localDb, final URIish uri) {
+		this.localDb = localDb;
+		this.uri = uri;
+	}
+
+	/**
+	 * @return collection of refs advertised by remote side.
+	 * @throws IllegalStateException
+	 *             if error occurred during earlier remote refs listing.
+	 */
+	public Collection<Ref> getRemoteRefs() {
+		checkState();
+		return remoteRefsMap.values();
+	}
+
+	/**
+	 * @param refName
+	 *            remote ref name to search for.
+	 * @return ref with specified refName or null if not found.
+	 * @throws IllegalStateException
+	 *             if error occurred during earlier remote refs listing.
+	 */
+	public Ref getRemoteRef(final String refName) {
+		checkState();
+		return remoteRefsMap.get(refName);
+	}
+
+	public void run(IProgressMonitor pm) throws InvocationTargetException,
+			InterruptedException {
+		Transport transport = null;
+		Connection connection = null;
+		try {
+			transport = Transport.open(localDb, uri);
+
+			if (pm != null)
+				pm.beginTask(CoreText.ListRemoteOperation_title,
+						IProgressMonitor.UNKNOWN);
+			connection = transport.openFetch();
+			remoteRefsMap = connection.getRefsMap();
+		} catch (NotSupportedException e) {
+			throw new InvocationTargetException(e);
+		} catch (TransportException e) {
+			throw new InvocationTargetException(e);
+		} finally {
+			if (connection != null)
+				connection.close();
+			if (transport != null)
+				transport.close();
+			if (pm != null)
+				pm.done();
+		}
+	}
+
+	private void checkState() {
+		if (remoteRefsMap == null)
+			throw new IllegalStateException(
+					"Error occurred during remote repo listing, no refs available");
+	}
+}
\ No newline at end of file
-- 
1.5.6.3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux