From 69a5683b827c1b97113a13974509321be3d67e49 Mon Sep 17 00:00:00 2001 From: Tomasz Zarna Date: Mon, 9 Jan 2012 12:13:12 +0100 Subject: [PATCH] Add options for setting context lines and prefixes to DiffCommand Change-Id: I539f3531e94c11c0f0a3e7096c0eb1b1c309898a --- .../org/eclipse/jgit/api/DiffCommandTest.java | 44 +++++++++++++++++ .../src/org/eclipse/jgit/api/DiffCommand.java | 48 +++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java index 0729ecbc3..16a3d608f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DiffCommandTest.java @@ -178,6 +178,50 @@ public class DiffCommandTest extends RepositoryTestCase { assertEquals(expected.toString(), actual); } + @Test + public void testDiffWithPrefixes() throws Exception { + write(new File(db.getWorkTree(), "test.txt"), "test"); + Git git = new Git(db); + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(db.getWorkTree(), "test.txt"), "test change"); + + OutputStream out = new ByteArrayOutputStream(); + git.diff().setOutputStream(out).setSourcePrefix("old/") + .setDestinationPrefix("new/") + .call(); + + String actual = out.toString(); + String expected = "diff --git old/test.txt new/test.txt\n" + + "index 30d74d2..4dba797 100644\n" + "--- old/test.txt\n" + + "+++ new/test.txt\n" + "@@ -1 +1 @@\n" + "-test\n" + + "\\ No newline at end of file\n" + "+test change\n" + + "\\ No newline at end of file\n"; + assertEquals(expected.toString(), actual); + } + + @Test + public void testDiffWithNegativeLineCount() throws Exception { + write(new File(db.getWorkTree(), "test.txt"), + "0\n1\n2\n3\n4\n5\n6\n7\n8\n9"); + Git git = new Git(db); + git.add().addFilepattern(".").call(); + git.commit().setMessage("Initial commit").call(); + write(new File(db.getWorkTree(), "test.txt"), + "0\n1\n2\n3\n4a\n5\n6\n7\n8\n9"); + + OutputStream out = new ByteArrayOutputStream(); + git.diff().setOutputStream(out).setContextLines(1) + .call(); + + String actual = out.toString(); + String expected = "diff --git a/test.txt b/test.txt\n" + + "index f55b5c9..c5ec8fd 100644\n" + "--- a/test.txt\n" + + "+++ b/test.txt\n" + "@@ -4,3 +4,3 @@\n" + " 3\n" + "-4\n" + + "+4a\n" + " 5\n"; + assertEquals(expected.toString(), actual); + } + private AbstractTreeIterator getTreeIterator(String name) throws IOException { final ObjectId id = db.resolve(name); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java index 5fff5b5f5..8143bc7b6 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java @@ -82,6 +82,12 @@ public class DiffCommand extends GitCommand> { private OutputStream out; + private int contextLines = -1; + + private String sourcePrefix; + + private String destinationPrefix; + /** * @param repo */ @@ -125,6 +131,12 @@ public class DiffCommand extends GitCommand> { } diffFmt.setPathFilter(pathFilter); + if (contextLines >= 0) + diffFmt.setContext(contextLines); + if (destinationPrefix != null) + diffFmt.setNewPrefix(destinationPrefix); + if (sourcePrefix != null) + diffFmt.setOldPrefix(sourcePrefix); List result = diffFmt.scan(oldTree, newTree); if (showNameAndStatusOnly) { @@ -199,4 +211,40 @@ public class DiffCommand extends GitCommand> { this.out = out; return this; } + + /** + * Set number of context lines instead of the usual three. + * + * @param contextLines + * the number of context lines + * @return this instance + */ + public DiffCommand setContextLines(int contextLines) { + this.contextLines = contextLines; + return this; + } + + /** + * Set the given source prefix instead of "a/". + * + * @param sourcePrefix + * the prefix + * @return this instance + */ + public DiffCommand setSourcePrefix(String sourcePrefix) { + this.sourcePrefix = sourcePrefix; + return this; + } + + /** + * Set the given destination prefix instead of "b/". + * + * @param destinationPrefix + * the prefix + * @return this instance + */ + public DiffCommand setDestinationPrefix(String destinationPrefix) { + this.destinationPrefix = destinationPrefix; + return this; + } } \ No newline at end of file