-
Notifications
You must be signed in to change notification settings - Fork 215
/
GitFunction.java
315 lines (293 loc) · 13 KB
/
GitFunction.java
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
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.connector.git;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.ListTagCommand;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
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.infinispan.schematic.document.Document;
import org.modeshape.jcr.federation.spi.DocumentWriter;
import org.modeshape.jcr.federation.spi.PageKey;
import org.modeshape.jcr.federation.spi.PageWriter;
/**
*
*/
public abstract class GitFunction {
protected static final String DELIMITER = "/";
protected static final String REMOTE_BRANCH_PREFIX = "refs/remotes/";
protected static final String TAG_PREFIX = "refs/tags/";
protected static final int DEFAULT_PAGE_SIZE = 15;
protected static final Comparator<Ref> REVERSE_REF_COMPARATOR = new Comparator<Ref>() {
@Override
public int compare( Ref o1,
Ref o2 ) {
return 0 - o1.getName().compareTo(o2.getName());
}
};
protected final String name;
protected final GitConnector connector;
protected int pageSize = DEFAULT_PAGE_SIZE;
protected GitFunction( String name,
GitConnector connector ) {
this.name = name;
this.connector = connector;
}
/**
* Get the name of this function.
*
* @return the name; never null
*/
public String getName() {
return name;
}
public boolean isPaged() {
return false;
}
public abstract Document execute( Repository repository,
Git git,
CallSpecification spec,
DocumentWriter writer,
Values values ) throws GitAPIException, IOException;
private Set<String> remoteBranchPrefixes() {
Set<String> prefixes = new HashSet<String>();
for (String remoteName : connector.remoteNames()) {
String prefix = remoteBranchPrefix(remoteName);
prefixes.add(prefix);
}
return prefixes;
}
private String remoteBranchPrefix( String remoteName ) {
return REMOTE_BRANCH_PREFIX + remoteName + "/";
}
/**
* Obtain the name of the branch reference
*
* @param branchName
* @return the branch ref name
*/
protected String branchRefForName( String branchName ) {
return remoteBranchPrefix(connector.remoteName()) + branchName;
}
/**
* Obtain the name of the branch reference
*
* @param branchName
* @param remoteName the name of the remote
* @return the branch ref name
*/
protected String branchRefForName( String branchName,
String remoteName ) {
return remoteBranchPrefix(remoteName) + branchName;
}
/**
* Resolve the branch name, tag name, or commit ID into the appropriate ObjectId. Note that the branch names are assumed to be
* from the {@link GitConnector#remoteName() remote}.
*
* @param repository the Repository object; may not be null
* @param branchOrTagOrCommitId the branch name, tag name, or commit ID; may not be null
* @return the resolved ObjectId, or null if the supplied string does not resolve to an object ID
* @throws IOException if there is a problem reading the Git repository
*/
protected ObjectId resolveBranchOrTagOrCommitId( Repository repository,
String branchOrTagOrCommitId ) throws IOException {
ObjectId objId = repository.resolve(branchOrTagOrCommitId);
if (objId == null) {
for (String remoteName : connector.remoteNames()) {
String branchRef = branchRefForName(branchOrTagOrCommitId, remoteName);
objId = repository.resolve(branchRef);
if (objId != null) break;
}
}
return objId;
}
/**
* Add the names of the branches as children of the current node.
*
* @param git the Git object; may not be null
* @param spec the call specification; may not be null
* @param writer the document writer for the current node; may not be null
* @throws GitAPIException if there is a problem accessing the Git repository
*/
protected void addBranchesAsChildren( Git git,
CallSpecification spec,
DocumentWriter writer ) throws GitAPIException {
Set<String> remoteBranchPrefixes = remoteBranchPrefixes();
if (remoteBranchPrefixes.isEmpty()) {
// Generate the child references to the LOCAL branches, which will be sorted by name ...
ListBranchCommand command = git.branchList();
List<Ref> branches = command.call();
// Reverse the sort of the branch names, since they might be version numbers ...
Collections.sort(branches, REVERSE_REF_COMPARATOR);
for (Ref ref : branches) {
String name = ref.getName();
writer.addChild(spec.childId(name), name);
}
return;
}
// There is at least one REMOTE branch, so generate the child references to the REMOTE branches,
// which will be sorted by name (by the command)...
ListBranchCommand command = git.branchList();
command.setListMode(ListMode.REMOTE);
List<Ref> branches = command.call();
// Reverse the sort of the branch names, since they might be version numbers ...
Collections.sort(branches, REVERSE_REF_COMPARATOR);
Set<String> uniqueNames = new HashSet<String>();
for (Ref ref : branches) {
String name = ref.getName();
if (uniqueNames.contains(name)) continue;
// We only want the branch if it matches one of the listed remotes ...
boolean skip = false;
for (String remoteBranchPrefix : remoteBranchPrefixes) {
if (name.startsWith(remoteBranchPrefix)) {
// Remove the prefix ...
name = name.replaceFirst(remoteBranchPrefix, "");
break;
}
// Otherwise, it's a remote branch from a different remote that we don't want ...
skip = true;
}
if (skip) continue;
if (uniqueNames.add(name)) writer.addChild(spec.childId(name), name);
}
}
/**
* Add the names of the tags as children of the current node.
*
* @param git the Git object; may not be null
* @param spec the call specification; may not be null
* @param writer the document writer for the current node; may not be null
* @throws GitAPIException if there is a problem accessing the Git repository
*/
protected void addTagsAsChildren( Git git,
CallSpecification spec,
DocumentWriter writer ) throws GitAPIException {
// Generate the child references to the branches, which will be sorted by name (by the command).
ListTagCommand command = git.tagList();
List<Ref> tags = command.call();
// Reverse the sort of the branch names, since they might be version numbers ...
Collections.sort(tags, REVERSE_REF_COMPARATOR);
for (Ref ref : tags) {
String fullName = ref.getName();
String name = fullName.replaceFirst(TAG_PREFIX, "");
writer.addChild(spec.childId(name), name);
}
}
/**
* Add the first page of commits in the history names of the tags as children of the current node.
*
* @param git the Git object; may not be null
* @param spec the call specification; may not be null
* @param writer the document writer for the current node; may not be null
* @param pageSize the number of commits to include, and the number of commits that will be in the next page (if there are
* more commits)
* @throws GitAPIException if there is a problem accessing the Git repository
*/
protected void addCommitsAsChildren( Git git,
CallSpecification spec,
DocumentWriter writer,
int pageSize ) throws GitAPIException {
// Add commits in the log ...
LogCommand command = git.log();
command.setSkip(0);
command.setMaxCount(pageSize);
// Add the first set of commits ...
int actual = 0;
String commitId = null;
for (RevCommit commit : command.call()) {
commitId = commit.getName();
writer.addChild(spec.childId(commitId), commitId);
++actual;
}
if (actual == pageSize) {
// We wrote the maximum number of commits, so there's (probably) another page ...
writer.addPage(spec.getId(), commitId, pageSize, PageWriter.UNKNOWN_TOTAL_SIZE);
}
}
/**
* Add an additional page of commits in the history names of the tags as children of the current node.
*
* @param git the Git object; may not be null
* @param repository the Repository object; may not be null
* @param spec the call specification; may not be null
* @param writer the page writer for the current node; may not be null
* @param pageKey the page key for this page; may not be null
* @throws GitAPIException if there is a problem accessing the Git repository
* @throws IOException if there is a problem reading the Git repository
*/
protected void addCommitsAsPageOfChildren( Git git,
Repository repository,
CallSpecification spec,
PageWriter writer,
PageKey pageKey ) throws GitAPIException, IOException {
RevWalk walker = new RevWalk(repository);
try {
// The offset is the ID of the last commit we read, so we'll need to skip the first commit
String lastCommitIdName = pageKey.getOffsetString();
ObjectId lastCommitId = repository.resolve(lastCommitIdName);
int pageSize = (int)pageKey.getBlockSize();
// int offset = pageKey.getOffsetInt();
// int maxCount = pageSize + offset;
LogCommand command = git.log();
command.add(lastCommitId);
command.setMaxCount(pageSize + 1);
// Add the first set of commits ...
int actual = 0;
String commitId = null;
for (RevCommit commit : command.call()) {
commitId = commit.getName();
if (commitId.equals(lastCommitIdName)) continue;
writer.addChild(spec.childId(commitId), commitId);
++actual;
}
if (actual == pageSize) {
assert commitId != null;
// We wrote the maximum number of commits, so there's (probably) another page ...
writer.addPage(pageKey.getParentId(), commitId, pageSize, PageWriter.UNKNOWN_TOTAL_SIZE);
}
} finally {
walker.dispose();
}
}
protected boolean isQueryable( CallSpecification callSpec ) {
// by default, a git function does not return queryable content
return false;
}
@Override
public String toString() {
return getName();
}
}