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
|
/*
* Copyright (C) 2013, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.api;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
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.errors.MissingObjectException;
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.FIFORevQueue;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
/**
* Command to find human-readable names of revisions.
*
* @see <a
* href="http://www.kernel.org/pub/software/scm/git/docs/git-name-rev.html"
* >Git documentation about name-rev</a>
* @since 3.0
*/
public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
/** Amount of slop to allow walking past the earliest requested commit. */
private static final int COMMIT_TIME_SLOP = 60 * 60 * 24;
/** Cost of traversing a merge commit compared to a linear history. */
private static final int MERGE_COST = 65535;
private static class NameRevCommit extends RevCommit {
private String tip;
private int distance;
private long cost;
private NameRevCommit(AnyObjectId id) {
super(id);
}
private StringBuilder format() {
StringBuilder sb = new StringBuilder(tip);
if (distance > 0)
sb.append('~').append(distance);
return sb;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getSimpleName())
.append('[');
if (tip != null) {
sb.append(format());
} else {
sb.append((Object) null);
}
sb.append(',').append(cost).append(']').append(' ')
.append(super.toString());
return sb.toString();
}
}
private final RevWalk walk;
private final List<String> prefixes;
private final List<ObjectId> revs;
private List<Ref> refs;
private int mergeCost;
/**
* Create a new name-rev command.
*
* @param repo
* the {@link org.eclipse.jgit.lib.Repository}
*/
protected NameRevCommand(Repository repo) {
super(repo);
mergeCost = MERGE_COST;
prefixes = new ArrayList<>(2);
revs = new ArrayList<>(2);
walk = new RevWalk(repo) {
@Override
public NameRevCommit createCommit(AnyObjectId id) {
return new NameRevCommit(id);
}
};
}
@Override
public Map<ObjectId, String> call() throws GitAPIException {
try {
Map<ObjectId, String> nonCommits = new HashMap<>();
FIFORevQueue pending = new FIFORevQueue();
if (refs != null) {
for (Ref ref : refs)
addRef(ref, nonCommits, pending);
}
addPrefixes(nonCommits, pending);
int cutoff = minCommitTime() - COMMIT_TIME_SLOP;
while (true) {
NameRevCommit c = (NameRevCommit) pending.next();
if (c == null)
break;
if (c.getCommitTime() < cutoff)
continue;
for (int i = 0; i < c.getParentCount(); i++) {
NameRevCommit p = (NameRevCommit) walk.parseCommit(c.getParent(i));
long cost = c.cost + (i > 0 ? mergeCost : 1);
if (p.tip == null || compare(c.tip, cost, p.tip, p.cost) < 0) {
if (i > 0) {
p.tip = c.format().append('^').append(i + 1).toString();
p.distance = 0;
} else {
p.tip = c.tip;
p.distance = c.distance + 1;
}
p.cost = cost;
pending.add(p);
}
}
}
Map<ObjectId, String> result =
new LinkedHashMap<>(revs.size());
for (ObjectId id : revs) {
RevObject o = walk.parseAny(id);
if (o instanceof NameRevCommit) {
NameRevCommit c = (NameRevCommit) o;
if (c.tip != null)
result.put(id, simplify(c.format().toString()));
} else {
String name = nonCommits.get(id);
if (name != null)
result.put(id, simplify(name));
}
}
setCallable(false);
return result;
} catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e);
} finally {
walk.close();
}
}
/**
* Add an object to search for.
*
* @param id
* object ID to add.
* @return {@code this}
* @throws org.eclipse.jgit.errors.MissingObjectException
* the object supplied is not available from the object
* database.
* @throws org.eclipse.jgit.api.errors.JGitInternalException
* a low-level exception of JGit has occurred. The original
* exception can be retrieved by calling
* {@link java.lang.Exception#getCause()}.
*/
public NameRevCommand add(ObjectId id) throws MissingObjectException,
JGitInternalException {
checkCallable();
try {
walk.parseAny(id);
} catch (MissingObjectException e) {
throw e;
} catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e);
}
revs.add(id.copy());
return this;
}
/**
* Add multiple objects to search for.
*
* @param ids
* object IDs to add.
* @return {@code this}
* @throws org.eclipse.jgit.errors.MissingObjectException
* the object supplied is not available from the object
* database.
* @throws org.eclipse.jgit.api.errors.JGitInternalException
* a low-level exception of JGit has occurred. The original
* exception can be retrieved by calling
* {@link java.lang.Exception#getCause()}.
*/
public NameRevCommand add(Iterable<ObjectId> ids)
throws MissingObjectException, JGitInternalException {
for (ObjectId id : ids)
add(id);
return this;
}
/**
* Add a ref prefix to the set that results must match.
* <p>
* If an object matches multiple refs equally well, the first matching ref
* added with {@link #addRef(Ref)} is preferred, or else the first matching
* prefix added by {@link #addPrefix(String)}.
*
* @param prefix
* prefix to add; the prefix must end with a slash
* @return {@code this}
*/
public NameRevCommand addPrefix(String prefix) {
checkCallable();
prefixes.add(prefix);
return this;
}
/**
* Add all annotated tags under {@code refs/tags/} to the set that all
* results must match.
* <p>
* Calls {@link #addRef(Ref)}; see that method for a note on matching
* priority.
*
* @return {@code this}
* @throws JGitInternalException
* a low-level exception of JGit has occurred. The original
* exception can be retrieved by calling
* {@link java.lang.Exception#getCause()}.
*/
public NameRevCommand addAnnotatedTags() {
checkCallable();
if (refs == null)
refs = new ArrayList<>();
try {
for (Ref ref : repo.getRefDatabase()
.getRefsByPrefix(Constants.R_TAGS)) {
ObjectId id = ref.getObjectId();
if (id != null && (walk.parseAny(id) instanceof RevTag))
addRef(ref);
}
} catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e);
}
return this;
}
/**
* Add a ref to the set that all results must match.
* <p>
* If an object matches multiple refs equally well, the first matching ref
* added with {@link #addRef(Ref)} is preferred, or else the first matching
* prefix added by {@link #addPrefix(String)}.
*
* @param ref
* ref to add.
* @return {@code this}
*/
public NameRevCommand addRef(Ref ref) {
checkCallable();
if (refs == null)
refs = new ArrayList<>();
refs.add(ref);
return this;
}
NameRevCommand setMergeCost(int cost) {
mergeCost = cost;
return this;
}
private void addPrefixes(Map<ObjectId, String> nonCommits,
FIFORevQueue pending) throws IOException {
if (!prefixes.isEmpty()) {
for (String prefix : prefixes)
addPrefix(prefix, nonCommits, pending);
} else if (refs == null)
addPrefix(Constants.R_REFS, nonCommits, pending);
}
private void addPrefix(String prefix, Map<ObjectId, String> nonCommits,
FIFORevQueue pending) throws IOException {
for (Ref ref : repo.getRefDatabase().getRefsByPrefix(prefix))
addRef(ref, nonCommits, pending);
}
private void addRef(Ref ref, Map<ObjectId, String> nonCommits,
FIFORevQueue pending) throws IOException {
if (ref.getObjectId() == null)
return;
RevObject o = walk.parseAny(ref.getObjectId());
while (o instanceof RevTag) {
RevTag t = (RevTag) o;
nonCommits.put(o, ref.getName());
o = t.getObject();
walk.parseHeaders(o);
}
if (o instanceof NameRevCommit) {
NameRevCommit c = (NameRevCommit) o;
if (c.tip == null)
c.tip = ref.getName();
pending.add(c);
} else if (!nonCommits.containsKey(o))
nonCommits.put(o, ref.getName());
}
private int minCommitTime() throws IOException {
int min = Integer.MAX_VALUE;
for (ObjectId id : revs) {
RevObject o = walk.parseAny(id);
while (o instanceof RevTag) {
o = ((RevTag) o).getObject();
walk.parseHeaders(o);
}
if (o instanceof RevCommit) {
RevCommit c = (RevCommit) o;
if (c.getCommitTime() < min)
min = c.getCommitTime();
}
}
return min;
}
private long compare(String leftTip, long leftCost, String rightTip, long rightCost) {
long c = leftCost - rightCost;
if (c != 0 || prefixes.isEmpty())
return c;
int li = -1;
int ri = -1;
for (int i = 0; i < prefixes.size(); i++) {
String prefix = prefixes.get(i);
if (li < 0 && leftTip.startsWith(prefix))
li = i;
if (ri < 0 && rightTip.startsWith(prefix))
ri = i;
}
// Don't tiebreak if prefixes are the same, in order to prefer first-parent
// paths.
return (long) li - ri;
}
private static String simplify(String refName) {
if (refName.startsWith(Constants.R_HEADS))
return refName.substring(Constants.R_HEADS.length());
if (refName.startsWith(Constants.R_TAGS))
return refName.substring(Constants.R_TAGS.length());
if (refName.startsWith(Constants.R_REFS))
return refName.substring(Constants.R_REFS.length());
return refName;
}
}
|