aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Zarna2012-03-02 15:06:09 (EST)
committerKevin Sawicki2012-03-02 15:06:09 (EST)
commit9c7371a8c40add3674106b301bacf060d092404b (patch)
treef39812f48b11a3d0effb974da671b44dc95200f5
parenta1f877e312bd63c63c6b9d6ba32b5c68bc43a10d (diff)
downloadjgit-9c7371a8c40add3674106b301bacf060d092404b.zip
jgit-9c7371a8c40add3674106b301bacf060d092404b.tar.gz
jgit-9c7371a8c40add3674106b301bacf060d092404b.tar.bz2
Allow to get end-of-line characters for a RawTextrefs/changes/45/5045/7
Bug: 370320 Change-Id: I18adc63596f4657516ccc6d704a561924c79d445 Signed-off-by: Kevin Sawicki <kevin@github.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java41
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java17
2 files changed, 58 insertions, 0 deletions
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 8d504e5..2a1e2c9 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
@@ -46,6 +46,7 @@ package org.eclipse.jgit.diff;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
@@ -189,6 +190,46 @@ public class RawTextTest {
assertEquals(new Edit(0, 1, 0, 2), e);
}
+ @Test
+ public void testLineDelimiter() throws Exception {
+ RawText rt = new RawText(Constants.encodeASCII("foo\n"));
+ assertEquals("\n", rt.getLineDelimiter());
+ assertFalse(rt.isMissingNewlineAtEnd());
+ rt = new RawText(Constants.encodeASCII("foo\r\n"));
+ assertEquals("\r\n", rt.getLineDelimiter());
+ assertFalse(rt.isMissingNewlineAtEnd());
+
+ rt = new RawText(Constants.encodeASCII("foo\nbar"));
+ assertEquals("\n", rt.getLineDelimiter());
+ assertTrue(rt.isMissingNewlineAtEnd());
+ rt = new RawText(Constants.encodeASCII("foo\r\nbar"));
+ assertEquals("\r\n", rt.getLineDelimiter());
+ assertTrue(rt.isMissingNewlineAtEnd());
+
+ rt = new RawText(Constants.encodeASCII("foo\nbar\r\n"));
+ assertEquals("\n", rt.getLineDelimiter());
+ assertFalse(rt.isMissingNewlineAtEnd());
+ rt = new RawText(Constants.encodeASCII("foo\r\nbar\n"));
+ assertEquals("\r\n", rt.getLineDelimiter());
+ assertFalse(rt.isMissingNewlineAtEnd());
+
+ rt = new RawText(Constants.encodeASCII("foo"));
+ assertNull(rt.getLineDelimiter());
+ assertTrue(rt.isMissingNewlineAtEnd());
+
+ rt = new RawText(Constants.encodeASCII(""));
+ assertNull(rt.getLineDelimiter());
+ assertTrue(rt.isMissingNewlineAtEnd());
+
+ rt = new RawText(Constants.encodeASCII("\n"));
+ assertEquals("\n", rt.getLineDelimiter());
+ assertFalse(rt.isMissingNewlineAtEnd());
+
+ rt = new RawText(Constants.encodeASCII("\r\n"));
+ assertEquals("\r\n", rt.getLineDelimiter());
+ assertFalse(rt.isMissingNewlineAtEnd());
+ }
+
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/RawText.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java
index f83df6b..fa9ca84 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java
@@ -276,4 +276,21 @@ public class RawText extends Sequence {
return false;
}
+
+ /**
+ * Get the line delimiter for the first line.
+ *
+ * @return the line delimiter or <code>null</code>
+ */
+ public String getLineDelimiter() {
+ if (size() == 0)
+ return null;
+ int e = getEnd(0);
+ if (content[e - 1] != '\n')
+ return null;
+ if (content.length > 1 && content[e - 2] == '\r')
+ return "\r\n";
+ else
+ return "\n";
+ }
}