Skip to content

Commit

Permalink
Merge pull request #21 from jglick/JENKINS-18390-prune
Browse files Browse the repository at this point in the history
[FIXED JENKINS-18390] -P did not work with -Q
  • Loading branch information
mc1arke committed Jun 21, 2013
2 parents c4e376e + 8499b09 commit b1f5341
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/main/java/hudson/scm/AbstractCvs.java
Expand Up @@ -27,6 +27,7 @@
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
Expand Down Expand Up @@ -156,7 +157,7 @@ protected boolean checkout(CvsRepository[] repositories, boolean isFlatten, File
updateCommand.setUpdateByDate(dateStamp);
}

if (!perform(updateCommand, targetWorkspace, listener, repository, moduleName, envVars)) {
if (!perform(updateCommand, targetWorkspace, listener, repository, moduleName, envVars, pruneEmptyDirectories)) {
if (cleanOnFailedUpdate) {
updateFailed = true;
} else {
Expand Down Expand Up @@ -205,7 +206,7 @@ protected boolean checkout(CvsRepository[] repositories, boolean isFlatten, File
// and specify which module to load
checkoutCommand.setModule(envVars.expand(cvsModule.getRemoteName()));

if (!perform(checkoutCommand, targetWorkspace, listener, repository, moduleName, envVars)) {
if (!perform(checkoutCommand, targetWorkspace, listener, repository, moduleName, envVars, pruneEmptyDirectories)) {
return false;
}

Expand All @@ -232,7 +233,7 @@ protected boolean checkout(CvsRepository[] repositories, boolean isFlatten, File
* @throws InterruptedException if the user cancels the action
*/
private boolean perform(final Command cvsCommand, final FilePath workspace, final TaskListener listener,
final CvsRepository repository, final String moduleName, final EnvVars envVars)
final CvsRepository repository, final String moduleName, final EnvVars envVars, final boolean pruneEmptyDirectories)
throws IOException, InterruptedException {

final Client cvsClient = getCvsClient(repository, envVars, listener);
Expand All @@ -259,7 +260,18 @@ public Boolean invoke(final File workspace, final VirtualChannel channel) throws
cvsClient.getEventManager().addCVSListener(basicListener);

try {
return cvsClient.executeCommand(cvsCommand, globalOptions);
if (!cvsClient.executeCommand(cvsCommand, globalOptions)) {
return false;
}
if (pruneEmptyDirectories && !isDisableCvsQuiet()) {
try {
pruneEmptyDirectories(new File(workspace, moduleName));
} catch (IOException e) {
e.printStackTrace(listener.error("CVS empty directory cleanup failed: " + e.getMessage()));
return false;
}
}
return true;
} catch (CommandAbortedException e) {
e.printStackTrace(listener.error("CVS Command aborted: " + e.getMessage()));
return false;
Expand All @@ -285,6 +297,34 @@ public Boolean invoke(final File workspace, final VirtualChannel channel) throws
return true;
}

/**
* JENKINS-18390: work around buggy client.
* Cannot copy similarly-named method from {@link UpdateCommand} due to license mismatch.
* Cannot easily call the method reflectively on the {@link Command} since {@link CheckoutCommand} has a different signature for it.
* Pending a fix in the client library, do it ourselves when necessary.
*/
private static void pruneEmptyDirectories(File d) throws IOException {
File[] kids = d.listFiles();
if (kids == null) {
throw new IOException("could not examine " + d);
}
for (File kid : kids) {
if (!kid.isDirectory()) {
continue;
}
if (!new File(kid, "CVS").isDirectory()) {
// not CVS-controlled, ignore
continue;
}
pruneEmptyDirectories(kid);
File[] subkids = kid.listFiles();
if (subkids != null && subkids.length == 1) {
// Just CVS.
Util.deleteRecursive(kid);
}
}
}

/**
* Gets an instance of the CVS client that can be used for connection to a repository. If the
* repository specifies a password then the client's connection will be set with this password.
Expand Down

1 comment on commit b1f5341

@jglick
Copy link
Member

@jglick jglick commented on b1f5341 Sep 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has been a while. Any plans to release an update to this plugin?

Please sign in to comment.