libgit2 / libgit2/libgit2sharp
i18n.commitEncoding is EVIL
还没有人认领这个 Issue。
- 主要语言
- C#
- 星标
- 3.5k
- 派生
- 925
- PR 合并指标
- 30 天内没有已合并 PR
描述
Recently days I am researching i18n.commitEncoding, It's real an EVIL!!
First of all, I am a Chinese that OS language is en-us with Chinese(simplified) region. The commonly used encoding is GB2312(also GBK, code page 936).
The i18n.commitEncoding was defined in here that Character encoding the commit messages are stored in; git itself does not care per se, but this information is necessary.
OK, It's mean this config will affect the commit messages. but, I have find no any documents to define the format of encoding. I open the references window of Git GUI, and find the Default File Contents Encoding field. First item is System(cp936) of change menu, however, have an item Chinese Simplified (GB2312). They are equivalent in my first impression of two items.
I created a new repository then submit 3 commits with difference i18n.commitEncoding to test they. Both of they commit messages are four words Chinese, and i18n.commitEncoding followed by default, gb2312, cp936. (pushed to https://github.com/Aimeast/TestForFirst/commits/i18n)
git.execan display the second commitGit GUIcan display the first and third commitsGitHubcan display the first and second commits, third commit displayed as JapaneseLibGit2Sharpcan display the first and second commits (third commit not same with Github)
OK, the result real funny. I decompress objects files for digging.
- The first and second commits (e2266b and 38a2b0) were stored as
utf-8 - The third commit (bd9f62) was stored as
gb2312
After my analysis, git.exe can identify format start with cp and followed by code page number. So, we can explain why there is such result.
Now, let's into the issue. The code
// NativeMethods.git_commit_message
[DllImport(libgit2)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(Utf8NoCleanupMarshaler))]
internal static extern string git_commit_message(GitObjectSafeHandle commit);
It's always marshal as Utf-8 result in third commit message was messy code. So, I suggest that return raw data then decode string in Proxy.git_commit_message. But, the evil is .Net Framework not support some encoding which supported by git.exe.
Hereafter is my code for detect the encoding for a commit
public static Encoding CpAsEncoding(this Commit commit)
{
try
{
var encoding = commit.Encoding;
if (encoding.StartsWith("cp", StringComparison.OrdinalIgnoreCase))
return Encoding.GetEncoding(int.Parse(encoding.Substring(2)));
return Encoding.GetEncoding(encoding);
}
catch
{
return Encoding.UTF8;
}
}
Not perfect codes, but It's working.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 NativeMethods.git_commit_message 和 Proxy.git_commit_message 开始,然后检查 Commit.Encoding 的公开方式,以及 Utf8NoCleanupMarshaler 如何处理 native 结果。使用 default、gb2312 和 cp936 编码与三个示例 commit 比较行为;当 Git 声明了受支持的代码页编码时,commit 消息能够正确解码,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- csharp, git
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100