View Javadoc
1   /*
2    * Copyright (C) 2011, Christoph Brill <egore911@egore911.de> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.api;
11  
12  import java.net.URISyntaxException;
13  import java.text.MessageFormat;
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.Collections;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.eclipse.jgit.api.errors.GitAPIException;
21  import org.eclipse.jgit.api.errors.InvalidRemoteException;
22  import org.eclipse.jgit.api.errors.JGitInternalException;
23  import org.eclipse.jgit.errors.NotSupportedException;
24  import org.eclipse.jgit.errors.TransportException;
25  import org.eclipse.jgit.internal.JGitText;
26  import org.eclipse.jgit.lib.Constants;
27  import org.eclipse.jgit.lib.Ref;
28  import org.eclipse.jgit.lib.Repository;
29  import org.eclipse.jgit.transport.FetchConnection;
30  import org.eclipse.jgit.transport.RefSpec;
31  import org.eclipse.jgit.transport.Transport;
32  import org.eclipse.jgit.transport.URIish;
33  
34  /**
35   * The ls-remote command
36   *
37   * @see <a
38   *      href="http://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html"
39   *      >Git documentation about ls-remote</a>
40   */
41  public class LsRemoteCommand extends
42  		TransportCommand<LsRemoteCommand, Collection<Ref>> {
43  
44  	private String remote = Constants.DEFAULT_REMOTE_NAME;
45  
46  	private boolean heads;
47  
48  	private boolean tags;
49  
50  	private String uploadPack;
51  
52  	/**
53  	 * Constructor for LsRemoteCommand
54  	 *
55  	 * @param repo
56  	 *            local repository or null for operation without local
57  	 *            repository
58  	 */
59  	public LsRemoteCommand(Repository repo) {
60  		super(repo);
61  	}
62  
63  	/**
64  	 * The remote (uri or name) used for the fetch operation. If no remote is
65  	 * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
66  	 * be used.
67  	 *
68  	 * @see Constants#DEFAULT_REMOTE_NAME
69  	 * @param remote
70  	 *            a {@link java.lang.String} object.
71  	 * @return {@code this}
72  	 */
73  	public LsRemoteCommand setRemote(String remote) {
74  		checkCallable();
75  		this.remote = remote;
76  		return this;
77  	}
78  
79  	/**
80  	 * Include refs/heads in references results
81  	 *
82  	 * @param heads
83  	 *            whether to include refs/heads
84  	 * @return {@code this}
85  	 */
86  	public LsRemoteCommand setHeads(boolean heads) {
87  		this.heads = heads;
88  		return this;
89  	}
90  
91  	/**
92  	 * Include refs/tags in references results
93  	 *
94  	 * @param tags
95  	 *            whether to include tags
96  	 * @return {@code this}
97  	 */
98  	public LsRemoteCommand setTags(boolean tags) {
99  		this.tags = tags;
100 		return this;
101 	}
102 
103 	/**
104 	 * The full path of git-upload-pack on the remote host
105 	 *
106 	 * @param uploadPack
107 	 *            the full path of executable providing the git-upload-pack
108 	 *            service on remote host
109 	 * @return {@code this}
110 	 */
111 	public LsRemoteCommand setUploadPack(String uploadPack) {
112 		this.uploadPack = uploadPack;
113 		return this;
114 	}
115 
116 	/**
117 	 * {@inheritDoc}
118 	 * <p>
119 	 * Execute the {@code LsRemote} command with all the options and parameters
120 	 * collected by the setter methods (e.g. {@link #setHeads(boolean)}) of this
121 	 * class. Each instance of this class should only be used for one invocation
122 	 * of the command. Don't call this method twice on an instance.
123 	 */
124 	@Override
125 	public Collection<Ref> call() throws GitAPIException,
126 			InvalidRemoteException,
127 			org.eclipse.jgit.api.errors.TransportException {
128 		return execute().values();
129 	}
130 
131 	/**
132 	 * Same as {@link #call()}, but return Map instead of Collection.
133 	 *
134 	 * @return a map from names to references in the remote repository
135 	 * @throws org.eclipse.jgit.api.errors.GitAPIException
136 	 *             or subclass thereof when an error occurs
137 	 * @throws org.eclipse.jgit.api.errors.InvalidRemoteException
138 	 *             when called with an invalid remote uri
139 	 * @throws org.eclipse.jgit.api.errors.TransportException
140 	 *             for errors that occurs during transport
141 	 * @since 3.5
142 	 */
143 	public Map<String, Ref> callAsMap() throws GitAPIException,
144 			InvalidRemoteException,
145 			org.eclipse.jgit.api.errors.TransportException {
146 		return Collections.unmodifiableMap(execute());
147 	}
148 
149 	private Map<String, Ref> execute() throws GitAPIException,
150 			InvalidRemoteException,
151 			org.eclipse.jgit.api.errors.TransportException {
152 		checkCallable();
153 
154 		try (Transport transport = repo != null
155 				? Transport.open(repo, remote)
156 				: Transport.open(new URIish(remote))) {
157 			transport.setOptionUploadPack(uploadPack);
158 			configure(transport);
159 			Collection<RefSpec> refSpecs = new ArrayList<>(1);
160 			if (tags)
161 				refSpecs.add(new RefSpec(
162 						"refs/tags/*:refs/remotes/origin/tags/*")); //$NON-NLS-1$
163 			if (heads)
164 				refSpecs.add(new RefSpec("refs/heads/*:refs/remotes/origin/*")); //$NON-NLS-1$
165 			Collection<Ref> refs;
166 			Map<String, Ref> refmap = new HashMap<>();
167 			try (FetchConnection fc = transport.openFetch()) {
168 				refs = fc.getRefs();
169 				if (refSpecs.isEmpty())
170 					for (Ref r : refs)
171 						refmap.put(r.getName(), r);
172 				else
173 					for (Ref r : refs)
174 						for (RefSpec rs : refSpecs)
175 							if (rs.matchSource(r)) {
176 								refmap.put(r.getName(), r);
177 								break;
178 							}
179 				return refmap;
180 			}
181 		} catch (URISyntaxException e) {
182 			throw new InvalidRemoteException(MessageFormat.format(
183 					JGitText.get().invalidRemote, remote), e);
184 		} catch (NotSupportedException e) {
185 			throw new JGitInternalException(
186 					JGitText.get().exceptionCaughtDuringExecutionOfLsRemoteCommand,
187 					e);
188 		} catch (TransportException e) {
189 			throw new org.eclipse.jgit.api.errors.TransportException(
190 					e.getMessage(),
191 					e);
192 		}
193 	}
194 
195 }