]> source.dussan.org Git - jgit.git/commitdiff
Fix diff when first text is the start of the other 56/3356/6
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Sun, 15 May 2011 18:34:05 +0000 (20:34 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Sun, 15 May 2011 18:34:05 +0000 (20:34 +0200)
The problem occurred when the first text ends in the middle
of the last line of the other text and the first text has no
end of line.

Bug: 344975
Change-Id: I1f0dd9f8062f2148a7c1341c9122202e082ad19d
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java
org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java

index 646e4d611208b6c35306d63679c404fe2fb05d52..8dca7b4260ac37ea46a57f51a1d04c184f3b0d2c 100644 (file)
@@ -45,13 +45,21 @@ package org.eclipse.jgit.diff;
 
 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;
@@ -247,6 +255,38 @@ public class DiffFormatterTest extends RepositoryTestCase {
                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();
index 7e6e1354d6cbbb22a58f46f9137b4748adea5d7a..8d504e55d4ab74db883eff494b882ac2f0ed3ae2 100644 (file)
@@ -163,6 +163,32 @@ public class RawTextTest {
                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++) {
index 3576c50044b586b4c8475a879f052af0060df519..2793fa2e8cafe213aa1e50db9c09a77a3a01e021 100644 (file)
@@ -318,7 +318,7 @@ public abstract class RawTextComparator extends SequenceComparator<RawText> {
 
        private static int findForwardLine(IntList lines, int idx, int ptr) {
                final int end = lines.size() - 2;
-               while (idx < end && lines.get(idx + 2) <= ptr)
+               while (idx < end && lines.get(idx + 2) < ptr)
                        idx++;
                return idx;
        }