import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import java.io.File;
assertEquals(side, lines.getSourceCommit(2));
}
}
+
+ @Test
+ public void testBlameWithNulByteInHistory() throws Exception {
+ try (Git git = new Git(db)) {
+ String[] content1 = { "First line", "Another line" };
+ writeTrashFile("file.txt", join(content1));
+ git.add().addFilepattern("file.txt").call();
+ RevCommit c1 = git.commit().setMessage("create file").call();
+
+ String[] content2 = { "First line", "Second line with NUL >\000<",
+ "Another line" };
+ assertTrue("Content should contain a NUL byte",
+ content2[1].indexOf(0) > 0);
+ writeTrashFile("file.txt", join(content2));
+ git.add().addFilepattern("file.txt").call();
+ git.commit().setMessage("add line with NUL").call();
+
+ String[] content3 = { "First line", "Second line with NUL >\000<",
+ "Third line" };
+ writeTrashFile("file.txt", join(content3));
+ git.add().addFilepattern("file.txt").call();
+ RevCommit c3 = git.commit().setMessage("change third line").call();
+
+ String[] content4 = { "First line", "Second line with NUL >\\000<",
+ "Third line" };
+ assertTrue("Content should not contain a NUL byte",
+ content4[1].indexOf(0) < 0);
+ writeTrashFile("file.txt", join(content4));
+ git.add().addFilepattern("file.txt").call();
+ RevCommit c4 = git.commit().setMessage("fix NUL line").call();
+
+ BlameResult lines = git.blame().setFilePath("file.txt").call();
+ assertEquals(3, lines.getResultContents().size());
+ assertEquals(c1, lines.getSourceCommit(0));
+ assertEquals(c4, lines.getSourceCommit(1));
+ assertEquals(c3, lines.getSourceCommit(2));
+ }
+ }
+
+ @Test
+ public void testBlameWithNulByteInTopRevision() throws Exception {
+ try (Git git = new Git(db)) {
+ String[] content1 = { "First line", "Another line" };
+ writeTrashFile("file.txt", join(content1));
+ git.add().addFilepattern("file.txt").call();
+ RevCommit c1 = git.commit().setMessage("create file").call();
+
+ String[] content2 = { "First line", "Second line with NUL >\000<",
+ "Another line" };
+ assertTrue("Content should contain a NUL byte",
+ content2[1].indexOf(0) > 0);
+ writeTrashFile("file.txt", join(content2));
+ git.add().addFilepattern("file.txt").call();
+ RevCommit c2 = git.commit().setMessage("add line with NUL").call();
+
+ String[] content3 = { "First line", "Second line with NUL >\000<",
+ "Third line" };
+ writeTrashFile("file.txt", join(content3));
+ git.add().addFilepattern("file.txt").call();
+ RevCommit c3 = git.commit().setMessage("change third line").call();
+
+ BlameResult lines = git.blame().setFilePath("file.txt").call();
+ assertEquals(3, lines.getResultContents().size());
+ assertEquals(c1, lines.getSourceCommit(0));
+ assertEquals(c2, lines.getSourceCommit(1));
+ assertEquals(c3, lines.getSourceCommit(2));
+ }
+ }
+
}
}
@Test
- public void testBinary() {
+ public void testNul() {
String input = "foo-a\nf\0o-b\n";
byte[] data = Constants.encodeASCII(input);
final RawText a = new RawText(data);
assertArrayEquals(a.content, data);
- assertEquals(a.size(), 1);
- assertEquals(a.getString(0, 1, false), input);
+ assertEquals(2, a.size());
+ assertEquals("foo-a\n", a.getString(0, 1, false));
+ assertEquals("f\0o-b\n", a.getString(1, 2, false));
+ assertEquals("foo-a", a.getString(0, 1, true));
+ assertEquals("f\0o-b", a.getString(1, 2, true));
}
@Test
* <p>
* The last element (index <code>map.size()-1</code>) always contains
* <code>end</code>.
- * <p>
- * If the data contains a '\0' anywhere, the whole region is considered
- * binary and a LineMap corresponding to a single line is returned.
- * </p>
*
* @param buf
* buffer to scan.
* line 1.
* @param end
* 1 past the end of the content within <code>buf</code>.
- * @return a line map indicating the starting position of each line, or a
- * map representing the entire buffer as a single line if
- * <code>buf</code> contains a NUL byte.
+ * @return a line map indicating the starting position of each line.
*/
public static final IntList lineMap(byte[] buf, int ptr, int end) {
- IntList map = lineMapOrNull(buf, ptr, end);
- if (map == null) {
- map = new IntList(3);
- map.add(Integer.MIN_VALUE);
+ IntList map = new IntList((end - ptr) / 36);
+ map.fillTo(1, Integer.MIN_VALUE);
+ for (; ptr < end; ptr = nextLF(buf, ptr)) {
map.add(ptr);
- map.add(end);
}
+ map.add(end);
return map;
}