/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command.unedit;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.command.unedit.AbstractUnEditCommand;
import org.apache.maven.scm.command.unedit.UnEditScmResult;
import org.apache.maven.scm.provider.ScmProviderRepository;
import com.perforce.maven.scm.provider.p4.command.P4ClientExecutor;
import com.perforce.maven.scm.provider.p4.command.P4Command;
import com.perforce.maven.scm.provider.p4.command.P4Executor;
import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.option.client.RevertFilesOptions;
/**
* The class uses the Perforce revert command to undo changes on opened files.
*/
public class P4UnEditCommand extends AbstractUnEditCommand implements P4Command {
/** The Constant command. */
public static final String command = "Perforce revert command";
/**
* @see org.apache.maven.scm.command.unedit.AbstractUnEditCommand
* executeUnEditCommand(org.apache.maven.scm.provider.ScmProviderRepository,
* org.apache.maven.scm.ScmFileSet)
*/
protected ScmResult executeUnEditCommand(ScmProviderRepository repo,
ScmFileSet files) throws ScmException {
P4UnEditResult p4Result = new P4UnEditResult();
P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
P4ClientExecutor executor = new P4ClientExecutor(p4Repo, files,
getLogger());
List<IFileSpec> fileSpecs = null;
try {
File workingDir = files.getBasedir();
String candir = workingDir.getCanonicalPath();
List<File> fileList = files.getFileList();
if (fileList != null) {
HashSet<String> filePaths = new HashSet<String>();
for (File file : fileList) {
if (!file.getName().contentEquals(".")) {
String canfile = file.getCanonicalPath();
if (canfile.startsWith(candir)) {
// Get the relative path
canfile = canfile.substring(candir.length() + 1);
}
filePaths.add(P4Executor.encodeWildcards(canfile));
}
}
if (!filePaths.isEmpty()) {
fileSpecs = FileSpecBuilder.makeFileSpecList(filePaths
.toArray(new String[filePaths.size()]));
}
}
} catch (IOException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
if (fileSpecs != null && !fileSpecs.isEmpty()) {
try {
RevertFilesOptions revertFilesOptions = new RevertFilesOptions();
List<IFileSpec> retFileSpecs = executor.getClient()
.revertFiles(fileSpecs, revertFilesOptions);
p4Result.process(retFileSpecs);
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
return new UnEditScmResult(
command,
p4Result.isSuccess() ? "Revert successful" : "Unable to revert",
p4Result.getCommandOutput(), p4Result.isSuccess());
}
}