Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Log.java 10KB

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