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.

Log.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.Collection;
  52. import java.util.Iterator;
  53. import java.util.Locale;
  54. import java.util.Map;
  55. import java.util.Set;
  56. import java.util.TimeZone;
  57. import org.eclipse.jgit.diff.DiffFormatter;
  58. import org.eclipse.jgit.diff.RawTextIgnoreAllWhitespace;
  59. import org.eclipse.jgit.diff.RawTextIgnoreLeadingWhitespace;
  60. import org.eclipse.jgit.diff.RawTextIgnoreTrailingWhitespace;
  61. import org.eclipse.jgit.diff.RawTextIgnoreWhitespaceChange;
  62. import org.eclipse.jgit.diff.RenameDetector;
  63. import org.eclipse.jgit.lib.AnyObjectId;
  64. import org.eclipse.jgit.lib.Constants;
  65. import org.eclipse.jgit.lib.PersonIdent;
  66. import org.eclipse.jgit.lib.Ref;
  67. import org.eclipse.jgit.revwalk.RevCommit;
  68. import org.eclipse.jgit.revwalk.RevTree;
  69. import org.eclipse.jgit.revwalk.RevWalk;
  70. import org.kohsuke.args4j.Option;
  71. @Command(common = true, usage = "usage_viewCommitHistory")
  72. class Log extends RevWalkTextBuiltin {
  73. private final TimeZone myTZ = TimeZone.getDefault();
  74. private final DateFormat fmt;
  75. private final DiffFormatter diffFmt = new DiffFormatter( //
  76. new BufferedOutputStream(System.out));
  77. private Map<AnyObjectId, Set<Ref>> allRefsByPeeledObjectId;
  78. @Option(name="--decorate", usage="usage_showRefNamesMatchingCommits")
  79. private boolean decorate;
  80. // BEGIN -- Options shared with Diff
  81. @Option(name = "-p", usage = "usage_showPatch")
  82. boolean showPatch;
  83. @Option(name = "-M", usage = "usage_detectRenames")
  84. private Boolean detectRenames;
  85. @Option(name = "--no-renames", usage = "usage_noRenames")
  86. void noRenames(@SuppressWarnings("unused") boolean on) {
  87. detectRenames = Boolean.FALSE;
  88. }
  89. @Option(name = "-l", usage = "usage_renameLimit")
  90. private Integer renameLimit;
  91. @Option(name = "--name-status", usage = "usage_nameStatus")
  92. private boolean showNameAndStatusOnly;
  93. @Option(name = "--ignore-space-at-eol")
  94. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  95. diffFmt.setRawTextFactory(RawTextIgnoreTrailingWhitespace.FACTORY);
  96. }
  97. @Option(name = "--ignore-leading-space")
  98. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  99. diffFmt.setRawTextFactory(RawTextIgnoreLeadingWhitespace.FACTORY);
  100. }
  101. @Option(name = "-b", aliases = { "--ignore-space-change" })
  102. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  103. diffFmt.setRawTextFactory(RawTextIgnoreWhitespaceChange.FACTORY);
  104. }
  105. @Option(name = "-w", aliases = { "--ignore-all-space" })
  106. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  107. diffFmt.setRawTextFactory(RawTextIgnoreAllWhitespace.FACTORY);
  108. }
  109. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  110. void unified(int lines) {
  111. diffFmt.setContext(lines);
  112. }
  113. @Option(name = "--abbrev", metaVar = "metaVar_n")
  114. void abbrev(int lines) {
  115. diffFmt.setAbbreviationLength(lines);
  116. }
  117. @Option(name = "--full-index")
  118. void abbrev(@SuppressWarnings("unused") boolean on) {
  119. diffFmt.setAbbreviationLength(Constants.OBJECT_ID_STRING_LENGTH);
  120. }
  121. @Option(name = "--src-prefix", usage = "usage_srcPrefix")
  122. void sourcePrefix(String path) {
  123. diffFmt.setOldPrefix(path);
  124. }
  125. @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
  126. void dstPrefix(String path) {
  127. diffFmt.setNewPrefix(path);
  128. }
  129. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  130. void noPrefix(@SuppressWarnings("unused") boolean on) {
  131. diffFmt.setOldPrefix("");
  132. diffFmt.setNewPrefix("");
  133. }
  134. // END -- Options shared with Diff
  135. Log() {
  136. fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy ZZZZZ", Locale.US);
  137. }
  138. @Override
  139. protected RevWalk createWalk() {
  140. RevWalk ret = super.createWalk();
  141. if (decorate)
  142. allRefsByPeeledObjectId = getRepository().getAllRefsByPeeledObjectId();
  143. return ret;
  144. }
  145. @Override
  146. protected void run() throws Exception {
  147. diffFmt.setRepository(db);
  148. try {
  149. diffFmt.setPathFilter(pathFilter);
  150. if (detectRenames != null)
  151. diffFmt.setDetectRenames(detectRenames.booleanValue());
  152. if (renameLimit != null && diffFmt.isDetectRenames()) {
  153. RenameDetector rd = diffFmt.getRenameDetector();
  154. rd.setRenameLimit(renameLimit.intValue());
  155. }
  156. super.run();
  157. } finally {
  158. diffFmt.release();
  159. }
  160. }
  161. @Override
  162. protected void show(final RevCommit c) throws Exception {
  163. out.print(CLIText.get().commitLabel);
  164. out.print(" ");
  165. c.getId().copyTo(outbuffer, out);
  166. if (decorate) {
  167. Collection<Ref> list = allRefsByPeeledObjectId.get(c);
  168. if (list != null) {
  169. out.print(" (");
  170. for (Iterator<Ref> i = list.iterator(); i.hasNext(); ) {
  171. out.print(i.next().getName());
  172. if (i.hasNext())
  173. out.print(" ");
  174. }
  175. out.print(")");
  176. }
  177. }
  178. out.println();
  179. final PersonIdent author = c.getAuthorIdent();
  180. out.println(MessageFormat.format(CLIText.get().authorInfo, author.getName(), author.getEmailAddress()));
  181. final TimeZone authorTZ = author.getTimeZone();
  182. fmt.setTimeZone(authorTZ != null ? authorTZ : myTZ);
  183. out.println(MessageFormat.format(CLIText.get().dateInfo, fmt.format(author.getWhen())));
  184. out.println();
  185. final String[] lines = c.getFullMessage().split("\n");
  186. for (final String s : lines) {
  187. out.print(" ");
  188. out.print(s);
  189. out.println();
  190. }
  191. out.println();
  192. if (c.getParentCount() == 1 && (showNameAndStatusOnly || showPatch))
  193. showDiff(c);
  194. out.flush();
  195. }
  196. private void showDiff(RevCommit c) throws IOException {
  197. final RevTree a = c.getParent(0).getTree();
  198. final RevTree b = c.getTree();
  199. if (showNameAndStatusOnly)
  200. Diff.nameStatus(out, diffFmt.scan(a, b));
  201. else {
  202. out.flush();
  203. diffFmt.format(a, b);
  204. diffFmt.flush();
  205. }
  206. out.println();
  207. }
  208. }