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

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