Skip to content
Permalink
Browse files

[JENKINS-14572] Add "Do not fetch tags" advanced clone option

  • Loading branch information
rhaendel committed Jun 27, 2015
1 parent 41dfda5 commit bfeda3e661531bd9baec937ec4a4944ac482692c
@@ -20,13 +20,19 @@
* @author Kohsuke Kawaguchi
*/
public class CloneOption extends GitSCMExtension {
private boolean shallow;
private String reference;
private Integer timeout;
private final boolean shallow;
private final boolean noTags;
private final String reference;
private final Integer timeout;

@DataBoundConstructor
public CloneOption(boolean shallow, String reference, Integer timeout) {
this(shallow, true, reference, timeout);
}

@DataBoundConstructor
public CloneOption(boolean shallow, boolean noTags, String reference, Integer timeout) {
this.shallow = shallow;
this.noTags = noTags;
this.reference = reference;
this.timeout = timeout;
}
@@ -35,10 +41,14 @@ public boolean isShallow() {
return shallow;
}

public boolean isNoTags() {
return noTags;
}

public String getReference() {
return reference;
}

public Integer getTimeout() {
return timeout;
}
@@ -49,13 +59,18 @@ public void decorateCloneCommand(GitSCM scm, Run<?, ?> build, GitClient git, Tas
listener.getLogger().println("Using shallow clone");
cmd.shallow();
}
if (noTags) {
listener.getLogger().println("Avoid fetching tags");
cmd.tags(false);
}
cmd.timeout(timeout);
cmd.reference(build.getEnvironment(listener).expand(reference));
}

@Override
public void decorateFetchCommand(GitSCM scm, GitClient git, TaskListener listener, FetchCommand cmd) throws IOException, InterruptedException, GitException {
cmd.timeout(timeout);
cmd.tags(!noTags);
cmd.timeout(timeout);
}

@Override
@@ -5,6 +5,9 @@ def f = namespace(lib.FormTagLib);
f.entry(title:_("Shallow clone"), field:"shallow") {
f.checkbox()
}
f.entry(title:_("Do not fetch tags"), field:"noTags") {
f.checkbox()
}
f.entry(title:_("Path of the reference repo to use during clone"), field:"reference") {
f.textbox()
}
@@ -0,0 +1,4 @@
<div>
Perform a clone without tags, saving time and disk space when you just want to access
what is specified by the refspec.
</div>
@@ -93,6 +93,10 @@ public void commit(final String fileName, final String fileContent, final Person
git.commit(message);
}

public void tag(String tagName, String comment) throws GitException, InterruptedException {
git.tag(tagName, comment);
}

public List<UserRemoteConfig> remoteConfigs() throws IOException {
List<UserRemoteConfig> list = new ArrayList<UserRemoteConfig>();
list.add(new UserRemoteConfig(gitDir.getAbsolutePath(), "origin", "", null));
@@ -0,0 +1,77 @@
package hudson.plugins.git.extensions.impl;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Set;

import hudson.model.Result;
import hudson.model.FreeStyleProject;
import hudson.plugins.git.TestGitRepo;
import hudson.plugins.git.extensions.GitSCMExtensionTest;
import hudson.plugins.git.extensions.GitSCMExtension;

import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.junit.Test;

/**
* @author Ronny Händel
*/
public class CloneOptionNoTagsTest extends GitSCMExtensionTest {

FreeStyleProject project;
TestGitRepo repo;

@Override
public void before() throws Exception {
repo = new TestGitRepo("repo", tmp.newFolder(), listener);
project = setupBasicProject(repo);
}

@Override
protected GitSCMExtension getExtension() {
final boolean shallowClone = true;
final boolean dontFetchTags = true;
final String noReference = null;
final Integer noTimeout = null;
return new CloneOption(shallowClone, dontFetchTags, noReference, noTimeout);
}

@Test
public void cloningShouldNotFetchTags() throws Exception {

repo.commit("repo-init", repo.johnDoe, "repo0 initial commit");
repo.tag("v0.0.1", "a tag that should never be fetched");

assertTrue("scm polling should detect a change after initial commit", project.poll(listener).hasChanges());

build(project, Result.SUCCESS);

assertTrue("there should no tags have been cloned from remote", allTagsInProjectWorkspace().isEmpty());
}

@Test
public void detectNoChangeAfterCreatingATag() throws Exception {

repo.commit("repo-init", repo.johnDoe, "repo0 initial commit");

assertTrue("scm polling should detect a change after initial commit", project.poll(listener).hasChanges());

build(project, Result.SUCCESS);

repo.tag("v0.0.1", "a tag that should never be fetched");

assertFalse("scm polling should not detect a change after creating a tag", project.poll(listener).hasChanges());

build(project, Result.SUCCESS);

assertTrue("there should no tags have been fetched from remote", allTagsInProjectWorkspace().isEmpty());
}

private Set<String> allTagsInProjectWorkspace() throws IOException, InterruptedException {
GitClient git = Git.with(listener, null).in(project.getWorkspace()).getClient();
return git.getTagNames("*");
}
}

0 comments on commit bfeda3e

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