Skip to content

Commit

Permalink
[JENKINS-48061] GitSCMHeadMixin and discover all published refs
Browse files Browse the repository at this point in the history
Since it seems like the git protocol doesn't allow discovering unpublished refs
anyway, we can do ahead and find the sha among all published refs instead
  • Loading branch information
rsandell committed Mar 19, 2018
1 parent 2eee3e5 commit d49fec4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 16 deletions.
40 changes: 29 additions & 11 deletions src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Expand Up @@ -713,12 +713,14 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta
client.addDefaultCredentials(getCredentials());
listener.getLogger().printf("Attempting to resolve %s from remote references...%n", revision);
Map<String, ObjectId> remoteReferences = client.getRemoteReferences(
getRemote(), null, true, true
getRemote(), null, false, false
);
String tagName = null;
Set<String> shortNameMatches = new TreeSet<>();
String shortHashMatch = null;
Set<String> fullTagMatches = new TreeSet<>();
Set<String> fullHashMatches = new TreeSet<>();
String fullHashMatch = null;
for (Map.Entry<String,ObjectId> entry: remoteReferences.entrySet()) {
String name = entry.getKey();
String rev = entry.getValue().name();
Expand Down Expand Up @@ -747,6 +749,18 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta
fullTagMatches.add(name);
continue;
}
if ("HEAD".equals(name)) {
//Skip HEAD as it should only appear during testing, not for standard bare repos iirc
continue;
}
if (rev.toLowerCase(Locale.ENGLISH).equals(revision.toLowerCase(Locale.ENGLISH))) {
fullHashMatches.add(name);
if (fullHashMatch == null) {
fullHashMatch = rev;
}
//Since it was a full match then the shortMatch below will also match, so just skip it
continue;
}
if (rev.toLowerCase(Locale.ENGLISH).startsWith(revision.toLowerCase(Locale.ENGLISH))) {
shortNameMatches.add(name);
if (shortHashMatch == null) {
Expand All @@ -768,6 +782,10 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta
context.wantTags(true);
context.withoutRefSpecs();
}
if (fullHashMatch != null) {
//since this would have been skipped if this was a head or a tag we can just return whatever
return new GitRefSCMRevision(new GitRefSCMHead(fullHashMatch, fullHashMatches.iterator().next()), fullHashMatch);
}
if (shortHashMatch != null) {
// woot this seems unambiguous
for (String name: shortNameMatches) {
Expand All @@ -776,15 +794,18 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta
// WIN it's also a branch
return new GitBranchSCMRevision(new GitBranchSCMHead(StringUtils.removeStart(name, Constants.R_HEADS)),
shortHashMatch);
} else if (name.startsWith(Constants.R_HEADS)) {
tagName = StringUtils.removeStart(name, Constants.R_TAGS);
context.wantBranches(false);
context.wantTags(true);
context.withoutRefSpecs();
}
}
// ok pick a tag so we can do minimal fetch
String name = StringUtils.removeStart(shortNameMatches.iterator().next(), Constants.R_TAGS);
listener.getLogger().printf("Selected match: %s revision %s%n", name, shortHashMatch);
tagName = name;
context.wantBranches(false);
context.wantTags(true);
context.withoutRefSpecs();
if (tagName != null) {
listener.getLogger().printf("Selected match: %s revision %s%n", tagName, shortHashMatch);
} else {
return new GitRefSCMRevision(new GitRefSCMHead(shortHashMatch, shortNameMatches.iterator().next()), shortHashMatch);
}
}
if (tagName != null) {
listener.getLogger().println(
Expand All @@ -809,9 +830,6 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException,
context,
listener, false);
}
if (revision.matches("[a-f0-9]{40}")) {
return new GitRefSCMRevision(new GitRefSCMHead(revision, revision), revision);
}
// Pokémon!... Got to catch them all
listener.getLogger().printf("Could not find %s in remote references. "
+ "Pulling heads to local for deep search...%n", revision);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/jenkins/plugins/git/GitBranchSCMHead.java
Expand Up @@ -29,11 +29,10 @@
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 {
public class GitBranchSCMHead extends SCMHead implements GitSCMHeadMixin {
/**
* Constructor.
*
Expand All @@ -43,6 +42,11 @@ public GitBranchSCMHead(@NonNull String name) {
super(name);
}

@Override
public String getRef() {
return "refs/heads/" + getName();
}

@Restricted(NoExternalUse.class)
@Extension
public static class SCMHeadMigrationImpl extends SCMHeadMigration<GitSCMSource, SCMHead, AbstractGitSCMSource.SCMRevisionImpl> {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/jenkins/plugins/git/GitRefSCMHead.java
Expand Up @@ -2,21 +2,29 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.mixin.SCMHeadMixin;

public class GitRefSCMHead extends SCMHead implements SCMHeadMixin {
public class GitRefSCMHead extends SCMHead implements GitSCMHeadMixin {
private final String ref;

/**
* Constructor.
*
* @param name the name.
*/
public GitRefSCMHead(@NonNull String name, String ref) {
public GitRefSCMHead(@NonNull String name, @NonNull String ref) {
super(name);
this.ref = ref;
}

/**
* Constructor.
*
* @param name the name.
*/
public GitRefSCMHead(@NonNull String name) {
this(name, name);
}

public String getRef() {
return ref;
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/jenkins/plugins/git/GitSCMHeadMixin.java
@@ -0,0 +1,32 @@
/*
* 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 jenkins.scm.api.mixin.SCMHeadMixin;

public interface GitSCMHeadMixin extends SCMHeadMixin {

String getRef();
}
27 changes: 27 additions & 0 deletions src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java
Expand Up @@ -508,6 +508,33 @@ public void retrieveRevision_customRef() throws Exception {
assertEquals("v3", fileAt(v3, run, source, listener));
}

@Issue("JENKINS-48061")
@Test
public void retrieveRevision_customRef_abbrev_sha1() throws Exception {
sampleRepo.init();
sampleRepo.write("file", "v1");
sampleRepo.git("commit", "--all", "--message=v1");
sampleRepo.git("tag", "v1");
String v1 = sampleRepo.head();
sampleRepo.write("file", "v2");
sampleRepo.git("commit", "--all", "--message=v2"); // master
sampleRepo.git("checkout", "-b", "dev");
sampleRepo.write("file", "v3");
sampleRepo.git("commit", "--all", "--message=v3"); // dev
String v3 = sampleRepo.head();
sampleRepo.git("update-ref", "refs/custom/foo", v3); // now this is an advertised ref so cannot be GC'd
sampleRepo.git("reset", "--hard", "HEAD^"); // dev
sampleRepo.write("file", "v4");
sampleRepo.git("commit", "--all", "--message=v4"); // dev
// SCM.checkout does not permit a null build argument, unfortunately.
Run<?,?> run = r.buildAndAssertSuccess(r.createFreeStyleProject());
GitSCMSource source = new GitSCMSource(sampleRepo.toString());
source.setTraits(Arrays.asList(new BranchDiscoveryTrait(), new TagDiscoveryTrait()));
StreamTaskListener listener = StreamTaskListener.fromStderr();
// Test retrieval of non head revision:
assertEquals("v3", fileAt(v3.substring(0, 7), run, source, listener));
}

private String fileAt(String revision, Run<?,?> run, SCMSource source, TaskListener listener) throws Exception {
SCMRevision rev = source.fetch(revision, listener);
if (rev == null) {
Expand Down

0 comments on commit d49fec4

Please sign in to comment.