Skip to content
Permalink
Browse files

[JENKINS-48061] Introduce GitBranchSCMHead

And migrate old usage of plain SCMHead
  • Loading branch information...
rsandell committed Mar 9, 2018
1 parent aecfb07 commit 50e4690cd0e32c35111dfd52e6a1d26eb9995468
@@ -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())) {
@@ -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
@@ -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
@@ -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);
@@ -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);
@@ -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);
}
}
@@ -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;
@@ -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;
}

}
}
@@ -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);
}


}
@@ -626,14 +626,14 @@ public boolean isMatch(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();
@@ -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 {
@@ -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;
@@ -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);

}

0 comments on commit 50e4690

Please sign in to comment.
You can’t perform that action at this time.