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

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