Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #11 from rpocase/master
Browse files Browse the repository at this point in the history
[JENKINS-28213] Force syncing a workspace that does not exist on the filesystem causes a FileNotFoundException
  • Loading branch information
p4paul committed May 12, 2015
2 parents f830dd4 + 0400e48 commit 939279e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
63 changes: 38 additions & 25 deletions src/main/java/org/jenkinsci/plugins/p4/client/ClientHelper.java
Expand Up @@ -3,10 +3,7 @@
import hudson.AbortException;
import hudson.model.TaskListener;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -150,7 +147,8 @@ public boolean updateFiles() throws Exception {
/**
* Sync files to workspace at the specified change/label.
*
* @param change
* @param buildChange Change to sync from
* @param populate Populate strategy
* @throws Exception
*/
public void syncFiles(Object buildChange, Populate populate)
Expand Down Expand Up @@ -233,28 +231,44 @@ public void tidyWorkspace(Populate populate) throws Exception {
files = FileSpecBuilder.makeFileSpecList(path);

if (populate instanceof AutoCleanImpl) {
// remove all pending files within workspace
tidyPending(files);

// clean files within workspace
tidyRevisions(files, populate);
}
tidyAutoCleanImpl(populate, files);
}

if (populate instanceof ForceCleanImpl) {
// remove all pending files within workspace
tidyPending(files);
tidyForceSyncImpl(populate, files);
}
}

// remove all versioned files (clean have list)
syncFiles(0, populate);
private void tidyForceSyncImpl(Populate populate, List<IFileSpec> files) throws Exception {
// remove all pending files within workspace
tidyPending(files);

// remove all files from workspace
String root = iclient.getRoot();
log("... rm -rf " + root);
FileUtils.forceDelete(new File(root));
}
}
// remove all versioned files (clean have list)
syncFiles(0, populate);

// remove all files from workspace
String root = iclient.getRoot();
log("... rm -rf " + root);
silentlyForceDelete(root);
}

private void tidyPending(List<IFileSpec> files) throws Exception {
private void silentlyForceDelete(String root) throws IOException {
try {
FileUtils.forceDelete(new File(root));
} catch (FileNotFoundException ignored) {

}
}

private void tidyAutoCleanImpl(Populate populate, List<IFileSpec> files) throws Exception {
// remove all pending files within workspace
tidyPending(files);

// clean files within workspace
tidyRevisions(files, populate);
}

private void tidyPending(List<IFileSpec> files) throws Exception {
TimeTask timer = new TimeTask();
log("SCM Task: reverting all pending and shelved revisions.");

Expand Down Expand Up @@ -624,8 +638,7 @@ public List<Integer> listChanges(Object from) throws Exception {

/**
* Show all changes within the scope of the client.
*
* @param from
*
* @return
* @throws Exception
*/
Expand Down Expand Up @@ -669,7 +682,7 @@ public List<Integer> listHaveChanges() throws Exception {
* Fetches a list of changes needed to update the workspace to the specified
* limit. The limit could be a Perforce change number or label.
*
* @param limit
* @param changeLimit
* @return
* @throws Exception
*/
Expand Down
@@ -1,15 +1,12 @@
package org.jenkinsci.plugins.p4.credentials;

import hudson.model.Hudson;

import org.kohsuke.stapler.export.ExportedBean;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsScope;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.export.ExportedBean;

@ExportedBean
public class P4Credentials implements Credentials {
Expand Down Expand Up @@ -39,7 +36,7 @@ public CredentialsScope getScope() {

@NonNull
public CredentialsDescriptor getDescriptor() {
return (CredentialsDescriptor) Hudson.getInstance().getDescriptorOrDie(
return (CredentialsDescriptor) Jenkins.getInstance().getDescriptorOrDie(
getClass());
}

Expand Down
18 changes: 7 additions & 11 deletions src/main/java/org/jenkinsci/plugins/p4/workspace/Workspace.java
@@ -1,23 +1,19 @@
package org.jenkinsci.plugins.p4.workspace;

import com.perforce.p4java.client.IClient;
import com.perforce.p4java.server.IOptionsServer;
import hudson.DescriptorExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Hudson;
import hudson.slaves.NodeProperty;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.p4.review.ReviewProp;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import jenkins.model.Jenkins;

import org.jenkinsci.plugins.p4.review.ReviewProp;

import com.perforce.p4java.client.IClient;
import com.perforce.p4java.server.IOptionsServer;

public abstract class Workspace implements Cloneable, ExtensionPoint,
Describable<Workspace> {

Expand Down Expand Up @@ -64,12 +60,12 @@ public abstract IClient setClient(IOptionsServer connection, String user)
throws Exception;

public WorkspaceDescriptor getDescriptor() {
return (WorkspaceDescriptor) Hudson.getInstance().getDescriptor(
return (WorkspaceDescriptor) Jenkins.getInstance().getDescriptor(
getClass());
}

public static DescriptorExtensionList<Workspace, WorkspaceDescriptor> all() {
return Hudson.getInstance()
return Jenkins.getInstance()
.<Workspace, WorkspaceDescriptor> getDescriptorList(
Workspace.class);
}
Expand Down

0 comments on commit 939279e

Please sign in to comment.