包含斯堪的纳维亚字符的 GIT 输出的编码问题

问题描述 投票:0回答:2

我正在使用 C# 程序读取 Git 日志输出并从中解析报告。基本上我运行一个命令

git log --name-status --pretty=fuller --after="2016-08-14" -before="2016-11-03"

使用以下代码。

// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command  that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo gitInfo = new System.Diagnostics.ProcessStartInfo();
gitInfo.CreateNoWindow = true;
gitInfo.RedirectStandardError = true;
gitInfo.RedirectStandardOutput = true;
gitInfo.FileName = GIT_installed_directory + @"\bin\git.exe";

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
gitInfo.RedirectStandardOutput = true;
gitInfo.UseShellExecute = false;
// Do not create the black window.
gitInfo.CreateNoWindow = true;
gitInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;

// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process GitProcess = new System.Diagnostics.Process();
gitInfo.Arguments = GIT_command; // such as "fetch orign"
gitInfo.WorkingDirectory = GIT_Repository_Path;
GitProcess.StartInfo = gitInfo;
GitProcess.Start();
// Get the output into a string
string result = GitProcess.StandardOutput.ReadToEnd();
result = result + GitProcess.StandardError.ReadToEnd();

GitProcess.WaitForExit();
GitProcess.Close();

从 Git 读取的结果以字符串形式获取。最初没有任何编码,Git 的输出以一种有趣的方式显示了所有斯堪的纳维亚字符。

例如“Käytettävyys”(“Käytettävyys”)

我将编码添加到UTF8之后

gitInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;

Git 提交中给出的注释获得了正确的编码,但文件名中的斯堪的纳维亚字符被转换为转义字符。

参见示例: 这是直接来自 Git cmd 控制台的。

D:\>git log --name-status --pretty=fuller --after="2016-08-14" --before="2016-11-07"
commit 07754d5dd6b0f105233e73068a636c59b875b5f6
Author:     xxxxxxxx
AuthorDate: Fri Nov 4 13:27:57 2016 +0200
Commit:     xxxxxxxx
CommitDate: Fri Nov 4 13:27:59 2016 +0200

Kosmeettinen muutos

M       xxxxxxxxxx/Pelkkää KÖKKÖÄ.mrx
M       xxxxxxxxxx/Pelkkää KÖKKÖÄ.rpx

commit 28713f66ad16231315e2cf5318e4e2b3815305eb
Author:     xxxxxxxxxx
AuthorDate: Fri Nov 4 13:24:48 2016 +0200
Commit:     xxxxxxxxxx
CommitDate: Fri Nov 4 13:24:51 2016 +0200

Lisätty gittiin ääkkösten ja öökkästen testaamista varten

A       xxxxxxxxxx/Some file.mrx
A       xxxxxxxxxx/Some file.rpx
A       xxxxxxxxxx/Pelkkää KÖKKÖÄ.mrx
A       xxxxxxxxxx/Pelkkää KÖKKÖÄ.rpx

commit 6276b2ef46c7d6ff737a65583c4afe6b02a01bb4

这与我的 C# 程序中的输出相同:

commit 07754d5dd6b0f105233e73068a636c59b875b5f6
Author:     xxxxxxxx
AuthorDate: Fri Nov 4 13:27:57 2016 +0200
Commit:     xxxxxxxx
CommitDate: Fri Nov 4 13:27:59 2016 +0200

Kosmeettinen muutos

M   "xxxxxxxx/Pelkk\303\244\303\244 K\303\226KK\303\226\303\204.mrx"
M   "xxxxxxxx/Pelkk\303\244\303\244 K\303\226KK\303\226\303\204.rpx"

commit 28713f66ad16231315e2cf5318e4e2b3815305eb
Author:     xxxxxxxx
AuthorDate: Fri Nov 4 13:24:48 2016 +0200
Commit:     xxxxxxxx
CommitDate: Fri Nov 4 13:24:51 2016 +0200

Lisätty gittiin ääkkösten ja öökkästen testaamista varten

A   xxxxxxxxxx/Some file.mrx
A   xxxxxxxxxx/Some file.mrx
A   "xxxxxxxx/Pelkk\303\244\303\244 K\303\226KK\303\226\303\204.mrx"
A   "xxxxxxxx/Pelkk\303\244\303\244 K\303\226KK\303\226\303\204.rpx"

commit 6276b2ef46c7d6ff737a65583c4afe6b02a01bb4

我应该进行什么样的额外转换才能获得正确的文件名?

c# git encoding utf-8
2个回答
0
投票

据我所知 GIT 使用

ErrorOutput
,所以:

gitInfo.Standard**Error**Encoding = System.Text.Encoding.UTF8;

是我的解决方案。


0
投票

这很古老,但谷歌搜索没有找到太多答案。

我通过使用“另存为”下拉菜单保存有问题的文件并选择编码(高级保存选项)进行了测试。从 1252 切换到 65001 (UTF-8) 使 Git 中的情况看起来更好。是的,我知道还有其他几种可能性(我一生中使用过的最高编号的代码页恰好工作得很好)。

© www.soinside.com 2019 - 2024. All rights reserved.