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.

AbstractFetchCommand.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>
  3. * Copyright (C) 2008-2010, Google Inc.
  4. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  5. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  6. * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com>
  7. * and other copyright owners as documented in the project's IP log.
  8. *
  9. * This program and the accompanying materials are made available
  10. * under the terms of the Eclipse Distribution License v1.0 which
  11. * accompanies this distribution, is reproduced below, and is
  12. * available at http://www.eclipse.org/org/documents/edl-v10.php
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials provided
  26. * with the distribution.
  27. *
  28. * - Neither the name of the Eclipse Foundation, Inc. nor the
  29. * names of its contributors may be used to endorse or promote
  30. * products derived from this software without specific prior
  31. * written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  40. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  43. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  45. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. package org.eclipse.jgit.pgm;
  48. import static java.lang.Character.valueOf;
  49. import java.io.IOException;
  50. import java.io.PrintWriter;
  51. import java.text.MessageFormat;
  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.RefUpdate;
  56. import org.eclipse.jgit.transport.FetchResult;
  57. import org.eclipse.jgit.transport.TrackingRefUpdate;
  58. import org.kohsuke.args4j.Option;
  59. abstract class AbstractFetchCommand extends TextBuiltin {
  60. @Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beMoreVerbose")
  61. private boolean verbose;
  62. protected void showFetchResult(final FetchResult r) throws IOException {
  63. ObjectReader reader = db.newObjectReader();
  64. try {
  65. boolean shownURI = false;
  66. for (final TrackingRefUpdate u : r.getTrackingRefUpdates()) {
  67. if (!verbose && u.getResult() == RefUpdate.Result.NO_CHANGE)
  68. continue;
  69. final char type = shortTypeOf(u.getResult());
  70. final String longType = longTypeOf(reader, u);
  71. final String src = abbreviateRef(u.getRemoteName(), false);
  72. final String dst = abbreviateRef(u.getLocalName(), true);
  73. if (!shownURI) {
  74. outw.println(MessageFormat.format(CLIText.get().fromURI,
  75. r.getURI()));
  76. shownURI = true;
  77. }
  78. outw.format(" %c %-17s %-10s -> %s", valueOf(type), longType, //$NON-NLS-1$
  79. src, dst);
  80. outw.println();
  81. }
  82. } finally {
  83. reader.release();
  84. }
  85. showRemoteMessages(r.getMessages());
  86. }
  87. static void showRemoteMessages(String pkt) {
  88. PrintWriter writer = new PrintWriter(System.err);
  89. while (0 < pkt.length()) {
  90. final int lf = pkt.indexOf('\n');
  91. final int cr = pkt.indexOf('\r');
  92. final int s;
  93. if (0 <= lf && 0 <= cr)
  94. s = Math.min(lf, cr);
  95. else if (0 <= lf)
  96. s = lf;
  97. else if (0 <= cr)
  98. s = cr;
  99. else {
  100. writer.print(MessageFormat.format(CLIText.get().remoteMessage,
  101. pkt));
  102. writer.println();
  103. break;
  104. }
  105. if (pkt.charAt(s) == '\r') {
  106. writer.print(MessageFormat.format(CLIText.get().remoteMessage,
  107. pkt.substring(0, s)));
  108. writer.print('\r');
  109. } else {
  110. writer.print(MessageFormat.format(CLIText.get().remoteMessage,
  111. pkt.substring(0, s)));
  112. writer.println();
  113. }
  114. pkt = pkt.substring(s + 1);
  115. }
  116. writer.flush();
  117. }
  118. private String longTypeOf(ObjectReader reader, final TrackingRefUpdate u) {
  119. final RefUpdate.Result r = u.getResult();
  120. if (r == RefUpdate.Result.LOCK_FAILURE)
  121. return "[lock fail]";
  122. if (r == RefUpdate.Result.IO_FAILURE)
  123. return "[i/o error]";
  124. if (r == RefUpdate.Result.REJECTED)
  125. return "[rejected]";
  126. if (ObjectId.zeroId().equals(u.getNewObjectId()))
  127. return "[deleted]";
  128. if (r == RefUpdate.Result.NEW) {
  129. if (u.getRemoteName().startsWith(Constants.R_HEADS))
  130. return "[new branch]";
  131. else if (u.getLocalName().startsWith(Constants.R_TAGS))
  132. return "[new tag]";
  133. return "[new]";
  134. }
  135. if (r == RefUpdate.Result.FORCED) {
  136. final String aOld = safeAbbreviate(reader, u.getOldObjectId());
  137. final String aNew = safeAbbreviate(reader, u.getNewObjectId());
  138. return aOld + "..." + aNew; //$NON-NLS-1$
  139. }
  140. if (r == RefUpdate.Result.FAST_FORWARD) {
  141. final String aOld = safeAbbreviate(reader, u.getOldObjectId());
  142. final String aNew = safeAbbreviate(reader, u.getNewObjectId());
  143. return aOld + ".." + aNew; //$NON-NLS-1$
  144. }
  145. if (r == RefUpdate.Result.NO_CHANGE)
  146. return "[up to date]";
  147. return "[" + r.name() + "]"; //$NON-NLS-1$//$NON-NLS-2$
  148. }
  149. private String safeAbbreviate(ObjectReader reader, ObjectId id) {
  150. try {
  151. return reader.abbreviate(id).name();
  152. } catch (IOException cannotAbbreviate) {
  153. return id.name();
  154. }
  155. }
  156. private static char shortTypeOf(final RefUpdate.Result r) {
  157. if (r == RefUpdate.Result.LOCK_FAILURE)
  158. return '!';
  159. if (r == RefUpdate.Result.IO_FAILURE)
  160. return '!';
  161. if (r == RefUpdate.Result.NEW)
  162. return '*';
  163. if (r == RefUpdate.Result.FORCED)
  164. return '+';
  165. if (r == RefUpdate.Result.FAST_FORWARD)
  166. return ' ';
  167. if (r == RefUpdate.Result.REJECTED)
  168. return '!';
  169. if (r == RefUpdate.Result.NO_CHANGE)
  170. return '=';
  171. return ' ';
  172. }
  173. }