Browse Source

Merge "Fix diff when first text is the start of the other"

stable-1.0
Shawn Pearce 14 years ago committed by Code Review
parent
commit
4b4df4ccf8
  1. 40
      org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/DiffFormatterTest.java
  2. 26
      org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java
  3. 2
      org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java

40
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();

26
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++) {

2
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;
}

Loading…
Cancel
Save