Search in sources :

Example 1 with PerforceScmProviderRepository

use of org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository 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 2 with PerforceScmProviderRepository

use of org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository in project maven-scm by apache.

the class PerforceScmProvider method makeProviderScmRepository.

public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter) throws ScmRepositoryException {
    String protocol = null;
    String path;
    int port = 0;
    String host = null;
    // minimal logic to support perforce protocols in scm url, and keep the next part unchange
    int i0 = scmSpecificUrl.indexOf(delimiter);
    if (i0 > 0) {
        protocol = scmSpecificUrl.substring(0, i0);
        HashSet<String> protocols = new HashSet<String>(Arrays.asList(PROTOCOLS));
        if (protocols.contains(protocol)) {
            scmSpecificUrl = scmSpecificUrl.substring(i0 + 1);
        } else {
            protocol = null;
        }
    }
    int i1 = scmSpecificUrl.indexOf(delimiter);
    int i2 = scmSpecificUrl.indexOf(delimiter, i1 + 1);
    if (i1 > 0) {
        int lastDelimiter = scmSpecificUrl.lastIndexOf(delimiter);
        path = scmSpecificUrl.substring(lastDelimiter + 1);
        host = scmSpecificUrl.substring(0, i1);
        // If there is tree parts in the scm url, the second is the port
        if (i2 >= 0) {
            try {
                String tmp = scmSpecificUrl.substring(i1 + 1, lastDelimiter);
                port = Integer.parseInt(tmp);
            } catch (NumberFormatException ex) {
                throw new ScmRepositoryException("The port has to be a number.");
            }
        }
    } else {
        path = scmSpecificUrl;
    }
    String user = null;
    String password = null;
    if (host != null && host.indexOf('@') > 1) {
        user = host.substring(0, host.indexOf('@'));
        host = host.substring(host.indexOf('@') + 1);
    }
    if (path.indexOf('@') > 1) {
        if (host != null) {
            if (getLogger().isWarnEnabled()) {
                getLogger().warn("Username as part of path is deprecated, the new format is " + "scm:perforce:[username@]host:port:path_to_repository");
            }
        }
        user = path.substring(0, path.indexOf('@'));
        path = path.substring(path.indexOf('@') + 1);
    }
    return new PerforceScmProviderRepository(protocol, host, port, path, user, password);
}
Also used : ScmRepositoryException(org.apache.maven.scm.repository.ScmRepositoryException) PerforceScmProviderRepository(org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository) HashSet(java.util.HashSet)

Example 3 with PerforceScmProviderRepository

use of org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository in project maven-scm by apache.

the class PerforceBlameCommand method executeBlameCommand.

public BlameScmResult executeBlameCommand(ScmProviderRepository repo, ScmFileSet workingDirectory, String filename) throws ScmException {
    // Call annotate command
    PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
    String clientspec = PerforceScmProvider.getClientspecName(getLogger(), p4repo, workingDirectory.getBasedir());
    Commandline cl = createCommandLine((PerforceScmProviderRepository) repo, workingDirectory.getBasedir(), filename, clientspec);
    PerforceBlameConsumer blameConsumer = new PerforceBlameConsumer(getLogger());
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    int exitCode;
    try {
        exitCode = CommandLineUtils.executeCommandLine(cl, blameConsumer, stderr);
    } catch (CommandLineException ex) {
        throw new ScmException("Error while executing command.", ex);
    }
    if (exitCode != 0) {
        return new BlameScmResult(cl.toString(), "The perforce command failed.", stderr.getOutput(), false);
    }
    // Call filelog command
    cl = createFilelogCommandLine((PerforceScmProviderRepository) repo, workingDirectory.getBasedir(), filename, clientspec);
    PerforceFilelogConsumer filelogConsumer = new PerforceFilelogConsumer(getLogger());
    try {
        exitCode = CommandLineUtils.executeCommandLine(cl, filelogConsumer, stderr);
    } catch (CommandLineException ex) {
        throw new ScmException("Error while executing command.", ex);
    }
    if (exitCode != 0) {
        return new BlameScmResult(cl.toString(), "The perforce command failed.", stderr.getOutput(), false);
    }
    // Combine results
    List<BlameLine> lines = blameConsumer.getLines();
    for (int i = 0; i < lines.size(); i++) {
        BlameLine line = lines.get(i);
        String revision = line.getRevision();
        line.setAuthor(filelogConsumer.getAuthor(revision));
        line.setDate(filelogConsumer.getDate(revision));
    }
    return new BlameScmResult(cl.toString(), lines);
}
Also used : ScmException(org.apache.maven.scm.ScmException) Commandline(org.codehaus.plexus.util.cli.Commandline) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) BlameLine(org.apache.maven.scm.command.blame.BlameLine) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) BlameScmResult(org.apache.maven.scm.command.blame.BlameScmResult) PerforceScmProviderRepository(org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository)

Example 4 with PerforceScmProviderRepository

use of org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository in project maven-scm by apache.

the class PerforceChangeLogCommand method executeChangeLogCommand.

protected ChangeLogScmResult executeChangeLogCommand(ScmProviderRepository repo, ScmFileSet fileSet, Date startDate, Date endDate, ScmBranch branch, String datePattern, ScmVersion startVersion, ScmVersion endVersion) throws ScmException {
    PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
    String clientspec = PerforceScmProvider.getClientspecName(getLogger(), p4repo, fileSet.getBasedir());
    Commandline cl = createCommandLine(p4repo, fileSet.getBasedir(), clientspec, null, startDate, endDate, startVersion, endVersion);
    String location = PerforceScmProvider.getRepoPath(getLogger(), p4repo, fileSet.getBasedir());
    PerforceChangesConsumer consumer = new PerforceChangesConsumer(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);
        }
    }
    List<String> changes = consumer.getChanges();
    cl = PerforceScmProvider.createP4Command(p4repo, fileSet.getBasedir());
    cl.createArg().setValue("describe");
    cl.createArg().setValue("-s");
    for (String change : changes) {
        cl.createArg().setValue(change);
    }
    PerforceDescribeConsumer describeConsumer = new PerforceDescribeConsumer(location, datePattern, getLogger());
    try {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(PerforceScmProvider.clean("Executing " + cl.toString()));
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        int exitCode = CommandLineUtils.executeCommandLine(cl, describeConsumer, 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);
        }
    }
    ChangeLogSet cls = new ChangeLogSet(describeConsumer.getModifications(), null, null);
    cls.setStartVersion(startVersion);
    cls.setEndVersion(endVersion);
    return new ChangeLogScmResult(cl.toString(), cls);
}
Also used : ChangeLogSet(org.apache.maven.scm.command.changelog.ChangeLogSet) Commandline(org.codehaus.plexus.util.cli.Commandline) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) ChangeLogScmResult(org.apache.maven.scm.command.changelog.ChangeLogScmResult) PerforceScmProviderRepository(org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository)

Example 5 with PerforceScmProviderRepository

use of org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository 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)

Aggregations

PerforceScmProviderRepository (org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository)31 ScmRepository (org.apache.maven.scm.repository.ScmRepository)23 Commandline (org.codehaus.plexus.util.cli.Commandline)19 File (java.io.File)6 ScmFileSet (org.apache.maven.scm.ScmFileSet)5 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)5 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)5 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 CommandParameters (org.apache.maven.scm.CommandParameters)1 ScmException (org.apache.maven.scm.ScmException)1 ScmFile (org.apache.maven.scm.ScmFile)1 ScmRevision (org.apache.maven.scm.ScmRevision)1 BlameLine (org.apache.maven.scm.command.blame.BlameLine)1 BlameScmResult (org.apache.maven.scm.command.blame.BlameScmResult)1 ChangeLogScmResult (org.apache.maven.scm.command.changelog.ChangeLogScmResult)1