我四处搜索,寻找任何提出类似问题的人。它确实不是似乎是 :set fileformat=dos
或 :set fileformat=unix
的简单情况。
用 :set fileencoding=latin1
和 :set fileformat=dos
写出文件改变了 git diff
报告所有行到附加了 ^M。
代码最初是愉快地存在的:
...
if (v == value32S)
{
...
我做了非常彻底的改进(在 vim 的屏幕上看起来不错):
...
if (v == value32S ||
v == value33)
{
...
但是 git diff
检查错误的变化显示:
diff --git a/csettings.cpp b/csettings.cpp index 1234..8901 100755 --- a/csettings.cpp +++ b/csettings.cpp @@ -2466,7 +2466,8 @@ bool MyClass::settingIsValid(QString s) #if CONFIG_1 || CONFIG_2 - if (v == value32S) + if (v == value32S ||^M + v == value33)^M { doSomething(new_v);
where the bold italic text is reverse video.
I have tried several means to make the apparently spurious carriage returns go away. First was to be sure there wasn't a hidden character. View with vim
:set list
:
...
if (v == value32S ||$
v == value33)$
{$
...
看起来不错。转储文件(微细节因保护NDA而有所不同,我懒得让它成为一个完美的欺骗):
$ hd csettings.cpp
(...)
0000eae0 xx xx xx xx xx xx xx xx xx 65 33 32 53 20 7c 7c |(v == value32S |||
0000eaf0 0d 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 20 |.. v |
0000eb00 3d 3a 20 xx xx xx xx xx xx 65 33 33 29 0d 0a 20 |== ...value33).. |
所有 其他行也以“0d 0a”结尾,所以这看起来不错。一个有趣的建议是使用 cat -e
(这对我来说是新的):
$ cat -e c.cpp
...
if (v == value32S ||^M$
v == value33)^M$
{^M$
...
另一个建议是使用 file
作为线索:
$ file csettings.cpp
csettings.cpp: C source, UTF-8 Unicode text, with CRLF line terminators
有趣的是,这是该目录(包含头文件和 cpp 代码)中唯一不是 ASCII 文本
的文件。有些文件有 CRLF 行终止符,有些则没有。此外,有些显示 C++ 源代码
,而其他显示的是 C 源代码
,我认为这并不重要。
删除文件和 git checkout
以获取新副本也将其显示为 UTF-8,我追踪到某些字符串中有度数符号(“°F”和“°C” ) 所以 UTF-8 似乎不是问题。
仍然,我不明白为什么使用 vim
编辑只有这些行 会导致这个问题。或者这可能不是问题?有什么想法吗?
----- 附录 -----
git config --get-regexp core.*
显示
core.repositoryformatversion 0
core.filemode true
core.bare false
core.logallrefupdates true
最佳答案
默认情况下,Git 假定您在存储库中使用 Unix 行结尾并将回车符突出显示为尾随空格。但是,默认情况下,它仅在新行中突出显示尾随空格,因为目标是避免引入新问题。
如果您运行 git diff --ws-error-highlight=all
,您会看到在被删除的行和上下文行上也有回车符。如果你不想看到这个,你可以将core.whitespace
设置为cr-at-eol
,这样就不会被高亮显示了。对此没有不良影响;它只是防止回车被视为尾随空格。
如果你打算在非 Windows 系统上使用这个项目,你应该将行尾转换为 Unix 并使用 .gitattributes
文件来指定 text
文本文件的属性,因此行尾会根据使用的操作系统自动转换。即使您的项目仅在 Windows 上使用,这也可能很有值(value),因为如果有人设置了 core.autocrlf
,您可能会以混合行结尾结束。
关于linux - 如何修复显示 ^M 和 "git diff"的已更改源代码行,但使用 vim/gedit/cat -e 看起来不错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54695796/