0
我在用C#程序读取Git日志输出并从中解析报告。基本上我运行命令包含斯堪的纳维亚字符的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中给出的承诺得到正确的编码,但在文件名中的斯堪的纳维亚字符转换为转义字符的意见。
请参阅示例: 这是直接从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
我应该做的是什么样的额外的转换也得到文件名吧?
Git本身(打算是)编码不可知的。如果你放入UTF-8,它会发回UTF-8。如果你把UTF-16-LE放进去,它会发送回去。如果你输入任意的字节码,它会将它们发送回去。另请参阅http://unicode.org/faq/utf_bom.html。对于*路径*,它不是Git,而是您的操作系统,它强制约束(例如,MacOS对代码的组合很挑剔)。你的C#接口也可能做它自己的事情 - 我没有使用C#(并避免一般的Windows),所以不能说这个问题的一面。 – torek
RTM:https://git-scm.com/docs/git-show/1.8.2.2 :)(特别是在该页面的底部,“讨论”部分) –