/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command.list;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmVersion;
import org.apache.maven.scm.command.list.AbstractListCommand;
import org.apache.maven.scm.command.list.ListScmResult;
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.server.GetDepotFilesOptions;
/**
* The class uses the Perforce files command to list details about specified
* files: depot file name, revision, file, type, change action and changelist
* number of the current head revision.
*/
public class P4ListCommand extends AbstractListCommand implements P4Command {
/** The Constant command. */
public static final String command = "Perforce files command";
/**
* @see org.apache.maven.scm.command.list.AbstractListCommand
* executeListCommand(org.apache.maven.scm.provider.ScmProviderRepository,
* org.apache.maven.scm.ScmFileSet, boolean,
* org.apache.maven.scm.ScmVersion)
*/
protected ListScmResult executeListCommand(ScmProviderRepository repo,
ScmFileSet files, boolean recursive, ScmVersion scmVersion)
throws ScmException {
P4ListResult p4Result = new P4ListResult();
P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
P4ClientExecutor executor = new P4ClientExecutor(p4Repo, files,
getLogger());
List<IFileSpec> fileSpecs = null;
List<File> fileList = files.getFileList();
if (fileList != null) {
List<String> filePaths = new ArrayList<String>();
for (File f : fileList) {
if (!f.getName().contentEquals(".")) {
filePaths.add(P4Executor.encodeWildcards(f.getAbsolutePath()));
}
}
if (!filePaths.isEmpty()) {
fileSpecs = FileSpecBuilder
.makeFileSpecList(filePaths.toArray(new String[filePaths
.size()]));
}
}
if (fileSpecs == null || fileSpecs.isEmpty()) {
if (P4Executor.isEmpty(files.getIncludes()) && P4Executor.isEmpty(files.getExcludes())) {
StringBuffer filePath = new StringBuffer();
filePath.append("...");
fileSpecs = FileSpecBuilder.makeFileSpecList(new String[] { filePath.toString() });
}
}
if (fileSpecs != null && !fileSpecs.isEmpty()) {
try {
GetDepotFilesOptions getDepotFilesOptions = new GetDepotFilesOptions();
List<IFileSpec> retFileSpecs = executor.getServer()
.getDepotFiles(fileSpecs, getDepotFilesOptions);
p4Result.process(retFileSpecs);
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
if (!p4Result.isSuccess()) {
return new ListScmResult(command, "The files command failed.",
p4Result.getCommandOutput(), p4Result.isSuccess());
}
return new ListScmResult(command, p4Result.getCommandResult());
}
}