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);
}
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());
}
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);
}
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());
}
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());
}
Aggregations