]> source.dussan.org Git - gitblit.git/commitdiff
Fixed UTF-8 encoding bug in diff formatter (issue 66)
authorJames Moger <james.moger@gitblit.com>
Thu, 23 Feb 2012 22:06:46 +0000 (17:06 -0500)
committerJames Moger <james.moger@gitblit.com>
Thu, 23 Feb 2012 22:06:46 +0000 (17:06 -0500)
src/com/gitblit/utils/GitBlitDiffFormatter.java
src/com/gitblit/utils/GitWebDiffFormatter.java
tests/com/gitblit/tests/GitServletTest.java

index 0e393fef872fd594e32fa79691fda0f833d54048..e9fbf5288ec10a1d06eea54e6d0f93e12ff82e06 100644 (file)
  */\r
 package com.gitblit.utils;\r
 \r
+import static org.eclipse.jgit.lib.Constants.encode;\r
+\r
 import java.io.ByteArrayOutputStream;\r
 import java.io.IOException;\r
 import java.io.OutputStream;\r
 \r
 import org.eclipse.jgit.diff.RawText;\r
+import org.eclipse.jgit.util.RawParseUtils;\r
 \r
 /**\r
  * Generates an html snippet of a diff in Gitblit's style.\r
@@ -86,11 +89,9 @@ public class GitBlitDiffFormatter extends GitWebDiffFormatter {
                        break;\r
                }\r
                os.write(prefix);\r
-               ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
-               text.writeLine(bos, cur);\r
-               String line = bos.toString();\r
+               String line = text.getString(cur);\r
                line = StringUtils.escapeForHtml(line, false);\r
-               os.write(line.getBytes());\r
+               os.write(encode(line));\r
                switch (prefix) {\r
                case '+':\r
                case '-':\r
@@ -110,7 +111,8 @@ public class GitBlitDiffFormatter extends GitWebDiffFormatter {
         */\r
        @Override\r
        public String getHtml() {\r
-               String html = os.toString();\r
+               ByteArrayOutputStream bos = (ByteArrayOutputStream) os;\r
+               String html = RawParseUtils.decode(bos.toByteArray());\r
                String[] lines = html.split("\n");\r
                StringBuilder sb = new StringBuilder();\r
                boolean inFile = false;\r
index 00ea121c64921a3f33d03d394b4f367b852eaaca..50c6c724019f1bcfcba091d7e34fd7a7212abd31 100644 (file)
@@ -15,6 +15,7 @@
  */\r
 package com.gitblit.utils;\r
 \r
+import static org.eclipse.jgit.lib.Constants.encode;\r
 import static org.eclipse.jgit.lib.Constants.encodeASCII;\r
 \r
 import java.io.ByteArrayOutputStream;\r
@@ -23,6 +24,7 @@ import java.io.OutputStream;
 \r
 import org.eclipse.jgit.diff.DiffFormatter;\r
 import org.eclipse.jgit.diff.RawText;\r
+import org.eclipse.jgit.util.RawParseUtils;\r
 \r
 /**\r
  * Returns an html snippet of the diff in the standard Gitweb style.\r
@@ -111,11 +113,9 @@ public class GitWebDiffFormatter extends DiffFormatter {
                        break;\r
                }\r
                os.write(prefix);\r
-               ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
-               text.writeLine(bos, cur);\r
-               String line = bos.toString();\r
+               String line = text.getString(cur);\r
                line = StringUtils.escapeForHtml(line, false);\r
-               os.write(line.getBytes());\r
+               os.write(encode(line));\r
                switch (prefix) {\r
                case '+':\r
                case '-':\r
@@ -133,7 +133,8 @@ public class GitWebDiffFormatter extends DiffFormatter {
         * @return\r
         */\r
        public String getHtml() {\r
-               String html = os.toString();\r
+               ByteArrayOutputStream bos = (ByteArrayOutputStream) os;\r
+               String html = RawParseUtils.decode(bos.toByteArray());\r
                String[] lines = html.split("\n");\r
                StringBuilder sb = new StringBuilder();\r
                sb.append("<div class=\"diff\">");\r
index 38d7fa9fe7cc257760f8796b60af71492d5d459a..2f9a9c423d05636d7ab6663244e38acdb85109c3 100644 (file)
@@ -13,6 +13,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 \r
 import org.eclipse.jgit.api.CloneCommand;\r
 import org.eclipse.jgit.api.Git;\r
+import org.eclipse.jgit.lib.Constants;\r
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;\r
 import org.eclipse.jgit.util.FileUtils;\r
 import org.junit.AfterClass;\r
@@ -113,9 +114,9 @@ public class GitServletTest {
        public void testAnonymousPush() throws Exception {\r
                Git git = Git.open(ticgitFolder);\r
                File file = new File(ticgitFolder, "TODO");\r
-               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
+               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);\r
                BufferedWriter w = new BufferedWriter(os);\r
-               w.write("// " + new Date().toString() + "\n");\r
+               w.write("// hellol中文 " + new Date().toString() + "\n");\r
                w.close();\r
                git.add().addFilepattern(file.getName()).call();\r
                git.commit().setMessage("test commit").call();\r
@@ -136,7 +137,7 @@ public class GitServletTest {
 \r
                Git git = Git.open(jgitFolder);\r
                File file = new File(jgitFolder, "TODO");\r
-               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
+               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);\r
                BufferedWriter w = new BufferedWriter(os);\r
                w.write("// " + new Date().toString() + "\n");\r
                w.close();\r
@@ -159,7 +160,7 @@ public class GitServletTest {
 \r
                Git git = Git.open(jgit2Folder);\r
                File file = new File(jgit2Folder, "NONBARE");\r
-               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
+               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);\r
                BufferedWriter w = new BufferedWriter(os);\r
                w.write("// " + new Date().toString() + "\n");\r
                w.close();\r