Browse Source

Fix apply patch which did not work with non-ascii characters

Bug: 483943
Change-Id: If28f64053d20ab1bee54245f223e952dc2fe392c
Signed-off-by: XinTong Wang <xintong@ca.ibm.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
tags/v4.3.0.201603230630-rc1
XinTong Wang 8 years ago
parent
commit
770d36c8ba

+ 3
- 3
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java View File

@@ -144,10 +144,10 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {

protected static void checkFile(File f, final String checkData)
throws IOException {
Reader r = new InputStreamReader(new FileInputStream(f), "ISO-8859-1");
Reader r = new InputStreamReader(new FileInputStream(f), "UTF-8");
try {
char[] data = new char[(int) f.length()];
if (f.length() != r.read(data))
char[] data = new char[checkData.length()];
if (checkData.length() != r.read(data))
throw new IOException("Internal error reading file data from "+f);
assertEquals(checkData, new String(data));
} finally {

+ 8
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII.patch View File

@@ -0,0 +1,8 @@
diff --git a/NonASCII b/NonASCII
index 2e65efe..7898192 100644
--- a/NonASCII
+++ b/NonASCII
@@ -1 +1 @@
-あ
\ No newline at end of file
+あ

+ 9
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2.patch View File

@@ -0,0 +1,9 @@
diff --git a/NonASCII2 b/NonASCII2
index 2e65efe..7898192 100644
--- a/NonASCII2
+++ b/NonASCII2
@@ -1 +1 @@
-你好, 世界!
\ No newline at end of file
+再见
\ No newline at end of file

+ 1
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PostImage View File

@@ -0,0 +1 @@
再见

+ 1
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PreImage View File

@@ -0,0 +1 @@
你好, 世界!

+ 7
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd.patch View File

@@ -0,0 +1,7 @@
diff --git a/NonASCIIAdd b/NonASCIIAdd
index 2e65efe..7898192 100644
--- a/NonASCIIAdd
+++ b/NonASCIIAdd
@@ -0,0 +1 @@
+あ
\ No newline at end of file

+ 8
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2.patch View File

@@ -0,0 +1,8 @@
diff --git a/NonASCIIAdd2 b/NonASCIIAdd2
new file mode 100644
index 2e65efe..7898192 100644
--- /dev/null
+++ b/NonASCIIAdd2
@@ -0,0 +1 @@
+あ
\ No newline at end of file

+ 1
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2_PostImage View File

@@ -0,0 +1 @@

+ 1
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PostImage View File

@@ -0,0 +1 @@

+ 0
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PreImage View File


+ 9
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel.patch View File

@@ -0,0 +1,9 @@
diff --git a/NonASCIIDel b/NonASCIIDel
deleted file mode 100644
new file mode 100644
index 2e65efe..7898192 100644
--- a/NonASCIIDel
+++ /dev/null
@@ -1 +0,0 @@
-あ
\ No newline at end of file

+ 1
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel_PreImage View File

@@ -0,0 +1 @@

+ 1
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PostImage View File

@@ -0,0 +1 @@

+ 1
- 0
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PreImage View File

@@ -0,0 +1 @@

+ 49
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java View File

@@ -186,6 +186,55 @@ public class ApplyCommandTest extends RepositoryTestCase {
b.getString(0, b.size(), false));
}

@Test
public void testNonASCII() throws Exception {
ApplyResult result = init("NonASCII");
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCII"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCII"),
b.getString(0, b.size(), false));
}

@Test
public void testNonASCII2() throws Exception {
ApplyResult result = init("NonASCII2");
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCII2"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCII2"),
b.getString(0, b.size(), false));
}

@Test
public void testNonASCIIAdd() throws Exception {
ApplyResult result = init("NonASCIIAdd");
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
b.getString(0, b.size(), false));
}

@Test
public void testNonASCIIAdd2() throws Exception {
ApplyResult result = init("NonASCIIAdd2", false, true);
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
result.getUpdatedFiles().get(0));
checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
b.getString(0, b.size(), false));
}

@Test
public void testNonASCIIDel() throws Exception {
ApplyResult result = init("NonASCIIDel", true, false);
assertEquals(1, result.getUpdatedFiles().size());
assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
result.getUpdatedFiles().get(0));
assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
}

private static byte[] readFile(final String patchFile) throws IOException {
final InputStream in = getTestResource(patchFile);
if (in == null) {

+ 6
- 4
org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java View File

@@ -201,10 +201,12 @@ public class ApplyCommand extends GitCommand<ApplyResult> {
oldLines.add(rt.getString(i));
List<String> newLines = new ArrayList<String>(oldLines);
for (HunkHeader hh : fh.getHunks()) {
StringBuilder hunk = new StringBuilder();
for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++)
hunk.append((char) hh.getBuffer()[j]);
RawText hrt = new RawText(hunk.toString().getBytes());

byte[] b = new byte[hh.getEndOffset() - hh.getStartOffset()];
System.arraycopy(hh.getBuffer(), hh.getStartOffset(), b, 0,
b.length);
RawText hrt = new RawText(b);

List<String> hunkLines = new ArrayList<String>(hrt.size());
for (int i = 0; i < hrt.size(); i++)
hunkLines.add(hrt.getString(i));

Loading…
Cancel
Save