Search in sources :

Example 1 with GitListConsumer

use of org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer in project maven-scm by apache.

the class GitTagCommand method executeTagCommand.

/**
 * {@inheritDoc}
 */
public ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters) throws ScmException {
    if (tag == null || StringUtils.isEmpty(tag.trim())) {
        throw new ScmException("tag name must be specified");
    }
    if (!fileSet.getFileList().isEmpty()) {
        throw new ScmException("This provider doesn't support tagging subsets of a directory");
    }
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
    try {
        FileUtils.fileWrite(messageFile.getAbsolutePath(), scmTagParameters.getMessage());
    } catch (IOException ex) {
        return new TagScmResult(null, "Error while making a temporary file for the commit message: " + ex.getMessage(), null, false);
    }
    try {
        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
        int exitCode;
        Commandline clTag = createCommandLine(repository, fileSet.getBasedir(), tag, messageFile);
        exitCode = GitCommandLineUtils.execute(clTag, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new TagScmResult(clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false);
        }
        if (repo.isPushChanges()) {
            // and now push the tag to the configured upstream repository
            Commandline clPush = createPushCommandLine(repository, fileSet, tag);
            exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr, getLogger());
            if (exitCode != 0) {
                return new TagScmResult(clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
            }
        }
        // plus search for the tagged files
        GitListConsumer listConsumer = new GitListConsumer(getLogger(), fileSet.getBasedir(), ScmFileStatus.TAGGED);
        Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
        exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckOutScmResult(clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
        }
        return new TagScmResult(clTag.toString(), listConsumer.getListedFiles());
    } finally {
        try {
            FileUtils.forceDelete(messageFile);
        } catch (IOException ex) {
        // ignore
        }
    }
}
Also used : ScmException(org.apache.maven.scm.ScmException) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) Commandline(org.codehaus.plexus.util.cli.Commandline) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) GitListConsumer(org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) IOException(java.io.IOException) TagScmResult(org.apache.maven.scm.command.tag.TagScmResult) File(java.io.File)

Example 2 with GitListConsumer

use of org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer in project maven-scm by apache.

the class GitCheckOutCommand method executeCheckOutCommand.

/**
 * For git, the given repository is a remote one.
 * We have to clone it first if the working directory does not contain a git repo yet,
 * otherwise we have to git-pull it.
 * <p/>
 * TODO We currently assume a '.git' directory, so this does not work for --bare repos
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version, boolean recursive, boolean shallow) throws ScmException {
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    if (GitScmProviderRepository.PROTOCOL_FILE.equals(repository.getFetchInfo().getProtocol()) && repository.getFetchInfo().getPath().indexOf(fileSet.getBasedir().getPath()) >= 0) {
        throw new ScmException("remote repository must not be the working directory");
    }
    int exitCode;
    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    String lastCommandLine = "git-nothing-to-do";
    if (!fileSet.getBasedir().exists() || !(new File(fileSet.getBasedir(), ".git").exists())) {
        if (fileSet.getBasedir().exists()) {
            // git refuses to clone otherwise
            fileSet.getBasedir().delete();
        }
        // no git repo seems to exist, let's clone the original repo
        Commandline clClone = createCloneCommand(repository, fileSet.getBasedir(), version, shallow);
        exitCode = GitCommandLineUtils.execute(clClone, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckOutScmResult(clClone.toString(), "The git-clone command failed.", stderr.getOutput(), false);
        }
        lastCommandLine = clClone.toString();
    }
    GitRemoteInfoCommand gitRemoteInfoCommand = new GitRemoteInfoCommand();
    gitRemoteInfoCommand.setLogger(getLogger());
    RemoteInfoScmResult result = gitRemoteInfoCommand.executeRemoteInfoCommand(repository, null, null);
    if (fileSet.getBasedir().exists() && new File(fileSet.getBasedir(), ".git").exists() && result.getBranches().size() > 0) {
        // git repo exists, so we must git-pull the changes
        Commandline clPull = createPullCommand(repository, fileSet.getBasedir(), version);
        exitCode = GitCommandLineUtils.execute(clPull, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckOutScmResult(clPull.toString(), "The git-pull command failed.", stderr.getOutput(), false);
        }
        lastCommandLine = clPull.toString();
        // and now lets do the git-checkout itself
        Commandline clCheckout = createCommandLine(repository, fileSet.getBasedir(), version);
        exitCode = GitCommandLineUtils.execute(clCheckout, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckOutScmResult(clCheckout.toString(), "The git-checkout command failed.", stderr.getOutput(), false);
        }
        lastCommandLine = clCheckout.toString();
    }
    // and now search for the files
    GitListConsumer listConsumer = new GitListConsumer(getLogger(), fileSet.getBasedir(), ScmFileStatus.CHECKED_IN);
    Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
    exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr, getLogger());
    if (exitCode != 0) {
        return new CheckOutScmResult(clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
    }
    return new CheckOutScmResult(lastCommandLine, listConsumer.getListedFiles());
}
Also used : ScmException(org.apache.maven.scm.ScmException) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) Commandline(org.codehaus.plexus.util.cli.Commandline) GitRemoteInfoCommand(org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand) RemoteInfoScmResult(org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) GitListConsumer(org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) File(java.io.File)

Example 3 with GitListConsumer

use of org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer in project maven-scm by apache.

the class GitBranchCommand method executeBranchCommand.

/**
 * {@inheritDoc}
 */
public ScmResult executeBranchCommand(ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message) throws ScmException {
    if (branch == null || StringUtils.isEmpty(branch.trim())) {
        throw new ScmException("branch name must be specified");
    }
    if (!fileSet.getFileList().isEmpty()) {
        throw new ScmException("This provider doesn't support branching subsets of a directory");
    }
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    Commandline cl = createCommandLine(repository, fileSet.getBasedir(), branch);
    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    int exitCode;
    exitCode = GitCommandLineUtils.execute(cl, stdout, stderr, getLogger());
    if (exitCode != 0) {
        return new BranchScmResult(cl.toString(), "The git-branch command failed.", stderr.getOutput(), false);
    }
    if (repo.isPushChanges()) {
        // and now push the branch to the upstream repository
        Commandline clPush = createPushCommandLine(repository, fileSet, branch);
        exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new BranchScmResult(clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
        }
    }
    // as last action we search for the branched files
    GitListConsumer listConsumer = new GitListConsumer(getLogger(), fileSet.getBasedir(), ScmFileStatus.TAGGED);
    Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir());
    exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr, getLogger());
    if (exitCode != 0) {
        return new BranchScmResult(clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false);
    }
    return new BranchScmResult(cl.toString(), listConsumer.getListedFiles());
}
Also used : ScmException(org.apache.maven.scm.ScmException) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) Commandline(org.codehaus.plexus.util.cli.Commandline) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) GitListConsumer(org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer) BranchScmResult(org.apache.maven.scm.command.branch.BranchScmResult)

Aggregations

ScmException (org.apache.maven.scm.ScmException)3 GitCommandLineUtils (org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils)3 GitListConsumer (org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer)3 GitScmProviderRepository (org.apache.maven.scm.provider.git.repository.GitScmProviderRepository)3 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)3 Commandline (org.codehaus.plexus.util.cli.Commandline)3 File (java.io.File)2 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)2 IOException (java.io.IOException)1 BranchScmResult (org.apache.maven.scm.command.branch.BranchScmResult)1 RemoteInfoScmResult (org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult)1 TagScmResult (org.apache.maven.scm.command.tag.TagScmResult)1 GitRemoteInfoCommand (org.apache.maven.scm.provider.git.gitexe.command.remoteinfo.GitRemoteInfoCommand)1