aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java131
1 files changed, 50 insertions, 81 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java
index 3363a0fc8f..c3415581ef 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java
@@ -1,47 +1,15 @@
/*
- * Copyright (C) 2011, Christoph Brill <egore911@egore911.de>
- * and other copyright owners as documented in the project's IP log.
+ * Copyright (C) 2011, 2022 Christoph Brill <egore911@egore911.de> and others
*
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * https://www.eclipse.org/org/documents/edl-v10.php.
*
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.api;
+import java.io.IOException;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.ArrayList;
@@ -53,8 +21,8 @@ import java.util.Map;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.JGitInternalException;
+import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.NotSupportedException;
-import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
@@ -63,6 +31,8 @@ import org.eclipse.jgit.transport.FetchConnection;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.transport.URIish;
+import org.eclipse.jgit.transport.UrlConfig;
+import org.eclipse.jgit.util.SystemReader;
/**
* The ls-remote command
@@ -83,6 +53,8 @@ public class LsRemoteCommand extends
private String uploadPack;
/**
+ * Constructor for LsRemoteCommand
+ *
* @param repo
* local repository or null for operation without local
* repository
@@ -98,6 +70,7 @@ public class LsRemoteCommand extends
*
* @see Constants#DEFAULT_REMOTE_NAME
* @param remote
+ * a {@link java.lang.String} object.
* @return {@code this}
*/
public LsRemoteCommand setRemote(String remote) {
@@ -110,6 +83,7 @@ public class LsRemoteCommand extends
* Include refs/heads in references results
*
* @param heads
+ * whether to include refs/heads
* @return {@code this}
*/
public LsRemoteCommand setHeads(boolean heads) {
@@ -121,6 +95,7 @@ public class LsRemoteCommand extends
* Include refs/tags in references results
*
* @param tags
+ * whether to include tags
* @return {@code this}
*/
public LsRemoteCommand setTags(boolean tags) {
@@ -132,6 +107,8 @@ public class LsRemoteCommand extends
* The full path of git-upload-pack on the remote host
*
* @param uploadPack
+ * the full path of executable providing the git-upload-pack
+ * service on remote host
* @return {@code this}
*/
public LsRemoteCommand setUploadPack(String uploadPack) {
@@ -140,19 +117,14 @@ public class LsRemoteCommand extends
}
/**
- * Executes the {@code LsRemote} command with all the options and parameters
+ * {@inheritDoc}
+ * <p>
+ * Execute the {@code LsRemote} command with all the options and parameters
* collected by the setter methods (e.g. {@link #setHeads(boolean)}) of this
* class. Each instance of this class should only be used for one invocation
* of the command. Don't call this method twice on an instance.
- *
- * @return a collection of references in the remote repository
- * @throws GitAPIException
- * or subclass thereof when an error occurs
- * @throws InvalidRemoteException
- * when called with an invalid remote uri
- * @throws org.eclipse.jgit.api.errors.TransportException
- * for errors that occurs during transport
*/
+ @Override
public Collection<Ref> call() throws GitAPIException,
InvalidRemoteException,
org.eclipse.jgit.api.errors.TransportException {
@@ -163,9 +135,9 @@ public class LsRemoteCommand extends
* Same as {@link #call()}, but return Map instead of Collection.
*
* @return a map from names to references in the remote repository
- * @throws GitAPIException
+ * @throws org.eclipse.jgit.api.errors.GitAPIException
* or subclass thereof when an error occurs
- * @throws InvalidRemoteException
+ * @throws org.eclipse.jgit.api.errors.InvalidRemoteException
* when called with an invalid remote uri
* @throws org.eclipse.jgit.api.errors.TransportException
* for errors that occurs during transport
@@ -182,53 +154,50 @@ public class LsRemoteCommand extends
org.eclipse.jgit.api.errors.TransportException {
checkCallable();
- Transport transport = null;
- FetchConnection fc = null;
- try {
- if (repo != null)
- transport = Transport.open(repo, remote);
- else
- transport = Transport.open(new URIish(remote));
+ try (Transport transport = repo != null
+ ? Transport.open(repo, remote)
+ : Transport.open(new URIish(translate(remote)))) {
transport.setOptionUploadPack(uploadPack);
configure(transport);
- Collection<RefSpec> refSpecs = new ArrayList<RefSpec>(1);
+ Collection<RefSpec> refSpecs = new ArrayList<>(1);
if (tags)
refSpecs.add(new RefSpec(
"refs/tags/*:refs/remotes/origin/tags/*")); //$NON-NLS-1$
if (heads)
refSpecs.add(new RefSpec("refs/heads/*:refs/remotes/origin/*")); //$NON-NLS-1$
Collection<Ref> refs;
- Map<String, Ref> refmap = new HashMap<String, Ref>();
- fc = transport.openFetch();
- refs = fc.getRefs();
- if (refSpecs.isEmpty())
- for (Ref r : refs)
- refmap.put(r.getName(), r);
- else
- for (Ref r : refs)
- for (RefSpec rs : refSpecs)
- if (rs.matchSource(r)) {
- refmap.put(r.getName(), r);
- break;
- }
- return refmap;
+ Map<String, Ref> refmap = new HashMap<>();
+ try (FetchConnection fc = transport.openFetch(refSpecs)) {
+ refs = fc.getRefs();
+ if (refSpecs.isEmpty())
+ for (Ref r : refs)
+ refmap.put(r.getName(), r);
+ else
+ for (Ref r : refs)
+ for (RefSpec rs : refSpecs)
+ if (rs.matchSource(r)) {
+ refmap.put(r.getName(), r);
+ break;
+ }
+ return refmap;
+ }
} catch (URISyntaxException e) {
throw new InvalidRemoteException(MessageFormat.format(
- JGitText.get().invalidRemote, remote));
+ JGitText.get().invalidRemote, remote), e);
} catch (NotSupportedException e) {
throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfLsRemoteCommand,
e);
- } catch (TransportException e) {
+ } catch (IOException | ConfigInvalidException e) {
throw new org.eclipse.jgit.api.errors.TransportException(
- e.getMessage(),
- e);
- } finally {
- if (fc != null)
- fc.close();
- if (transport != null)
- transport.close();
+ e.getMessage(), e);
}
}
+ private String translate(String uri)
+ throws IOException, ConfigInvalidException {
+ UrlConfig urls = new UrlConfig(
+ SystemReader.getInstance().getUserConfig());
+ return urls.replace(uri);
+ }
}