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

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