gpt4 book ai didi

java - 为什么 GIT 中的文本行结尾不统一,尽管 * text=auto

转载 作者:行者123 更新时间:2023-12-01 19:48:02 24 4
gpt4 key购买 nike

我正在克隆现有的 GIT 存储库,并且 .gitattributes 指定 * text=auto !eol

根据我对文档的阅读,text=auto 指定所有“文本”文件都应在存储库中标准化。

Git recognizes files encoded in ASCII or one of its supersets (e.g. UTF-8, ISO-8859-1, …​) as text files

“标准化”意味着 LF 仅作为行结尾。

所以,我不知道如何检查混淆的存储库中的行结尾,但在工作区中我得到了混合文本(.java/.sql 文件)。有些以 LF 结尾,有些以 CRLF 结尾。

由于 .gitattributes' !eol 意味着没有工作区行结尾的规范,我认为它们将与存储库中的一样。

那么,为什么要混合呢?理想情况下,我希望在存储库和工作区中所有文本文件行都以 LF 结尾?

最佳答案

Since .gitattributes' !eol in means no specification for workspace line endings, I assumed they'd be as they are in the repo.

是的,但仅仅因为text=auto当前设置并不意味着文件是用LF checkin 的。仅仅因为文件是用 LF checkin 的,并不意味着工作副本也必须是 LF。这只是意味着您 checkin 的任何新内容都将在 checkin 期间从 CRLF 转换为 LF...但不是工作副本!

简而言之,您会得到 CRLF 和 LF 的混合,因为存储库混合了 CRLF 和 LF。 text=auto 所做的只是使 checkin 的内容在存储库中为 LF。

示例

这里有一个例子来说明。

让我们首先创建一个新的存储库并添加一个以 CRLF 行结尾的文本文件:

$ git init
Initialized empty Git repository in /.../test/.git/
$ cat > file.txt
line 1
line 2
$ unix2dos file.txt
unix2dos: converting file file.txt to DOS format...
$ git add file.txt
$ git commit -m 'added file'
[master (root-commit) f21d72f] added file
1 file changed, 2 insertions(+)
create mode 100644 file.txt

此时,存储库中的文件工作副本中的文件都有CRLF。

我们现在将设置text=auto eol。这不会更改文件或将其标记为脏。仍然是 CRLF。

$ cat > .gitattributes
* text=auto !eol
$ git add .
$ git commit -m 'add .gitattributes'
[master 1844576] add .gitattributes
1 file changed, 1 insertion(+)
create mode 100644 .gitattributes
$ file file.txt
file.txt: ASCII text, with CRLF line terminators
$ git status
On branch master
nothing to commit, working tree clean

我们可以删除该文件并 checkout 它,但由于存储库中的文件具有 CRLF,因此我们在 checkout 时仍然会得到 CRLF。

$ rm file.txt
$ git checkout -- file.txt
$ file file.txt
file.txt: ASCII text, with CRLF, LF line terminators

现在让我们添加一个带有 CRLF 的新文件。这将在存储库中标准化,但工作副本将不受影响。

$ cat > file2.txt
line 3
line 4
$ unix2dos file2.txt
unix2dos: converting file file2.txt to DOS format...
$ git add file2.txt
warning: CRLF will be replaced by LF in file2.txt.
The file will have its original line endings
in your working directory
$ git commit -m 'added file 2'
[master cc2c5c3] added file 2
1 file changed, 2 insertions(+)
create mode 100644 file2.txt
$ file file.txt file2.txt
file.txt: ASCII text, with CRLF line terminators
file2.txt: ASCII text, with CRLF line terminators

即使该文件在存储库中为 LF,在工作副本中为 CRLF,但它并不脏,再次检查它不会改变它。

$ git status
On branch master
nothing to commit, working tree clean
$ git checkout -- file.txt file2.txt
$ file file.txt file2.txt
file.txt: ASCII text, with CRLF line terminators
file2.txt: ASCII text, with CRLF line terminators

我们可以通过删除它然后再次检查来获取 LF 版本:

$ rm file2.txt
$ git checkout -- file2.txt
$ file file2.txt
file2.txt: ASCII text

请注意,它现在有 LF。

设置 eol=lf 无法修复您的存储库

只有当您的存储库首先包含正确规范化的文件时,eol 设置才真正起作用。更改它不会修复我们的 file.txt

$ cat >.gitattributes
*.txt text eol=lf
$ git commit -m 'set eol=lf'
[master c9e346b] set eol=lf
1 file changed, 1 insertion(+), 1 deletion(-)
$ rm file.txt
$ git checkout -- file.txt
$ file file.txt
file.txt: ASCII text, with CRLF line terminators

如何修复

解决问题并让 LF 无处不在的直接方法是在文件上实际运行 dos2unix。一旦您执行此操作并提交规范化文件,您将在 checkout 时随处看到 LF。

关于java - 为什么 GIT 中的文本行结尾不统一,尽管 * text=auto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52448013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com