Search in sources :

Example 1 with GitStatusConsumer

use of org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer in project maven-scm by apache.

the class GitCheckInCommand method executeCheckInCommand.

/**
 * {@inheritDoc}
 */
protected CheckInScmResult executeCheckInCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
    int exitCode;
    File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
    try {
        FileUtils.fileWrite(messageFile.getAbsolutePath(), message);
    } catch (IOException ex) {
        return new CheckInScmResult(null, "Error while making a temporary file for the commit message: " + ex.getMessage(), null, false);
    }
    try {
        if (!fileSet.getFileList().isEmpty()) {
            // if specific fileSet is given, we have to git-add them first
            // otherwise we will use 'git-commit -a' later
            Commandline clAdd = GitAddCommand.createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
            exitCode = GitCommandLineUtils.execute(clAdd, stdout, stderr, getLogger());
            if (exitCode != 0) {
                return new CheckInScmResult(clAdd.toString(), "The git-add command failed.", stderr.getOutput(), false);
            }
        }
        // SCM-709: statusCommand uses repositoryRoot instead of workingDirectory, adjust it with
        // relativeRepositoryPath
        Commandline clRevparse = GitStatusCommand.createRevparseShowToplevelCommand(fileSet);
        stdout = new CommandLineUtils.StringStreamConsumer();
        stderr = new CommandLineUtils.StringStreamConsumer();
        URI relativeRepositoryPath = null;
        exitCode = GitCommandLineUtils.execute(clRevparse, stdout, stderr, getLogger());
        if (exitCode != 0) {
            // git-status returns non-zero if nothing to do
            if (getLogger().isInfoEnabled()) {
                getLogger().info("Could not resolve toplevel");
            }
        } else {
            relativeRepositoryPath = GitStatusConsumer.resolveURI(stdout.getOutput().trim(), fileSet.getBasedir().toURI());
        }
        // git-commit doesn't show single files, but only summary :/
        // so we must run git-status and consume the output
        // borrow a few things from the git-status command
        Commandline clStatus = GitStatusCommand.createCommandLine(repository, fileSet);
        GitStatusConsumer statusConsumer = new GitStatusConsumer(getLogger(), fileSet.getBasedir(), relativeRepositoryPath);
        exitCode = GitCommandLineUtils.execute(clStatus, statusConsumer, stderr, getLogger());
        if (exitCode != 0) {
            // git-status returns non-zero if nothing to do
            if (getLogger().isInfoEnabled()) {
                getLogger().info("nothing added to commit but untracked files present (use \"git add\" to " + "track)");
            }
        }
        if (statusConsumer.getChangedFiles().isEmpty()) {
            return new CheckInScmResult(null, statusConsumer.getChangedFiles());
        }
        Commandline clCommit = createCommitCommandLine(repository, fileSet, messageFile);
        exitCode = GitCommandLineUtils.execute(clCommit, stdout, stderr, getLogger());
        if (exitCode != 0) {
            return new CheckInScmResult(clCommit.toString(), "The git-commit command failed.", stderr.getOutput(), false);
        }
        if (repo.isPushChanges()) {
            Commandline cl = createPushCommandLine(getLogger(), repository, fileSet, version);
            exitCode = GitCommandLineUtils.execute(cl, stdout, stderr, getLogger());
            if (exitCode != 0) {
                return new CheckInScmResult(cl.toString(), "The git-push command failed.", stderr.getOutput(), false);
            }
        }
        List<ScmFile> checkedInFiles = new ArrayList<ScmFile>(statusConsumer.getChangedFiles().size());
        // rewrite all detected files to now have status 'checked_in'
        for (ScmFile changedFile : statusConsumer.getChangedFiles()) {
            ScmFile scmfile = new ScmFile(changedFile.getPath(), ScmFileStatus.CHECKED_IN);
            if (fileSet.getFileList().isEmpty()) {
                checkedInFiles.add(scmfile);
            } else {
                // if a specific fileSet is given, we have to check if the file is really tracked
                for (File f : fileSet.getFileList()) {
                    if (FilenameUtils.separatorsToUnix(f.getPath()).equals(scmfile.getPath())) {
                        checkedInFiles.add(scmfile);
                    }
                }
            }
        }
        return new CheckInScmResult(clCommit.toString(), checkedInFiles);
    } finally {
        try {
            FileUtils.forceDelete(messageFile);
        } catch (IOException ex) {
        // ignore
        }
    }
}
Also used : GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) Commandline(org.codehaus.plexus.util.cli.Commandline) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CheckInScmResult(org.apache.maven.scm.command.checkin.CheckInScmResult) URI(java.net.URI) ScmFile(org.apache.maven.scm.ScmFile) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) GitStatusConsumer(org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File)

Example 2 with GitStatusConsumer

use of org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer in project maven-scm by apache.

the class GitAddCommand method executeAddCommand.

/**
 * {@inheritDoc}
 */
protected ScmResult executeAddCommand(ScmProviderRepository repo, ScmFileSet fileSet, String message, boolean binary) throws ScmException {
    GitScmProviderRepository repository = (GitScmProviderRepository) repo;
    if (fileSet.getFileList().isEmpty()) {
        throw new ScmException("You must provide at least one file/directory to add");
    }
    AddScmResult result = executeAddFileSet(fileSet);
    if (result != null) {
        return result;
    }
    // SCM-709: statusCommand uses repositoryRoot instead of workingDirectory, adjust it with relativeRepositoryPath
    Commandline clRevparse = GitStatusCommand.createRevparseShowToplevelCommand(fileSet);
    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    URI relativeRepositoryPath = null;
    int exitCode;
    exitCode = GitCommandLineUtils.execute(clRevparse, stdout, stderr, getLogger());
    if (exitCode != 0) {
        // git-status returns non-zero if nothing to do
        if (getLogger().isInfoEnabled()) {
            getLogger().info("Could not resolve toplevel");
        }
    } else {
        relativeRepositoryPath = GitStatusConsumer.resolveURI(stdout.getOutput().trim(), fileSet.getBasedir().toURI());
    }
    // git-add doesn't show single files, but only summary :/
    // so we must run git-status and consume the output
    // borrow a few things from the git-status command
    Commandline clStatus = GitStatusCommand.createCommandLine(repository, fileSet);
    GitStatusConsumer statusConsumer = new GitStatusConsumer(getLogger(), fileSet.getBasedir(), relativeRepositoryPath);
    stderr = new CommandLineUtils.StringStreamConsumer();
    exitCode = GitCommandLineUtils.execute(clStatus, statusConsumer, stderr, getLogger());
    if (exitCode != 0) {
        // git-status returns non-zero if nothing to do
        if (getLogger().isInfoEnabled()) {
            getLogger().info("nothing added to commit but untracked files present (use \"git add\" to track)");
        }
    }
    List<ScmFile> changedFiles = new ArrayList<ScmFile>();
    // rewrite all detected files to now have status 'checked_in'
    for (ScmFile scmfile : statusConsumer.getChangedFiles()) {
        // if a specific fileSet is given, we have to check if the file is really tracked
        for (File f : fileSet.getFileList()) {
            if (FilenameUtils.separatorsToUnix(f.getPath()).equals(scmfile.getPath())) {
                changedFiles.add(scmfile);
            }
        }
    }
    Commandline cl = createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
    return new AddScmResult(cl.toString(), changedFiles);
}
Also used : ScmException(org.apache.maven.scm.ScmException) GitScmProviderRepository(org.apache.maven.scm.provider.git.repository.GitScmProviderRepository) Commandline(org.codehaus.plexus.util.cli.Commandline) AddScmResult(org.apache.maven.scm.command.add.AddScmResult) ArrayList(java.util.ArrayList) URI(java.net.URI) ScmFile(org.apache.maven.scm.ScmFile) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) GitStatusConsumer(org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File)

Aggregations

File (java.io.File)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 ScmFile (org.apache.maven.scm.ScmFile)2 GitCommandLineUtils (org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils)2 GitStatusConsumer (org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer)2 GitScmProviderRepository (org.apache.maven.scm.provider.git.repository.GitScmProviderRepository)2 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)2 Commandline (org.codehaus.plexus.util.cli.Commandline)2 IOException (java.io.IOException)1 ScmException (org.apache.maven.scm.ScmException)1 AddScmResult (org.apache.maven.scm.command.add.AddScmResult)1 CheckInScmResult (org.apache.maven.scm.command.checkin.CheckInScmResult)1