/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command.branch;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
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.branch.AbstractBranchCommand;
import org.apache.maven.scm.command.branch.BranchScmResult;
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.ChangelistStatus;
import com.perforce.p4java.core.IBranchMapping;
import com.perforce.p4java.core.IBranchSpec;
import com.perforce.p4java.core.IChangelist;
import com.perforce.p4java.core.ViewMap;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.generic.core.BranchSpec;
import com.perforce.p4java.impl.generic.core.Changelist;
import com.perforce.p4java.impl.mapbased.server.Server;
import com.perforce.p4java.option.changelist.SubmitOptions;
import com.perforce.p4java.option.client.IntegrateFilesOptions;
import com.perforce.p4java.option.client.ResolveFilesAutoOptions;
/**
* The class uses the Perforce integrate command to prepares the propagation of
* change from source files to target files
*/
public class P4BranchCommand extends AbstractBranchCommand implements P4Command {
/** The Constant command. */
public static final String command = "Perforce integrate command";
/**
* @see org.apache.maven.scm.command.branch.AbstractBranchCommand#executeBranchCommand
* (org.apache.maven.scm.provider.ScmProviderRepository,
* org.apache.maven.scm.ScmFileSet, java.lang.String, java.lang.String)
*/
protected ScmResult executeBranchCommand(ScmProviderRepository repo,
ScmFileSet files, String branchName, String message)
throws ScmException {
if (branchName == null || P4Executor.isEmpty(branchName.trim())) {
throw new ScmException("branch name must be specified");
}
P4BranchResult p4Result = new P4BranchResult();
P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
P4ClientExecutor executor = new P4ClientExecutor(p4Repo, files,
getLogger());
String fromRoot = executor.getRepoLocation(P4Executor
.encodeWildcards(files.getBasedir().getAbsolutePath()));
String left = executor.getRepoLocation(files.getBasedir()
.getAbsolutePath());
String branchDepotBasePath = left.substring(0,
fromRoot.lastIndexOf("/"));
String right = branchDepotBasePath + "/" + branchName;
String leftRepoPath = P4Executor.getCanonicalRepoPath(left);
if (leftRepoPath.contains(" ")) {
leftRepoPath = "\"" + leftRepoPath + "\"";
}
String rightRepoPath = P4Executor.getCanonicalRepoPath(right);
if (rightRepoPath.contains(" ")) {
rightRepoPath = "\"" + rightRepoPath + "\"";
}
try {
ViewMap<IBranchMapping> viewMap = new ViewMap<IBranchMapping>();
viewMap.addEntry(new BranchSpec.BranchViewMapping(0,
P4Executor.encodeWildcards(leftRepoPath) + " "
+ P4Executor.encodeWildcards(rightRepoPath)));
String encodedBranchName = P4Executor
.encodeWildcards(branchName.trim().replaceAll("\\s+", "_"));
IBranchSpec newBranch = new BranchSpec(encodedBranchName,
executor.getP4User(), branchName, false, null, null,
viewMap);
String createResult = executor.getServer().createBranchSpec(
newBranch);
if (getLogger().isDebugEnabled()) {
getLogger().debug(createResult);
}
IBranchSpec branchSpec = executor.getServer().getBranchSpec(
encodedBranchName);
if (branchSpec == null) {
throw new ScmException("Fail to create branch spec.");
}
Changelist newChangelistImpl = new Changelist(IChangelist.UNKNOWN,
executor.getClient().getName(),
executor.getP4User(), ChangelistStatus.NEW,
new Date(), message, false,
(Server) executor.getServer());
IChangelist retChangelist = executor.getClient()
.createChangelist(newChangelistImpl);
retChangelist = executor.getServer().getChangelist(
retChangelist.getId());
IntegrateFilesOptions integrateFilesOptions = new IntegrateFilesOptions();
integrateFilesOptions.setChangelistId(retChangelist.getId());
List<IFileSpec> retFileSpecs = executor.getClient()
.integrateFiles(null, null, encodedBranchName,
integrateFilesOptions);
ResolveFilesAutoOptions resolveFilesAutoOptions = new ResolveFilesAutoOptions();
resolveFilesAutoOptions.setAcceptTheirs(true);
retFileSpecs = executor.getClient().resolveFilesAuto(
retFileSpecs, resolveFilesAutoOptions);
// Update the changelist
if (retChangelist.canUpdate()) {
retChangelist.update();
}
// Refresh the changelist
if (retChangelist.canRefresh()) {
retChangelist.refresh();
}
// Get the jobs for submit
List<String> jobs = getJobs(System
.getProperty(P4Executor.P4_JOBS_PROPERTY));
SubmitOptions submitOptions = new SubmitOptions();
if (jobs != null) {
submitOptions.setJobIds(jobs);
}
// Submit
retFileSpecs = retChangelist.submit(submitOptions);
// Process the result filespecs
p4Result.process(retFileSpecs);
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
return new BranchScmResult(command, p4Result.getCommandResult());
}
/**
* Gets the tokenized (by whitespace) jobs.
*
* @param jobs
* the jobs
* @return the jobs
*/
private List<String> getJobs(String jobs) {
if (jobs != null) {
String[] tokens = jobs.trim().split("\\s+");
if (tokens != null && tokens.length > 0) {
return new ArrayList<String>(Arrays.asList(tokens));
}
}
return null;
}
}
# |
Change |
User |
Description |
Committed |
|
#2
|
10055 |
Matt Attaway |
Pull over numerous changes from @dantran's branch. |
|
|
#1
|
8496 |
Matt Attaway |
Move P4Ant and P4Maven into their proper homes |
|
|
//public/perforce/p4maven/main/com.perforce.maven/src/main/java/com/perforce/maven/scm/provider/p4/command/branch/P4BranchCommand.java |
#1
|
8270 |
Matt Attaway |
Initial add of P4Maven source code |
|
|