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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.HashMap;
  49. import java.util.Map;
  50. import org.eclipse.jgit.JGitText;
  51. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.errors.NotSupportedException;
  54. import org.eclipse.jgit.errors.TransportException;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.Ref;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.transport.FetchConnection;
  59. import org.eclipse.jgit.transport.RefSpec;
  60. import org.eclipse.jgit.transport.Transport;
  61. /**
  62. * The ls-remote command
  63. *
  64. * @see <a
  65. * href="http://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html"
  66. * >Git documentation about ls-remote</a>
  67. */
  68. public class LsRemoteCommand extends
  69. TransportCommand<LsRemoteCommand, Collection<Ref>> {
  70. private String remote = Constants.DEFAULT_REMOTE_NAME;
  71. private boolean heads;
  72. private boolean tags;
  73. private String uploadPack;
  74. /**
  75. * @param repo
  76. */
  77. public LsRemoteCommand(Repository repo) {
  78. super(repo);
  79. }
  80. /**
  81. * The remote (uri or name) used for the fetch operation. If no remote is
  82. * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
  83. * be used.
  84. *
  85. * @see Constants#DEFAULT_REMOTE_NAME
  86. * @param remote
  87. * @return {@code this}
  88. */
  89. public LsRemoteCommand setRemote(String remote) {
  90. checkCallable();
  91. this.remote = remote;
  92. return this;
  93. }
  94. /**
  95. * Include refs/heads in references results
  96. *
  97. * @param heads
  98. */
  99. public void setHeads(boolean heads) {
  100. this.heads = heads;
  101. }
  102. /**
  103. * Include refs/tags in references results
  104. *
  105. * @param tags
  106. */
  107. public void setTags(boolean tags) {
  108. this.tags = tags;
  109. }
  110. /**
  111. * The full path of git-upload-pack on the remote host
  112. *
  113. * @param uploadPack
  114. */
  115. public void setUploadPack(String uploadPack) {
  116. this.uploadPack = uploadPack;
  117. }
  118. public Collection<Ref> call() throws Exception {
  119. checkCallable();
  120. try {
  121. Transport transport = Transport.open(repo, remote);
  122. transport.setOptionUploadPack(uploadPack);
  123. configure(transport);
  124. try {
  125. Collection<RefSpec> refSpecs = new ArrayList<RefSpec>(1);
  126. if (tags)
  127. refSpecs.add(new RefSpec(
  128. "refs/tags/*:refs/remotes/origin/tags/*"));
  129. if (heads)
  130. refSpecs.add(new RefSpec(
  131. "refs/heads/*:refs/remotes/origin/*"));
  132. Collection<Ref> refs;
  133. Map<String, Ref> refmap = new HashMap<String, Ref>();
  134. FetchConnection fc = transport.openFetch();
  135. try {
  136. refs = fc.getRefs();
  137. if (refSpecs.isEmpty())
  138. for (Ref r : refs)
  139. refmap.put(r.getName(), r);
  140. else
  141. for (Ref r : refs)
  142. for (RefSpec rs : refSpecs)
  143. if (rs.matchSource(r)) {
  144. refmap.put(r.getName(), r);
  145. break;
  146. }
  147. } finally {
  148. fc.close();
  149. }
  150. return refmap.values();
  151. } catch (TransportException e) {
  152. throw new JGitInternalException(
  153. JGitText.get().exceptionCaughtDuringExecutionOfLsRemoteCommand,
  154. e);
  155. } finally {
  156. transport.close();
  157. }
  158. } catch (URISyntaxException e) {
  159. throw new InvalidRemoteException(MessageFormat.format(
  160. JGitText.get().invalidRemote, remote));
  161. } catch (NotSupportedException e) {
  162. throw new JGitInternalException(
  163. JGitText.get().exceptionCaughtDuringExecutionOfLsRemoteCommand,
  164. e);
  165. }
  166. }
  167. }