Browse Source

Make output stream optional in DiffCommand

Use the NullOutputStream.INSTANCE value when the
configured output stream is null or the command is
configured to only show name and status.

Also only set the context and prefix options if
formatting is actually being performed.

Bug: 377157
Change-Id: I333cfcc82ee746f3c6a8e94c09dcc803ffbb4b3a
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-2.0
Kevin Sawicki 13 years ago committed by Matthias Sohn
parent
commit
6ebc477c0b
  1. 19
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java
  2. 18
      org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java

19
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java

@ -43,6 +43,8 @@
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -222,6 +224,23 @@ public class DiffCommandTest extends RepositoryTestCase {
assertEquals(expected.toString(), actual); assertEquals(expected.toString(), actual);
} }
@Test
public void testNoOutputStreamSet() throws Exception {
File file = writeTrashFile("test.txt", "a");
assertTrue(file.setLastModified(file.lastModified() - 5000));
Git git = new Git(db);
git.add().addFilepattern(".").call();
write(file, "b");
List<DiffEntry> diffs = git.diff().call();
assertNotNull(diffs);
assertEquals(1, diffs.size());
DiffEntry diff = diffs.get(0);
assertEquals(ChangeType.MODIFY, diff.getChangeType());
assertEquals("test.txt", diff.getOldPath());
assertEquals("test.txt", diff.getNewPath());
}
private AbstractTreeIterator getTreeIterator(String name) private AbstractTreeIterator getTreeIterator(String name)
throws IOException { throws IOException {
final ObjectId id = db.resolve(name); final ObjectId id = db.resolve(name);

18
org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java

@ -64,6 +64,7 @@ import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.filter.TreeFilter; import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.io.NullOutputStream;
/** /**
* Show changes between commits, commit and working tree, etc. * Show changes between commits, commit and working tree, etc.
@ -108,8 +109,11 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> {
* @return a DiffEntry for each path which is different * @return a DiffEntry for each path which is different
*/ */
public List<DiffEntry> call() throws GitAPIException, IOException { public List<DiffEntry> call() throws GitAPIException, IOException {
final DiffFormatter diffFmt = new DiffFormatter( final DiffFormatter diffFmt;
new BufferedOutputStream(out)); if (out != null && !showNameAndStatusOnly)
diffFmt = new DiffFormatter(new BufferedOutputStream(out));
else
diffFmt = new DiffFormatter(NullOutputStream.INSTANCE);
diffFmt.setRepository(repo); diffFmt.setRepository(repo);
diffFmt.setProgressMonitor(monitor); diffFmt.setProgressMonitor(monitor);
try { try {
@ -136,17 +140,17 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> {
} }
diffFmt.setPathFilter(pathFilter); diffFmt.setPathFilter(pathFilter);
List<DiffEntry> result = diffFmt.scan(oldTree, newTree);
if (showNameAndStatusOnly)
return result;
else {
if (contextLines >= 0) if (contextLines >= 0)
diffFmt.setContext(contextLines); diffFmt.setContext(contextLines);
if (destinationPrefix != null) if (destinationPrefix != null)
diffFmt.setNewPrefix(destinationPrefix); diffFmt.setNewPrefix(destinationPrefix);
if (sourcePrefix != null) if (sourcePrefix != null)
diffFmt.setOldPrefix(sourcePrefix); diffFmt.setOldPrefix(sourcePrefix);
List<DiffEntry> result = diffFmt.scan(oldTree, newTree);
if (showNameAndStatusOnly) {
return result;
} else {
diffFmt.format(result); diffFmt.format(result);
diffFmt.flush(); diffFmt.flush();
return result; return result;

Loading…
Cancel
Save