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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.Collections;
  53. import java.util.Iterator;
  54. import java.util.List;
  55. import java.util.Locale;
  56. import java.util.Map;
  57. import java.util.Set;
  58. import java.util.TimeZone;
  59. import org.eclipse.jgit.diff.DiffEntry;
  60. import org.eclipse.jgit.diff.DiffFormatter;
  61. import org.eclipse.jgit.diff.RawTextIgnoreAllWhitespace;
  62. import org.eclipse.jgit.diff.RawTextIgnoreLeadingWhitespace;
  63. import org.eclipse.jgit.diff.RawTextIgnoreTrailingWhitespace;
  64. import org.eclipse.jgit.diff.RawTextIgnoreWhitespaceChange;
  65. import org.eclipse.jgit.diff.RenameDetector;
  66. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  67. import org.eclipse.jgit.lib.AnyObjectId;
  68. import org.eclipse.jgit.lib.Constants;
  69. import org.eclipse.jgit.lib.PersonIdent;
  70. import org.eclipse.jgit.lib.Ref;
  71. import org.eclipse.jgit.revwalk.FollowFilter;
  72. import org.eclipse.jgit.revwalk.RevCommit;
  73. import org.eclipse.jgit.revwalk.RevWalk;
  74. import org.eclipse.jgit.treewalk.TreeWalk;
  75. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  76. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  77. import org.kohsuke.args4j.Option;
  78. @Command(common = true, usage = "usage_viewCommitHistory")
  79. class Log extends RevWalkTextBuiltin {
  80. private final TimeZone myTZ = TimeZone.getDefault();
  81. private final DateFormat fmt;
  82. private final DiffFormatter diffFmt = new DiffFormatter( //
  83. new BufferedOutputStream(System.out));
  84. private Map<AnyObjectId, Set<Ref>> allRefsByPeeledObjectId;
  85. @Option(name="--decorate", usage="usage_showRefNamesMatchingCommits")
  86. private boolean decorate;
  87. // BEGIN -- Options shared with Diff
  88. @Option(name = "-p", usage = "usage_showPatch")
  89. boolean showPatch;
  90. @Option(name = "-M", usage = "usage_detectRenames")
  91. private boolean detectRenames;
  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.setRawTextFactory(RawTextIgnoreTrailingWhitespace.FACTORY);
  99. }
  100. @Option(name = "--ignore-leading-space")
  101. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  102. diffFmt.setRawTextFactory(RawTextIgnoreLeadingWhitespace.FACTORY);
  103. }
  104. @Option(name = "-b", aliases = { "--ignore-space-change" })
  105. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  106. diffFmt.setRawTextFactory(RawTextIgnoreWhitespaceChange.FACTORY);
  107. }
  108. @Option(name = "-w", aliases = { "--ignore-all-space" })
  109. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  110. diffFmt.setRawTextFactory(RawTextIgnoreAllWhitespace.FACTORY);
  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. // END -- Options shared with Diff
  125. Log() {
  126. fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy ZZZZZ", Locale.US);
  127. }
  128. @Override
  129. protected RevWalk createWalk() {
  130. RevWalk ret = super.createWalk();
  131. if (decorate)
  132. allRefsByPeeledObjectId = getRepository().getAllRefsByPeeledObjectId();
  133. return ret;
  134. }
  135. @Override
  136. protected void show(final RevCommit c) throws Exception {
  137. out.print(CLIText.get().commitLabel);
  138. out.print(" ");
  139. c.getId().copyTo(outbuffer, out);
  140. if (decorate) {
  141. Collection<Ref> list = allRefsByPeeledObjectId.get(c.copy());
  142. if (list != null) {
  143. out.print(" (");
  144. for (Iterator<Ref> i = list.iterator(); i.hasNext(); ) {
  145. out.print(i.next().getName());
  146. if (i.hasNext())
  147. out.print(" ");
  148. }
  149. out.print(")");
  150. }
  151. }
  152. out.println();
  153. final PersonIdent author = c.getAuthorIdent();
  154. out.println(MessageFormat.format(CLIText.get().authorInfo, author.getName(), author.getEmailAddress()));
  155. final TimeZone authorTZ = author.getTimeZone();
  156. fmt.setTimeZone(authorTZ != null ? authorTZ : myTZ);
  157. out.println(MessageFormat.format(CLIText.get().dateInfo, fmt.format(author.getWhen())));
  158. out.println();
  159. final String[] lines = c.getFullMessage().split("\n");
  160. for (final String s : lines) {
  161. out.print(" ");
  162. out.print(s);
  163. out.println();
  164. }
  165. out.println();
  166. if (c.getParentCount() == 1 && (showNameAndStatusOnly || showPatch))
  167. showDiff(c);
  168. out.flush();
  169. }
  170. private void showDiff(RevCommit c) throws IOException {
  171. final TreeWalk tw = new TreeWalk(db);
  172. tw.setRecursive(true);
  173. tw.reset();
  174. tw.addTree(c.getParent(0).getTree());
  175. tw.addTree(c.getTree());
  176. tw.setFilter(AndTreeFilter.create(pathFilter, TreeFilter.ANY_DIFF));
  177. List<DiffEntry> files = DiffEntry.scan(tw);
  178. if (pathFilter instanceof FollowFilter && isAdd(files)) {
  179. // The file we are following was added here, find where it
  180. // came from so we can properly show the rename or copy,
  181. // then continue digging backwards.
  182. //
  183. tw.reset();
  184. tw.addTree(c.getParent(0).getTree());
  185. tw.addTree(c.getTree());
  186. tw.setFilter(TreeFilter.ANY_DIFF);
  187. files = updateFollowFilter(detectRenames(DiffEntry.scan(tw)));
  188. } else if (detectRenames)
  189. files = detectRenames(files);
  190. if (showNameAndStatusOnly) {
  191. Diff.nameStatus(out, files);
  192. } else {
  193. diffFmt.setRepository(db);
  194. diffFmt.format(files);
  195. diffFmt.flush();
  196. }
  197. out.println();
  198. }
  199. private List<DiffEntry> detectRenames(List<DiffEntry> files)
  200. throws IOException {
  201. RenameDetector rd = new RenameDetector(db);
  202. if (renameLimit != null)
  203. rd.setRenameLimit(renameLimit.intValue());
  204. rd.addAll(files);
  205. return rd.compute();
  206. }
  207. private boolean isAdd(List<DiffEntry> files) {
  208. String oldPath = ((FollowFilter) pathFilter).getPath();
  209. for (DiffEntry ent : files) {
  210. if (ent.getChangeType() == ChangeType.ADD
  211. && ent.getNewPath().equals(oldPath))
  212. return true;
  213. }
  214. return false;
  215. }
  216. private List<DiffEntry> updateFollowFilter(List<DiffEntry> files) {
  217. String oldPath = ((FollowFilter) pathFilter).getPath();
  218. for (DiffEntry ent : files) {
  219. if (isRename(ent) && ent.getNewPath().equals(oldPath)) {
  220. pathFilter = FollowFilter.create(ent.getOldPath());
  221. return Collections.singletonList(ent);
  222. }
  223. }
  224. return Collections.emptyList();
  225. }
  226. private static boolean isRename(DiffEntry ent) {
  227. return ent.getChangeType() == ChangeType.RENAME
  228. || ent.getChangeType() == ChangeType.COPY;
  229. }
  230. }