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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. /** {@inheritDoc} */
  156. @Override
  157. protected void init(Repository repository, String gitDir) {
  158. super.init(repository, gitDir);
  159. diffFmt = new DiffFormatter(new BufferedOutputStream(outs));
  160. }
  161. /** {@inheritDoc} */
  162. @Override
  163. protected void run() throws Exception {
  164. diffFmt.setRepository(db);
  165. try {
  166. diffFmt.setPathFilter(pathFilter);
  167. if (detectRenames != null)
  168. diffFmt.setDetectRenames(detectRenames.booleanValue());
  169. if (renameLimit != null && diffFmt.isDetectRenames()) {
  170. RenameDetector rd = diffFmt.getRenameDetector();
  171. rd.setRenameLimit(renameLimit.intValue());
  172. }
  173. if (!noStandardNotes || !additionalNoteRefs.isEmpty()) {
  174. createWalk();
  175. noteMaps = new LinkedHashMap<>();
  176. if (!noStandardNotes) {
  177. addNoteMap(Constants.R_NOTES_COMMITS);
  178. }
  179. if (!additionalNoteRefs.isEmpty()) {
  180. for (String notesRef : additionalNoteRefs) {
  181. if (!notesRef.startsWith(Constants.R_NOTES)) {
  182. notesRef = Constants.R_NOTES + notesRef;
  183. }
  184. addNoteMap(notesRef);
  185. }
  186. }
  187. }
  188. if (decorate)
  189. allRefsByPeeledObjectId = getRepository()
  190. .getAllRefsByPeeledObjectId();
  191. super.run();
  192. } finally {
  193. diffFmt.close();
  194. }
  195. }
  196. private void addNoteMap(String notesRef) throws IOException {
  197. Ref notes = db.exactRef(notesRef);
  198. if (notes == null)
  199. return;
  200. RevCommit notesCommit = argWalk.parseCommit(notes.getObjectId());
  201. noteMaps.put(notesRef,
  202. NoteMap.read(argWalk.getObjectReader(), notesCommit));
  203. }
  204. /** {@inheritDoc} */
  205. @Override
  206. protected void show(RevCommit c) throws Exception {
  207. outw.print(CLIText.get().commitLabel);
  208. outw.print(" "); //$NON-NLS-1$
  209. c.getId().copyTo(outbuffer, outw);
  210. if (decorate) {
  211. Collection<Ref> list = allRefsByPeeledObjectId.get(c);
  212. if (list != null) {
  213. outw.print(" ("); //$NON-NLS-1$
  214. for (Iterator<Ref> i = list.iterator(); i.hasNext(); ) {
  215. outw.print(i.next().getName());
  216. if (i.hasNext())
  217. outw.print(" "); //$NON-NLS-1$
  218. }
  219. outw.print(")"); //$NON-NLS-1$
  220. }
  221. }
  222. outw.println();
  223. final PersonIdent author = c.getAuthorIdent();
  224. outw.println(MessageFormat.format(CLIText.get().authorInfo, author.getName(), author.getEmailAddress()));
  225. outw.println(MessageFormat.format(CLIText.get().dateInfo,
  226. dateFormatter.formatDate(author)));
  227. outw.println();
  228. final String[] lines = c.getFullMessage().split("\n"); //$NON-NLS-1$
  229. for (String s : lines) {
  230. outw.print(" "); //$NON-NLS-1$
  231. outw.print(s);
  232. outw.println();
  233. }
  234. c.disposeBody();
  235. outw.println();
  236. if (showNotes(c))
  237. outw.println();
  238. if (c.getParentCount() <= 1 && (showNameAndStatusOnly || showPatch))
  239. showDiff(c);
  240. outw.flush();
  241. }
  242. /**
  243. * @param c
  244. * @return <code>true</code> if at least one note was printed,
  245. * <code>false</code> otherwise
  246. * @throws IOException
  247. */
  248. private boolean showNotes(RevCommit c) throws IOException {
  249. if (noteMaps == null)
  250. return false;
  251. boolean printEmptyLine = false;
  252. boolean atLeastOnePrinted = false;
  253. for (Map.Entry<String, NoteMap> e : noteMaps.entrySet()) {
  254. String label = null;
  255. String notesRef = e.getKey();
  256. if (! notesRef.equals(Constants.R_NOTES_COMMITS)) {
  257. if (notesRef.startsWith(Constants.R_NOTES))
  258. label = notesRef.substring(Constants.R_NOTES.length());
  259. else
  260. label = notesRef;
  261. }
  262. boolean printedNote = showNotes(c, e.getValue(), label,
  263. printEmptyLine);
  264. atLeastOnePrinted |= printedNote;
  265. printEmptyLine = printedNote;
  266. }
  267. return atLeastOnePrinted;
  268. }
  269. /**
  270. * @param c
  271. * @param map
  272. * @param label
  273. * @param emptyLine
  274. * @return <code>true</code> if note was printed, <code>false</code>
  275. * otherwise
  276. * @throws IOException
  277. */
  278. private boolean showNotes(RevCommit c, NoteMap map, String label,
  279. boolean emptyLine)
  280. throws IOException {
  281. ObjectId blobId = map.get(c);
  282. if (blobId == null)
  283. return false;
  284. if (emptyLine)
  285. outw.println();
  286. outw.print("Notes"); //$NON-NLS-1$
  287. if (label != null) {
  288. outw.print(" ("); //$NON-NLS-1$
  289. outw.print(label);
  290. outw.print(")"); //$NON-NLS-1$
  291. }
  292. outw.println(":"); //$NON-NLS-1$
  293. try {
  294. RawText rawText = new RawText(argWalk.getObjectReader()
  295. .open(blobId).getCachedBytes(Integer.MAX_VALUE));
  296. for (int i = 0; i < rawText.size(); i++) {
  297. outw.print(" "); //$NON-NLS-1$
  298. outw.println(rawText.getString(i));
  299. }
  300. } catch (LargeObjectException e) {
  301. outw.println(MessageFormat.format(
  302. CLIText.get().noteObjectTooLargeToPrint, blobId.name()));
  303. }
  304. return true;
  305. }
  306. private void showDiff(RevCommit c) throws IOException {
  307. final RevTree a = c.getParentCount() > 0 ? c.getParent(0).getTree()
  308. : null;
  309. final RevTree b = c.getTree();
  310. if (showNameAndStatusOnly)
  311. Diff.nameStatus(outw, diffFmt.scan(a, b));
  312. else {
  313. outw.flush();
  314. diffFmt.format(a, b);
  315. diffFmt.flush();
  316. }
  317. outw.println();
  318. }
  319. }