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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.pgm;
  45. import static java.lang.Character.valueOf;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.api.PushCommand;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.ObjectReader;
  55. import org.eclipse.jgit.lib.Ref;
  56. import org.eclipse.jgit.lib.TextProgressMonitor;
  57. import org.eclipse.jgit.pgm.internal.CLIText;
  58. import org.eclipse.jgit.transport.PushResult;
  59. import org.eclipse.jgit.transport.RefSpec;
  60. import org.eclipse.jgit.transport.RemoteRefUpdate;
  61. import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
  62. import org.eclipse.jgit.transport.Transport;
  63. import org.eclipse.jgit.transport.URIish;
  64. import org.kohsuke.args4j.Argument;
  65. import org.kohsuke.args4j.Option;
  66. @Command(common = true, usage = "usage_UpdateRemoteRepositoryFromLocalRefs")
  67. class Push extends TextBuiltin {
  68. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  69. int timeout = -1;
  70. @Argument(index = 0, metaVar = "metaVar_uriish")
  71. private String remote = Constants.DEFAULT_REMOTE_NAME;
  72. @Argument(index = 1, metaVar = "metaVar_refspec")
  73. private final List<RefSpec> refSpecs = new ArrayList<RefSpec>();
  74. @Option(name = "--all")
  75. private boolean all;
  76. @Option(name = "--tags")
  77. private boolean tags;
  78. @Option(name = "--verbose", aliases = { "-v" })
  79. private boolean verbose = false;
  80. @Option(name = "--thin")
  81. private boolean thin = Transport.DEFAULT_PUSH_THIN;
  82. @Option(name = "--no-thin")
  83. void nothin(@SuppressWarnings("unused") final boolean ignored) {
  84. thin = false;
  85. }
  86. @Option(name = "--force", aliases = { "-f" })
  87. private boolean force;
  88. @Option(name = "--receive-pack", metaVar = "metaVar_path")
  89. private String receivePack;
  90. @Option(name = "--dry-run")
  91. private boolean dryRun;
  92. private boolean shownURI;
  93. @Override
  94. protected void run() throws Exception {
  95. Git git = new Git(db);
  96. PushCommand push = git.push();
  97. push.setDryRun(dryRun);
  98. push.setForce(force);
  99. push.setProgressMonitor(new TextProgressMonitor());
  100. push.setReceivePack(receivePack);
  101. push.setRefSpecs(refSpecs);
  102. if (all)
  103. push.setPushAll();
  104. if (tags)
  105. push.setPushTags();
  106. push.setRemote(remote);
  107. push.setThin(thin);
  108. push.setTimeout(timeout);
  109. Iterable<PushResult> results = push.call();
  110. for (PushResult result : results) {
  111. ObjectReader reader = db.newObjectReader();
  112. try {
  113. printPushResult(reader, result.getURI(), result);
  114. } finally {
  115. reader.release();
  116. }
  117. }
  118. }
  119. private void printPushResult(final ObjectReader reader, final URIish uri,
  120. final PushResult result) throws IOException {
  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(reader, 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(reader, 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(reader, uri, result, rru);
  141. }
  142. AbstractFetchCommand.showRemoteMessages(errw, result.getMessages());
  143. if (everythingUpToDate)
  144. outw.println(CLIText.get().everythingUpToDate);
  145. }
  146. private void printRefUpdateResult(final ObjectReader reader,
  147. final URIish uri, final PushResult result, final RemoteRefUpdate rru)
  148. throws IOException {
  149. if (!shownURI) {
  150. shownURI = true;
  151. outw.println(MessageFormat.format(CLIText.get().pushTo, uri));
  152. }
  153. final String remoteName = rru.getRemoteName();
  154. final String srcRef = rru.isDelete() ? null : rru.getSrcRef();
  155. switch (rru.getStatus()) {
  156. case OK:
  157. if (rru.isDelete())
  158. printUpdateLine('-', "[deleted]", null, remoteName, null);
  159. else {
  160. final Ref oldRef = result.getAdvertisedRef(remoteName);
  161. if (oldRef == null) {
  162. final String summary;
  163. if (remoteName.startsWith(Constants.R_TAGS))
  164. summary = "[new tag]";
  165. else
  166. summary = "[new branch]";
  167. printUpdateLine('*', summary, srcRef, remoteName, null);
  168. } else {
  169. boolean fastForward = rru.isFastForward();
  170. final char flag = fastForward ? ' ' : '+';
  171. final String summary = safeAbbreviate(reader, oldRef
  172. .getObjectId())
  173. + (fastForward ? ".." : "...") //$NON-NLS-1$ //$NON-NLS-2$
  174. + safeAbbreviate(reader, rru.getNewObjectId());
  175. final String message = fastForward ? null : CLIText.get().forcedUpdate;
  176. printUpdateLine(flag, summary, srcRef, remoteName, message);
  177. }
  178. }
  179. break;
  180. case NON_EXISTING:
  181. printUpdateLine('X', "[no match]", null, remoteName, null);
  182. break;
  183. case REJECTED_NODELETE:
  184. printUpdateLine('!', "[rejected]", null, remoteName,
  185. CLIText.get().remoteSideDoesNotSupportDeletingRefs);
  186. break;
  187. case REJECTED_NONFASTFORWARD:
  188. printUpdateLine('!', "[rejected]", srcRef, remoteName,
  189. CLIText.get().nonFastForward);
  190. break;
  191. case REJECTED_REMOTE_CHANGED:
  192. final String message = MessageFormat.format(
  193. CLIText.get().remoteRefObjectChangedIsNotExpectedOne,
  194. safeAbbreviate(reader, rru.getExpectedOldObjectId()));
  195. printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
  196. break;
  197. case REJECTED_OTHER_REASON:
  198. printUpdateLine('!', "[remote rejected]", srcRef, remoteName, rru
  199. .getMessage());
  200. break;
  201. case UP_TO_DATE:
  202. if (verbose)
  203. printUpdateLine('=', "[up to date]", srcRef, remoteName, null);
  204. break;
  205. case NOT_ATTEMPTED:
  206. case AWAITING_REPORT:
  207. printUpdateLine('?', "[unexpected push-process behavior]", srcRef,
  208. remoteName, rru.getMessage());
  209. break;
  210. }
  211. }
  212. private static String safeAbbreviate(ObjectReader reader, ObjectId id) {
  213. try {
  214. return reader.abbreviate(id).name();
  215. } catch (IOException cannotAbbreviate) {
  216. return id.name();
  217. }
  218. }
  219. private void printUpdateLine(final char flag, final String summary,
  220. final String srcRef, final String destRef, final String message)
  221. throws IOException {
  222. outw.format(" %c %-17s", valueOf(flag), summary); //$NON-NLS-1$
  223. if (srcRef != null)
  224. outw.format(" %s ->", abbreviateRef(srcRef, true)); //$NON-NLS-1$
  225. outw.format(" %s", abbreviateRef(destRef, true)); //$NON-NLS-1$
  226. if (message != null)
  227. outw.format(" (%s)", message); //$NON-NLS-1$
  228. outw.println();
  229. }
  230. }