Skip to content

Commit 0e07d20

Browse files
committedDec 12, 2016
[JENKINS-39355 Follow-up] Early access of work in progress
1 parent 37a9588 commit 0e07d20

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed
 

‎pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
<dependency>
165165
<groupId>org.jenkins-ci.plugins</groupId>
166166
<artifactId>scm-api</artifactId>
167-
<version>1.3</version>
167+
<version>1.4-SNAPSHOT</version>
168168
</dependency>
169169
<dependency>
170170
<groupId>org.jenkins-ci.plugins.workflow</groupId>

‎src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

+38-9
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@
5555
import hudson.scm.SCM;
5656
import hudson.security.ACL;
5757
import jenkins.model.Jenkins;
58+
import jenkins.scm.api.SCMFile;
5859
import jenkins.scm.api.SCMHead;
5960
import jenkins.scm.api.SCMHeadObserver;
61+
import jenkins.scm.api.SCMProbe;
62+
import jenkins.scm.api.SCMProbeStat;
6063
import jenkins.scm.api.SCMRevision;
6164
import jenkins.scm.api.SCMSource;
6265
import jenkins.scm.api.SCMSourceCriteria;
6366
import jenkins.scm.api.SCMSourceOwner;
6467
import org.apache.commons.lang.StringUtils;
68+
import org.eclipse.jgit.lib.FileMode;
6569
import org.eclipse.jgit.lib.ObjectId;
6670
import org.eclipse.jgit.lib.Repository;
6771
import org.eclipse.jgit.revwalk.RevCommit;
@@ -225,33 +229,38 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException,
225229
}, listener, /* we don't prune remotes here, as we just want one head's revision */false);
226230
}
227231

228-
@NonNull
229232
@Override
230-
protected void retrieve(@NonNull final SCMHeadObserver observer,
233+
protected void retrieve(@CheckForNull final SCMSourceCriteria criteria,
234+
@NonNull final SCMHeadObserver observer,
231235
@NonNull final TaskListener listener)
232236
throws IOException, InterruptedException {
233237
doRetrieve(new Retriever<Void>() {
234238
@Override
235239
public Void run(GitClient client, String remoteName) throws IOException, InterruptedException {
236240
final Repository repository = client.getRepository();
237241
listener.getLogger().println("Getting remote branches...");
238-
SCMSourceCriteria branchCriteria = getCriteria();
239242
try (RevWalk walk = new RevWalk(repository)) {
240243
walk.setRetainBody(false);
241244
for (Branch b : client.getRemoteBranches()) {
245+
checkInterrupt();
242246
if (!b.getName().startsWith(remoteName + "/")) {
243247
continue;
244248
}
245249
final String branchName = StringUtils.removeStart(b.getName(), remoteName + "/");
246250
listener.getLogger().println("Checking branch " + branchName);
247-
if (isExcluded(branchName)) {
251+
if (isExcluded(branchName)){
248252
continue;
249253
}
250-
if (branchCriteria != null) {
254+
if (criteria != null) {
251255
RevCommit commit = walk.parseCommit(b.getSHA1());
252256
final long lastModified = TimeUnit.SECONDS.toMillis(commit.getCommitTime());
253257
final RevTree tree = commit.getTree();
254-
SCMSourceCriteria.Probe probe = new SCMSourceCriteria.Probe() {
258+
SCMSourceCriteria.Probe probe = new SCMProbe() {
259+
@Override
260+
public void close() throws IOException {
261+
// no-op
262+
}
263+
255264
@Override
256265
public String name() {
257266
return branchName;
@@ -263,13 +272,33 @@ public long lastModified() {
263272
}
264273

265274
@Override
266-
public boolean exists(@NonNull String path) throws IOException {
275+
@NonNull
276+
public SCMProbeStat stat(@NonNull String path) throws IOException {
267277
try (TreeWalk tw = TreeWalk.forPath(repository, path, tree)) {
268-
return tw != null;
278+
if (tw == null) {
279+
return SCMProbeStat.fromType(SCMFile.Type.NONEXISTENT);
280+
}
281+
FileMode fileMode = tw.getFileMode(0);
282+
if (fileMode == FileMode.MISSING) {
283+
return SCMProbeStat.fromType(SCMFile.Type.NONEXISTENT);
284+
}
285+
if (fileMode == FileMode.EXECUTABLE_FILE) {
286+
return SCMProbeStat.fromType(SCMFile.Type.REGULAR_FILE);
287+
}
288+
if (fileMode == FileMode.REGULAR_FILE) {
289+
return SCMProbeStat.fromType(SCMFile.Type.REGULAR_FILE);
290+
}
291+
if (fileMode == FileMode.SYMLINK) {
292+
return SCMProbeStat.fromType(SCMFile.Type.LINK);
293+
}
294+
if (fileMode == FileMode.TREE) {
295+
return SCMProbeStat.fromType(SCMFile.Type.DIRECTORY);
296+
}
297+
return SCMProbeStat.fromType(SCMFile.Type.OTHER);
269298
}
270299
}
271300
};
272-
if (branchCriteria.isHead(probe, listener)) {
301+
if (criteria.isHead(probe, listener)) {
273302
listener.getLogger().println("Met criteria");
274303
} else {
275304
listener.getLogger().println("Does not meet criteria");

‎src/test/java/jenkins/plugins/git/AbstractGitSCMSourceRetrieveHeadsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void correctGitToolIsUsed() throws Exception {
7676
public void correctGitToolIsUsed2() throws Exception {
7777
try {
7878
// Should throw exception confirming that Git#using was used correctly
79-
gitSCMSource.retrieve(PowerMockito.mock(SCMHeadObserver.class), TaskListener.NULL);
79+
gitSCMSource.retrieve(null, PowerMockito.mock(SCMHeadObserver.class), TaskListener.NULL);
8080
} catch (GitToolNotSpecified e) {
8181
Assert.fail("Git client was constructed with arbitrary git tool");
8282
}

0 commit comments

Comments
 (0)
Please sign in to comment.