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

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