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

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