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.

FetchCommand.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  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.List;
  48. import org.eclipse.jgit.api.errors.GitAPIException;
  49. import org.eclipse.jgit.api.errors.InvalidRemoteException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  52. import org.eclipse.jgit.errors.NotSupportedException;
  53. import org.eclipse.jgit.errors.TransportException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.NullProgressMonitor;
  57. import org.eclipse.jgit.lib.ProgressMonitor;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.transport.FetchResult;
  60. import org.eclipse.jgit.transport.RefSpec;
  61. import org.eclipse.jgit.transport.TagOpt;
  62. import org.eclipse.jgit.transport.Transport;
  63. /**
  64. * A class used to execute a {@code Fetch} command. It has setters for all
  65. * supported options and arguments of this command and a {@link #call()} method
  66. * to finally execute the command.
  67. *
  68. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html"
  69. * >Git documentation about Fetch</a>
  70. */
  71. public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
  72. private String remote = Constants.DEFAULT_REMOTE_NAME;
  73. private List<RefSpec> refSpecs;
  74. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  75. private boolean checkFetchedObjects;
  76. private boolean removeDeletedRefs;
  77. private boolean dryRun;
  78. private boolean thin = Transport.DEFAULT_FETCH_THIN;
  79. private TagOpt tagOption;
  80. /**
  81. * @param repo
  82. */
  83. protected FetchCommand(Repository repo) {
  84. super(repo);
  85. refSpecs = new ArrayList<RefSpec>(3);
  86. }
  87. /**
  88. * Executes the {@code fetch} command with all the options and parameters
  89. * collected by the setter methods of this class. Each instance of this
  90. * class should only be used for one invocation of the command (means: one
  91. * call to {@link #call()})
  92. *
  93. * @return a {@link FetchResult} object representing the successful fetch
  94. * result
  95. * @throws InvalidRemoteException
  96. * when called with an invalid remote uri
  97. * @throws org.eclipse.jgit.api.errors.TransportException
  98. * when an error occurs during transport
  99. */
  100. public FetchResult call() throws GitAPIException, InvalidRemoteException,
  101. org.eclipse.jgit.api.errors.TransportException {
  102. checkCallable();
  103. try {
  104. Transport transport = Transport.open(repo, remote);
  105. try {
  106. transport.setCheckFetchedObjects(checkFetchedObjects);
  107. transport.setRemoveDeletedRefs(removeDeletedRefs);
  108. transport.setDryRun(dryRun);
  109. if (tagOption != null)
  110. transport.setTagOpt(tagOption);
  111. transport.setFetchThin(thin);
  112. configure(transport);
  113. FetchResult result = transport.fetch(monitor, refSpecs);
  114. return result;
  115. } finally {
  116. transport.close();
  117. }
  118. } catch (NoRemoteRepositoryException e) {
  119. throw new InvalidRemoteException(MessageFormat.format(
  120. JGitText.get().invalidRemote, remote), e);
  121. } catch (TransportException e) {
  122. throw new org.eclipse.jgit.api.errors.TransportException(
  123. e.getMessage(), e);
  124. } catch (URISyntaxException e) {
  125. throw new InvalidRemoteException(MessageFormat.format(
  126. JGitText.get().invalidRemote, remote));
  127. } catch (NotSupportedException e) {
  128. throw new JGitInternalException(
  129. JGitText.get().exceptionCaughtDuringExecutionOfFetchCommand,
  130. e);
  131. }
  132. }
  133. /**
  134. * The remote (uri or name) used for the fetch operation. If no remote is
  135. * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
  136. * be used.
  137. *
  138. * @see Constants#DEFAULT_REMOTE_NAME
  139. * @param remote
  140. * @return {@code this}
  141. */
  142. public FetchCommand setRemote(String remote) {
  143. checkCallable();
  144. this.remote = remote;
  145. return this;
  146. }
  147. /**
  148. * @return the remote used for the remote operation
  149. */
  150. public String getRemote() {
  151. return remote;
  152. }
  153. /**
  154. * @return the timeout used for the fetch operation
  155. */
  156. public int getTimeout() {
  157. return timeout;
  158. }
  159. /**
  160. * @return whether to check received objects checked for validity
  161. */
  162. public boolean isCheckFetchedObjects() {
  163. return checkFetchedObjects;
  164. }
  165. /**
  166. * If set to true, objects received will be checked for validity
  167. *
  168. * @param checkFetchedObjects
  169. * @return {@code this}
  170. */
  171. public FetchCommand setCheckFetchedObjects(boolean checkFetchedObjects) {
  172. checkCallable();
  173. this.checkFetchedObjects = checkFetchedObjects;
  174. return this;
  175. }
  176. /**
  177. * @return whether or not to remove refs which no longer exist in the source
  178. */
  179. public boolean isRemoveDeletedRefs() {
  180. return removeDeletedRefs;
  181. }
  182. /**
  183. * If set to true, refs are removed which no longer exist in the source
  184. *
  185. * @param removeDeletedRefs
  186. * @return {@code this}
  187. */
  188. public FetchCommand setRemoveDeletedRefs(boolean removeDeletedRefs) {
  189. checkCallable();
  190. this.removeDeletedRefs = removeDeletedRefs;
  191. return this;
  192. }
  193. /**
  194. * @return the progress monitor for the fetch operation
  195. */
  196. public ProgressMonitor getProgressMonitor() {
  197. return monitor;
  198. }
  199. /**
  200. * The progress monitor associated with the fetch operation. By default,
  201. * this is set to <code>NullProgressMonitor</code>
  202. *
  203. * @see NullProgressMonitor
  204. *
  205. * @param monitor
  206. * @return {@code this}
  207. */
  208. public FetchCommand setProgressMonitor(ProgressMonitor monitor) {
  209. checkCallable();
  210. this.monitor = monitor;
  211. return this;
  212. }
  213. /**
  214. * @return the ref specs
  215. */
  216. public List<RefSpec> getRefSpecs() {
  217. return refSpecs;
  218. }
  219. /**
  220. * The ref specs to be used in the fetch operation
  221. *
  222. * @param specs
  223. * @return {@code this}
  224. */
  225. public FetchCommand setRefSpecs(RefSpec... specs) {
  226. checkCallable();
  227. this.refSpecs.clear();
  228. for (RefSpec spec : specs)
  229. refSpecs.add(spec);
  230. return this;
  231. }
  232. /**
  233. * The ref specs to be used in the fetch operation
  234. *
  235. * @param specs
  236. * @return {@code this}
  237. */
  238. public FetchCommand setRefSpecs(List<RefSpec> specs) {
  239. checkCallable();
  240. this.refSpecs.clear();
  241. this.refSpecs.addAll(specs);
  242. return this;
  243. }
  244. /**
  245. * @return the dry run preference for the fetch operation
  246. */
  247. public boolean isDryRun() {
  248. return dryRun;
  249. }
  250. /**
  251. * Sets whether the fetch operation should be a dry run
  252. *
  253. * @param dryRun
  254. * @return {@code this}
  255. */
  256. public FetchCommand setDryRun(boolean dryRun) {
  257. checkCallable();
  258. this.dryRun = dryRun;
  259. return this;
  260. }
  261. /**
  262. * @return the thin-pack preference for fetch operation
  263. */
  264. public boolean isThin() {
  265. return thin;
  266. }
  267. /**
  268. * Sets the thin-pack preference for fetch operation.
  269. *
  270. * Default setting is Transport.DEFAULT_FETCH_THIN
  271. *
  272. * @param thin
  273. * @return {@code this}
  274. */
  275. public FetchCommand setThin(boolean thin) {
  276. checkCallable();
  277. this.thin = thin;
  278. return this;
  279. }
  280. /**
  281. * Sets the specification of annotated tag behavior during fetch
  282. *
  283. * @param tagOpt
  284. * @return {@code this}
  285. */
  286. public FetchCommand setTagOpt(TagOpt tagOpt) {
  287. checkCallable();
  288. this.tagOption = tagOpt;
  289. return this;
  290. }
  291. }