PageRenderTime 21ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Push.java

https://bitbucket.org/albfan/jgit
Java | 262 lines | 180 code | 36 blank | 46 comment | 29 complexity | 7a7ffc4639debdd473fdd32609ff7fee MD5 | raw file
  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 java.io.IOException;
  46. import java.text.MessageFormat;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.api.PushCommand;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectReader;
  54. import org.eclipse.jgit.lib.Ref;
  55. import org.eclipse.jgit.lib.TextProgressMonitor;
  56. import org.eclipse.jgit.transport.PushResult;
  57. import org.eclipse.jgit.transport.RefSpec;
  58. import org.eclipse.jgit.transport.RemoteRefUpdate;
  59. import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
  60. import org.eclipse.jgit.transport.Transport;
  61. import org.eclipse.jgit.transport.URIish;
  62. import org.kohsuke.args4j.Argument;
  63. import org.kohsuke.args4j.Option;
  64. @Command(common = true, usage = "usage_UpdateRemoteRepositoryFromLocalRefs")
  65. class Push extends TextBuiltin {
  66. @Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
  67. int timeout = -1;
  68. @Argument(index = 0, metaVar = "metaVar_uriish")
  69. private String remote = Constants.DEFAULT_REMOTE_NAME;
  70. @Argument(index = 1, metaVar = "metaVar_refspec")
  71. private final List<RefSpec> refSpecs = new ArrayList<RefSpec>();
  72. @Option(name = "--all")
  73. private boolean all;
  74. @Option(name = "--tags")
  75. private boolean tags;
  76. @Option(name = "--verbose", aliases = { "-v" })
  77. private boolean verbose = false;
  78. @Option(name = "--thin")
  79. private boolean thin = Transport.DEFAULT_PUSH_THIN;
  80. @Option(name = "--no-thin")
  81. void nothin(final boolean ignored) {
  82. thin = false;
  83. }
  84. @Option(name = "--force", aliases = { "-f" })
  85. private boolean force;
  86. @Option(name = "--receive-pack", metaVar = "metaVar_path")
  87. private String receivePack;
  88. @Option(name = "--dry-run")
  89. private boolean dryRun;
  90. private boolean shownURI;
  91. @Override
  92. protected void run() throws Exception {
  93. Git git = new Git(db);
  94. PushCommand push = git.push();
  95. push.setDryRun(dryRun);
  96. push.setForce(force);
  97. push.setProgressMonitor(new TextProgressMonitor());
  98. push.setReceivePack(receivePack);
  99. push.setRefSpecs(refSpecs);
  100. if (all)
  101. push.setPushAll();
  102. if (tags)
  103. push.setPushTags();
  104. push.setRemote(remote);
  105. push.setThin(thin);
  106. push.setTimeout(timeout);
  107. Iterable<PushResult> results = push.call();
  108. for (PushResult result : results) {
  109. ObjectReader reader = db.newObjectReader();
  110. try {
  111. printPushResult(reader, result.getURI(), result);
  112. } finally {
  113. reader.release();
  114. }
  115. }
  116. }
  117. private void printPushResult(final ObjectReader reader, final URIish uri,
  118. final PushResult result) {
  119. shownURI = false;
  120. boolean everythingUpToDate = true;
  121. // at first, print up-to-date ones...
  122. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  123. if (rru.getStatus() == Status.UP_TO_DATE) {
  124. if (verbose)
  125. printRefUpdateResult(reader, uri, result, rru);
  126. } else
  127. everythingUpToDate = false;
  128. }
  129. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  130. // ...then successful updates...
  131. if (rru.getStatus() == Status.OK)
  132. printRefUpdateResult(reader, uri, result, rru);
  133. }
  134. for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
  135. // ...finally, others (problematic)
  136. if (rru.getStatus() != Status.OK
  137. && rru.getStatus() != Status.UP_TO_DATE)
  138. printRefUpdateResult(reader, uri, result, rru);
  139. }
  140. AbstractFetchCommand.showRemoteMessages(result.getMessages());
  141. if (everythingUpToDate)
  142. out.println(CLIText.get().everythingUpToDate);
  143. }
  144. private void printRefUpdateResult(final ObjectReader reader,
  145. final URIish uri, final PushResult result, final RemoteRefUpdate rru) {
  146. if (!shownURI) {
  147. shownURI = true;
  148. out.println(MessageFormat.format(CLIText.get().pushTo, 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 = safeAbbreviate(reader, oldRef
  169. .getObjectId())
  170. + (fastForward ? ".." : "...")
  171. + safeAbbreviate(reader, rru.getNewObjectId());
  172. final String message = fastForward ? null : CLIText.get().forcedUpdate;
  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. CLIText.get().remoteSideDoesNotSupportDeletingRefs);
  183. break;
  184. case REJECTED_NONFASTFORWARD:
  185. printUpdateLine('!', "[rejected]", srcRef, remoteName,
  186. CLIText.get().nonFastForward);
  187. break;
  188. case REJECTED_REMOTE_CHANGED:
  189. final String message = MessageFormat.format(
  190. CLIText.get().remoteRefObjectChangedIsNotExpectedOne,
  191. safeAbbreviate(reader, rru.getExpectedOldObjectId()));
  192. printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
  193. break;
  194. case REJECTED_OTHER_REASON:
  195. printUpdateLine('!', "[remote rejected]", srcRef, remoteName, rru
  196. .getMessage());
  197. break;
  198. case UP_TO_DATE:
  199. if (verbose)
  200. printUpdateLine('=', "[up to date]", srcRef, remoteName, null);
  201. break;
  202. case NOT_ATTEMPTED:
  203. case AWAITING_REPORT:
  204. printUpdateLine('?', "[unexpected push-process behavior]", srcRef,
  205. remoteName, rru.getMessage());
  206. break;
  207. }
  208. }
  209. private String safeAbbreviate(ObjectReader reader, ObjectId id) {
  210. try {
  211. return reader.abbreviate(id).name();
  212. } catch (IOException cannotAbbreviate) {
  213. return id.name();
  214. }
  215. }
  216. private void printUpdateLine(final char flag, final String summary,
  217. final String srcRef, final String destRef, final String message) {
  218. out.format(" %c %-17s", flag, summary);
  219. if (srcRef != null)
  220. out.format(" %s ->", abbreviateRef(srcRef, true));
  221. out.format(" %s", abbreviateRef(destRef, true));
  222. if (message != null)
  223. out.format(" (%s)", message);
  224. out.println();
  225. }
  226. }