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.

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