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

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