Search in sources :

Example 6 with DiffScmResult

use of org.apache.maven.scm.command.diff.DiffScmResult in project maven-scm by apache.

the class JazzScmProvider method diff.

/**
 * {@inheritDoc}
 */
protected DiffScmResult diff(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
    getLogger().debug("JazzScmProvider:diff()");
    JazzDiffCommand command = new JazzDiffCommand();
    command.setLogger(getLogger());
    return (DiffScmResult) command.execute(repository, fileSet, parameters);
}
Also used : JazzDiffCommand(org.apache.maven.scm.provider.jazz.command.diff.JazzDiffCommand) DiffScmResult(org.apache.maven.scm.command.diff.DiffScmResult)

Example 7 with DiffScmResult

use of org.apache.maven.scm.command.diff.DiffScmResult in project maven-scm by apache.

the class JazzDiffCommand method executeDiffCommand.

/**
 * {@inheritDoc}
 */
protected DiffScmResult executeDiffCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startRevision, ScmVersion endRevision) throws ScmException {
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Executing diff command...");
    }
    File baseDir = fileSet.getBasedir();
    File parentFolder = (baseDir.getParentFile() != null) ? baseDir.getParentFile() : baseDir;
    // First execute the status command to get the list of changed files.
    JazzStatusCommand statusCmd = new JazzStatusCommand();
    statusCmd.setLogger(getLogger());
    StatusScmResult statusCmdResult = statusCmd.executeStatusCommand(repo, fileSet);
    List<ScmFile> statusScmFiles = statusCmdResult.getChangedFiles();
    // In this case, we also use it across multiple calls to "scm diff" so that we
    // sum all output into on.
    JazzScmCommand diffCmd = null;
    StringBuilder patch = new StringBuilder();
    Map<String, CharSequence> differences = new HashMap<String, CharSequence>();
    // Now lets iterate through them
    for (ScmFile file : statusScmFiles) {
        if (file.getStatus() == ScmFileStatus.MODIFIED) {
            // The "scm status" command returns files relative to the sandbox root.
            // Whereas the "scm diff" command needs them relative to the working directory.
            File fullPath = new File(parentFolder, file.getPath());
            String relativePath = fullPath.toString().substring(baseDir.toString().length());
            getLogger().debug("Full Path     : '" + fullPath + "'");
            getLogger().debug("Relative Path : '" + relativePath + "'");
            // Now call "scm diff on it"
            // In this case, we use the DebugLoggerConsumer's ability to store captured output
            DebugLoggerConsumer diffConsumer = new DebugLoggerConsumer(getLogger());
            ErrorConsumer errConsumer = new ErrorConsumer(getLogger());
            diffCmd = createDiffCommand(repo, fileSet, relativePath);
            int status = diffCmd.execute(diffConsumer, errConsumer);
            if (status != 0) {
                // Return a false result (not the usual SCMResult)
                return new DiffScmResult(diffCmd.toString(), "The scm diff command failed.", errConsumer.getOutput(), false);
            }
            // Append to patch (all combined)
            patch.append(diffConsumer.getOutput());
            // Set the differences map <File, <CharSequence>
            differences.put(relativePath, diffConsumer.getOutput());
        }
    }
    return new DiffScmResult(diffCmd.toString(), statusCmdResult.getChangedFiles(), differences, patch.toString());
}
Also used : StatusScmResult(org.apache.maven.scm.command.status.StatusScmResult) HashMap(java.util.HashMap) ErrorConsumer(org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer) JazzScmCommand(org.apache.maven.scm.provider.jazz.command.JazzScmCommand) ScmFile(org.apache.maven.scm.ScmFile) JazzStatusCommand(org.apache.maven.scm.provider.jazz.command.status.JazzStatusCommand) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) DebugLoggerConsumer(org.apache.maven.scm.provider.jazz.command.consumer.DebugLoggerConsumer) DiffScmResult(org.apache.maven.scm.command.diff.DiffScmResult)

Example 8 with DiffScmResult

use of org.apache.maven.scm.command.diff.DiffScmResult in project maven-scm by apache.

the class HgDiffCommandTckTest method testDiffCommand.

public void testDiffCommand() throws Exception {
    ScmRepository repository = getScmRepository();
    // ----------------------------------------------------------------------
    // Change the files
    // ----------------------------------------------------------------------
    // 
    // readme.txt is changed (changed file in the root directory)
    // project.xml is added (added file in the root directory)
    // src/test/resources is untouched (a empty directory is left untouched)
    // src/test/java is untouched (a non empty directory is left untouched)
    // This following test has no meaning to mercurial as mercurial does not track
    // empty directories, only the files contained within
    // See: http://www.selenic.com/mercurial/wiki/index.cgi/FAQ
    // src/test/java/org (a empty directory is added)
    // src/main/java/org/Foo.java (a non empty directory is added)
    // 
    // /readme.txt
    ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
    // /project.xml
    ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
    addToWorkingTree(getWorkingCopy(), new File("project.xml"), repository);
    // /src/test/java/org
    // ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
    // 
    // addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"),
    // repository);
    // /src/main/java/org/Foo.java
    ScmTestCase.makeFile(getWorkingCopy(), "/src/main/java/org/Foo.java");
    // addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"),
    // repository);
    // src/main/java/org/Foo.java
    addToWorkingTree(getWorkingCopy(), new File("src/main/java/org/Foo.java"), repository);
    // ----------------------------------------------------------------------
    // Diff the project
    // ----------------------------------------------------------------------
    ScmProvider provider = getScmManager().getProviderByUrl(getScmUrl());
    ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
    DiffScmResult result = provider.diff(repository, fileSet, (ScmVersion) null, null);
    // todo: check asserts
    // assertNotNull("The command returned a null result.", result);
    // assertResultIsSuccess(result);
    List<ScmFile> changedFiles = result.getChangedFiles();
    Map<String, CharSequence> differences = result.getDifferences();
    // assertEquals("Expected 3 files in the changed files list "
    // + changedFiles, 3, changedFiles.size());
    // assertEquals("Expected 3 files in the differences list " + differences,
    // 3, differences.size());
    // ----------------------------------------------------------------------
    // Assert the files in the changed files list
    // ----------------------------------------------------------------------
    Iterator<ScmFile> files = new TreeSet<ScmFile>(changedFiles).iterator();
    // Check Foo.java
    ScmFile file = (ScmFile) files.next();
    // assertPath("/src/main/java/org/Foo.java", file.getPath());
    // assertTrue(file.getStatus().isDiff());
    String postRangeStr = "+/src/main/java/org/Foo.java\n\\ No newline at end of file\n";
    String actualStr = differences.get(file.getPath()).toString();
    // assertTrue(actualStr.endsWith(postRangeStr));
    // Check readme.txt
    file = (ScmFile) files.next();
    // assertPath("/readme.txt", file.getPath());
    // assertTrue(file.getStatus().isDiff());
    postRangeStr = "-/readme.txt\n\\ No newline at end of file\n+changed readme.txt\n\\ No newline at end of file\n";
    actualStr = differences.get(file.getPath()).toString();
    // assertTrue(actualStr.endsWith(postRangeStr));
    // Check project.xml
    file = (ScmFile) files.next();
    // assertPath("/project.xml", file.getPath());
    postRangeStr = "+changed project.xml\n\\ No newline at end of file\n";
    actualStr = differences.get(file.getPath()).toString();
    // assertTrue(actualStr.endsWith(postRangeStr));
    // assertTrue(file.getStatus().isDiff());
    assertTrue(true);
}
Also used : ScmProvider(org.apache.maven.scm.provider.ScmProvider) ScmRepository(org.apache.maven.scm.repository.ScmRepository) ScmFileSet(org.apache.maven.scm.ScmFileSet) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) DiffScmResult(org.apache.maven.scm.command.diff.DiffScmResult) ScmFile(org.apache.maven.scm.ScmFile)

Example 9 with DiffScmResult

use of org.apache.maven.scm.command.diff.DiffScmResult in project maven-scm by apache.

the class DiffCommandTckTest method testDiffCommand.

public void testDiffCommand() throws Exception {
    ScmRepository repository = getScmRepository();
    // ----------------------------------------------------------------------
    // Change the files
    // ----------------------------------------------------------------------
    // 
    // readme.txt is changed (changed file in the root directory)
    // project.xml is added (added file in the root directory)
    // src/test/resources is untouched (a empty directory is left untouched)
    // src/test/java is untouched (a non empty directory is left untouched)
    // src/test/java/org (a empty directory is added)
    // src/main/java/org/Foo.java (a non empty directory is added)
    // 
    // /readme.txt
    ScmTestCase.makeFile(getWorkingCopy(), "/readme.txt", "changed readme.txt");
    // /project.xml
    ScmTestCase.makeFile(getWorkingCopy(), "/project.xml", "changed project.xml");
    addToWorkingTree(getWorkingCopy(), new File("project.xml"), repository);
    // /src/test/java/org
    ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
    addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"), repository);
    // /src/main/java/org/Foo.java
    ScmTestCase.makeFile(getWorkingCopy(), "/src/main/java/org/Foo.java");
    addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"), repository);
    // src/main/java/org/Foo.java
    addToWorkingTree(getWorkingCopy(), new File("src/main/java/org/Foo.java"), repository);
    // ----------------------------------------------------------------------
    // Diff the project
    // ----------------------------------------------------------------------
    ScmProvider provider = getScmManager().getProviderByUrl(getScmUrl());
    ScmFileSet fileSet = new ScmFileSet(getWorkingCopy());
    DiffScmResult result = provider.diff(repository, fileSet, null, (ScmVersion) null);
    assertNotNull("The command returned a null result.", result);
    assertResultIsSuccess(result);
    List<ScmFile> changedFiles = result.getChangedFiles();
    Map<String, CharSequence> differences = result.getDifferences();
    assertEquals("Expected 3 files in the changed files list " + changedFiles, 3, changedFiles.size());
    assertEquals("Expected 3 files in the differences list " + differences, 3, differences.size());
    // ----------------------------------------------------------------------
    // Assert the files in the changed files list
    // ----------------------------------------------------------------------
    Iterator<ScmFile> files = new TreeSet<ScmFile>(changedFiles).iterator();
    // Check Foo.java
    ScmFile file = files.next();
    assertPath("/src/main/java/org/Foo.java", file.getPath());
    assertTrue(file.getStatus().isDiff());
    String postRangeStr = "+/src/main/java/org/Foo.java\n\\ No newline at end of file\n";
    String actualStr = differences.get(file.getPath()).toString();
    assertTrue(actualStr.endsWith(postRangeStr));
    // Check readme.txt
    file = files.next();
    assertPath("/readme.txt", file.getPath());
    assertTrue(file.getStatus().isDiff());
    postRangeStr = "-/readme.txt\n\\ No newline at end of file\n+changed readme.txt\n\\ No newline at end of file\n";
    actualStr = differences.get(file.getPath()).toString();
    assertTrue(actualStr.endsWith(postRangeStr));
    // Check project.xml
    file = files.next();
    assertPath("/project.xml", file.getPath());
    postRangeStr = "+changed project.xml\n\\ No newline at end of file\n";
    actualStr = differences.get(file.getPath()).toString();
    assertTrue(actualStr.endsWith(postRangeStr));
    assertTrue(file.getStatus().isDiff());
}
Also used : ScmProvider(org.apache.maven.scm.provider.ScmProvider) ScmRepository(org.apache.maven.scm.repository.ScmRepository) ScmFileSet(org.apache.maven.scm.ScmFileSet) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) DiffScmResult(org.apache.maven.scm.command.diff.DiffScmResult) ScmFile(org.apache.maven.scm.ScmFile)

Example 10 with DiffScmResult

use of org.apache.maven.scm.command.diff.DiffScmResult 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)21 ScmException (org.apache.maven.scm.ScmException)6 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)6 Commandline (org.codehaus.plexus.util.cli.Commandline)5 ScmFile (org.apache.maven.scm.ScmFile)4 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)4 File (java.io.File)3 ScmResult (org.apache.maven.scm.ScmResult)3 ScmRepository (org.apache.maven.scm.repository.ScmRepository)3 IOException (java.io.IOException)2 ScmFileSet (org.apache.maven.scm.ScmFileSet)2 ScmProvider (org.apache.maven.scm.provider.ScmProvider)2 CvsDiffConsumer (org.apache.maven.scm.provider.cvslib.command.diff.CvsDiffConsumer)2 GitDiffConsumer (org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1