diff options
author | XinTong Wang <xintong@ca.ibm.com> | 2015-12-08 15:03:26 -0500 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2016-03-03 01:43:02 +0100 |
commit | 770d36c8bafba2ad20686c161246a3a239cdf1d9 (patch) | |
tree | 321c15c91a7e17b61a05af454545e0ac44e4b4c3 | |
parent | be8c525fa0664d429e23cbf070420cb84a4a1607 (diff) | |
download | jgit-770d36c8bafba2ad20686c161246a3a239cdf1d9.tar.gz jgit-770d36c8bafba2ad20686c161246a3a239cdf1d9.zip |
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>
16 files changed, 106 insertions, 7 deletions
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java index 0db71f7cbc..86b87b4075 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java @@ -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 { diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII.patch new file mode 100644 index 0000000000..d6408c99ff --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII.patch @@ -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 ++あ diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2.patch new file mode 100644 index 0000000000..cea6cfd156 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2.patch @@ -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 diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PostImage new file mode 100644 index 0000000000..5c8e34dbdb --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PostImage @@ -0,0 +1 @@ +再见
\ No newline at end of file diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PreImage new file mode 100644 index 0000000000..43696610a6 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PreImage @@ -0,0 +1 @@ +你好, 世界!
\ No newline at end of file diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd.patch new file mode 100644 index 0000000000..de31a9eee3 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd.patch @@ -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 diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2.patch new file mode 100644 index 0000000000..5b41e9c26a --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2.patch @@ -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 diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2_PostImage new file mode 100644 index 0000000000..0575c798f0 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2_PostImage @@ -0,0 +1 @@ +あ
\ No newline at end of file diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PostImage new file mode 100644 index 0000000000..0575c798f0 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PostImage @@ -0,0 +1 @@ +あ
\ No newline at end of file diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PreImage new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PreImage diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel.patch new file mode 100644 index 0000000000..80e8f82cce --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel.patch @@ -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 diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel_PreImage new file mode 100644 index 0000000000..0575c798f0 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel_PreImage @@ -0,0 +1 @@ +あ
\ No newline at end of file diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PostImage new file mode 100644 index 0000000000..f2435a2989 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PostImage @@ -0,0 +1 @@ +あ diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PreImage new file mode 100644 index 0000000000..0575c798f0 --- /dev/null +++ b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PreImage @@ -0,0 +1 @@ +あ
\ No newline at end of file diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java index d842046bb7..239c844c33 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java @@ -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) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java index 676ae03009..bde450f99d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java @@ -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)); |