Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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