Search in sources :

Example 6 with CheckOutScmResult

use of org.apache.maven.scm.command.checkout.CheckOutScmResult in project maven-scm by apache.

the class PerforceCheckOutCommand method executeCheckOutCommand.

/**
 * Check out the depot code at <code>repo.getPath()</code> into the target
 * directory at <code>files.getBasedir</code>. Perforce does not support
 * arbitrary checkout of versioned source so we need to set up a well-known
 * clientspec which will hold the required info.
 * <p/>
 * 1) A clientspec will be created or updated which holds a temporary
 * mapping from the repo path to the target directory.
 * 2) This clientspec is sync'd to pull all the files onto the client
 * <p/>
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet files, ScmVersion version, boolean recursive, boolean shallow) throws ScmException {
    PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
    File workingDirectory = new File(files.getBasedir().getAbsolutePath());
    actualLocation = PerforceScmProvider.getRepoPath(getLogger(), prepo, files.getBasedir());
    String specname = PerforceScmProvider.getClientspecName(getLogger(), prepo, workingDirectory);
    PerforceCheckOutConsumer consumer = new PerforceCheckOutConsumer(specname, actualLocation);
    if (getLogger().isInfoEnabled()) {
        getLogger().info("Checkout working directory: " + workingDirectory);
    }
    Commandline cl = null;
    // Create or update a clientspec so we can checkout the code to a particular location
    try {
        // Ahhh, glorious Perforce.  Create and update of clientspecs is the exact
        // same operation so we don't need to distinguish between the two modes.
        cl = PerforceScmProvider.createP4Command(prepo, workingDirectory);
        cl.createArg().setValue("client");
        cl.createArg().setValue("-i");
        if (getLogger().isInfoEnabled()) {
            getLogger().info("Executing: " + PerforceScmProvider.clean(cl.toString()));
        }
        String client = PerforceScmProvider.createClientspec(getLogger(), prepo, workingDirectory, actualLocation);
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Updating clientspec:\n" + client);
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        int exitCode = CommandLineUtils.executeCommandLine(cl, new ByteArrayInputStream(client.getBytes()), consumer, err);
        if (exitCode != 0) {
            String cmdLine = CommandLineUtils.toString(cl.getCommandline());
            StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
            msg.append('\n');
            msg.append("Command line was:" + cmdLine);
            throw new CommandLineException(msg.toString());
        }
    } catch (CommandLineException e) {
        if (getLogger().isErrorEnabled()) {
            getLogger().error("CommandLineException " + e.getMessage(), e);
        }
    }
    boolean clientspecExists = consumer.isSuccess();
    // Perform the actual checkout using that clientspec
    try {
        if (clientspecExists) {
            try {
                getLastChangelist(prepo, workingDirectory, specname);
                cl = createCommandLine(prepo, workingDirectory, version, specname);
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Executing: " + PerforceScmProvider.clean(cl.toString()));
                }
                Process proc = cl.execute();
                BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("Consuming: " + line);
                    }
                    consumer.consumeLine(line);
                }
                CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
                int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, err);
                if (exitCode != 0) {
                    String cmdLine = CommandLineUtils.toString(cl.getCommandline());
                    StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
                    msg.append('\n');
                    msg.append("Command line was:" + cmdLine);
                    throw new CommandLineException(msg.toString());
                }
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Perforce sync complete.");
                }
            } catch (CommandLineException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("CommandLineException " + e.getMessage(), e);
                }
            } catch (IOException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("IOException " + e.getMessage(), e);
                }
            }
        }
        if (consumer.isSuccess()) {
            return new CheckOutScmResult(cl.toString(), consumer.getCheckedout());
        } else {
            return new CheckOutScmResult(cl.toString(), "Unable to sync.  Are you logged in?", consumer.getOutput(), consumer.isSuccess());
        }
    } finally {
        // Support transient clientspecs as we don't want to create 1000s of permanent clientspecs
        if (clientspecExists && !prepo.isPersistCheckout()) {
            // Delete the clientspec
            InputStreamReader isReader = null;
            InputStreamReader isReaderErr = null;
            try {
                cl = PerforceScmProvider.createP4Command(prepo, workingDirectory);
                cl.createArg().setValue("client");
                cl.createArg().setValue("-d");
                cl.createArg().setValue(specname);
                if (getLogger().isInfoEnabled()) {
                    getLogger().info("Executing: " + PerforceScmProvider.clean(cl.toString()));
                }
                Process proc = cl.execute();
                isReader = new InputStreamReader(proc.getInputStream());
                BufferedReader br = new BufferedReader(isReader);
                String line;
                while ((line = br.readLine()) != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("Consuming: " + line);
                    }
                    consumer.consumeLine(line);
                }
                br.close();
                // Read errors from STDERR
                isReaderErr = new InputStreamReader(proc.getErrorStream());
                BufferedReader brErr = new BufferedReader(isReaderErr);
                while ((line = brErr.readLine()) != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("Consuming stderr: " + line);
                    }
                    consumer.consumeLine(line);
                }
                brErr.close();
            } catch (CommandLineException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("CommandLineException " + e.getMessage(), e);
                }
            } catch (IOException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("IOException " + e.getMessage(), e);
                }
            } finally {
                IOUtil.close(isReader);
                IOUtil.close(isReaderErr);
            }
        } else if (clientspecExists) {
            // SCM-165 Save clientspec in memory so we can reuse it with further commands in this VM.
            System.setProperty(PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY, specname);
        }
    }
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) ByteArrayInputStream(java.io.ByteArrayInputStream) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) BufferedReader(java.io.BufferedReader) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) PerforceScmProviderRepository(org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository) File(java.io.File)

Example 7 with CheckOutScmResult

use of org.apache.maven.scm.command.checkout.CheckOutScmResult in project maven-scm by apache.

the class PerforceUpdateCommand method executeUpdateCommand.

/**
 * {@inheritDoc}
 */
protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet files, ScmVersion scmVersion) throws ScmException {
    // In Perforce, there is no difference between update and checkout.
    // Here we just run the checkout command and map the result onto an
    // UpdateScmResult.
    PerforceCheckOutCommand command = new PerforceCheckOutCommand();
    command.setLogger(getLogger());
    CommandParameters params = new CommandParameters();
    params.setScmVersion(CommandParameter.SCM_VERSION, scmVersion);
    CheckOutScmResult cosr = (CheckOutScmResult) command.execute(repo, files, params);
    if (!cosr.isSuccess()) {
        return new UpdateScmResult(cosr.getCommandLine(), cosr.getProviderMessage(), cosr.getCommandOutput(), false);
    }
    PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
    String clientspec = PerforceScmProvider.getClientspecName(getLogger(), p4repo, files.getBasedir());
    Commandline cl = createCommandLine(p4repo, files.getBasedir(), clientspec);
    @SuppressWarnings("unused") String location = PerforceScmProvider.getRepoPath(getLogger(), p4repo, files.getBasedir());
    PerforceHaveConsumer consumer = new PerforceHaveConsumer(getLogger());
    try {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(PerforceScmProvider.clean("Executing " + cl.toString()));
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, err);
        if (exitCode != 0) {
            String cmdLine = CommandLineUtils.toString(cl.getCommandline());
            StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
            msg.append('\n');
            msg.append("Command line was:" + cmdLine);
            throw new CommandLineException(msg.toString());
        }
    } catch (CommandLineException e) {
        if (getLogger().isErrorEnabled()) {
            getLogger().error("CommandLineException " + e.getMessage(), e);
        }
    }
    return new UpdateScmResultWithRevision(cosr.getCommandLine(), cosr.getCheckedOutFiles(), String.valueOf(consumer.getHave()));
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) UpdateScmResultWithRevision(org.apache.maven.scm.command.update.UpdateScmResultWithRevision) UpdateScmResult(org.apache.maven.scm.command.update.UpdateScmResult) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) CommandParameters(org.apache.maven.scm.CommandParameters) PerforceScmProviderRepository(org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository) PerforceCheckOutCommand(org.apache.maven.scm.provider.perforce.command.checkout.PerforceCheckOutCommand)

Example 8 with CheckOutScmResult

use of org.apache.maven.scm.command.checkout.CheckOutScmResult in project maven-scm by apache.

the class LocalScmProvider method checkout.

/**
 * {@inheritDoc}
 */
public CheckOutScmResult checkout(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
    LocalCheckOutCommand command = new LocalCheckOutCommand();
    command.setLogger(getLogger());
    return (CheckOutScmResult) command.execute(repository, fileSet, parameters);
}
Also used : CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) LocalCheckOutCommand(org.apache.maven.scm.provider.local.command.checkout.LocalCheckOutCommand)

Example 9 with CheckOutScmResult

use of org.apache.maven.scm.command.checkout.CheckOutScmResult in project maven-scm by apache.

the class ClearCaseScmProvider method checkout.

/**
 * {@inheritDoc}
 */
public CheckOutScmResult checkout(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
    ClearCaseCheckOutCommand command = new ClearCaseCheckOutCommand();
    command.setLogger(getLogger());
    command.setSettings(settings);
    return (CheckOutScmResult) command.execute(repository, fileSet, parameters);
}
Also used : ClearCaseCheckOutCommand(org.apache.maven.scm.provider.clearcase.command.checkout.ClearCaseCheckOutCommand) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult)

Example 10 with CheckOutScmResult

use of org.apache.maven.scm.command.checkout.CheckOutScmResult in project maven-scm by apache.

the class HgCheckOutCommand method executeCheckOutCommand.

/**
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion scmVersion, boolean recursive, boolean shallow) throws ScmException {
    HgScmProviderRepository repository = (HgScmProviderRepository) repo;
    String url = repository.getURI();
    File checkoutDir = fileSet.getBasedir();
    try {
        if (getLogger().isInfoEnabled()) {
            getLogger().info("Removing " + checkoutDir);
        }
        FileUtils.deleteDirectory(checkoutDir);
    } catch (IOException e) {
        throw new ScmException("Cannot remove " + checkoutDir);
    }
    // Do the actual checkout
    List<String> cmdList = new ArrayList<String>();
    if (repo.isPushChanges()) {
        cmdList.add(HgCommandConstants.CLONE_CMD);
    } else {
        cmdList.add(HgCommandConstants.UPDATE_CMD);
    }
    if (scmVersion != null && !StringUtils.isEmpty(scmVersion.getName())) {
        cmdList.add(HgCommandConstants.REVISION_OPTION);
        cmdList.add(scmVersion.getName());
    }
    if (!repo.isPushChanges()) {
        cmdList.add(HgCommandConstants.CLEAN_OPTION);
    }
    cmdList.add(url);
    cmdList.add(checkoutDir.getAbsolutePath());
    String[] checkoutCmd = cmdList.toArray(new String[0]);
    HgConsumer checkoutConsumer = new HgConsumer(getLogger());
    HgUtils.execute(checkoutConsumer, getLogger(), checkoutDir.getParentFile(), checkoutCmd);
    // Do inventory to find list of checkedout files
    String[] inventoryCmd = new String[] { HgCommandConstants.INVENTORY_CMD };
    HgCheckOutConsumer consumer = new HgCheckOutConsumer(getLogger(), checkoutDir);
    ScmResult result = HgUtils.execute(consumer, getLogger(), checkoutDir, inventoryCmd);
    return new CheckOutScmResult(consumer.getCheckedOutFiles(), result);
}
Also used : ScmException(org.apache.maven.scm.ScmException) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) ScmResult(org.apache.maven.scm.ScmResult) HgConsumer(org.apache.maven.scm.provider.hg.command.HgConsumer) ArrayList(java.util.ArrayList) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) IOException(java.io.IOException) HgScmProviderRepository(org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository) File(java.io.File)

Aggregations

CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)57 File (java.io.File)24 ScmFileSet (org.apache.maven.scm.ScmFileSet)20 ScmFile (org.apache.maven.scm.ScmFile)14 ScmException (org.apache.maven.scm.ScmException)11 ScmTag (org.apache.maven.scm.ScmTag)9 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)9 Commandline (org.codehaus.plexus.util.cli.Commandline)8 Test (org.junit.Test)8 CommandParameters (org.apache.maven.scm.CommandParameters)7 AbstractAccuRevCommandTest (org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommandTest)7 IOException (java.io.IOException)6 ScmFileMatcher.assertHasScmFile (org.apache.maven.scm.ScmFileMatcher.assertHasScmFile)6 CheckInScmResult (org.apache.maven.scm.command.checkin.CheckInScmResult)6 ScmManager (org.apache.maven.scm.manager.ScmManager)5 ScmRepository (org.apache.maven.scm.repository.ScmRepository)5 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)5 ArrayList (java.util.ArrayList)4 ScmResult (org.apache.maven.scm.ScmResult)4 AddScmResult (org.apache.maven.scm.command.add.AddScmResult)3