From b34048803ad6cf0a0a0c998696a41de118715452 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 10 Sep 2012 16:25:10 -0400 Subject: [PATCH] Fix bug in diff view for filenames with non-ASCII characters (issue 128) --- docs/03_faq.mkd | 7 ++++ docs/04_releases.mkd | 8 +++++ .../gitblit/utils/GitBlitDiffFormatter.java | 15 ++++++-- .../gitblit/utils/GitWebDiffFormatter.java | 10 +++--- src/com/gitblit/utils/StringUtils.java | 36 +++++++++++++++++++ 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/docs/03_faq.mkd b/docs/03_faq.mkd index 36be82bf..cdf3d59e 100644 --- a/docs/03_faq.mkd +++ b/docs/03_faq.mkd @@ -71,6 +71,13 @@ Add *-Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true* to *CATALIN 2. *web.mountParameters = false* and use non-pretty, parameterized urls 3. *web.forwardSlashCharacter = !* which tells Gitblit to use **!** instead of **/** +#### UTF-8 Filenames + +Tomcat also dislikes urls with non-ASCII characters. If your repositories have non-ASCII filenames you will have to modify your connector properties to allow UTF-8 encoded urls. + +[Tomcat Character Encoding](http://wiki.apache.org/tomcat/FAQ/CharacterEncoding) +[Tomcat Connector Properties](http://tomcat.apache.org/tomcat-6.0-doc/config/http.html) + ## General Interest Questions ### Gitblit? What kind of name is that? diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd index db7815b2..d7b598b9 100644 --- a/docs/04_releases.mkd +++ b/docs/04_releases.mkd @@ -9,6 +9,14 @@ If you are updating from an earlier release AND you have indexed branches with t **%VERSION%** ([go](http://code.google.com/p/gitblit/downloads/detail?name=%GO%) | [war](http://code.google.com/p/gitblit/downloads/detail?name=%WAR%) | [express](http://code.google.com/p/gitblit/downloads/detail?name=%EXPRESS%) | [fedclient](http://code.google.com/p/gitblit/downloads/detail?name=%FEDCLIENT%) | [manager](http://code.google.com/p/gitblit/downloads/detail?name=%MANAGER%) | [api](http://code.google.com/p/gitblit/downloads/detail?name=%API%)) based on [%JGIT%][jgit]   *released %BUILDDATE%* +#### fixes + +- Fixed bug in the diff view for filenames that have non-ASCII characters (issue 128) + +#### additions + +- added RedmineUserService (github/mallowlabs) + #### changes - Emit a warning in the log file if running on a Tomcat-based servlet container which is unfriendly to %2F forward-slash url encoding AND Gitblit is configured to mount parameters with %2F forward-slash url encoding (Github/jpyeron, issue 126) diff --git a/src/com/gitblit/utils/GitBlitDiffFormatter.java b/src/com/gitblit/utils/GitBlitDiffFormatter.java index e9fbf528..2966aa8a 100644 --- a/src/com/gitblit/utils/GitBlitDiffFormatter.java +++ b/src/com/gitblit/utils/GitBlitDiffFormatter.java @@ -127,15 +127,24 @@ public class GitBlitDiffFormatter extends GitWebDiffFormatter { } else if (line.startsWith("---") || line.startsWith("+++")) { // skip --- +++ lines } else if (line.startsWith("diff")) { + line = StringUtils.convertOctal(line); if (line.indexOf(oldnull) > -1) { // a is null, use b line = line.substring(("diff --git " + oldnull).length()).trim(); // trim b/ - line = line.substring(2); + line = line.substring(2).trim(); } else { // use a - line = line.substring("diff --git a/".length()).trim(); - line = line.substring(0, line.indexOf(" b/")).trim(); + line = line.substring("diff --git ".length()).trim(); + line = line.substring(line.startsWith("\"a/") ? 3 : 2); + line = line.substring(0, line.indexOf(" b/") > -1 ? line.indexOf(" b/") : line.indexOf("\"b/")).trim(); + } + + if (line.charAt(0) == '"') { + line = line.substring(1); + } + if (line.charAt(line.length() - 1) == '"') { + line = line.substring(0, line.length() - 1); } if (inFile) { sb.append("\n"); diff --git a/src/com/gitblit/utils/GitWebDiffFormatter.java b/src/com/gitblit/utils/GitWebDiffFormatter.java index 50c6c724..e657dc58 100644 --- a/src/com/gitblit/utils/GitWebDiffFormatter.java +++ b/src/com/gitblit/utils/GitWebDiffFormatter.java @@ -106,10 +106,10 @@ public class GitWebDiffFormatter extends DiffFormatter { throws IOException { switch (prefix) { case '+': - os.write("".getBytes()); + os.write("".getBytes()); break; case '-': - os.write("".getBytes()); + os.write("".getBytes()); break; } os.write(prefix); @@ -140,11 +140,11 @@ public class GitWebDiffFormatter extends DiffFormatter { sb.append("
"); for (String line : lines) { if (line.startsWith("diff")) { - sb.append("
").append(line).append("
"); + sb.append("
").append(StringUtils.convertOctal(line)).append("
"); } else if (line.startsWith("---")) { - sb.append("").append(line).append("
"); + sb.append("").append(StringUtils.convertOctal(line)).append("
"); } else if (line.startsWith("+++")) { - sb.append("").append(line).append("
"); + sb.append("").append(StringUtils.convertOctal(line)).append("
"); } else { sb.append(line).append('\n'); } diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index 08fd4972..e4407901 100644 --- a/src/com/gitblit/utils/StringUtils.java +++ b/src/com/gitblit/utils/StringUtils.java @@ -15,6 +15,7 @@ */ package com.gitblit.utils; +import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; @@ -624,4 +625,39 @@ public class StringUtils { } return url; } + + /** + * Converts a string with \nnn sequences into a UTF-8 encoded string. + * @param input + * @return + */ + public static String convertOctal(String input) { + try { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + Pattern p = Pattern.compile("(\\\\\\d{3})"); + Matcher m = p.matcher(input); + int i = 0; + while (m.find()) { + bytes.write(input.substring(i, m.start()).getBytes("UTF-8")); + // replace octal encoded value + // strip leading \ character + String oct = m.group().substring(1); + bytes.write(Integer.parseInt(oct, 8)); + i = m.end(); + } + if (bytes.size() == 0) { + // no octal matches + return input; + } else { + if (i < input.length()) { + // add remainder of string + bytes.write(input.substring(i).getBytes("UTF-8")); + } + } + return bytes.toString("UTF-8"); + } catch (Exception e) { + e.printStackTrace(); + } + return input; + } } \ No newline at end of file -- 2.39.5