Search in sources :

Example 1 with GitDiffConsumer

use of org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer in project maven-scm by apache.

the class JGitDiffCommand method callDiff.

public DiffScmResult callDiff(Git git, ScmVersion startRevision, ScmVersion endRevision) throws IOException, GitAPIException, ScmException {
    AbstractTreeIterator oldTree = null;
    if (startRevision != null && StringUtils.isNotEmpty(startRevision.getName().trim())) {
        String startRev = startRevision.getName().trim();
        oldTree = getTreeIterator(git.getRepository(), startRev);
    }
    AbstractTreeIterator newTree = null;
    if (endRevision != null && StringUtils.isNotEmpty(endRevision.getName().trim())) {
        String endRev = endRevision.getName().trim();
        newTree = getTreeIterator(git.getRepository(), endRev);
    }
    OutputStream out = new ByteArrayOutputStream();
    git.diff().setOutputStream(out).setOldTree(oldTree).setNewTree(newTree).setCached(false).call();
    git.diff().setOutputStream(out).setOldTree(oldTree).setNewTree(newTree).setCached(true).call();
    out.flush();
    GitDiffConsumer consumer = new GitDiffConsumer(getLogger(), null);
    String fullDiff = out.toString();
    out.close();
    String[] lines = fullDiff.split("\n");
    for (String aLine : lines) {
        consumer.consumeLine(aLine);
    }
    return new DiffScmResult("JGit diff", consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch());
}
Also used : AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) GitDiffConsumer(org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DiffScmResult(org.apache.maven.scm.command.diff.DiffScmResult)

Example 2 with GitDiffConsumer

use of org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer in project maven-scm by apache.

the class GitDiffCommand method executeDiffCommand.

/**
 * {@inheritDoc}
 */
protected DiffScmResult executeDiffCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion) throws ScmException {
    GitDiffConsumer consumer = new GitDiffConsumer(getLogger(), fileSet.getBasedir());
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    int exitCode;
    Commandline clDiff2Index = createCommandLine(fileSet.getBasedir(), startVersion, endVersion, false);
    exitCode = GitCommandLineUtils.execute(clDiff2Index, consumer, stderr, getLogger());
    if (exitCode != 0) {
        return new DiffScmResult(clDiff2Index.toString(), "The git-diff command failed.", stderr.getOutput(), false);
    }
    Commandline clDiff2Head = createCommandLine(fileSet.getBasedir(), startVersion, endVersion, true);
    exitCode = GitCommandLineUtils.execute(clDiff2Head, consumer, stderr, getLogger());
    if (exitCode != 0) {
        return new DiffScmResult(clDiff2Head.toString(), "The git-diff command failed.", stderr.getOutput(), false);
    }
    return new DiffScmResult(clDiff2Index.toString(), consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch());
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) GitDiffConsumer(org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer) GitCommandLineUtils(org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) DiffScmResult(org.apache.maven.scm.command.diff.DiffScmResult)

Aggregations

DiffScmResult (org.apache.maven.scm.command.diff.DiffScmResult)2 GitDiffConsumer (org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 GitCommandLineUtils (org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils)1 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)1 Commandline (org.codehaus.plexus.util.cli.Commandline)1 AbstractTreeIterator (org.eclipse.jgit.treewalk.AbstractTreeIterator)1