为什么在Linux上,.gitattributes不能覆盖core.autocrlf配置?

12
在为一个项目设置Git时,我注意到Linux和Windows上的行尾规范化处理方式有所不同。
根据我对Git文档的理解,在Windows上的行为是正确的。具体来说,当存在.gitattributes文件时,它应该覆盖core.autocrlf设置。
以下表格显示了我进行的一些实验的结果。左侧两列显示.gitattributes文件和core.autocrlf设置。其他列显示以下git命令的结果:
  1. git rm --cached <file>(强制执行下一次检出以执行行归一化处理)。
  2. git checkout HEAD -- <file>(检出文件,应用行结束标准化)
  3. git ls-files --eol <file>(检查工作树中的行结束标准)
如你所见,在Linux上,似乎即使存在.gitattributes文件,core.autocrlf设置也会生效。
我希望得到一些帮助来确定这是否实际上是一个错误。
2个回答

2
看起来误解是将text属性解释为与autocrlf配置设置相同的意思。
从gitattributes文档(https://git-scm.com/docs/gitattributes)中可以了解到:
该属性启用和控制行尾规范化。当规范化文本文件时,其行尾在存储库中被转换为LF。要控制工作目录中使用的行尾样式,请使用单个文件的eol属性和所有文本文件的core.eol配置变量。请注意,core.autocrlf覆盖core.eol(加粗部分为重点)。
因此,如果您想使用属性来控制工作树中的行尾,您需要设置eol属性(而不仅仅是text属性)。
实际上,在处理行尾方面涉及到几个属性和几个配置选项,并且在Windows和*nix之间默认行为存在差异(出于设计目的)。从git config文档(https://git-scm.com/docs/git-config)中可以了解到:
core.eol
设置在core.autocrlf为false时为具有text属性设置的文件在工作目录中使用的行结束类型。可选值为lf、crlf和native,其中native使用平台的本机行结尾。默认值为native。
因此,回答您的问题:不,您描述的不是错误。

我还是有些困惑。根据你的解释,即使我将core.autocrlf设置为“false”,为什么在Windows上仍然会得到CRLF呢?这应该覆盖core.eol,对吧? - jgreen81
@eversceptic - 这就是gitattributes文档所说的,但最好要根据core.eol和core.autocrlf的定义来解释(最坏的情况下 - 我认为 - gitattributes文档只是想指出eol设置可能不是明确的)。请阅读上面提到的core.eol文档片段。当autocrlf为false时使用它。 - Mark Adelsberger

1
注意:.gitattributes文档已在Git 2.21(2019年2月)进行了更新。

查看提交 c9446f0, 提交 2b68085 (2019年1月29日) 作者为Jeff King (peff)
帮助者:Torsten Bögershausen (tboegi)
(由Junio C Hamano -- gitster --提交13e2630中合并,2019年2月7日)

doc/gitattributes: 澄清 "autocrlf 覆盖 eol"

只有在 core.autocrlf 被设置为除 "false" 以外的值时,我们才会使用它来覆盖 core.eol
让我们更加明确地阐述这一点,并指向 git-config 的定义,其中对此进行了更详细的讨论。

请注意,将 core.autocrlf 设置为 trueinput 将覆盖 core.eol(请参见 git-config 中这些选项的定义)。

这与 Mark Adelsberger答案 关于 core.eol 配置 相关。
但是,这在 Git 2.21 中已经发生了变化:

docs/config: clarify "text property" in core.eol

`core.eol`:

Sets the line ending type to use in the working directory for files that are
have the text property set when core.autocrlf is false
marked as text (either by having the text attribute set, or by having text=auto and Git auto-detecting the contents as text).

Alternatives are 'lf', 'crlf' and 'native', which uses the platform's native line ending.
The default value is native.
See gitattributes for more information on end-of-line conversion.
Note that this value is ignored if core.autocrlf is set to true or input.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接