Skip to content

Commit

Permalink
Files/changes added to allow clean before checkout resolving JENKINS-…
Browse files Browse the repository at this point in the history
  • Loading branch information
dwang-sugarcrm committed Apr 5, 2014
1 parent bc57353 commit fb12fed
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
@@ -0,0 +1,46 @@
package hudson.plugins.git.extensions.impl;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.TaskListener;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;

import org.jenkinsci.plugins.gitclient.CloneCommand;
import org.jenkinsci.plugins.gitclient.FetchCommand;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;

/**
* git-clean before the checkout.
*
* @author David S Wang
*/
public class CleanBeforeCheckout extends GitSCMExtension {
@DataBoundConstructor
public CleanBeforeCheckout() {
}

@Override
public void decorateFetchCommand(GitSCM scm, GitClient git, TaskListener listener, FetchCommand cmd) throws IOException, InterruptedException, GitException {
listener.getLogger().println("Cleaning workspace");
git.clean();
// TODO: revisit how to hand off to SubmoduleOption
for (GitSCMExtension ext : scm.getExtensions()) {
ext.onClean(scm, git);
}
}

@Extension
public static class DescriptorImpl extends GitSCMExtensionDescriptor {
@Override
public String getDisplayName() {
return "Clean before checkout";
}
}
}
@@ -0,0 +1,8 @@
<div>
Clean up the workspace before every checkout by deleting all untracked files and directories,
including those which are specified in <tt>.gitignore</tt>.
It also resets all <em>tracked</em> files to their versioned state.
This ensures that the workspace is
in the same state as if you cloned and checked out in a brand-new empty directory, and ensures
that your build is not affected by the files generated by the previous build.
</div>
1 change: 1 addition & 0 deletions src/test/java/hudson/plugins/git/AbstractGitTestCase.java
Expand Up @@ -4,6 +4,7 @@
import hudson.FilePath;
import hudson.model.*;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.CleanBeforeCheckout;
import hudson.plugins.git.extensions.impl.DisableRemotePoll;
import hudson.plugins.git.extensions.impl.PathRestriction;
import hudson.plugins.git.extensions.impl.RelativeTargetDirectory;
Expand Down
33 changes: 32 additions & 1 deletion src/test/java/hudson/plugins/git/GitSCMTest.java
Expand Up @@ -8,6 +8,7 @@
import hudson.plugins.git.browser.GithubWeb;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.AuthorInChangelog;
import hudson.plugins.git.extensions.impl.CleanBeforeCheckout;
import hudson.plugins.git.extensions.impl.LocalBranch;
import hudson.plugins.git.extensions.impl.PreBuildMerge;
import hudson.plugins.git.extensions.impl.RelativeTargetDirectory;
Expand All @@ -29,10 +30,10 @@
import com.google.common.collect.Collections2;

import hudson.util.StreamTaskListener;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.PersonIdent;

import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.jenkinsci.plugins.gitclient.Git;
Expand Down Expand Up @@ -234,6 +235,36 @@ public void testBasicExcludedRegion() throws Exception {
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}

public void testCleanBeforeCheckout() throws Exception {
FreeStyleProject p = setupProject("master", false, null, null, "Jane Doe", null);
((GitSCM)p.getScm()).getExtensions().add(new CleanBeforeCheckout());
final String commitFile1 = "commitFile1";
final String commitFile2 = "commitFile2";
commit(commitFile1, johnDoe, janeDoe, "Commit number 1");
commit(commitFile2, johnDoe, janeDoe, "Commit number 2");
final FreeStyleBuild firstBuild = build(p, Result.SUCCESS, commitFile1);
final String branch1 = "Branch1";
final String branch2 = "Branch2";
List<BranchSpec> branches = new ArrayList<BranchSpec>();
branches.add(new BranchSpec("master"));
branches.add(new BranchSpec(branch1));
branches.add(new BranchSpec(branch2));
git.branch(branch1);
git.checkout(branch1);
p.poll(listener).hasChanges();
assertTrue(firstBuild.getLog().contains("Cleaning"));
assertTrue(firstBuild.getLog().indexOf("Cleaning") > firstBuild.getLog().indexOf("Cloning")); //clean should be after clone
assertTrue(firstBuild.getLog().indexOf("Cleaning") < firstBuild.getLog().indexOf("Checking out")); //clean before checkout
assertTrue(firstBuild.getWorkspace().child(commitFile1).exists());
git.checkout(branch1);
final FreeStyleBuild secondBuild = build(p, Result.SUCCESS, commitFile2);
p.poll(listener).hasChanges();
assertTrue(secondBuild.getLog().contains("Cleaning"));
assertTrue(secondBuild.getLog().indexOf("Cleaning") < secondBuild.getLog().indexOf("Fetching upstream changes"));
assertTrue(secondBuild.getWorkspace().child(commitFile2).exists());


}
@Bug(value = 8342)
public void testExcludedRegionMultiCommit() throws Exception {
// Got 2 projects, each one should only build if changes in its own file
Expand Down

0 comments on commit fb12fed

Please sign in to comment.