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

时间:2016-11-07 10:36:53

标签: c# git encoding utf-8

我正在使用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的输出以一种有趣的方式显示了所有斯堪的纳维亚人物。

E.g。 “Käytettävyys”(“Käytettävyys”)

我将编码添加到UTF8后

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

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

参见示例: 这是直接来自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

我还需要进行哪些额外的转换才能获得正确的文件名?

1 个答案:

答案 0 :(得分:0)

据我所知,GIT使用ErrorOutput,所以:

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

是我的解决方案。

相关问题