From d79163389dae35ae0a6c2fed8e63eb2207a59a54 Mon Sep 17 00:00:00 2001 From: Marek Zawirski Date: Sun, 17 Aug 2008 22:44:06 +0200 Subject: [PATCH] Create ListRemoteOperation for listing remote repo branches This code is a common task, so it's now bundled in this operation with simple progress monitor information. Signed-off-by: Marek Zawirski Signed-off-by: Robin Rosenberg --- .../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(+) 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 8fbda4aa..5974a5f2 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 3b3c229e..c412161c 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 00000000..ecb2697d --- /dev/null +++ b/org.spearce.egit.core/src/org/spearce/egit/core/op/ListRemoteOperation.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (C) 2008, Marek Zawirski + * Copyright (C) 2008, Shawn O. Pearce + * + * 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 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 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 -- 2.11.4.GIT