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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.transport.PushResult;
  58. import org.eclipse.jgit.transport.RefSpec;
  59. import org.eclipse.jgit.transport.RemoteRefUpdate;
  60. import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
  61. import org.eclipse.jgit.transport.Transport;
  62. import org.eclipse.jgit.transport.URIish;
  63. import org.kohsuke.args4j.Argument;
  64. import org.kohsuke.args4j.Option;
  65. @Command(common = true, usage = "usage_UpdateRemoteRepositoryFromLocalRefs")
  66. class Push extends TextBuiltin {
  67. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  68. int timeout = -1;
  69. @Argument(index = 0, metaVar = "metaVar_uriish")
  70. private String remote = Constants.DEFAULT_REMOTE_NAME;
  71. @Argument(index = 1, metaVar = "metaVar_refspec")
  72. private final List<RefSpec> refSpecs = new ArrayList<RefSpec>();
  73. @Option(name = "--all")
  74. private boolean all;
  75. @Option(name = "--tags")
  76. private boolean tags;
  77. @Option(name = "--verbose", aliases = { "-v" })
  78. private boolean verbose = false;
  79. @Option(name = "--thin")
  80. private boolean thin = Transport.DEFAULT_PUSH_THIN;
  81. @Option(name = "--no-thin")
  82. void nothin(@SuppressWarnings("unused") final boolean ignored) {
  83. thin = false;
  84. }
  85. @Option(name = "--force", aliases = { "-f" })
  86. private boolean force;
  87. @Option(name = "--receive-pack", metaVar = "metaVar_path")
  88. private String receivePack;
  89. @Option(name = "--dry-run")
  90. private boolean dryRun;
  91. private boolean shownURI;
  92. @Override
  93. protected void run() throws Exception {
  94. Git git = new Git(db);
  95. PushCommand push = git.push();
  96. push.setDryRun(dryRun);
  97. push.setForce(force);
  98. push.setProgressMonitor(new TextProgressMonitor());
  99. push.setReceivePack(receivePack);
  100. push.setRefSpecs(refSpecs);
  101. if (all)
  102. push.setPushAll();
  103. if (tags)
  104. push.setPushTags();
  105. push.setRemote(remote);
  106. push.setThin(thin);
  107. push.setTimeout(timeout);
  108. Iterable<PushResult> results = push.call();
  109. for (PushResult result : results) {
  110. ObjectReader reader = db.newObjectReader();
  111. try {
  112. printPushResult(reader, result.getURI(), result);
  113. } finally {
  114. reader.release();
  115. }
  116. }
  117. }
  118. private void printPushResult(final ObjectReader reader, final URIish uri,
  119. final PushResult result) throws IOException {
  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(reader, 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(reader, 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(reader, uri, result, rru);
  140. }
  141. AbstractFetchCommand.showRemoteMessages(result.getMessages());
  142. if (everythingUpToDate)
  143. outw.println(CLIText.get().everythingUpToDate);
  144. }
  145. private void printRefUpdateResult(final ObjectReader reader,
  146. final URIish uri, final PushResult result, final RemoteRefUpdate rru)
  147. throws IOException {
  148. if (!shownURI) {
  149. shownURI = true;
  150. outw.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 = safeAbbreviate(reader, oldRef
  171. .getObjectId())
  172. + (fastForward ? ".." : "...") //$NON-NLS-1$ //$NON-NLS-2$
  173. + safeAbbreviate(reader, rru.getNewObjectId());
  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. safeAbbreviate(reader, rru.getExpectedOldObjectId()));
  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 String safeAbbreviate(ObjectReader reader, ObjectId id) {
  212. try {
  213. return reader.abbreviate(id).name();
  214. } catch (IOException cannotAbbreviate) {
  215. return id.name();
  216. }
  217. }
  218. private void printUpdateLine(final char flag, final String summary,
  219. final String srcRef, final String destRef, final String message)
  220. throws IOException {
  221. outw.format(" %c %-17s", valueOf(flag), summary); //$NON-NLS-1$
  222. if (srcRef != null)
  223. outw.format(" %s ->", abbreviateRef(srcRef, true)); //$NON-NLS-1$
  224. outw.format(" %s", abbreviateRef(destRef, true)); //$NON-NLS-1$
  225. if (message != null)
  226. outw.format(" (%s)", message); //$NON-NLS-1$
  227. outw.println();
  228. }
  229. }