/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command.add;
import java.io.File;
import java.io.IOException;
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.ScmResult;
import org.apache.maven.scm.command.add.AbstractAddCommand;
import org.apache.maven.scm.command.add.AddScmResult;
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.AddFilesOptions;
/**
* The class uses the Perforce add command to open new files to add to the
* Perforce depot.
*/
public class P4AddCommand extends AbstractAddCommand implements P4Command {
/** The Constant command. */
public static final String command = "Perforce add command";
/**
* @see org.apache.maven.scm.command.add.AbstractAddCommand#executeAddCommand
* (org.apache.maven.scm.provider.ScmProviderRepository,
* org.apache.maven.scm.ScmFileSet, java.lang.String, boolean)
*/
protected ScmResult executeAddCommand(ScmProviderRepository repo,
ScmFileSet files, String message, boolean binary)
throws ScmException {
P4AddResult p4Result = new P4AddResult();
P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
P4ClientExecutor executor = new P4ClientExecutor(p4Repo, files,
getLogger());
// Default the "includes" to the "**" pattern
if (P4Executor.isEmpty(files.getIncludes())) {
try {
files = new ScmFileSet(files.getBasedir(), "**",
files.getExcludes());
} catch (IOException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
}
}
List<IFileSpec> fileSpecs = null;
@SuppressWarnings("unchecked")
List<File> fileList = files.getFileList();
if (fileList != null) {
List<String> filePaths = new ArrayList<String>();
for (File f : fileList) {
if (!f.getName().contentEquals(".")) { // Ignore the "." file
filePaths.add(f.getAbsolutePath());
}
}
if (!filePaths.isEmpty()) {
fileSpecs = FileSpecBuilder.makeFileSpecList(filePaths
.toArray(new String[filePaths.size()]));
}
}
// Note: add command doesn't support "..." as a filespec
if (fileSpecs != null && !fileSpecs.isEmpty()) {
AddFilesOptions addFilesOptions = new AddFilesOptions();
// Allow adding files with filenames that contain wildcard
// characters. Filenames that contain the special characters
// '@', '#', '%' or '*' are reformatted to encode the characters
// using ASCII hexadecimal representation. After the files are
// added, you must refer to them using the reformatted file
// name, because Perforce does not recognize the local
// filesystem name.
addFilesOptions.setUseWildcards(true);
try {
List<IFileSpec> retFileSpecs = executor.getClient().addFiles(
fileSpecs, addFilesOptions);
// Process the result filespecs
p4Result.process(retFileSpecs);
// Process the result filespecs as additions
p4Result.processAdditions(retFileSpecs);
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
List<String> additions = p4Result.getAdditions();
return new AddScmResult(command, additions);
}
}