diff options
author | Robin Rosenberg <robin.rosenberg@dewire.com> | 2011-05-15 20:34:05 +0200 |
---|---|---|
committer | Robin Rosenberg <robin.rosenberg@dewire.com> | 2011-05-15 20:34:05 +0200 |
commit | 51a5cc7f1a1033664ee2fb760ed217b665b12b34 (patch) | |
tree | 6def9f99e035a6ba3f97417557cea0caff0e8dce | |
parent | b8fdda11cc4a786c24ba309ec755321dba6d9955 (diff) | |
download | jgit-51a5cc7f1a1033664ee2fb760ed217b665b12b34.tar.gz jgit-51a5cc7f1a1033664ee2fb760ed217b665b12b34.zip |
Fix diff when first text is the start of the other
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>
3 files changed, 67 insertions, 1 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 646e4d6112..8dca7b4260 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 @@ -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(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java index 7e6e1354d6..8d504e55d4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java @@ -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++) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java index 3576c50044..2793fa2e8c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java @@ -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; } |