Skip to content

Commit

Permalink
[JENKINS-48061] Introduce GitBranchSCMHead
Browse files Browse the repository at this point in the history
And migrate old usage of plain SCMHead
  • Loading branch information
rsandell committed Mar 9, 2018
1 parent aecfb07 commit 50e4690
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 18 deletions.
29 changes: 18 additions & 11 deletions src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Expand Up @@ -381,7 +381,14 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException,
// tag does not exist
return null;
}
} else {
} else if (head instanceof GitBranchSCMHead) {
for (Branch b : client.getRemoteBranches()) {
String branchName = StringUtils.removeStart(b.getName(), remoteName + "/");
if (branchName.equals(head.getName())) {
return new GitBranchSCMRevision((GitBranchSCMHead)head, b.getSHA1String());
}
}
} else { //TODO change to something
for (Branch b : client.getRemoteBranches()) {
String branchName = StringUtils.removeStart(b.getName(), remoteName + "/");
if (branchName.equals(head.getName())) {
Expand Down Expand Up @@ -554,7 +561,7 @@ private void discoverBranches(final Repository repository,
}
count++;
final String branchName = StringUtils.removeStart(ref.getKey(), Constants.R_HEADS);
if (request.process(new SCMHead(branchName),
if (request.process(new GitBranchSCMHead(branchName),
new SCMSourceRequest.IntermediateLambda<ObjectId>() {
@Nullable
@Override
Expand All @@ -563,23 +570,23 @@ public ObjectId create() throws IOException, InterruptedException {
return ref.getValue();
}
},
new SCMSourceRequest.ProbeLambda<SCMHead, ObjectId>() {
new SCMSourceRequest.ProbeLambda<GitBranchSCMHead, ObjectId>() {
@NonNull
@Override
public SCMSourceCriteria.Probe create(@NonNull SCMHead head,
public SCMSourceCriteria.Probe create(@NonNull GitBranchSCMHead head,
@Nullable ObjectId revisionInfo)
throws IOException, InterruptedException {
RevCommit commit = walk.parseCommit(revisionInfo);
final long lastModified = TimeUnit.SECONDS.toMillis(commit.getCommitTime());
final RevTree tree = commit.getTree();
return new TreeWalkingSCMProbe(branchName, lastModified, repository, tree);
}
}, new SCMSourceRequest.LazyRevisionLambda<SCMHead, SCMRevision, ObjectId>() {
}, new SCMSourceRequest.LazyRevisionLambda<GitBranchSCMHead, SCMRevision, ObjectId>() {
@NonNull
@Override
public SCMRevision create(@NonNull SCMHead head, @Nullable ObjectId intermediate)
public SCMRevision create(@NonNull GitBranchSCMHead head, @Nullable ObjectId intermediate)
throws IOException, InterruptedException {
return new SCMRevisionImpl(head, ref.getValue().name());
return new GitBranchSCMRevision(head, ref.getValue().name());
}
}, new SCMSourceRequest.Witness() {
@Override
Expand Down Expand Up @@ -718,7 +725,7 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta
if (name.equals(Constants.R_HEADS + revision)) {
listener.getLogger().printf("Found match: %s revision %s%n", name, rev);
// WIN!
return new SCMRevisionImpl(new SCMHead(revision), rev);
return new GitBranchSCMRevision(new GitBranchSCMHead(revision), rev);
}
if (name.equals(Constants.R_TAGS+revision)) {
listener.getLogger().printf("Found match: %s revision %s%n", name, rev);
Expand All @@ -732,7 +739,7 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta
if (name.startsWith(Constants.R_HEADS) && revision.equalsIgnoreCase(rev)) {
listener.getLogger().printf("Found match: %s revision %s%n", name, rev);
// WIN!
return new SCMRevisionImpl(new SCMHead(StringUtils.removeStart(name, Constants.R_HEADS)), rev);
return new GitBranchSCMRevision(new GitBranchSCMHead(StringUtils.removeStart(name, Constants.R_HEADS)), rev);
}
if (name.startsWith(Constants.R_TAGS) && revision.equalsIgnoreCase(rev)) {
listener.getLogger().printf("Candidate match: %s revision %s%n", name, rev);
Expand Down Expand Up @@ -767,7 +774,7 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta
if (name.startsWith(Constants.R_HEADS)) {
listener.getLogger().printf("Selected match: %s revision %s%n", name, shortHashMatch);
// WIN it's also a branch
return new SCMRevisionImpl(new SCMHead(StringUtils.removeStart(name, Constants.R_HEADS)),
return new GitBranchSCMRevision(new GitBranchSCMHead(StringUtils.removeStart(name, Constants.R_HEADS)),
shortHashMatch);
}
}
Expand Down Expand Up @@ -831,7 +838,7 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException,
}
listener.getLogger()
.printf("Selected match: %s revision %s%n", name, hash);
return new SCMRevisionImpl(new SCMHead(name), hash);
return new GitBranchSCMRevision(new GitBranchSCMHead(name), hash);
} catch (GitException x) {
x.printStackTrace(listener.error("Could not resolve %s", revision));
return null;
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/jenkins/plugins/git/GitBranchSCMHead.java
@@ -0,0 +1,69 @@
/*
* The MIT License
*
* Copyright (c) 2018 CloudBees, Inc.
*
* 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 edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadMigration;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.mixin.SCMHeadMixin;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

public class GitBranchSCMHead extends SCMHead implements SCMHeadMixin {
/**
* Constructor.
*
* @param name the name.
*/
public GitBranchSCMHead(@NonNull String name) {
super(name);
}

@Restricted(NoExternalUse.class)
@Extension
public static class SCMHeadMigrationImpl extends SCMHeadMigration<GitSCMSource, SCMHead, AbstractGitSCMSource.SCMRevisionImpl> {

public SCMHeadMigrationImpl() {
super(GitSCMSource.class, SCMHead.class, AbstractGitSCMSource.SCMRevisionImpl.class);
}

@Override
public SCMHead migrate(@NonNull GitSCMSource source, @NonNull SCMHead head) {
return new GitBranchSCMHead(head.getName());
}

@Override
public SCMRevision migrate(@NonNull GitSCMSource source, @NonNull AbstractGitSCMSource.SCMRevisionImpl revision) {
if (revision.getHead().getClass() == SCMHead.class) {
return new GitBranchSCMRevision((GitBranchSCMHead) migrate(source, revision.getHead()),
revision.getHash());
}
return null;
}

}
}
35 changes: 35 additions & 0 deletions src/main/java/jenkins/plugins/git/GitBranchSCMRevision.java
@@ -0,0 +1,35 @@
/*
* The MIT License
*
* Copyright (c) 2018 CloudBees, Inc.
*
* 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;


public class GitBranchSCMRevision extends AbstractGitSCMSource.SCMRevisionImpl {

public GitBranchSCMRevision(GitBranchSCMHead head, String hash) {
super(head, hash);
}


}
4 changes: 2 additions & 2 deletions src/main/java/jenkins/plugins/git/GitSCMSource.java
Expand Up @@ -626,14 +626,14 @@ public Map<SCMHead, SCMRevision> heads(@NonNull SCMSource source) {
return Collections.emptyMap();
}
if (GitStatus.looselyMatches(u, remote)) {
SCMHead head = new SCMHead(branch);
GitBranchSCMHead head = new GitBranchSCMHead(branch);
for (SCMHeadPrefilter filter: ctx.prefilters()) {
if (filter.isExcluded(git, head)) {
return Collections.emptyMap();
}
}
return Collections.<SCMHead, SCMRevision>singletonMap(head,
sha1 != null ? new SCMRevisionImpl(head, sha1) : null);
sha1 != null ? new GitBranchSCMRevision(head, sha1) : null);
}
}
return Collections.emptyMap();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/jenkins/plugins/git/GitSCMTelescope.java
Expand Up @@ -212,12 +212,12 @@ public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevis
getTimestamp(remote, credentials, name)
);
} else if (name.startsWith(Constants.R_HEADS)) {
head = new SCMHead(name.substring(Constants.R_HEADS.length()));
head = new GitBranchSCMHead(name.substring(Constants.R_HEADS.length()));
} else {
if (name.startsWith(config.getName() + "/")) {
head = new SCMHead(name.substring(config.getName().length() + 1));
head = new GitBranchSCMHead(name.substring(config.getName().length() + 1));
} else {
head = new SCMHead(name);
head = new GitBranchSCMHead(name);
}
}
} else {
Expand Down
Expand Up @@ -177,7 +177,7 @@ public void lastModified_Smokes() throws Exception {
sampleRepo.init();
sampleRepo.git("checkout", "-b", "dev");
SCMSource source = new GitSCMSource(null, sampleRepo.toString(), "", "*", "", true);
SCMRevision revision = source.fetch(new SCMHead("dev"), null);
SCMRevision revision = source.fetch(new GitBranchSCMHead("dev"), null);
sampleRepo.write("file", "modified");
sampleRepo.git("commit", "--all", "--message=dev");
final long fileSystemAllowedOffset = isWindows() ? 4000 : 1500;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/jenkins/plugins/git/GitSCMSourceTest.java
Expand Up @@ -94,7 +94,7 @@ public void testSourceOwnerTriggeredByDoNotifyCommit() throws Exception {
jenkins.getInstance().getExtensionList(SCMEventListener.class).get(SCMEventListenerImpl.class)
.waitSCMHeadEvent(1, TimeUnit.SECONDS);
assertThat(event, notNullValue());
assertThat((Iterable<SCMHead>) event.heads(gitSCMSource).keySet(), hasItem(is(new SCMHead("master"))));
assertThat((Iterable<SCMHead>) event.heads(gitSCMSource).keySet(), hasItem(is(new GitBranchSCMHead("master"))));
verify(scmSourceOwner, times(0)).onSCMSourceUpdated(gitSCMSource);

}
Expand Down

0 comments on commit 50e4690

Please sign in to comment.