1 /*
2 * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43 package org.eclipse.jgit.api;
44
45 import static org.eclipse.jgit.lib.RefDatabase.ALL;
46
47 import java.io.IOException;
48 import java.text.MessageFormat;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.Map;
52
53 import org.eclipse.jgit.api.errors.GitAPIException;
54 import org.eclipse.jgit.api.errors.JGitInternalException;
55 import org.eclipse.jgit.api.errors.NoHeadException;
56 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
57 import org.eclipse.jgit.errors.MissingObjectException;
58 import org.eclipse.jgit.internal.JGitText;
59 import org.eclipse.jgit.lib.AnyObjectId;
60 import org.eclipse.jgit.lib.Constants;
61 import org.eclipse.jgit.lib.ObjectId;
62 import org.eclipse.jgit.lib.Ref;
63 import org.eclipse.jgit.lib.Repository;
64 import org.eclipse.jgit.revwalk.RevCommit;
65 import org.eclipse.jgit.revwalk.RevWalk;
66 import org.eclipse.jgit.revwalk.filter.AndRevFilter;
67 import org.eclipse.jgit.revwalk.filter.MaxCountRevFilter;
68 import org.eclipse.jgit.revwalk.filter.RevFilter;
69 import org.eclipse.jgit.revwalk.filter.SkipRevFilter;
70 import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
71 import org.eclipse.jgit.treewalk.filter.PathFilter;
72 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
73 import org.eclipse.jgit.treewalk.filter.TreeFilter;
74
75 /**
76 * A class used to execute a {@code Log} command. It has setters for all
77 * supported options and arguments of this command and a {@link #call()} method
78 * to finally execute the command. Each instance of this class should only be
79 * used for one invocation of the command (means: one call to {@link #call()})
80 * <p>
81 * Examples (<code>git</code> is a {@link org.eclipse.jgit.api.Git} instance):
82 * <p>
83 * Get newest 10 commits, starting from the current branch:
84 *
85 * <pre>
86 * ObjectId head = repository.resolve(Constants.HEAD);
87 *
88 * Iterable<RevCommit> commits = git.log().add(head).setMaxCount(10).call();
89 * </pre>
90 * <p>
91 *
92 * <p>
93 * Get commits only for a specific file:
94 *
95 * <pre>
96 * git.log().add(head).addPath("dir/filename.txt").call();
97 * </pre>
98 * <p>
99 *
100 * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html"
101 * >Git documentation about Log</a>
102 */
103 public class LogCommand extends GitCommand<Iterable<RevCommit>> {
104 private RevWalk walk;
105
106 private boolean startSpecified = false;
107
108 private RevFilter revFilter;
109
110 private final List<PathFilter> pathFilters = new ArrayList<>();
111
112 private int maxCount = -1;
113
114 private int skip = -1;
115
116 /**
117 * Constructor for LogCommand.
118 *
119 * @param repo
120 * the {@link org.eclipse.jgit.lib.Repository}
121 */
122 protected LogCommand(Repository repo) {
123 super(repo);
124 walk = new RevWalk(repo);
125 }
126
127 /**
128 * {@inheritDoc}
129 * <p>
130 * Executes the {@code Log} command with all the options and parameters
131 * collected by the setter methods (e.g. {@link #add(AnyObjectId)},
132 * {@link #not(AnyObjectId)}, ..) of this class. Each instance of this class
133 * should only be used for one invocation of the command. Don't call this
134 * method twice on an instance.
135 */
136 @Override
137 public Iterable<RevCommit> call() throws GitAPIException, NoHeadException {
138 checkCallable();
139 if (pathFilters.size() > 0)
140 walk.setTreeFilter(AndTreeFilter.create(
141 PathFilterGroup.create(pathFilters), TreeFilter.ANY_DIFF));
142 if (skip > -1 && maxCount > -1)
143 walk.setRevFilter(AndRevFilter.create(SkipRevFilter.create(skip),
144 MaxCountRevFilter.create(maxCount)));
145 else if (skip > -1)
146 walk.setRevFilter(SkipRevFilter.create(skip));
147 else if (maxCount > -1)
148 walk.setRevFilter(MaxCountRevFilter.create(maxCount));
149 if (!startSpecified) {
150 try {
151 ObjectId headId = repo.resolve(Constants.HEAD);
152 if (headId == null)
153 throw new NoHeadException(
154 JGitText.get().noHEADExistsAndNoExplicitStartingRevisionWasSpecified);
155 add(headId);
156 } catch (IOException e) {
157 // all exceptions thrown by add() shouldn't occur and represent
158 // severe low-level exception which are therefore wrapped
159 throw new JGitInternalException(
160 JGitText.get().anExceptionOccurredWhileTryingToAddTheIdOfHEAD,
161 e);
162 }
163 }
164
165 if (this.revFilter != null) {
166 walk.setRevFilter(this.revFilter);
167 }
168
169 setCallable(false);
170 return walk;
171 }
172
173 /**
174 * Mark a commit to start graph traversal from.
175 *
176 * @see RevWalk#markStart(RevCommit)
177 * @param start
178 * the id of the commit to start from
179 * @return {@code this}
180 * @throws org.eclipse.jgit.errors.MissingObjectException
181 * the commit supplied is not available from the object
182 * database. This usually indicates the supplied commit is
183 * invalid, but the reference was constructed during an earlier
184 * invocation to
185 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupCommit(AnyObjectId)}.
186 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
187 * the object was not parsed yet and it was discovered during
188 * parsing that it is not actually a commit. This usually
189 * indicates the caller supplied a non-commit SHA-1 to
190 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupCommit(AnyObjectId)}.
191 * @throws JGitInternalException
192 * a low-level exception of JGit has occurred. The original
193 * exception can be retrieved by calling
194 * {@link java.lang.Exception#getCause()}. Expect only
195 * {@code IOException's} to be wrapped. Subclasses of
196 * {@link java.io.IOException} (e.g.
197 * {@link org.eclipse.jgit.errors.MissingObjectException}) are
198 * typically not wrapped here but thrown as original exception
199 */
200 public LogCommand add(AnyObjectId start) throws MissingObjectException,
201 IncorrectObjectTypeException {
202 return add(true, start);
203 }
204
205 /**
206 * Same as {@code --not start}, or {@code ^start}
207 *
208 * @param start
209 * a {@link org.eclipse.jgit.lib.AnyObjectId}
210 * @return {@code this}
211 * @throws org.eclipse.jgit.errors.MissingObjectException
212 * the commit supplied is not available from the object
213 * database. This usually indicates the supplied commit is
214 * invalid, but the reference was constructed during an earlier
215 * invocation to
216 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupCommit(AnyObjectId)}.
217 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
218 * the object was not parsed yet and it was discovered during
219 * parsing that it is not actually a commit. This usually
220 * indicates the caller supplied a non-commit SHA-1 to
221 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupCommit(AnyObjectId)}.
222 * @throws JGitInternalException
223 * a low-level exception of JGit has occurred. The original
224 * exception can be retrieved by calling
225 * {@link java.lang.Exception#getCause()}. Expect only
226 * {@code IOException's} to be wrapped. Subclasses of
227 * {@link java.io.IOException} (e.g.
228 * {@link org.eclipse.jgit.errors.MissingObjectException}) are
229 * typically not wrapped here but thrown as original exception
230 */
231 public LogCommand not(AnyObjectId start) throws MissingObjectException,
232 IncorrectObjectTypeException {
233 return add(false, start);
234 }
235
236 /**
237 * Adds the range {@code since..until}
238 *
239 * @param since
240 * a {@link org.eclipse.jgit.lib.AnyObjectId} object.
241 * @param until
242 * a {@link org.eclipse.jgit.lib.AnyObjectId} object.
243 * @return {@code this}
244 * @throws org.eclipse.jgit.errors.MissingObjectException
245 * the commit supplied is not available from the object
246 * database. This usually indicates the supplied commit is
247 * invalid, but the reference was constructed during an earlier
248 * invocation to
249 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupCommit(AnyObjectId)}.
250 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
251 * the object was not parsed yet and it was discovered during
252 * parsing that it is not actually a commit. This usually
253 * indicates the caller supplied a non-commit SHA-1 to
254 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupCommit(AnyObjectId)}.
255 * @throws JGitInternalException
256 * a low-level exception of JGit has occurred. The original
257 * exception can be retrieved by calling
258 * {@link java.lang.Exception#getCause()}. Expect only
259 * {@code IOException's} to be wrapped. Subclasses of
260 * {@link java.io.IOException} (e.g.
261 * {@link org.eclipse.jgit.errors.MissingObjectException}) are
262 * typically not wrapped here but thrown as original exception
263 */
264 public LogCommand addRange(AnyObjectId since, AnyObjectId until)
265 throws MissingObjectException, IncorrectObjectTypeException {
266 return not(since).add(until);
267 }
268
269 /**
270 * Add all refs as commits to start the graph traversal from.
271 *
272 * @see #add(AnyObjectId)
273 * @return {@code this}
274 * @throws java.io.IOException
275 * the references could not be accessed
276 */
277 public LogCommand all() throws IOException {
278 Map<String, Ref> refs = getRepository().getRefDatabase().getRefs(ALL);
279 for (Ref ref : refs.values()) {
280 if(!ref.isPeeled())
281 ref = getRepository().peel(ref);
282
283 ObjectId objectId = ref.getPeeledObjectId();
284 if (objectId == null)
285 objectId = ref.getObjectId();
286 RevCommit commit = null;
287 try {
288 commit = walk.parseCommit(objectId);
289 } catch (MissingObjectException e) {
290 // ignore: the ref points to an object that does not exist;
291 // it should be ignored as traversal starting point.
292 } catch (IncorrectObjectTypeException e) {
293 // ignore: the ref points to an object that is not a commit
294 // (e.g. a tree or a blob);
295 // it should be ignored as traversal starting point.
296 }
297 if (commit != null)
298 add(commit);
299 }
300 return this;
301 }
302
303 /**
304 * Show only commits that affect any of the specified paths. The path must
305 * either name a file or a directory exactly and use <code>/</code> (slash)
306 * as separator. Note that regex expressions or wildcards are not supported.
307 *
308 * @param path
309 * a repository-relative path (with <code>/</code> as separator)
310 * @return {@code this}
311 */
312 public LogCommand addPath(String path) {
313 checkCallable();
314 pathFilters.add(PathFilter.create(path));
315 return this;
316 }
317
318 /**
319 * Skip the number of commits before starting to show the commit output.
320 *
321 * @param skip
322 * the number of commits to skip
323 * @return {@code this}
324 */
325 public LogCommand setSkip(int skip) {
326 checkCallable();
327 this.skip = skip;
328 return this;
329 }
330
331 /**
332 * Limit the number of commits to output.
333 *
334 * @param maxCount
335 * the limit
336 * @return {@code this}
337 */
338 public LogCommand setMaxCount(int maxCount) {
339 checkCallable();
340 this.maxCount = maxCount;
341 return this;
342 }
343
344 private LogCommand add(boolean include, AnyObjectId start)
345 throws MissingObjectException, IncorrectObjectTypeException,
346 JGitInternalException {
347 checkCallable();
348 try {
349 if (include) {
350 walk.markStart(walk.lookupCommit(start));
351 startSpecified = true;
352 } else
353 walk.markUninteresting(walk.lookupCommit(start));
354 return this;
355 } catch (MissingObjectException e) {
356 throw e;
357 } catch (IncorrectObjectTypeException e) {
358 throw e;
359 } catch (IOException e) {
360 throw new JGitInternalException(MessageFormat.format(
361 JGitText.get().exceptionOccurredDuringAddingOfOptionToALogCommand
362 , start), e);
363 }
364 }
365
366
367 /**
368 * Set a filter for the <code>LogCommand</code>.
369 *
370 * @param aFilter
371 * the filter that this instance of <code>LogCommand</code>
372 * should use
373 * @return {@code this}
374 * @since 4.4
375 */
376 public LogCommand setRevFilter(RevFilter aFilter) {
377 checkCallable();
378 this.revFilter = aFilter;
379 return this;
380 }
381 }