選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PushCommand.java 11KB

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