import static org.junit.Assert.assertEquals;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+
+import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
+import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.RepositoryTestCase;
import org.eclipse.jgit.patch.FileHeader;
import org.eclipse.jgit.patch.HunkHeader;
+import org.eclipse.jgit.treewalk.FileTreeIterator;
+import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import org.junit.After;
assertEquals(0, hh.toEditList().size());
}
+ @Test
+ public void testDiff() throws Exception {
+ write(new File(db.getDirectory().getParent(), "test.txt"), "test");
+ File folder = new File(db.getDirectory().getParent(), "folder");
+ folder.mkdir();
+ write(new File(folder, "folder.txt"), "folder");
+ Git git = new Git(db);
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("Initial commit").call();
+ write(new File(folder, "folder.txt"), "folder change");
+
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ DiffFormatter df = new DiffFormatter(new BufferedOutputStream(os));
+ df.setRepository(db);
+ df.setPathFilter(PathFilter.create("folder"));
+ DirCacheIterator oldTree = new DirCacheIterator(db.readDirCache());
+ FileTreeIterator newTree = new FileTreeIterator(db);
+ df.format(oldTree, newTree);
+ df.flush();
+
+ String actual = os.toString();
+ String expected =
+ "diff --git a/folder/folder.txt b/folder/folder.txt\n"
+ + "index 0119635..95c4c65 100644\n"
+ + "--- a/folder/folder.txt\n" + "+++ b/folder/folder.txt\n"
+ + "@@ -1 +1 @@\n" + "-folder\n"
+ + "\\ No newline at end of file\n" + "+folder change\n"
+ + "\\ No newline at end of file\n";
+
+ assertEquals(expected.toString(), actual);
+ }
+
private String makeDiffHeader(String pathA, String pathB, ObjectId aId,
ObjectId bId) {
String a = aId.abbreviate(8).name();
assertEquals(new Edit(0, 2, 0, 1), e);
}
+ @Test
+ public void testComparatorReduceCommonStartButLastLineNoEol()
+ throws UnsupportedEncodingException {
+ RawText a;
+ RawText b;
+ Edit e;
+ a = new RawText("start".getBytes("UTF-8"));
+ b = new RawText("start of line".getBytes("UTF-8"));
+ e = new Edit(0, 1, 0, 1);
+ e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
+ assertEquals(new Edit(0, 1, 0, 1), e);
+ }
+
+ @Test
+ public void testComparatorReduceCommonStartButLastLineNoEol_2()
+ throws UnsupportedEncodingException {
+ RawText a;
+ RawText b;
+ Edit e;
+ a = new RawText("start".getBytes("UTF-8"));
+ b = new RawText("start of\nlastline".getBytes("UTF-8"));
+ e = new Edit(0, 1, 0, 2);
+ e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
+ assertEquals(new Edit(0, 1, 0, 2), e);
+ }
+
private static RawText t(String text) {
StringBuilder r = new StringBuilder();
for (int i = 0; i < text.length(); i++) {