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>
stable-4.3
XinTong Wang 9 years ago committed by Matthias Sohn
parent
commit
770d36c8ba
  1. 6
      org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/RepositoryTestCase.java
  2. 8
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII.patch
  3. 9
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2.patch
  4. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PostImage
  5. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PreImage
  6. 7
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd.patch
  7. 8
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2.patch
  8. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2_PostImage
  9. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PostImage
  10. 0
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PreImage
  11. 9
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel.patch
  12. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel_PreImage
  13. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PostImage
  14. 1
      org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PreImage
  15. 49
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java
  16. 10
      org.eclipse.jgit/src/org/eclipse/jgit/api/ApplyCommand.java

6
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 {

8
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
+あ

9
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

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PostImage

@ -0,0 +1 @@
再见

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII2_PreImage

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

7
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

8
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

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd2_PostImage

@ -0,0 +1 @@

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIAdd_PostImage

@ -0,0 +1 @@

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

9
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

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCIIDel_PreImage

@ -0,0 +1 @@

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PostImage

@ -0,0 +1 @@

1
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/NonASCII_PreImage

@ -0,0 +1 @@

49
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) {

10
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));

Loading…
Cancel
Save