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.

Show.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2006-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.pgm;
  46. import java.io.BufferedOutputStream;
  47. import java.io.IOException;
  48. import java.text.DateFormat;
  49. import java.text.MessageFormat;
  50. import java.text.SimpleDateFormat;
  51. import java.util.Locale;
  52. import java.util.TimeZone;
  53. import org.eclipse.jgit.diff.DiffFormatter;
  54. import org.eclipse.jgit.diff.RawTextComparator;
  55. import org.eclipse.jgit.diff.RenameDetector;
  56. import org.eclipse.jgit.errors.CorruptObjectException;
  57. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  58. import org.eclipse.jgit.errors.MissingObjectException;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.FileMode;
  61. import org.eclipse.jgit.lib.ObjectId;
  62. import org.eclipse.jgit.lib.PersonIdent;
  63. import org.eclipse.jgit.pgm.internal.CLIText;
  64. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.revwalk.RevObject;
  67. import org.eclipse.jgit.revwalk.RevTag;
  68. import org.eclipse.jgit.revwalk.RevTree;
  69. import org.eclipse.jgit.revwalk.RevWalk;
  70. import org.eclipse.jgit.treewalk.TreeWalk;
  71. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  72. import org.kohsuke.args4j.Argument;
  73. import org.kohsuke.args4j.Option;
  74. @Command(common = true, usage = "usage_show")
  75. class Show extends TextBuiltin {
  76. private final TimeZone myTZ = TimeZone.getDefault();
  77. private final DateFormat fmt;
  78. private final DiffFormatter diffFmt = new DiffFormatter( //
  79. new BufferedOutputStream(System.out));
  80. @Argument(index = 0, metaVar = "metaVar_object")
  81. private String objectName;
  82. @Option(name = "--", metaVar = "metaVar_path", multiValued = true, handler = PathTreeFilterHandler.class)
  83. protected TreeFilter pathFilter = TreeFilter.ALL;
  84. // BEGIN -- Options shared with Diff
  85. @Option(name = "-p", usage = "usage_showPatch")
  86. boolean showPatch;
  87. @Option(name = "-M", usage = "usage_detectRenames")
  88. private Boolean detectRenames;
  89. @Option(name = "--no-renames", usage = "usage_noRenames")
  90. void noRenames(@SuppressWarnings("unused") boolean on) {
  91. detectRenames = Boolean.FALSE;
  92. }
  93. @Option(name = "-l", usage = "usage_renameLimit")
  94. private Integer renameLimit;
  95. @Option(name = "--name-status", usage = "usage_nameStatus")
  96. private boolean showNameAndStatusOnly;
  97. @Option(name = "--ignore-space-at-eol")
  98. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  99. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
  100. }
  101. @Option(name = "--ignore-leading-space")
  102. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  103. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
  104. }
  105. @Option(name = "-b", aliases = { "--ignore-space-change" })
  106. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  107. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
  108. }
  109. @Option(name = "-w", aliases = { "--ignore-all-space" })
  110. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  111. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
  112. }
  113. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  114. void unified(int lines) {
  115. diffFmt.setContext(lines);
  116. }
  117. @Option(name = "--abbrev", metaVar = "metaVar_n")
  118. void abbrev(int lines) {
  119. diffFmt.setAbbreviationLength(lines);
  120. }
  121. @Option(name = "--full-index")
  122. void abbrev(@SuppressWarnings("unused") boolean on) {
  123. diffFmt.setAbbreviationLength(Constants.OBJECT_ID_STRING_LENGTH);
  124. }
  125. @Option(name = "--src-prefix", usage = "usage_srcPrefix")
  126. void sourcePrefix(String path) {
  127. diffFmt.setOldPrefix(path);
  128. }
  129. @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
  130. void dstPrefix(String path) {
  131. diffFmt.setNewPrefix(path);
  132. }
  133. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  134. void noPrefix(@SuppressWarnings("unused") boolean on) {
  135. diffFmt.setOldPrefix(""); //$NON-NLS-1$
  136. diffFmt.setNewPrefix(""); //$NON-NLS-1$
  137. }
  138. // END -- Options shared with Diff
  139. Show() {
  140. fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy ZZZZZ", Locale.US); //$NON-NLS-1$
  141. }
  142. @SuppressWarnings("boxing")
  143. @Override
  144. protected void run() throws Exception {
  145. diffFmt.setRepository(db);
  146. try {
  147. diffFmt.setPathFilter(pathFilter);
  148. if (detectRenames != null)
  149. diffFmt.setDetectRenames(detectRenames.booleanValue());
  150. if (renameLimit != null && diffFmt.isDetectRenames()) {
  151. RenameDetector rd = diffFmt.getRenameDetector();
  152. rd.setRenameLimit(renameLimit.intValue());
  153. }
  154. ObjectId objectId;
  155. if (objectName == null)
  156. objectId = db.resolve(Constants.HEAD);
  157. else
  158. objectId = db.resolve(objectName);
  159. RevWalk rw = new RevWalk(db);
  160. try {
  161. RevObject obj = rw.parseAny(objectId);
  162. while (obj instanceof RevTag) {
  163. show((RevTag) obj);
  164. obj = ((RevTag) obj).getObject();
  165. rw.parseBody(obj);
  166. }
  167. switch (obj.getType()) {
  168. case Constants.OBJ_COMMIT:
  169. show(rw, (RevCommit) obj);
  170. break;
  171. case Constants.OBJ_TREE:
  172. outw.print("tree "); //$NON-NLS-1$
  173. outw.print(objectName);
  174. outw.println();
  175. outw.println();
  176. show((RevTree) obj);
  177. break;
  178. case Constants.OBJ_BLOB:
  179. db.open(obj, obj.getType()).copyTo(System.out);
  180. outw.flush();
  181. break;
  182. default:
  183. throw die(MessageFormat.format(
  184. CLIText.get().cannotReadBecause, obj.name(),
  185. obj.getType()));
  186. }
  187. } finally {
  188. rw.release();
  189. }
  190. } finally {
  191. diffFmt.release();
  192. }
  193. }
  194. private void show(RevTag tag) throws IOException {
  195. outw.print(CLIText.get().tagLabel);
  196. outw.print(" "); //$NON-NLS-1$
  197. outw.print(tag.getTagName());
  198. outw.println();
  199. final PersonIdent tagger = tag.getTaggerIdent();
  200. if (tagger != null) {
  201. outw.println(MessageFormat.format(CLIText.get().taggerInfo,
  202. tagger.getName(), tagger.getEmailAddress()));
  203. final TimeZone taggerTZ = tagger.getTimeZone();
  204. fmt.setTimeZone(taggerTZ != null ? taggerTZ : myTZ);
  205. outw.println(MessageFormat.format(CLIText.get().dateInfo,
  206. fmt.format(tagger.getWhen())));
  207. }
  208. outw.println();
  209. final String[] lines = tag.getFullMessage().split("\n"); //$NON-NLS-1$
  210. for (final String s : lines) {
  211. outw.print(" "); //$NON-NLS-1$
  212. outw.print(s);
  213. outw.println();
  214. }
  215. outw.println();
  216. }
  217. private void show(RevTree obj) throws MissingObjectException,
  218. IncorrectObjectTypeException, CorruptObjectException, IOException {
  219. final TreeWalk walk = new TreeWalk(db);
  220. walk.reset();
  221. walk.addTree(obj);
  222. while (walk.next()) {
  223. outw.print(walk.getPathString());
  224. final FileMode mode = walk.getFileMode(0);
  225. if (mode == FileMode.TREE)
  226. outw.print("/"); //$NON-NLS-1$
  227. outw.println();
  228. }
  229. }
  230. private void show(RevWalk rw, final RevCommit c) throws Exception {
  231. char[] outbuffer = new char[Constants.OBJECT_ID_LENGTH * 2];
  232. outw.print(CLIText.get().commitLabel);
  233. outw.print(" "); //$NON-NLS-1$
  234. c.getId().copyTo(outbuffer, outw);
  235. outw.println();
  236. final PersonIdent author = c.getAuthorIdent();
  237. outw.println(MessageFormat.format(CLIText.get().authorInfo,
  238. author.getName(), author.getEmailAddress()));
  239. final TimeZone authorTZ = author.getTimeZone();
  240. fmt.setTimeZone(authorTZ != null ? authorTZ : myTZ);
  241. outw.println(MessageFormat.format(CLIText.get().dateInfo,
  242. fmt.format(author.getWhen())));
  243. outw.println();
  244. final String[] lines = c.getFullMessage().split("\n"); //$NON-NLS-1$
  245. for (final String s : lines) {
  246. outw.print(" "); //$NON-NLS-1$
  247. outw.print(s);
  248. outw.println();
  249. }
  250. outw.println();
  251. if (c.getParentCount() == 1) {
  252. rw.parseHeaders(c.getParent(0));
  253. showDiff(c);
  254. }
  255. outw.flush();
  256. }
  257. private void showDiff(RevCommit c) throws IOException {
  258. final RevTree a = c.getParent(0).getTree();
  259. final RevTree b = c.getTree();
  260. if (showNameAndStatusOnly)
  261. Diff.nameStatus(outw, diffFmt.scan(a, b));
  262. else {
  263. outw.flush();
  264. diffFmt.format(a, b);
  265. diffFmt.flush();
  266. }
  267. outw.println();
  268. }
  269. }