-
Notifications
You must be signed in to change notification settings - Fork 1k
/
AbstractGitSCMSource.java
414 lines (371 loc) · 16.2 KB
/
AbstractGitSCMSource.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* The MIT License
*
* Copyright (c) 2013-2014, CloudBees, Inc., Stephen Connolly, Amadeus IT Group.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.plugins.git;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.Revision;
import hudson.plugins.git.SubmoduleConfig;
import hudson.plugins.git.UserRemoteConfig;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.BuildChooserSetting;
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildChooser;
import hudson.plugins.git.util.BuildChooserContext;
import hudson.plugins.git.util.BuildChooserDescriptor;
import hudson.plugins.git.util.BuildData;
import hudson.plugins.git.util.DefaultBuildChooser;
import hudson.scm.SCM;
import hudson.security.ACL;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceCriteria;
import jenkins.scm.api.SCMSourceOwner;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
/**
* @author Stephen Connolly
*/
public abstract class AbstractGitSCMSource extends SCMSource {
/**
* Keep one lock per cache directory. Lazy populated, but never purge, except on restart.
*/
private static final ConcurrentMap<String, Lock> cacheLocks = new ConcurrentHashMap<String, Lock>();
public AbstractGitSCMSource(String id) {
super(id);
}
public abstract String getCredentialsId();
public abstract String getRemote();
public abstract String getIncludes();
public abstract String getExcludes();
public String getRemoteName() {
return "origin";
}
@CheckForNull
@Override
protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener listener)
throws IOException, InterruptedException {
String cacheEntry = getCacheEntry();
Lock cacheLock = getCacheLock(cacheEntry);
cacheLock.lock();
try {
File cacheDir = getCacheDir(cacheEntry);
Git git = Git.with(listener, new EnvVars(EnvVars.masterEnvVars)).in(cacheDir);
GitClient client = git.getClient();
client.addDefaultCredentials(getCredentials());
if (!client.hasGitRepo()) {
listener.getLogger().println("Creating git repository in " + cacheDir);
client.init();
}
String remoteName = getRemoteName();
listener.getLogger().println("Setting " + remoteName + " to " + getRemote());
client.setRemoteUrl(remoteName, getRemote());
listener.getLogger().println("Fetching " + remoteName + "...");
List<RefSpec> refSpecs = getRefSpecs();
client.fetch(remoteName, refSpecs.toArray(new RefSpec[refSpecs.size()]));
// we don't prune remotes here, as we just want one head's revision
for (Branch b : client.getRemoteBranches()) {
String branchName = StringUtils.removeStart(b.getName(), remoteName + "/");
if (branchName.equals(head.getName())) {
return new SCMRevisionImpl(head, b.getSHA1String());
}
}
return null;
} finally {
cacheLock.unlock();
}
}
@NonNull
@Override
protected void retrieve(@NonNull final SCMHeadObserver observer,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
String cacheEntry = getCacheEntry();
Lock cacheLock = getCacheLock(cacheEntry);
cacheLock.lock();
try {
File cacheDir = getCacheDir(cacheEntry);
Git git = Git.with(listener, new EnvVars(EnvVars.masterEnvVars)).in(cacheDir);
GitClient client = git.getClient();
client.addDefaultCredentials(getCredentials());
if (!client.hasGitRepo()) {
listener.getLogger().println("Creating git repository in " + cacheDir);
client.init();
}
String remoteName = getRemoteName();
listener.getLogger().println("Setting " + remoteName + " to " + getRemote());
client.setRemoteUrl(remoteName, getRemote());
listener.getLogger().println("Fetching " + remoteName + "...");
List<RefSpec> refSpecs = getRefSpecs();
client.fetch(remoteName, refSpecs.toArray(new RefSpec[refSpecs.size()]));
listener.getLogger().println("Pruning stale remotes...");
final Repository repository = client.getRepository();
try {
client.prune(new RemoteConfig(repository.getConfig(), remoteName));
} catch (UnsupportedOperationException e) {
e.printStackTrace(listener.error("Could not prune stale remotes"));
} catch (URISyntaxException e) {
e.printStackTrace(listener.error("Could not prune stale remotes"));
}
listener.getLogger().println("Getting remote branches...");
SCMSourceCriteria branchCriteria = getCriteria();
RevWalk walk = new RevWalk(repository);
try {
walk.setRetainBody(false);
for (Branch b : client.getRemoteBranches()) {
if (!b.getName().startsWith(remoteName + "/")) {
continue;
}
final String branchName = StringUtils.removeStart(b.getName(), remoteName + "/");
listener.getLogger().println("Checking branch " + branchName);
if (isExcluded(branchName)){
continue;
}
if (branchCriteria != null) {
RevCommit commit = walk.parseCommit(b.getSHA1());
final long lastModified = TimeUnit.SECONDS.toMillis(commit.getCommitTime());
final RevTree tree = commit.getTree();
SCMSourceCriteria.Probe probe = new SCMSourceCriteria.Probe() {
@Override
public String name() {
return branchName;
}
@Override
public long lastModified() {
return lastModified;
}
@Override
public boolean exists(@NonNull String path) throws IOException {
TreeWalk tw = TreeWalk.forPath(repository, path, tree);
try {
return tw != null;
} finally {
if (tw != null) {
tw.release();
}
}
}
};
if (branchCriteria.isHead(probe, listener)) {
listener.getLogger().println("Met criteria");
} else {
listener.getLogger().println("Does not meet criteria");
continue;
}
}
SCMHead head = new SCMHead(branchName);
SCMRevision hash = new SCMRevisionImpl(head, b.getSHA1String());
observer.observe(head, hash);
if (!observer.isObserving()) {
return;
}
}
} finally {
walk.dispose();
}
listener.getLogger().println("Done.");
} finally {
cacheLock.unlock();
}
}
protected String getCacheEntry() {
return "git-" + Util.getDigestOf(getRemote());
}
protected static File getCacheDir(String cacheEntry) {
File cacheDir = new File(new File(Jenkins.getInstance().getRootDir(), "caches"), cacheEntry);
cacheDir.getParentFile().mkdirs();
return cacheDir;
}
protected static Lock getCacheLock(String cacheEntry) {
Lock cacheLock;
while (null == (cacheLock = cacheLocks.get(cacheEntry))) {
cacheLocks.putIfAbsent(cacheEntry, new ReentrantLock());
}
return cacheLock;
}
protected StandardUsernameCredentials getCredentials() {
return CredentialsMatchers
.firstOrNull(
CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, getOwner(),
ACL.SYSTEM, URIRequirementBuilder.fromUri(getRemote()).build()),
CredentialsMatchers.allOf(CredentialsMatchers.withId(getCredentialsId()),
GitClient.CREDENTIALS_MATCHER));
}
protected abstract List<RefSpec> getRefSpecs();
@NonNull
@Override
public SCM build(@NonNull SCMHead head, @CheckForNull SCMRevision revision) {
BuildChooser buildChooser = revision instanceof SCMRevisionImpl ? new SpecificRevisionBuildChooser(
(SCMRevisionImpl) revision) : new DefaultBuildChooser();
return new GitSCM(
getRemoteConfigs(),
Collections.singletonList(new BranchSpec(head.getName())),
false, Collections.<SubmoduleConfig>emptyList(),
null, null, Collections.<GitSCMExtension>singletonList(new BuildChooserSetting(buildChooser)));
}
protected List<UserRemoteConfig> getRemoteConfigs() {
List<RefSpec> refSpecs = getRefSpecs();
List<UserRemoteConfig> result = new ArrayList<UserRemoteConfig>(refSpecs.size());
String remote = getRemote();
for (RefSpec refSpec : refSpecs) {
result.add(new UserRemoteConfig(remote, getRemoteName(), refSpec.toString(), getCredentialsId()));
}
return result;
}
/**
* Returns true if the branchName isn't matched by includes or is matched by excludes.
*
* @param branchName
* @return true if branchName is excluded or is not included
*/
protected boolean isExcluded (String branchName){
return !Pattern.matches(getPattern(getIncludes()), branchName) || (Pattern.matches(getPattern(getExcludes()), branchName));
}
/**
* Returns the pattern corresponding to the branches containing wildcards.
*
* @param branchName
* @return pattern corresponding to the branches containing wildcards
*/
private String getPattern(String branches){
StringBuilder quotedBranches = new StringBuilder();
for (String wildcard : branches.split(" ")){
StringBuilder quotedBranch = new StringBuilder();
for(String branch : wildcard.split("\\*")){
if (wildcard.startsWith("*") || quotedBranches.length()>0) {
quotedBranch.append(".*");
}
quotedBranch.append(Pattern.quote(branch));
}
if (wildcard.endsWith("*")){
quotedBranch.append(".*");
}
if (quotedBranches.length()>0) {
quotedBranches.append("|");
}
quotedBranches.append(quotedBranch);
}
return quotedBranches.toString();
}
/**
* Our implementation.
*/
public static class SCMRevisionImpl extends SCMRevision {
/**
* The subversion revision.
*/
private String hash;
public SCMRevisionImpl(SCMHead head, String hash) {
super(head);
this.hash = hash;
}
public String getHash() {
return hash;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SCMRevisionImpl that = (SCMRevisionImpl) o;
return StringUtils.equals(hash, that.hash) && getHead().equals(that.getHead());
}
@Override
public int hashCode() {
return hash != null ? hash.hashCode() : 0;
}
}
public static class SpecificRevisionBuildChooser extends BuildChooser {
private final Revision revision;
public SpecificRevisionBuildChooser(SCMRevisionImpl revision) {
ObjectId sha1 = ObjectId.fromString(revision.getHash());
String name = revision.getHead().getName();
this.revision = new Revision(sha1, Collections.singleton(new Branch(name, sha1)));
}
@Override
public Collection<Revision> getCandidateRevisions(boolean isPollCall, String singleBranch, GitClient git,
TaskListener listener, BuildData buildData,
BuildChooserContext context)
throws GitException, IOException, InterruptedException {
return Collections.singleton(revision);
}
@Override
public Build prevBuildForChangelog(String branch, @Nullable BuildData data, GitClient git,
BuildChooserContext context) throws IOException, InterruptedException {
// we have ditched that crazy multiple branch stuff from the regular GIT SCM.
return data == null ? null : data.lastBuild;
}
@Extension
public static class DescriptorImpl extends BuildChooserDescriptor {
@Override
public String getDisplayName() {
return "Specific revision";
}
public boolean isApplicable(java.lang.Class<? extends Item> job) {
return SCMSourceOwner.class.isAssignableFrom(job);
}
}
}
}