diff options
author | Robin Rosenberg <robin.rosenberg@dewire.com> | 2014-06-29 22:31:26 +0200 |
---|---|---|
committer | Robin Rosenberg <robin.rosenberg@dewire.com> | 2014-07-06 18:45:48 +0200 |
commit | 89ad10ec2768d9957b16ead55e1bfd94f81146d2 (patch) | |
tree | 1c52d6f853a5ba52351e56f603ada37ce3fb0025 /org.eclipse.jgit.test | |
parent | 71c18201c1d438cc048060b61e4a1777ae9f9c1f (diff) | |
download | jgit-89ad10ec2768d9957b16ead55e1bfd94f81146d2.tar.gz jgit-89ad10ec2768d9957b16ead55e1bfd94f81146d2.zip |
Handle diff formatting when there is nothing to compare with
DiffFormatter now suports either side being null and the log program
will output the diff for the first commit.
Bug: 395791
Change-Id: I378957b57e9ad1f7195ba416f402178453f0ebd3
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java index 60b7460222..91498e7dfc 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java @@ -53,11 +53,13 @@ import org.eclipse.jgit.diff.DiffEntry.ChangeType; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.junit.TestRepository; +import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.patch.FileHeader; import org.eclipse.jgit.patch.HunkHeader; +import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.filter.PathFilter; import org.eclipse.jgit.util.FileUtils; @@ -341,6 +343,82 @@ public class DiffFormatterTest extends RepositoryTestCase { assertEquals(expected, actual); } + @Test + public void testDiffRootNullToTree() throws Exception { + write(new File(db.getDirectory().getParent(), "test.txt"), "test"); + File folder = new File(db.getDirectory().getParent(), "folder"); + FileUtils.mkdir(folder); + write(new File(folder, "folder.txt"), "folder"); + Git git = new Git(db); + git.add().addFilepattern(".").call(); + RevCommit commit = git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "folder change"); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); + dfmt.setRepository(db); + dfmt.setPathFilter(PathFilter.create("folder")); + dfmt.format(null, commit.getTree().getId()); + dfmt.flush(); + + String actual = os.toString("UTF-8"); + String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" + + "new file mode 100644\n" + + "index 0000000..0119635\n" + + "--- /dev/null\n" + + "+++ b/folder/folder.txt\n" + + "@@ -0,0 +1 @@\n" + + "+folder\n" + + "\\ No newline at end of file\n"; + + assertEquals(expected, actual); + } + + @Test + public void testDiffRootTreeToNull() throws Exception { + write(new File(db.getDirectory().getParent(), "test.txt"), "test"); + File folder = new File(db.getDirectory().getParent(), "folder"); + FileUtils.mkdir(folder); + write(new File(folder, "folder.txt"), "folder"); + Git git = new Git(db); + git.add().addFilepattern(".").call(); + RevCommit commit = git.commit().setMessage("Initial commit").call(); + write(new File(folder, "folder.txt"), "folder change"); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); + dfmt.setRepository(db); + dfmt.setPathFilter(PathFilter.create("folder")); + dfmt.format(commit.getTree().getId(), null); + dfmt.flush(); + + String actual = os.toString("UTF-8"); + String expected = "diff --git a/folder/folder.txt b/folder/folder.txt\n" + + "deleted file mode 100644\n" + + "index 0119635..0000000\n" + + "--- a/folder/folder.txt\n" + + "+++ /dev/null\n" + + "@@ -1 +0,0 @@\n" + + "-folder\n" + + "\\ No newline at end of file\n"; + + assertEquals(expected, actual); + } + + @Test + public void testDiffNullToNull() throws Exception { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + DiffFormatter dfmt = new DiffFormatter(new SafeBufferedOutputStream(os)); + dfmt.setRepository(db); + dfmt.format((AnyObjectId) null, null); + dfmt.flush(); + + String actual = os.toString("UTF-8"); + String expected = ""; + + assertEquals(expected, actual); + } + private static String makeDiffHeader(String pathA, String pathB, ObjectId aId, ObjectId bId) { |