summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Zschocke <f.zschocke+git@gmail.com>2022-11-11 12:12:13 +0100
committerFlorian Zschocke <f.zschocke+git@gmail.com>2022-11-11 12:12:13 +0100
commitd00bdf2feed0f0a2eefe437ab9dc1561f0049002 (patch)
treeb7efe127817f4471364b4f7a9880731f7dc5c5c9
parentd9933399a64be0ae30b8ea63d568b9092b850386 (diff)
downloadgitblit-d00bdf2feed0f0a2eefe437ab9dc1561f0049002.tar.gz
gitblit-d00bdf2feed0f0a2eefe437ab9dc1561f0049002.zip
bugtraq: Fallback to UTF-8 if commit encoding is unsupported
Reading the encoding of a commit can result in a Unsupported- or IllegalCharsetException. This happens when for whatever reason the commit has an encoding recorded that the system doesn't understand. Instead of completely failing, fallback to UTF-8.
-rw-r--r--src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java
index bce0bd8a..ca0b317f 100644
--- a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java
+++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java
@@ -31,6 +31,9 @@ package com.syntevo.bugtraq;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -55,6 +58,8 @@ import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
public final class BugtraqConfig {
// Constants ==============================================================
@@ -229,7 +234,7 @@ public final class BugtraqConfig {
FileMode entmode = tw.getFileMode(0);
if (FileMode.REGULAR_FILE == entmode) {
ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
- content = new String(ldr.getCachedBytes(), commit.getEncoding());
+ content = new String(ldr.getCachedBytes(), guessEncoding(commit));
break;
}
}
@@ -265,6 +270,15 @@ public final class BugtraqConfig {
return baseConfig;
}
+ @NotNull
+ private static Charset guessEncoding(RevCommit commit) {
+ try {
+ return commit.getEncoding();
+ } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
+ return UTF_8;
+ }
+ }
+
@Nullable
private static String getString(@Nullable String subsection, @NotNull String key, @NotNull Config config, @Nullable Config baseConfig) {
final String value = config.getString(BUGTRAQ, subsection, key);