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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.errors.RevisionSyntaxException;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.FileMode;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.PersonIdent;
  64. import org.eclipse.jgit.lib.Repository;
  65. import org.eclipse.jgit.pgm.internal.CLIText;
  66. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  67. import org.eclipse.jgit.revwalk.RevCommit;
  68. import org.eclipse.jgit.revwalk.RevObject;
  69. import org.eclipse.jgit.revwalk.RevTag;
  70. import org.eclipse.jgit.revwalk.RevTree;
  71. import org.eclipse.jgit.revwalk.RevWalk;
  72. import org.eclipse.jgit.treewalk.TreeWalk;
  73. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  74. import org.kohsuke.args4j.Argument;
  75. import org.kohsuke.args4j.Option;
  76. @Command(common = true, usage = "usage_show")
  77. class Show extends TextBuiltin {
  78. private final TimeZone myTZ = TimeZone.getDefault();
  79. private final DateFormat fmt;
  80. private DiffFormatter diffFmt;
  81. @Argument(index = 0, metaVar = "metaVar_object")
  82. private String objectName;
  83. @Option(name = "--", metaVar = "metaVar_path", handler = PathTreeFilterHandler.class)
  84. protected TreeFilter pathFilter = TreeFilter.ALL;
  85. // BEGIN -- Options shared with Diff
  86. @Option(name = "-p", usage = "usage_showPatch")
  87. boolean showPatch;
  88. @Option(name = "-M", usage = "usage_detectRenames")
  89. private Boolean detectRenames;
  90. @Option(name = "--no-renames", usage = "usage_noRenames")
  91. void noRenames(@SuppressWarnings("unused") boolean on) {
  92. detectRenames = Boolean.FALSE;
  93. }
  94. @Option(name = "-l", usage = "usage_renameLimit")
  95. private Integer renameLimit;
  96. @Option(name = "--name-status", usage = "usage_nameStatus")
  97. private boolean showNameAndStatusOnly;
  98. @Option(name = "--ignore-space-at-eol")
  99. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  100. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
  101. }
  102. @Option(name = "--ignore-leading-space")
  103. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  104. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
  105. }
  106. @Option(name = "-b", aliases = { "--ignore-space-change" })
  107. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  108. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
  109. }
  110. @Option(name = "-w", aliases = { "--ignore-all-space" })
  111. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  112. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
  113. }
  114. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  115. void unified(int lines) {
  116. diffFmt.setContext(lines);
  117. }
  118. @Option(name = "--abbrev", metaVar = "metaVar_n")
  119. void abbrev(int lines) {
  120. diffFmt.setAbbreviationLength(lines);
  121. }
  122. @Option(name = "--full-index")
  123. void abbrev(@SuppressWarnings("unused") boolean on) {
  124. diffFmt.setAbbreviationLength(Constants.OBJECT_ID_STRING_LENGTH);
  125. }
  126. @Option(name = "--src-prefix", usage = "usage_srcPrefix")
  127. void sourcePrefix(String path) {
  128. diffFmt.setOldPrefix(path);
  129. }
  130. @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
  131. void dstPrefix(String path) {
  132. diffFmt.setNewPrefix(path);
  133. }
  134. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  135. void noPrefix(@SuppressWarnings("unused") boolean on) {
  136. diffFmt.setOldPrefix(""); //$NON-NLS-1$
  137. diffFmt.setNewPrefix(""); //$NON-NLS-1$
  138. }
  139. // END -- Options shared with Diff
  140. Show() {
  141. fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy ZZZZZ", Locale.US); //$NON-NLS-1$
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. protected void init(Repository repository, String gitDir) {
  146. super.init(repository, gitDir);
  147. diffFmt = new DiffFormatter(new BufferedOutputStream(outs));
  148. }
  149. /** {@inheritDoc} */
  150. @SuppressWarnings("boxing")
  151. @Override
  152. protected void run() {
  153. diffFmt.setRepository(db);
  154. try {
  155. diffFmt.setPathFilter(pathFilter);
  156. if (detectRenames != null) {
  157. diffFmt.setDetectRenames(detectRenames.booleanValue());
  158. }
  159. if (renameLimit != null && diffFmt.isDetectRenames()) {
  160. RenameDetector rd = diffFmt.getRenameDetector();
  161. rd.setRenameLimit(renameLimit.intValue());
  162. }
  163. ObjectId objectId;
  164. if (objectName == null) {
  165. objectId = db.resolve(Constants.HEAD);
  166. } else {
  167. objectId = db.resolve(objectName);
  168. }
  169. try (RevWalk rw = new RevWalk(db)) {
  170. RevObject obj = rw.parseAny(objectId);
  171. while (obj instanceof RevTag) {
  172. show((RevTag) obj);
  173. obj = ((RevTag) obj).getObject();
  174. rw.parseBody(obj);
  175. }
  176. switch (obj.getType()) {
  177. case Constants.OBJ_COMMIT:
  178. show(rw, (RevCommit) obj);
  179. break;
  180. case Constants.OBJ_TREE:
  181. outw.print("tree "); //$NON-NLS-1$
  182. outw.print(objectName);
  183. outw.println();
  184. outw.println();
  185. show((RevTree) obj);
  186. break;
  187. case Constants.OBJ_BLOB:
  188. db.open(obj, obj.getType()).copyTo(System.out);
  189. outw.flush();
  190. break;
  191. default:
  192. throw die(MessageFormat.format(
  193. CLIText.get().cannotReadBecause, obj.name(),
  194. obj.getType()));
  195. }
  196. }
  197. } catch (RevisionSyntaxException | IOException e) {
  198. throw die(e.getMessage(), e);
  199. } finally {
  200. diffFmt.close();
  201. }
  202. }
  203. private void show(RevTag tag) throws IOException {
  204. outw.print(CLIText.get().tagLabel);
  205. outw.print(" "); //$NON-NLS-1$
  206. outw.print(tag.getTagName());
  207. outw.println();
  208. final PersonIdent tagger = tag.getTaggerIdent();
  209. if (tagger != null) {
  210. outw.println(MessageFormat.format(CLIText.get().taggerInfo,
  211. tagger.getName(), tagger.getEmailAddress()));
  212. final TimeZone taggerTZ = tagger.getTimeZone();
  213. fmt.setTimeZone(taggerTZ != null ? taggerTZ : myTZ);
  214. outw.println(MessageFormat.format(CLIText.get().dateInfo,
  215. fmt.format(tagger.getWhen())));
  216. }
  217. outw.println();
  218. final String[] lines = tag.getFullMessage().split("\n"); //$NON-NLS-1$
  219. for (String s : lines) {
  220. outw.print(" "); //$NON-NLS-1$
  221. outw.print(s);
  222. outw.println();
  223. }
  224. outw.println();
  225. }
  226. private void show(RevTree obj) throws MissingObjectException,
  227. IncorrectObjectTypeException, CorruptObjectException, IOException {
  228. try (TreeWalk walk = new TreeWalk(db)) {
  229. walk.reset();
  230. walk.addTree(obj);
  231. while (walk.next()) {
  232. outw.print(walk.getPathString());
  233. final FileMode mode = walk.getFileMode(0);
  234. if (mode == FileMode.TREE)
  235. outw.print("/"); //$NON-NLS-1$
  236. outw.println();
  237. }
  238. }
  239. }
  240. private void show(RevWalk rw, RevCommit c) throws IOException {
  241. char[] outbuffer = new char[Constants.OBJECT_ID_LENGTH * 2];
  242. outw.print(CLIText.get().commitLabel);
  243. outw.print(" "); //$NON-NLS-1$
  244. c.getId().copyTo(outbuffer, outw);
  245. outw.println();
  246. final PersonIdent author = c.getAuthorIdent();
  247. outw.println(MessageFormat.format(CLIText.get().authorInfo,
  248. author.getName(), author.getEmailAddress()));
  249. final TimeZone authorTZ = author.getTimeZone();
  250. fmt.setTimeZone(authorTZ != null ? authorTZ : myTZ);
  251. outw.println(MessageFormat.format(CLIText.get().dateInfo,
  252. fmt.format(author.getWhen())));
  253. outw.println();
  254. final String[] lines = c.getFullMessage().split("\n"); //$NON-NLS-1$
  255. for (String s : lines) {
  256. outw.print(" "); //$NON-NLS-1$
  257. outw.print(s);
  258. outw.println();
  259. }
  260. outw.println();
  261. if (c.getParentCount() == 1) {
  262. rw.parseHeaders(c.getParent(0));
  263. showDiff(c);
  264. }
  265. outw.flush();
  266. }
  267. private void showDiff(RevCommit c) throws IOException {
  268. final RevTree a = c.getParent(0).getTree();
  269. final RevTree b = c.getTree();
  270. if (showNameAndStatusOnly)
  271. Diff.nameStatus(outw, diffFmt.scan(a, b));
  272. else {
  273. outw.flush();
  274. diffFmt.format(a, b);
  275. diffFmt.flush();
  276. }
  277. outw.println();
  278. }
  279. }