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

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