From a6c6607368d8012814df6cece0749f6a078894c3 Mon Sep 17 00:00:00 2001 From: Dean Ferreyra Date: Thu, 30 Sep 2021 16:07:58 -0700 Subject: [PATCH] Change the utf-8 encoding to elide the BOM The "main_encoding" encoding is used for creating file lists. Without this change, if "main_encoding" is set to "utf-8", file lists that are written include the UTF-8 BOM which causes errors in Mercurial. --- src/Mercurial.Net/ClientExecutable.cs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Mercurial.Net/ClientExecutable.cs b/src/Mercurial.Net/ClientExecutable.cs index 7e11ca0..45936a8 100644 --- a/src/Mercurial.Net/ClientExecutable.cs +++ b/src/Mercurial.Net/ClientExecutable.cs @@ -325,7 +325,7 @@ public static Encoding GetMainEncoding() try { - return Encoding.GetEncoding(encName); + return GetEncoding(encName); } catch { @@ -347,7 +347,7 @@ public static Encoding GetTerminalEncoding() try { - return Encoding.GetEncoding(encName); + return GetEncoding(encName); } catch { @@ -356,6 +356,28 @@ public static Encoding GetTerminalEncoding() } + /// + /// Returns an encoding based on the name. Specifically for the + /// case of "utf-8", it returns a UTF-8 encoding without the BOM. + /// + private static Encoding GetEncoding(string encName) + { + // By retrieving the requested encoding first we can compare + // against the well-defined web name "utf-8" and not worry + // about how the encoding providers resolved the string in + // encName. + var encoding = Encoding.GetEncoding(encName); + if (encoding.WebName == "utf-8") + { + // Return UTF-8 encoding without the BOM. + return new UTF8Encoding(false, true); + } + else + { + return encoding; + } + } + /// /// Attempts to locate the command line client executable. ///