You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LsRemoteCommand.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2011, Christoph Brill <egore911@egore911.de>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import java.net.URISyntaxException;
  45. import java.text.MessageFormat;
  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.Collections;
  49. import java.util.HashMap;
  50. import java.util.Map;
  51. import org.eclipse.jgit.api.errors.GitAPIException;
  52. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  53. import org.eclipse.jgit.api.errors.JGitInternalException;
  54. import org.eclipse.jgit.errors.NotSupportedException;
  55. import org.eclipse.jgit.errors.TransportException;
  56. import org.eclipse.jgit.internal.JGitText;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.Ref;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.transport.FetchConnection;
  61. import org.eclipse.jgit.transport.RefSpec;
  62. import org.eclipse.jgit.transport.Transport;
  63. import org.eclipse.jgit.transport.URIish;
  64. /**
  65. * The ls-remote command
  66. *
  67. * @see <a
  68. * href="http://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html"
  69. * >Git documentation about ls-remote</a>
  70. */
  71. public class LsRemoteCommand extends
  72. TransportCommand<LsRemoteCommand, Collection<Ref>> {
  73. private String remote = Constants.DEFAULT_REMOTE_NAME;
  74. private boolean heads;
  75. private boolean tags;
  76. private String uploadPack;
  77. /**
  78. * @param repo
  79. * local repository or null for operation without local
  80. * repository
  81. */
  82. public LsRemoteCommand(Repository repo) {
  83. super(repo);
  84. }
  85. /**
  86. * The remote (uri or name) used for the fetch operation. If no remote is
  87. * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
  88. * be used.
  89. *
  90. * @see Constants#DEFAULT_REMOTE_NAME
  91. * @param remote
  92. * @return {@code this}
  93. */
  94. public LsRemoteCommand setRemote(String remote) {
  95. checkCallable();
  96. this.remote = remote;
  97. return this;
  98. }
  99. /**
  100. * Include refs/heads in references results
  101. *
  102. * @param heads
  103. * @return {@code this}
  104. */
  105. public LsRemoteCommand setHeads(boolean heads) {
  106. this.heads = heads;
  107. return this;
  108. }
  109. /**
  110. * Include refs/tags in references results
  111. *
  112. * @param tags
  113. * @return {@code this}
  114. */
  115. public LsRemoteCommand setTags(boolean tags) {
  116. this.tags = tags;
  117. return this;
  118. }
  119. /**
  120. * The full path of git-upload-pack on the remote host
  121. *
  122. * @param uploadPack
  123. * @return {@code this}
  124. */
  125. public LsRemoteCommand setUploadPack(String uploadPack) {
  126. this.uploadPack = uploadPack;
  127. return this;
  128. }
  129. /**
  130. * Executes the {@code LsRemote} command with all the options and parameters
  131. * collected by the setter methods (e.g. {@link #setHeads(boolean)}) of this
  132. * class. Each instance of this class should only be used for one invocation
  133. * of the command. Don't call this method twice on an instance.
  134. *
  135. * @return a collection of references in the remote repository
  136. * @throws InvalidRemoteException
  137. * when called with an invalid remote uri
  138. * @throws org.eclipse.jgit.api.errors.TransportException
  139. * for errors that occurs during transport
  140. */
  141. public Collection<Ref> call() throws GitAPIException,
  142. InvalidRemoteException,
  143. org.eclipse.jgit.api.errors.TransportException {
  144. return execute().values();
  145. }
  146. /**
  147. * Same as {@link #call()}, but return Map instead of Collection.
  148. *
  149. * @return a map from names to references in the remote repository
  150. * @throws InvalidRemoteException
  151. * when called with an invalid remote uri
  152. * @throws org.eclipse.jgit.api.errors.TransportException
  153. * for errors that occurs during transport
  154. * @since 3.5
  155. */
  156. public Map<String, Ref> callAsMap() throws GitAPIException,
  157. InvalidRemoteException,
  158. org.eclipse.jgit.api.errors.TransportException {
  159. return Collections.unmodifiableMap(execute());
  160. }
  161. protected Map<String, Ref> execute() throws GitAPIException,
  162. InvalidRemoteException,
  163. org.eclipse.jgit.api.errors.TransportException {
  164. checkCallable();
  165. Transport transport = null;
  166. FetchConnection fc = null;
  167. try {
  168. if (repo != null)
  169. transport = Transport.open(repo, remote);
  170. else
  171. transport = Transport.open(new URIish(remote));
  172. transport.setOptionUploadPack(uploadPack);
  173. configure(transport);
  174. Collection<RefSpec> refSpecs = new ArrayList<RefSpec>(1);
  175. if (tags)
  176. refSpecs.add(new RefSpec(
  177. "refs/tags/*:refs/remotes/origin/tags/*")); //$NON-NLS-1$
  178. if (heads)
  179. refSpecs.add(new RefSpec("refs/heads/*:refs/remotes/origin/*")); //$NON-NLS-1$
  180. Collection<Ref> refs;
  181. Map<String, Ref> refmap = new HashMap<String, Ref>();
  182. fc = transport.openFetch();
  183. refs = fc.getRefs();
  184. if (refSpecs.isEmpty())
  185. for (Ref r : refs)
  186. refmap.put(r.getName(), r);
  187. else
  188. for (Ref r : refs)
  189. for (RefSpec rs : refSpecs)
  190. if (rs.matchSource(r)) {
  191. refmap.put(r.getName(), r);
  192. break;
  193. }
  194. return refmap;
  195. } catch (URISyntaxException e) {
  196. throw new InvalidRemoteException(MessageFormat.format(
  197. JGitText.get().invalidRemote, remote));
  198. } catch (NotSupportedException e) {
  199. throw new JGitInternalException(
  200. JGitText.get().exceptionCaughtDuringExecutionOfLsRemoteCommand,
  201. e);
  202. } catch (TransportException e) {
  203. throw new org.eclipse.jgit.api.errors.TransportException(
  204. e.getMessage(),
  205. e);
  206. } finally {
  207. if (fc != null)
  208. fc.close();
  209. if (transport != null)
  210. transport.close();
  211. }
  212. }
  213. }