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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.MessageFormat;
  49. import java.util.ArrayList;
  50. import java.util.Collection;
  51. import java.util.Iterator;
  52. import java.util.LinkedHashMap;
  53. import java.util.List;
  54. import java.util.Map;
  55. import java.util.Set;
  56. import org.eclipse.jgit.diff.DiffFormatter;
  57. import org.eclipse.jgit.diff.RawText;
  58. import org.eclipse.jgit.diff.RawTextComparator;
  59. import org.eclipse.jgit.diff.RenameDetector;
  60. import org.eclipse.jgit.errors.LargeObjectException;
  61. import org.eclipse.jgit.lib.AnyObjectId;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.PersonIdent;
  65. import org.eclipse.jgit.lib.Ref;
  66. import org.eclipse.jgit.notes.NoteMap;
  67. import org.eclipse.jgit.revwalk.RevCommit;
  68. import org.eclipse.jgit.revwalk.RevTree;
  69. import org.eclipse.jgit.util.GitDateFormatter;
  70. import org.eclipse.jgit.util.GitDateFormatter.Format;
  71. import org.kohsuke.args4j.Option;
  72. @Command(common = true, usage = "usage_viewCommitHistory")
  73. class Log extends RevWalkTextBuiltin {
  74. private GitDateFormatter dateFormatter = new GitDateFormatter(
  75. Format.DEFAULT);
  76. private final DiffFormatter diffFmt = new DiffFormatter( //
  77. new BufferedOutputStream(System.out));
  78. private Map<AnyObjectId, Set<Ref>> allRefsByPeeledObjectId;
  79. private Map<String, NoteMap> noteMaps;
  80. @Option(name="--decorate", usage="usage_showRefNamesMatchingCommits")
  81. private boolean decorate;
  82. @Option(name = "--no-standard-notes", usage = "usage_noShowStandardNotes")
  83. private boolean noStandardNotes;
  84. private List<String> additionalNoteRefs = new ArrayList<String>();
  85. @Option(name = "--show-notes", usage = "usage_showNotes", metaVar = "metaVar_ref")
  86. void addAdditionalNoteRef(String notesRef) {
  87. additionalNoteRefs.add(notesRef);
  88. }
  89. @Option(name = "--date", usage = "usage_date")
  90. void dateFormat(String date) {
  91. if (date.toLowerCase().equals(date))
  92. date = date.toUpperCase();
  93. dateFormatter = new GitDateFormatter(Format.valueOf(date));
  94. }
  95. // BEGIN -- Options shared with Diff
  96. @Option(name = "-p", usage = "usage_showPatch")
  97. boolean showPatch;
  98. @Option(name = "-M", usage = "usage_detectRenames")
  99. private Boolean detectRenames;
  100. @Option(name = "--no-renames", usage = "usage_noRenames")
  101. void noRenames(@SuppressWarnings("unused") boolean on) {
  102. detectRenames = Boolean.FALSE;
  103. }
  104. @Option(name = "-l", usage = "usage_renameLimit")
  105. private Integer renameLimit;
  106. @Option(name = "--name-status", usage = "usage_nameStatus")
  107. private boolean showNameAndStatusOnly;
  108. @Option(name = "--ignore-space-at-eol")
  109. void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
  110. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
  111. }
  112. @Option(name = "--ignore-leading-space")
  113. void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
  114. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
  115. }
  116. @Option(name = "-b", aliases = { "--ignore-space-change" })
  117. void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
  118. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
  119. }
  120. @Option(name = "-w", aliases = { "--ignore-all-space" })
  121. void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
  122. diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
  123. }
  124. @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
  125. void unified(int lines) {
  126. diffFmt.setContext(lines);
  127. }
  128. @Option(name = "--abbrev", metaVar = "metaVar_n")
  129. void abbrev(int lines) {
  130. diffFmt.setAbbreviationLength(lines);
  131. }
  132. @Option(name = "--full-index")
  133. void abbrev(@SuppressWarnings("unused") boolean on) {
  134. diffFmt.setAbbreviationLength(Constants.OBJECT_ID_STRING_LENGTH);
  135. }
  136. @Option(name = "--src-prefix", usage = "usage_srcPrefix")
  137. void sourcePrefix(String path) {
  138. diffFmt.setOldPrefix(path);
  139. }
  140. @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
  141. void dstPrefix(String path) {
  142. diffFmt.setNewPrefix(path);
  143. }
  144. @Option(name = "--no-prefix", usage = "usage_noPrefix")
  145. void noPrefix(@SuppressWarnings("unused") boolean on) {
  146. diffFmt.setOldPrefix("");
  147. diffFmt.setNewPrefix("");
  148. }
  149. // END -- Options shared with Diff
  150. Log() {
  151. dateFormatter = new GitDateFormatter(Format.DEFAULT);
  152. }
  153. @Override
  154. protected void run() throws Exception {
  155. diffFmt.setRepository(db);
  156. try {
  157. diffFmt.setPathFilter(pathFilter);
  158. if (detectRenames != null)
  159. diffFmt.setDetectRenames(detectRenames.booleanValue());
  160. if (renameLimit != null && diffFmt.isDetectRenames()) {
  161. RenameDetector rd = diffFmt.getRenameDetector();
  162. rd.setRenameLimit(renameLimit.intValue());
  163. }
  164. if (!noStandardNotes || !additionalNoteRefs.isEmpty()) {
  165. createWalk();
  166. noteMaps = new LinkedHashMap<String, NoteMap>();
  167. if (!noStandardNotes) {
  168. addNoteMap(Constants.R_NOTES_COMMITS);
  169. }
  170. if (!additionalNoteRefs.isEmpty()) {
  171. for (String notesRef : additionalNoteRefs) {
  172. if (!notesRef.startsWith(Constants.R_NOTES)) {
  173. notesRef = Constants.R_NOTES + notesRef;
  174. }
  175. addNoteMap(notesRef);
  176. }
  177. }
  178. }
  179. if (decorate)
  180. allRefsByPeeledObjectId = getRepository()
  181. .getAllRefsByPeeledObjectId();
  182. super.run();
  183. } finally {
  184. diffFmt.release();
  185. }
  186. }
  187. private void addNoteMap(String notesRef) throws IOException {
  188. Ref notes = db.getRef(notesRef);
  189. if (notes == null)
  190. return;
  191. RevCommit notesCommit = argWalk.parseCommit(notes.getObjectId());
  192. noteMaps.put(notesRef,
  193. NoteMap.read(argWalk.getObjectReader(), notesCommit));
  194. }
  195. @Override
  196. protected void show(final RevCommit c) throws Exception {
  197. out.print(CLIText.get().commitLabel);
  198. out.print(" ");
  199. c.getId().copyTo(outbuffer, out);
  200. if (decorate) {
  201. Collection<Ref> list = allRefsByPeeledObjectId.get(c);
  202. if (list != null) {
  203. out.print(" (");
  204. for (Iterator<Ref> i = list.iterator(); i.hasNext(); ) {
  205. out.print(i.next().getName());
  206. if (i.hasNext())
  207. out.print(" ");
  208. }
  209. out.print(")");
  210. }
  211. }
  212. out.println();
  213. final PersonIdent author = c.getAuthorIdent();
  214. out.println(MessageFormat.format(CLIText.get().authorInfo, author.getName(), author.getEmailAddress()));
  215. out.println(MessageFormat.format(CLIText.get().dateInfo,
  216. dateFormatter.formatDate(author)));
  217. out.println();
  218. final String[] lines = c.getFullMessage().split("\n");
  219. for (final String s : lines) {
  220. out.print(" ");
  221. out.print(s);
  222. out.println();
  223. }
  224. out.println();
  225. if (showNotes(c))
  226. out.println();
  227. if (c.getParentCount() == 1 && (showNameAndStatusOnly || showPatch))
  228. showDiff(c);
  229. out.flush();
  230. }
  231. /**
  232. * @param c
  233. * @return <code>true</code> if at least one note was printed,
  234. * <code>false</code> otherwise
  235. * @throws IOException
  236. */
  237. private boolean showNotes(RevCommit c) throws IOException {
  238. if (noteMaps == null)
  239. return false;
  240. boolean printEmptyLine = false;
  241. boolean atLeastOnePrinted = false;
  242. for (Map.Entry<String, NoteMap> e : noteMaps.entrySet()) {
  243. String label = null;
  244. String notesRef = e.getKey();
  245. if (! notesRef.equals(Constants.R_NOTES_COMMITS)) {
  246. if (notesRef.startsWith(Constants.R_NOTES))
  247. label = notesRef.substring(Constants.R_NOTES.length());
  248. else
  249. label = notesRef;
  250. }
  251. boolean printedNote = showNotes(c, e.getValue(), label,
  252. printEmptyLine);
  253. atLeastOnePrinted |= printedNote;
  254. printEmptyLine = printedNote;
  255. }
  256. return atLeastOnePrinted;
  257. }
  258. /**
  259. * @param c
  260. * @param map
  261. * @param label
  262. * @param emptyLine
  263. * @return <code>true</code> if note was printed, <code>false</code>
  264. * otherwise
  265. * @throws IOException
  266. */
  267. private boolean showNotes(RevCommit c, NoteMap map, String label,
  268. boolean emptyLine)
  269. throws IOException {
  270. ObjectId blobId = map.get(c);
  271. if (blobId == null)
  272. return false;
  273. if (emptyLine)
  274. out.println();
  275. out.print("Notes");
  276. if (label != null) {
  277. out.print(" (");
  278. out.print(label);
  279. out.print(")");
  280. }
  281. out.println(":");
  282. try {
  283. RawText rawText = new RawText(argWalk.getObjectReader()
  284. .open(blobId).getCachedBytes(Integer.MAX_VALUE));
  285. for (int i = 0; i < rawText.size(); i++) {
  286. out.print(" ");
  287. out.println(rawText.getString(i));
  288. }
  289. } catch (LargeObjectException e) {
  290. out.println(MessageFormat.format(
  291. CLIText.get().noteObjectTooLargeToPrint, blobId.name()));
  292. }
  293. return true;
  294. }
  295. private void showDiff(RevCommit c) throws IOException {
  296. final RevTree a = c.getParent(0).getTree();
  297. final RevTree b = c.getTree();
  298. if (showNameAndStatusOnly)
  299. Diff.nameStatus(out, diffFmt.scan(a, b));
  300. else {
  301. out.flush();
  302. diffFmt.format(a, b);
  303. diffFmt.flush();
  304. }
  305. out.println();
  306. }
  307. }