armink / SFUD

An using JEDEC's SFDP standard serial (SPI) flash universal driver library | 一款使用 JEDEC SFDP 标准的串行 (SPI) Flash 通用驱动库
MIT License
1.25k stars 444 forks source link

The encoding of source code cannot be processed correctly by Git | 源代码的编码无法被 Git 正确处理 #70

Closed pzhlkj6612 closed 1 year ago

pzhlkj6612 commented 2 years ago

Background | 背景

Some editors automatically chose the "UTF-8" encoding when opening the "sfud/src/sfud_sfdp.c" file. In VS Code, we can see the following on line 226 of this file: 一些编辑器自动选择“UTF-8”编码来打开“sfud/src/sfud_sfdp.c”。在 VS Code 中,我们能在这个文件的第226行看到如下内容:

    //TODO ĿǰΪ 1.0 ���ṩ�ķ�ʽ������֧�� V1.5 �����ϵķ�ʽ��ȡ page size

I guess the garbled text is Chinese, so I tried to reopen the file with the "GB 2312" encoding: 我猜这些乱码实际上是中文文本,因此试着用“GB 2312”编码重新打开了这个文件:

    //TODO 目前为 1.0 所提供的方式,后期支持 V1.5 及以上的方式读取 page size

Now it's readable. 现在这些内容是可读的了。

Since modern frontend frameworks and web browsers are smart enough, we don't seem to be bothered by encoding issue on GitHub: 由于现代的前端框架与浏览器足够智能,在 GitHub 上我们似乎不会被编码所困扰:

https://github.com/armink/SFUD/blob/09911665bbcc90e4fb7b96a0ac0ee6bd483be220/sfud/src/sfud_sfdp.c#L226

Problem | 问题

Let's make some changes in "sfud/src/sfud_sfdp.c". Open the file with "GB 2312" in VS Code and add some characters like this: 在“sfud/src/sfud_sfdp.c”里做些修改吧。以“GB 2312”编码打开它并添加一些字符:

-     //TODO 目前为 1.0 所提供的方式,后期支持 V1.5 及以上的方式读取 page size
+     //TODO 目前为 1.0 所提供的方式,后期支持 V1.5 及以上的方式读取 page size 中文Chinese中文

Then, call "git diff": 接着执行“git diff”:

$ git diff --unified=0 sfud/src/sfud_sfdp.c
diff --git a/sfud/src/sfud_sfdp.c b/sfud/src/sfud_sfdp.c
index c77a755..a5f4a3c 100644
--- a/sfud/src/sfud_sfdp.c
+++ b/sfud/src/sfud_sfdp.c
@@ -226 +226 @@ static bool read_basic_table(sfud_flash *flash, sfdp_para_header *basic_header)
-    //TODO ĿǰΪ 1.0 <CB><F9><CC>ṩ<B5>ķ<BD>ʽ<A3><AC><BA><F3><C6><DA>֧<B3><D6> V1.5 <BC><B0><D2><D4><C9>ϵķ<BD>ʽ<B6><C1>ȡ page size
+    //TODO ĿǰΪ 1.0 <CB><F9><CC>ṩ<B5>ķ<BD>ʽ<A3><AC><BA><F3><C6><DA>֧<B3><D6> V1.5 <BC><B0><D2><D4><C9>ϵķ<BD>ʽ<B6><C1>ȡ page size <D6><D0><CE><C4>Chinese<D6><D0><CE><C4>

Well, these characters represented in hexadecimal are not what we expect. How to make the file encoding correctly recognized by Git? 呃,我们并不需要这些十六进制形式的字符。那么如何让 Git 正确识别文件的编码呢?

After some searching, I've found two solutions: 经过搜索,我找到了两种方法:

Reference | 参考

slipperstree commented 2 years ago

You can enable VSCode [Auto guess encoding] to avoid this. Hope it can help.

pzhlkj6612 commented 1 year ago

adfc1c714b73fd6b4f0a4a025f362a2305e7c205 fixed this issue, now the output of "git-diff" looks fine: adfc1c714b73fd6b4f0a4a025f362a2305e7c205 修复了这个问题,现在“git-diff”的输出看起来没问题了:

$ git diff --unified=0 -- sfud/src/sfud_sfdp.c
diff --git a/sfud/src/sfud_sfdp.c b/sfud/src/sfud_sfdp.c
index 0564f12..e19b4ac 100644
--- a/sfud/src/sfud_sfdp.c
+++ b/sfud/src/sfud_sfdp.c
@@ -226 +226 @@ static bool read_basic_table(sfud_flash *flash, sfdp_para_header *basic_header)
-    //TODO 目前为 1.0 所提供的方式,后期支持 V1.5 及以上的方式读取 page size
+    //TODO 目前为 1.0 所提供的方式,后期支持 V1.5 及以上的方式读取 page size 中文Chinese中文