Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Push.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 = "--atomic")
  77. private boolean atomic;
  78. @Option(name = "--tags")
  79. private boolean tags;
  80. @Option(name = "--verbose", aliases = { "-v" })
  81. private boolean verbose = false;
  82. @Option(name = "--thin")
  83. private boolean thin = Transport.DEFAULT_PUSH_THIN;
  84. @Option(name = "--no-thin")
  85. void nothin(@SuppressWarnings("unused") final boolean ignored) {
  86. thin = false;
  87. }
  88. @Option(name = "--force", aliases = { "-f" })
  89. private boolean force;
  90. @Option(name = "--receive-pack", metaVar = "metaVar_path")
  91. private String receivePack;
  92. @Option(name = "--dry-run")
  93. private boolean dryRun;
  94. private boolean shownURI;
  95. @Override
  96. protected void run() throws Exception {
  97. try (Git git = new Git(db)) {
  98. PushCommand push = git.push();
  99. push.setDryRun(dryRun);
  100. push.setForce(force);
  101. push.setProgressMonitor(new TextProgressMonitor(errw));
  102. push.setReceivePack(receivePack);
  103. push.setRefSpecs(refSpecs);
  104. if (all)
  105. push.setPushAll();
  106. if (tags)
  107. push.setPushTags();
  108. push.setRemote(remote);
  109. push.setThin(thin);
  110. push.setAtomic(atomic);
  111. push.setTimeout(timeout);
  112. Iterable<PushResult> results = push.call();
  113. for (PushResult result : results) {
  114. try (ObjectReader reader = db.newObjectReader()) {
  115. printPushResult(reader, result.getURI(), result);
  116. }
  117. }
  118. }
  119. }
  120. private void printPushResult(final ObjectReader reader, final URIish uri,
  121. final PushResult result) throws IOException {
  122. shownURI = false;
  123. boolean everythingUpToDate = true;
  124. // at first, print up-to-date ones...
  125. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  126. if (rru.getStatus() == Status.UP_TO_DATE) {
  127. if (verbose)
  128. printRefUpdateResult(reader, uri, result, rru);
  129. } else
  130. everythingUpToDate = false;
  131. }
  132. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  133. // ...then successful updates...
  134. if (rru.getStatus() == Status.OK)
  135. printRefUpdateResult(reader, uri, result, rru);
  136. }
  137. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  138. // ...finally, others (problematic)
  139. if (rru.getStatus() != Status.OK
  140. && rru.getStatus() != Status.UP_TO_DATE)
  141. printRefUpdateResult(reader, uri, result, rru);
  142. }
  143. AbstractFetchCommand.showRemoteMessages(errw, result.getMessages());
  144. if (everythingUpToDate)
  145. outw.println(CLIText.get().everythingUpToDate);
  146. }
  147. private void printRefUpdateResult(final ObjectReader reader,
  148. final URIish uri, final PushResult result, final RemoteRefUpdate rru)
  149. throws IOException {
  150. if (!shownURI) {
  151. shownURI = true;
  152. outw.println(MessageFormat.format(CLIText.get().pushTo, uri));
  153. }
  154. final String remoteName = rru.getRemoteName();
  155. final String srcRef = rru.isDelete() ? null : rru.getSrcRef();
  156. switch (rru.getStatus()) {
  157. case OK:
  158. if (rru.isDelete())
  159. printUpdateLine('-', "[deleted]", null, remoteName, null);
  160. else {
  161. final Ref oldRef = result.getAdvertisedRef(remoteName);
  162. if (oldRef == null) {
  163. final String summary;
  164. if (remoteName.startsWith(Constants.R_TAGS))
  165. summary = "[new tag]";
  166. else
  167. summary = "[new branch]";
  168. printUpdateLine('*', summary, srcRef, remoteName, null);
  169. } else {
  170. boolean fastForward = rru.isFastForward();
  171. final char flag = fastForward ? ' ' : '+';
  172. final String summary = safeAbbreviate(reader, oldRef
  173. .getObjectId())
  174. + (fastForward ? ".." : "...") //$NON-NLS-1$ //$NON-NLS-2$
  175. + safeAbbreviate(reader, rru.getNewObjectId());
  176. final String message = fastForward ? null : CLIText.get().forcedUpdate;
  177. printUpdateLine(flag, summary, srcRef, remoteName, message);
  178. }
  179. }
  180. break;
  181. case NON_EXISTING:
  182. printUpdateLine('X', "[no match]", null, remoteName, null);
  183. break;
  184. case REJECTED_NODELETE:
  185. printUpdateLine('!', "[rejected]", null, remoteName,
  186. CLIText.get().remoteSideDoesNotSupportDeletingRefs);
  187. break;
  188. case REJECTED_NONFASTFORWARD:
  189. printUpdateLine('!', "[rejected]", srcRef, remoteName,
  190. CLIText.get().nonFastForward);
  191. break;
  192. case REJECTED_REMOTE_CHANGED:
  193. final String message = MessageFormat.format(
  194. CLIText.get().remoteRefObjectChangedIsNotExpectedOne,
  195. safeAbbreviate(reader, rru.getExpectedOldObjectId()));
  196. printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
  197. break;
  198. case REJECTED_OTHER_REASON:
  199. printUpdateLine('!', "[remote rejected]", srcRef, remoteName, rru
  200. .getMessage());
  201. break;
  202. case UP_TO_DATE:
  203. if (verbose)
  204. printUpdateLine('=', "[up to date]", srcRef, remoteName, null);
  205. break;
  206. case NOT_ATTEMPTED:
  207. case AWAITING_REPORT:
  208. printUpdateLine('?', "[unexpected push-process behavior]", srcRef,
  209. remoteName, rru.getMessage());
  210. break;
  211. }
  212. }
  213. private static String safeAbbreviate(ObjectReader reader, ObjectId id) {
  214. try {
  215. return reader.abbreviate(id).name();
  216. } catch (IOException cannotAbbreviate) {
  217. return id.name();
  218. }
  219. }
  220. private void printUpdateLine(final char flag, final String summary,
  221. final String srcRef, final String destRef, final String message)
  222. throws IOException {
  223. outw.format(" %c %-17s", valueOf(flag), summary); //$NON-NLS-1$
  224. if (srcRef != null)
  225. outw.format(" %s ->", abbreviateRef(srcRef, true)); //$NON-NLS-1$
  226. outw.format(" %s", abbreviateRef(destRef, true)); //$NON-NLS-1$
  227. if (message != null)
  228. outw.format(" (%s)", message); //$NON-NLS-1$
  229. outw.println();
  230. }
  231. }