1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.pgm;
14
15 import java.io.BufferedOutputStream;
16 import java.io.IOException;
17 import java.text.MessageFormat;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.eclipse.jgit.diff.DiffFormatter;
28 import org.eclipse.jgit.diff.RawText;
29 import org.eclipse.jgit.diff.RawTextComparator;
30 import org.eclipse.jgit.diff.RenameDetector;
31 import org.eclipse.jgit.errors.LargeObjectException;
32 import org.eclipse.jgit.lib.AnyObjectId;
33 import org.eclipse.jgit.lib.Constants;
34 import org.eclipse.jgit.lib.GpgConfig;
35 import org.eclipse.jgit.lib.GpgSignatureVerifier;
36 import org.eclipse.jgit.lib.GpgSignatureVerifier.SignatureVerification;
37 import org.eclipse.jgit.lib.GpgSignatureVerifierFactory;
38 import org.eclipse.jgit.lib.ObjectId;
39 import org.eclipse.jgit.lib.PersonIdent;
40 import org.eclipse.jgit.lib.Ref;
41 import org.eclipse.jgit.lib.Repository;
42 import org.eclipse.jgit.notes.NoteMap;
43 import org.eclipse.jgit.pgm.internal.CLIText;
44 import org.eclipse.jgit.pgm.internal.VerificationUtils;
45 import org.eclipse.jgit.revwalk.RevCommit;
46 import org.eclipse.jgit.revwalk.RevTree;
47 import org.eclipse.jgit.util.GitDateFormatter;
48 import org.eclipse.jgit.util.GitDateFormatter.Format;
49 import org.kohsuke.args4j.Option;
50
51 @Command(common = true, usage = "usage_viewCommitHistory")
52 class Log extends RevWalkTextBuiltin {
53
54 private GitDateFormatter dateFormatter = new GitDateFormatter(
55 Format.DEFAULT);
56
57 private DiffFormatter diffFmt;
58
59 private Map<AnyObjectId, Set<Ref>> allRefsByPeeledObjectId;
60
61 private Map<String, NoteMap> noteMaps;
62
63 @Option(name="--decorate", usage="usage_showRefNamesMatchingCommits")
64 private boolean decorate;
65
66 @Option(name = "--no-standard-notes", usage = "usage_noShowStandardNotes")
67 private boolean noStandardNotes;
68
69 private List<String> additionalNoteRefs = new ArrayList<>();
70
71 @Option(name = "--show-notes", usage = "usage_showNotes", metaVar = "metaVar_ref")
72 void addAdditionalNoteRef(String notesRef) {
73 additionalNoteRefs.add(notesRef);
74 }
75
76 @Option(name = "--show-signature", usage = "usage_showSignature")
77 private boolean showSignature;
78
79 @Option(name = "--date", usage = "usage_date")
80 void dateFormat(String date) {
81 if (date.toLowerCase(Locale.ROOT).equals(date))
82 date = date.toUpperCase(Locale.ROOT);
83 dateFormatter = new GitDateFormatter(Format.valueOf(date));
84 }
85
86
87 @Option(name = "-p", usage = "usage_showPatch")
88 boolean showPatch;
89
90 @Option(name = "-M", usage = "usage_detectRenames")
91 private Boolean detectRenames;
92
93 @Option(name = "--no-renames", usage = "usage_noRenames")
94 void noRenames(@SuppressWarnings("unused") boolean on) {
95 detectRenames = Boolean.FALSE;
96 }
97
98 @Option(name = "-l", usage = "usage_renameLimit")
99 private Integer renameLimit;
100
101 @Option(name = "--name-status", usage = "usage_nameStatus")
102 private boolean showNameAndStatusOnly;
103
104 @Option(name = "--ignore-space-at-eol")
105 void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
106 diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
107 }
108
109 @Option(name = "--ignore-leading-space")
110 void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
111 diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
112 }
113
114 @Option(name = "-b", aliases = { "--ignore-space-change" })
115 void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
116 diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
117 }
118
119 @Option(name = "-w", aliases = { "--ignore-all-space" })
120 void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
121 diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
122 }
123
124 @Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
125 void unified(int lines) {
126 diffFmt.setContext(lines);
127 }
128
129 @Option(name = "--abbrev", metaVar = "metaVar_n")
130 void abbrev(int lines) {
131 diffFmt.setAbbreviationLength(lines);
132 }
133
134 @Option(name = "--full-index")
135 void abbrev(@SuppressWarnings("unused") boolean on) {
136 diffFmt.setAbbreviationLength(Constants.OBJECT_ID_STRING_LENGTH);
137 }
138
139 @Option(name = "--src-prefix", usage = "usage_srcPrefix")
140 void sourcePrefix(String path) {
141 diffFmt.setOldPrefix(path);
142 }
143
144 @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
145 void dstPrefix(String path) {
146 diffFmt.setNewPrefix(path);
147 }
148
149 @Option(name = "--no-prefix", usage = "usage_noPrefix")
150 void noPrefix(@SuppressWarnings("unused") boolean on) {
151 diffFmt.setOldPrefix("");
152 diffFmt.setNewPrefix("");
153 }
154
155
156
157
158 private GpgSignatureVerifier verifier;
159
160 private GpgConfig config;
161
162 Log() {
163 dateFormatter = new GitDateFormatter(Format.DEFAULT);
164 }
165
166
167 @Override
168 protected void init(Repository repository, String gitDir) {
169 super.init(repository, gitDir);
170 diffFmt = new DiffFormatter(new BufferedOutputStream(outs));
171 }
172
173
174 @Override
175 protected void run() {
176 config = new GpgConfig(db.getConfig());
177 diffFmt.setRepository(db);
178 try {
179 diffFmt.setPathFilter(pathFilter);
180 if (detectRenames != null) {
181 diffFmt.setDetectRenames(detectRenames.booleanValue());
182 }
183 if (renameLimit != null && diffFmt.isDetectRenames()) {
184 RenameDetector rd = diffFmt.getRenameDetector();
185 rd.setRenameLimit(renameLimit.intValue());
186 }
187
188 if (!noStandardNotes || !additionalNoteRefs.isEmpty()) {
189 createWalk();
190 noteMaps = new LinkedHashMap<>();
191 if (!noStandardNotes) {
192 addNoteMap(Constants.R_NOTES_COMMITS);
193 }
194 if (!additionalNoteRefs.isEmpty()) {
195 for (String notesRef : additionalNoteRefs) {
196 if (!notesRef.startsWith(Constants.R_NOTES)) {
197 notesRef = Constants.R_NOTES + notesRef;
198 }
199 addNoteMap(notesRef);
200 }
201 }
202 }
203
204 if (decorate) {
205 allRefsByPeeledObjectId = getRepository()
206 .getAllRefsByPeeledObjectId();
207 }
208 super.run();
209 } catch (Exception e) {
210 throw die(e.getMessage(), e);
211 } finally {
212 diffFmt.close();
213 if (verifier != null) {
214 verifier.clear();
215 }
216 }
217 }
218
219 private void addNoteMap(String notesRef) throws IOException {
220 Ref notes = db.exactRef(notesRef);
221 if (notes == null)
222 return;
223 RevCommit notesCommit = argWalk.parseCommit(notes.getObjectId());
224 noteMaps.put(notesRef,
225 NoteMap.read(argWalk.getObjectReader(), notesCommit));
226 }
227
228
229 @Override
230 protected void show(RevCommit c) throws Exception {
231 outw.print(CLIText.get().commitLabel);
232 outw.print(" ");
233 c.getId().copyTo(outbuffer, outw);
234 if (decorate) {
235 Collection<Ref> list = allRefsByPeeledObjectId.get(c);
236 if (list != null) {
237 outw.print(" (");
238 for (Iterator<Ref> i = list.iterator(); i.hasNext(); ) {
239 outw.print(i.next().getName());
240 if (i.hasNext())
241 outw.print(" ");
242 }
243 outw.print(")");
244 }
245 }
246 outw.println();
247
248 if (showSignature) {
249 showSignature(c);
250 }
251 final PersonIdent author = c.getAuthorIdent();
252 outw.println(MessageFormat.format(CLIText.get().authorInfo, author.getName(), author.getEmailAddress()));
253 outw.println(MessageFormat.format(CLIText.get().dateInfo,
254 dateFormatter.formatDate(author)));
255
256 outw.println();
257 final String[] lines = c.getFullMessage().split("\n");
258 for (String s : lines) {
259 outw.print(" ");
260 outw.print(s);
261 outw.println();
262 }
263 c.disposeBody();
264
265 outw.println();
266 if (showNotes(c))
267 outw.println();
268
269 if (c.getParentCount() <= 1 && (showNameAndStatusOnly || showPatch))
270 showDiff(c);
271 outw.flush();
272 }
273
274 private void showSignature(RevCommit c) throws IOException {
275 if (c.getRawGpgSignature() == null) {
276 return;
277 }
278 if (verifier == null) {
279 GpgSignatureVerifierFactory factory = GpgSignatureVerifierFactory
280 .getDefault();
281 if (factory == null) {
282 throw die(CLIText.get().logNoSignatureVerifier, null);
283 }
284 verifier = factory.getVerifier();
285 }
286 SignatureVerification verification = verifier.verifySignature(c,
287 config);
288 if (verification == null) {
289 return;
290 }
291 VerificationUtils.writeVerification(outw, verification,
292 verifier.getName(), c.getCommitterIdent());
293 }
294
295
296
297
298
299
300
301 private boolean showNotes(RevCommit c) throws IOException {
302 if (noteMaps == null)
303 return false;
304
305 boolean printEmptyLine = false;
306 boolean atLeastOnePrinted = false;
307 for (Map.Entry<String, NoteMap> e : noteMaps.entrySet()) {
308 String label = null;
309 String notesRef = e.getKey();
310 if (! notesRef.equals(Constants.R_NOTES_COMMITS)) {
311 if (notesRef.startsWith(Constants.R_NOTES))
312 label = notesRef.substring(Constants.R_NOTES.length());
313 else
314 label = notesRef;
315 }
316 boolean printedNote = showNotes(c, e.getValue(), label,
317 printEmptyLine);
318 atLeastOnePrinted |= printedNote;
319 printEmptyLine = printedNote;
320 }
321 return atLeastOnePrinted;
322 }
323
324
325
326
327
328
329
330
331
332
333 private boolean showNotes(RevCommit c, NoteMap map, String label,
334 boolean emptyLine)
335 throws IOException {
336 ObjectId blobId = map.get(c);
337 if (blobId == null)
338 return false;
339 if (emptyLine)
340 outw.println();
341 outw.print("Notes");
342 if (label != null) {
343 outw.print(" (");
344 outw.print(label);
345 outw.print(")");
346 }
347 outw.println(":");
348 try {
349 RawText rawText = new RawText(argWalk.getObjectReader()
350 .open(blobId).getCachedBytes(Integer.MAX_VALUE));
351 for (int i = 0; i < rawText.size(); i++) {
352 outw.print(" ");
353 outw.println(rawText.getString(i));
354 }
355 } catch (LargeObjectException e) {
356 outw.println(MessageFormat.format(
357 CLIText.get().noteObjectTooLargeToPrint, blobId.name()));
358 }
359 return true;
360 }
361
362 private void showDiff(RevCommit c) throws IOException {
363 final RevTree a = c.getParentCount() > 0 ? c.getParent(0).getTree()
364 : null;
365 final RevTree b = c.getTree();
366
367 if (showNameAndStatusOnly)
368 Diff.nameStatus(outw, diffFmt.scan(a, b));
369 else {
370 outw.flush();
371 diffFmt.format(a, b);
372 diffFmt.flush();
373 }
374 outw.println();
375 }
376 }