diff --git a/src/main/java/hudson/plugins/mercurial/MercurialSCM.java b/src/main/java/hudson/plugins/mercurial/MercurialSCM.java index 2fb63888..24a50831 100755 --- a/src/main/java/hudson/plugins/mercurial/MercurialSCM.java +++ b/src/main/java/hudson/plugins/mercurial/MercurialSCM.java @@ -151,6 +151,8 @@ public enum RevisionType { @Deprecated private String branch; + private String headName = "default"; + /** Slash-separated subdirectory of the workspace in which the repository will be kept; null for top level. */ private String subdir; @@ -300,6 +302,14 @@ public boolean isDisableChangeLog() { this.revision = Util.fixEmpty(revision) == null ? "default" : revision; } + public @NonNull String getHeadName() { + return headName; + } + + public final void setHeadName(@NonNull String headName) { + this.headName = Util.fixEmpty(headName) == null ? "default" : headName; + } + @Deprecated public String getBranch() { if (revisionType != RevisionType.BRANCH) { diff --git a/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java b/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java index c0713c94..ae13aac7 100644 --- a/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java +++ b/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java @@ -115,10 +115,13 @@ B withSource(String source) { MercurialSCM result = new MercurialSCM(source()); if (revision instanceof MercurialSCMSource.MercurialRevision) { result.setRevisionType(MercurialSCM.RevisionType.CHANGESET); - result.setRevision(((MercurialSCMSource.MercurialRevision) revision).getHash()); + MercurialSCMSource.MercurialRevision mercurialRevision = (MercurialSCMSource.MercurialRevision) revision; + result.setRevision(mercurialRevision.getHash()); + result.setHeadName(mercurialRevision.getHead().getName()); } else { result.setRevisionType(MercurialSCM.RevisionType.BRANCH); result.setRevision(head().getName()); + result.setHeadName(head().getName()); } result.setBrowser(browser()); result.setClean(clean()); diff --git a/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java b/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java new file mode 100644 index 00000000..63e01d25 --- /dev/null +++ b/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java @@ -0,0 +1,88 @@ +package hudson.plugins.mercurial; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import hudson.FilePath; +import hudson.tools.ToolProperty; +import hudson.util.LogTaskListener; +import hudson.util.StreamTaskListener; +import jenkins.scm.api.SCMHead; +import jenkins.scm.api.SCMHeadObserver; +import jenkins.scm.api.SCMRevision; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.jvnet.hudson.test.JenkinsRule; + +import static org.junit.Assert.assertEquals; + +public class MercurialSCMBuilderTest { + @Rule + public JenkinsRule j = new JenkinsRule(); + @Rule + public MercurialRule m = new MercurialRule(j); + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + private static LogTaskListener listener; + private static MercurialSCMSource mercurialSCMSource; + + @Before + public void prepareEnvironment() throws Exception { + String instName = "caching"; + MercurialInstallation installation = new MercurialInstallation(instName, "", "hg", false, true, null, false, null, + Collections.>emptyList()); + listener = new LogTaskListener(Logger.getLogger(MercurialSCMSourceTest.class.getName()), Level.INFO); + j.jenkins.getDescriptorByType(MercurialInstallation.DescriptorImpl.class).setInstallations(installation); + FilePath repo = new FilePath(tmp.getRoot()); + installation.forNode(j.jenkins, StreamTaskListener.fromStdout()); + m.hg(repo, "init"); + repo.child("file").write("initial content", "UTF-8"); + m.hg(repo, "commit", "--addremove", "--message=initial"); + m.hg(repo, "tag", "version-1.0"); + m.hg(repo, "branch", "my-branch"); + repo.child("file2").write("content in branch", "UTF-8"); + m.hg(repo, "commit", "--addremove", "--message=branch"); + m.hg(repo, "tag", "version-1.1"); + + installation.forNode(j.jenkins, StreamTaskListener.fromStdout()); + mercurialSCMSource = new MercurialSCMSource(null, instName, tmp.getRoot().toURI().toURL().toString(), null, null, null, null, null, true); + } + + @Test + public void headNameEquals() throws IOException, InterruptedException { + Map result = mercurialSCMSource.fetch(null, SCMHeadObserver.collect(), null, null).result(); + for (Map.Entry entry : result.entrySet()) { + MercurialSCM mercurialSCM = new MercurialSCMBuilder(entry.getKey(), entry.getValue(), "", "") + .build(); + assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); + assertEquals(entry.getValue().getHead().getName(), mercurialSCM.getHeadName()); + assertEquals(entry.getKey().getName(), mercurialSCM.getHeadName()); + } + } + + @Test + public void headNameDefault() throws IOException, InterruptedException { + SCMRevision revision = mercurialSCMSource.fetch("version-1.0", listener, null); + MercurialSCM mercurialSCM = new MercurialSCMBuilder(revision.getHead(), revision, "", "") + .build(); + assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); + assertEquals("default", mercurialSCM.getHeadName()); + assertEquals(revision.getHead().getName(), mercurialSCM.getHeadName()); + } + + @Test + public void headNameNonDefault() throws IOException, InterruptedException { + SCMRevision revision = mercurialSCMSource.fetch("version-1.1", listener, null); + MercurialSCM mercurialSCM = new MercurialSCMBuilder(revision.getHead(), revision, "", "") + .build(); + assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); + assertEquals("my-branch", mercurialSCM.getHeadName()); + assertEquals(revision.getHead().getName(), mercurialSCM.getHeadName()); + } +}