您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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