/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command.add;
import java.io.IOException;
import java.util.List;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
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 org.codehaus.plexus.util.StringUtils;
import com.perforce.maven.scm.provider.p4.command.P4Command;
import com.perforce.maven.scm.provider.p4.util.P4Utils;
import com.perforce.p4java.client.IClient;
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";
private IClient client;
public void setClient( IClient client )
{
this.client = client;
}
/**
* @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();
// Default the "includes" to the "**" pattern
if ( files.getFileList().isEmpty() && StringUtils.isBlank( files.getIncludes() ) )
{
try
{
// FIXME, comply with P4IGNORE
files = new ScmFileSet( files.getBasedir(), "**", files.getExcludes() );
}
catch ( IOException e )
{
if ( getLogger().isErrorEnabled() )
{
getLogger().error( e );
}
}
}
List<IFileSpec> fileSpecs = P4Utils.scmFileSet2P4FileSpecsWithNoP4WildCard( files );
// Note: add command doesn't support "..." as a filespec
if ( !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 = client.addFiles( fileSpecs, addFilesOptions );
// Process the result filespecs
p4Result.process( retFileSpecs );
// Process the result filespecs as additions
p4Result.processAdditions( retFileSpecs );
}
catch ( P4JavaException e )
{
throw new ScmException( e.getLocalizedMessage(), e );
}
}
List<ScmFile> additions = p4Result.getAdditions();
return new AddScmResult( command, additions );
}
}