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.

PushCommand.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.io.IOException;
  45. import java.net.URISyntaxException;
  46. import java.text.MessageFormat;
  47. import java.util.ArrayList;
  48. import java.util.Collection;
  49. import java.util.Collections;
  50. import java.util.List;
  51. import org.eclipse.jgit.JGitText;
  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.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.transport.CredentialsProvider;
  61. import org.eclipse.jgit.transport.PushResult;
  62. import org.eclipse.jgit.transport.RefSpec;
  63. import org.eclipse.jgit.transport.RemoteConfig;
  64. import org.eclipse.jgit.transport.RemoteRefUpdate;
  65. import org.eclipse.jgit.transport.Transport;
  66. /**
  67. * A class used to execute a {@code Push} command. It has setters for all
  68. * supported options and arguments of this command and a {@link #call()} method
  69. * to finally execute the command.
  70. *
  71. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
  72. * >Git documentation about Push</a>
  73. */
  74. public class PushCommand extends GitCommand<Iterable<PushResult>> {
  75. private String remote = Constants.DEFAULT_REMOTE_NAME;
  76. private List<RefSpec> refSpecs;
  77. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  78. private String receivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
  79. private boolean dryRun;
  80. private boolean force;
  81. private boolean thin = Transport.DEFAULT_PUSH_THIN;
  82. private int timeout;
  83. private CredentialsProvider credentialsProvider;
  84. /**
  85. * @param repo
  86. */
  87. protected PushCommand(Repository repo) {
  88. super(repo);
  89. refSpecs = new ArrayList<RefSpec>(3);
  90. }
  91. /**
  92. * Executes the {@code push} command with all the options and parameters
  93. * collected by the setter methods of this class. Each instance of this
  94. * class should only be used for one invocation of the command (means: one
  95. * call to {@link #call()})
  96. *
  97. * @return an iteration over {@link PushResult} objects
  98. * @throws InvalidRemoteException
  99. * when called with an invalid remote uri
  100. * @throws JGitInternalException
  101. * a low-level exception of JGit has occurred. The original
  102. * exception can be retrieved by calling
  103. * {@link Exception#getCause()}.
  104. */
  105. public Iterable<PushResult> call() throws JGitInternalException,
  106. InvalidRemoteException {
  107. checkCallable();
  108. ArrayList<PushResult> pushResults = new ArrayList<PushResult>(3);
  109. try {
  110. if (force) {
  111. final List<RefSpec> orig = new ArrayList<RefSpec>(refSpecs);
  112. refSpecs.clear();
  113. for (final RefSpec spec : orig)
  114. refSpecs.add(spec.setForceUpdate(true));
  115. }
  116. final List<Transport> transports;
  117. transports = Transport.openAll(repo, remote, Transport.Operation.PUSH);
  118. for (final Transport transport : transports) {
  119. if (0 <= timeout)
  120. transport.setTimeout(timeout);
  121. transport.setPushThin(thin);
  122. if (receivePack != null)
  123. transport.setOptionReceivePack(receivePack);
  124. transport.setDryRun(dryRun);
  125. transport.setCredentialsProvider(credentialsProvider);
  126. final Collection<RemoteRefUpdate> toPush = transport
  127. .findRemoteRefUpdatesFor(refSpecs);
  128. try {
  129. PushResult result = transport.push(monitor, toPush);
  130. pushResults.add(result);
  131. } catch (TransportException e) {
  132. throw new JGitInternalException(
  133. JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
  134. e);
  135. } finally {
  136. transport.close();
  137. }
  138. }
  139. } catch (URISyntaxException e) {
  140. throw new InvalidRemoteException(MessageFormat.format(
  141. JGitText.get().invalidRemote, remote));
  142. } catch (NotSupportedException e) {
  143. throw new JGitInternalException(
  144. JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
  145. e);
  146. } catch (IOException e) {
  147. throw new JGitInternalException(
  148. JGitText.get().exceptionCaughtDuringExecutionOfPushCommand,
  149. e);
  150. }
  151. return pushResults;
  152. }
  153. /**
  154. * The remote (uri or name) used for the push operation. If no remote is
  155. * set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
  156. * be used.
  157. *
  158. * @see Constants#DEFAULT_REMOTE_NAME
  159. * @param remote
  160. * @return {@code this}
  161. */
  162. public PushCommand setRemote(String remote) {
  163. checkCallable();
  164. this.remote = remote;
  165. return this;
  166. }
  167. /**
  168. * @return the remote used for the remote operation
  169. */
  170. public String getRemote() {
  171. return remote;
  172. }
  173. /**
  174. * The remote executable providing receive-pack service for pack transports.
  175. * If no receive-pack is set, the default value of
  176. * <code>RemoteConfig.DEFAULT_RECEIVE_PACK</code> will be used.
  177. *
  178. * @see RemoteConfig#DEFAULT_RECEIVE_PACK
  179. * @param receivePack
  180. * @return {@code this}
  181. */
  182. public PushCommand setReceivePack(String receivePack) {
  183. checkCallable();
  184. this.receivePack = receivePack;
  185. return this;
  186. }
  187. /**
  188. * @return the receive-pack used for the remote operation
  189. */
  190. public String getReceivePack() {
  191. return receivePack;
  192. }
  193. /**
  194. * @param timeout
  195. * the timeout used for the push operation
  196. * @return {@code this}
  197. */
  198. public PushCommand setTimeout(int timeout) {
  199. checkCallable();
  200. this.timeout = timeout;
  201. return this;
  202. }
  203. /**
  204. * @return the timeout used for the push operation
  205. */
  206. public int getTimeout() {
  207. return timeout;
  208. }
  209. /**
  210. * @return the progress monitor for the push operation
  211. */
  212. public ProgressMonitor getProgressMonitor() {
  213. return monitor;
  214. }
  215. /**
  216. * The progress monitor associated with the push operation. By default, this
  217. * is set to <code>NullProgressMonitor</code>
  218. *
  219. * @see NullProgressMonitor
  220. *
  221. * @param monitor
  222. * @return {@code this}
  223. */
  224. public PushCommand setProgressMonitor(ProgressMonitor monitor) {
  225. checkCallable();
  226. this.monitor = monitor;
  227. return this;
  228. }
  229. /**
  230. * @return the ref specs
  231. */
  232. public List<RefSpec> getRefSpecs() {
  233. return refSpecs;
  234. }
  235. /**
  236. * The ref specs to be used in the push operation
  237. *
  238. * @param specs
  239. * @return {@code this}
  240. */
  241. public PushCommand setRefSpecs(RefSpec... specs) {
  242. checkCallable();
  243. this.refSpecs.clear();
  244. Collections.addAll(refSpecs, specs);
  245. return this;
  246. }
  247. /**
  248. * The ref specs to be used in the push operation
  249. *
  250. * @param specs
  251. * @return {@code this}
  252. */
  253. public PushCommand setRefSpecs(List<RefSpec> specs) {
  254. checkCallable();
  255. this.refSpecs.clear();
  256. this.refSpecs.addAll(specs);
  257. return this;
  258. }
  259. /**
  260. * @return the dry run preference for the push operation
  261. */
  262. public boolean isDryRun() {
  263. return dryRun;
  264. }
  265. /**
  266. * Sets whether the push operation should be a dry run
  267. *
  268. * @param dryRun
  269. * @return {@code this}
  270. */
  271. public PushCommand setDryRun(boolean dryRun) {
  272. checkCallable();
  273. this.dryRun = dryRun;
  274. return this;
  275. }
  276. /**
  277. * @return the thin-pack preference for push operation
  278. */
  279. public boolean isThin() {
  280. return thin;
  281. }
  282. /**
  283. * Sets the thin-pack preference for push operation.
  284. *
  285. * Default setting is Transport.DEFAULT_PUSH_THIN
  286. *
  287. * @param thin
  288. * @return {@code this}
  289. */
  290. public PushCommand setThin(boolean thin) {
  291. checkCallable();
  292. this.thin = thin;
  293. return this;
  294. }
  295. /**
  296. * @return the force preference for push operation
  297. */
  298. public boolean isForce() {
  299. return force;
  300. }
  301. /**
  302. * Sets the force preference for push operation.
  303. *
  304. * @param force
  305. * @return {@code this}
  306. */
  307. public PushCommand setForce(boolean force) {
  308. checkCallable();
  309. this.force = force;
  310. return this;
  311. }
  312. /**
  313. * @param credentialsProvider
  314. * the {@link CredentialsProvider} to use
  315. * @return {@code this}
  316. */
  317. public PushCommand setCredentialsProvider(
  318. CredentialsProvider credentialsProvider) {
  319. checkCallable();
  320. this.credentialsProvider = credentialsProvider;
  321. return this;
  322. }
  323. }