From b34048803ad6cf0a0a0c998696a41de118715452 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 10 Sep 2012 16:25:10 -0400
Subject: [PATCH] Fix bug in diff view for filenames with non-ASCII characters (issue 128)

---
 src/com/gitblit/utils/StringUtils.java          |   36 ++++++++++++++++++
 src/com/gitblit/utils/GitBlitDiffFormatter.java |   15 ++++++-
 docs/04_releases.mkd                            |    8 ++++
 docs/03_faq.mkd                                 |    7 +++
 src/com/gitblit/utils/GitWebDiffFormatter.java  |   10 ++--
 5 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/docs/03_faq.mkd b/docs/03_faq.mkd
index 36be82b..cdf3d59 100644
--- a/docs/03_faq.mkd
+++ b/docs/03_faq.mkd
@@ -71,6 +71,13 @@
 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 db7815b..d7b598b 100644
--- a/docs/04_releases.mkd
+++ b/docs/04_releases.mkd
@@ -9,6 +9,14 @@
 
 **%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] &nbsp; *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 e9fbf52..2966aa8 100644
--- a/src/com/gitblit/utils/GitBlitDiffFormatter.java
+++ b/src/com/gitblit/utils/GitBlitDiffFormatter.java
@@ -127,15 +127,24 @@
 			} 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("</tbody></table></div>\n");
diff --git a/src/com/gitblit/utils/GitWebDiffFormatter.java b/src/com/gitblit/utils/GitWebDiffFormatter.java
index 50c6c72..e657dc5 100644
--- a/src/com/gitblit/utils/GitWebDiffFormatter.java
+++ b/src/com/gitblit/utils/GitWebDiffFormatter.java
@@ -106,10 +106,10 @@
 			throws IOException {
 		switch (prefix) {
 		case '+':
-			os.write("<span class=\"diff add\">".getBytes());
+			os.write("<span style=\"color:#008000;\">".getBytes());
 			break;
 		case '-':
-			os.write("<span class=\"diff remove\">".getBytes());
+			os.write("<span style=\"color:#800000;\">".getBytes());
 			break;
 		}
 		os.write(prefix);
@@ -140,11 +140,11 @@
 		sb.append("<div class=\"diff\">");
 		for (String line : lines) {
 			if (line.startsWith("diff")) {
-				sb.append("<div class=\"diff header\">").append(line).append("</div>");
+				sb.append("<div class=\"diff header\">").append(StringUtils.convertOctal(line)).append("</div>");
 			} else if (line.startsWith("---")) {
-				sb.append("<span class=\"diff remove\">").append(line).append("</span><br/>");
+				sb.append("<span style=\"color:#800000;\">").append(StringUtils.convertOctal(line)).append("</span><br/>");
 			} else if (line.startsWith("+++")) {
-				sb.append("<span class=\"diff add\">").append(line).append("</span><br/>");
+				sb.append("<span style=\"color:#008000;\">").append(StringUtils.convertOctal(line)).append("</span><br/>");
 			} else {
 				sb.append(line).append('\n');
 			}
diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java
index 08fd497..e440790 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 @@
 		}
 		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

--
Gitblit v1.9.1