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

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