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.

Push.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@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.pgm;
  44. import java.text.MessageFormat;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.List;
  48. import org.kohsuke.args4j.Argument;
  49. import org.kohsuke.args4j.Option;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.Ref;
  52. import org.eclipse.jgit.lib.TextProgressMonitor;
  53. import org.eclipse.jgit.transport.PushResult;
  54. import org.eclipse.jgit.transport.RefSpec;
  55. import org.eclipse.jgit.transport.RemoteRefUpdate;
  56. import org.eclipse.jgit.transport.Transport;
  57. import org.eclipse.jgit.transport.URIish;
  58. import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
  59. @Command(common = true, usage = "usage_UpdateRemoteRepositoryFromLocalRefs")
  60. class Push extends TextBuiltin {
  61. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  62. int timeout = -1;
  63. @Argument(index = 0, metaVar = "metaVar_uriish")
  64. private String remote = Constants.DEFAULT_REMOTE_NAME;
  65. @Argument(index = 1, metaVar = "metaVar_refspec")
  66. private final List<RefSpec> refSpecs = new ArrayList<RefSpec>();
  67. @Option(name = "--all")
  68. void addAll(final boolean ignored) {
  69. refSpecs.add(Transport.REFSPEC_PUSH_ALL);
  70. }
  71. @Option(name = "--tags")
  72. void addTags(final boolean ignored) {
  73. refSpecs.add(Transport.REFSPEC_TAGS);
  74. }
  75. @Option(name = "--verbose", aliases = { "-v" })
  76. private boolean verbose = false;
  77. @Option(name = "--thin")
  78. private boolean thin = Transport.DEFAULT_PUSH_THIN;
  79. @Option(name = "--no-thin")
  80. void nothin(final boolean ignored) {
  81. thin = false;
  82. }
  83. @Option(name = "--force", aliases = { "-f" })
  84. private boolean force;
  85. @Option(name = "--receive-pack", metaVar = "metaVar_path")
  86. private String receivePack;
  87. @Option(name = "--dry-run")
  88. private boolean dryRun;
  89. private boolean shownURI;
  90. @Override
  91. protected void run() throws Exception {
  92. if (force) {
  93. final List<RefSpec> orig = new ArrayList<RefSpec>(refSpecs);
  94. refSpecs.clear();
  95. for (final RefSpec spec : orig)
  96. refSpecs.add(spec.setForceUpdate(true));
  97. }
  98. final List<Transport> transports;
  99. transports = Transport.openAll(db, remote, Transport.Operation.PUSH);
  100. for (final Transport transport : transports) {
  101. if (0 <= timeout)
  102. transport.setTimeout(timeout);
  103. transport.setPushThin(thin);
  104. if (receivePack != null)
  105. transport.setOptionReceivePack(receivePack);
  106. transport.setDryRun(dryRun);
  107. final Collection<RemoteRefUpdate> toPush = transport
  108. .findRemoteRefUpdatesFor(refSpecs);
  109. final URIish uri = transport.getURI();
  110. final PushResult result;
  111. try {
  112. result = transport.push(new TextProgressMonitor(), toPush);
  113. } finally {
  114. transport.close();
  115. }
  116. printPushResult(uri, result);
  117. }
  118. }
  119. private void printPushResult(final URIish uri,
  120. final PushResult result) {
  121. shownURI = false;
  122. boolean everythingUpToDate = true;
  123. // at first, print up-to-date ones...
  124. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  125. if (rru.getStatus() == Status.UP_TO_DATE) {
  126. if (verbose)
  127. printRefUpdateResult(uri, result, rru);
  128. } else
  129. everythingUpToDate = false;
  130. }
  131. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  132. // ...then successful updates...
  133. if (rru.getStatus() == Status.OK)
  134. printRefUpdateResult(uri, result, rru);
  135. }
  136. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  137. // ...finally, others (problematic)
  138. if (rru.getStatus() != Status.OK
  139. && rru.getStatus() != Status.UP_TO_DATE)
  140. printRefUpdateResult(uri, result, rru);
  141. }
  142. AbstractFetchCommand.showRemoteMessages(result.getMessages());
  143. if (everythingUpToDate)
  144. out.println(CLIText.get().everythingUpToDate);
  145. }
  146. private void printRefUpdateResult(final URIish uri,
  147. final PushResult result, final RemoteRefUpdate rru) {
  148. if (!shownURI) {
  149. shownURI = true;
  150. out.println(MessageFormat.format(CLIText.get().pushTo, uri));
  151. }
  152. final String remoteName = rru.getRemoteName();
  153. final String srcRef = rru.isDelete() ? null : rru.getSrcRef();
  154. switch (rru.getStatus()) {
  155. case OK:
  156. if (rru.isDelete())
  157. printUpdateLine('-', "[deleted]", null, remoteName, null);
  158. else {
  159. final Ref oldRef = result.getAdvertisedRef(remoteName);
  160. if (oldRef == null) {
  161. final String summary;
  162. if (remoteName.startsWith(Constants.R_TAGS))
  163. summary = "[new tag]";
  164. else
  165. summary = "[new branch]";
  166. printUpdateLine('*', summary, srcRef, remoteName, null);
  167. } else {
  168. boolean fastForward = rru.isFastForward();
  169. final char flag = fastForward ? ' ' : '+';
  170. final String summary = oldRef.getObjectId().abbreviate(db)
  171. .name()
  172. + (fastForward ? ".." : "...")
  173. + rru.getNewObjectId().abbreviate(db).name();
  174. final String message = fastForward ? null : CLIText.get().forcedUpdate;
  175. printUpdateLine(flag, summary, srcRef, remoteName, message);
  176. }
  177. }
  178. break;
  179. case NON_EXISTING:
  180. printUpdateLine('X', "[no match]", null, remoteName, null);
  181. break;
  182. case REJECTED_NODELETE:
  183. printUpdateLine('!', "[rejected]", null, remoteName,
  184. CLIText.get().remoteSideDoesNotSupportDeletingRefs);
  185. break;
  186. case REJECTED_NONFASTFORWARD:
  187. printUpdateLine('!', "[rejected]", srcRef, remoteName,
  188. CLIText.get().nonFastForward);
  189. break;
  190. case REJECTED_REMOTE_CHANGED:
  191. final String message = MessageFormat.format(
  192. CLIText.get().remoteRefObjectChangedIsNotExpectedOne
  193. , rru.getExpectedOldObjectId().abbreviate(db).name());
  194. printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
  195. break;
  196. case REJECTED_OTHER_REASON:
  197. printUpdateLine('!', "[remote rejected]", srcRef, remoteName, rru
  198. .getMessage());
  199. break;
  200. case UP_TO_DATE:
  201. if (verbose)
  202. printUpdateLine('=', "[up to date]", srcRef, remoteName, null);
  203. break;
  204. case NOT_ATTEMPTED:
  205. case AWAITING_REPORT:
  206. printUpdateLine('?', "[unexpected push-process behavior]", srcRef,
  207. remoteName, rru.getMessage());
  208. break;
  209. }
  210. }
  211. private void printUpdateLine(final char flag, final String summary,
  212. final String srcRef, final String destRef, final String message) {
  213. out.format(" %c %-17s", flag, summary);
  214. if (srcRef != null)
  215. out.format(" %s ->", abbreviateRef(srcRef, true));
  216. out.format(" %s", abbreviateRef(destRef, true));
  217. if (message != null)
  218. out.format(" (%s)", message);
  219. out.println();
  220. }
  221. }