Skip to content

Commit

Permalink
Fixed UTF-8 encoding bug in diff formatter (issue 66)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitblit committed Feb 23, 2012
1 parent 7a1889b commit e5cb554
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/com/gitblit/utils/GitBlitDiffFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
package com.gitblit.utils;

import static org.eclipse.jgit.lib.Constants.encode;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.util.RawParseUtils;

/**
* Generates an html snippet of a diff in Gitblit's style.
Expand Down Expand Up @@ -86,11 +89,9 @@ protected void writeLine(final char prefix, final RawText text, final int cur)
break;
}
os.write(prefix);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
text.writeLine(bos, cur);
String line = bos.toString();
String line = text.getString(cur);
line = StringUtils.escapeForHtml(line, false);
os.write(line.getBytes());
os.write(encode(line));
switch (prefix) {
case '+':
case '-':
Expand All @@ -110,7 +111,8 @@ protected void writeLine(final char prefix, final RawText text, final int cur)
*/
@Override
public String getHtml() {
String html = os.toString();
ByteArrayOutputStream bos = (ByteArrayOutputStream) os;
String html = RawParseUtils.decode(bos.toByteArray());
String[] lines = html.split("\n");
StringBuilder sb = new StringBuilder();
boolean inFile = false;
Expand Down
11 changes: 6 additions & 5 deletions src/com/gitblit/utils/GitWebDiffFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.gitblit.utils;

import static org.eclipse.jgit.lib.Constants.encode;
import static org.eclipse.jgit.lib.Constants.encodeASCII;

import java.io.ByteArrayOutputStream;
Expand All @@ -23,6 +24,7 @@

import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.util.RawParseUtils;

/**
* Returns an html snippet of the diff in the standard Gitweb style.
Expand Down Expand Up @@ -111,11 +113,9 @@ protected void writeLine(final char prefix, final RawText text, final int cur)
break;
}
os.write(prefix);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
text.writeLine(bos, cur);
String line = bos.toString();
String line = text.getString(cur);
line = StringUtils.escapeForHtml(line, false);
os.write(line.getBytes());
os.write(encode(line));
switch (prefix) {
case '+':
case '-':
Expand All @@ -133,7 +133,8 @@ protected void writeLine(final char prefix, final RawText text, final int cur)
* @return
*/
public String getHtml() {
String html = os.toString();
ByteArrayOutputStream bos = (ByteArrayOutputStream) os;
String html = RawParseUtils.decode(bos.toByteArray());
String[] lines = html.split("\n");
StringBuilder sb = new StringBuilder();
sb.append("<div class=\"diff\">");
Expand Down
9 changes: 5 additions & 4 deletions tests/com/gitblit/tests/GitServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.util.FileUtils;
import org.junit.AfterClass;
Expand Down Expand Up @@ -113,9 +114,9 @@ public void testBogusLoginClone() throws Exception {
public void testAnonymousPush() throws Exception {
Git git = Git.open(ticgitFolder);
File file = new File(ticgitFolder, "TODO");
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
BufferedWriter w = new BufferedWriter(os);
w.write("// " + new Date().toString() + "\n");
w.write("// hellol中文 " + new Date().toString() + "\n");
w.close();
git.add().addFilepattern(file.getName()).call();
git.commit().setMessage("test commit").call();
Expand All @@ -136,7 +137,7 @@ public void testSubfolderPush() throws Exception {

Git git = Git.open(jgitFolder);
File file = new File(jgitFolder, "TODO");
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
BufferedWriter w = new BufferedWriter(os);
w.write("// " + new Date().toString() + "\n");
w.close();
Expand All @@ -159,7 +160,7 @@ public void testPushToNonBareRepository() throws Exception {

Git git = Git.open(jgit2Folder);
File file = new File(jgit2Folder, "NONBARE");
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));
OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
BufferedWriter w = new BufferedWriter(os);
w.write("// " + new Date().toString() + "\n");
w.close();
Expand Down

0 comments on commit e5cb554

Please sign in to comment.