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