gitattributes中的"!eol"是什么意思?

31
最近我在一个.gitattributes文件中看到以下内容: "* text=auto !eol"
"!eol"是什么意思?

eol = 行末 (可能的猜测)。!eol 很可能与“非行末”有关。 - Bleeding Fingers
4个回答

25

Git有两个属性与行尾处理有关:

  1. text

文档说:

该属性启用和控制行尾标准化。当规范化文本文件时,其行尾在存储库中被转换为LF。

这实际上意味着当您提交到存储库时,它会将行尾转换为LF。

  1. eol

文档说:

该属性设置要在工作目录中使用的特定行结尾样式。它启用行尾标准化而不进行任何内容检查,有效地设置了text属性。

因此,text属性影响文件在存储库中的外观,eol属性影响文件在工作目录中的外观。

现在,一个属性可以有4种状态:

未指定值:
例如:* text

未设置:
例如:* -text

具有特定值:
例如:* text = auto

未指定:
例如:*!text

因此,* text=auto !eol表示:

所有文件的属性text设置为autoeol属性未指定。阅读文档,我们发现text=auto意味着您让Git决定文件是否为文本文件,如果是,则将使其规范化(将行尾在存储库中设置为LF)。

!eol表示将属性eol明确设置为未指定。在这种情况下,它与不指定它是相同的,指示Git查看core.autocrlfcore.eol配置设置以查看如何处理工作目录中的行结尾。请注意以下内容:

core.eol配置变量控制Git在您的工作目录中使用哪些行结尾标准化文件;默认情况下,使用本地平台的本机行结尾,或者如果设置了core.autocrlf,则使用CRLF。

但是,在以下情况下,您将使用!eol

* text=auto eol=crlf
test.txt !eol

基本上是将test.txt文件的eol属性从CRLF覆盖为未指定。这意味着对于除test.txt之外的所有文件,Git将在检出时将行尾转换为CRLF。对于test.txt,Git将遵循core.autocrlfcore.eol配置设置,因此在任何给定系统上,行尾可能是LF或CRLF。


6
* text=auto !eol

含义:

  • 二进制文件不进行任何行尾(EOL)转换。
  • 对于文本文件,检出文件时将EOL转换为操作系统相关的EOL(Unix转换为LF,Windows转换为CR+LF),并在检入时将其替换为LF。

为什么不进行EOL转换?(!是用于覆盖而不是否定。) - michas
@michas,我不确定你在这里的疑问是什么——上面的!eol仅适用于__非__文本文件。 - devnull
@RobertDailey 你好像忽略了 text=auto 这一部分! - devnull
@devnull 我想这就是我感到困惑的地方。text=auto 会使通配符文件被 Git 自动识别为文本或二进制文件。然而,我不确定第二个属性(!eol)如何影响每个文件。如果未设置 eol,它是否也会自动确定文件是二进制还是非二进制,并适当地翻译行结尾呢?为什么不完全省略 !eol(因为在我看来它的效果与不设置相同)? - void.pointer
1
@RobertDailey 以这种方式考虑:*表示匹配所有文件,text=auto表示根据操作系统相关的EOL处理文本文件,因此!eol适用于剩余的即二进制文件。 - devnull
显示剩余4条评论

4

根据文档,它基本上禁用了eol

有时你需要覆盖某个属性的设置,将其路径状态更改为未指定状态。这可以通过在属性名称前加上感叹号!来完成。

eol 的作用如下:

此属性设置特定的行尾样式以在工作目录中使用。它启用行尾规范化而不进行任何内容检查,从而设置文本属性。


2

简短版本:

如果Git认为内容是文本,它的行尾在提交时被规范化为LF。还原某些嵌套的.gitattributes文件中的任何显式eol设置。

请参阅man gitattributes

   Each line in gitattributes file is of form:

       pattern attr1 attr2 ...

   Sometimes you would need to override an setting of an attribute for a path to
   Unspecified state. This can be done by listing the name of the attribute
   prefixed with an exclamation point !.

   text
       This attribute enables and controls end-of-line normalization. When a text
       file is normalized, its line endings are converted to LF in the
       repository. To control what line ending style is used in the working
       directory, use the eol attribute for a single file and the core.eol
       configuration variable for all text files.

       Set to string value "auto"
           When text is set to "auto", the path is marked for automatic
           end-of-line normalization. If Git decides that the content is text,
           its line endings are normalized to LF on checkin.

   eol
       This attribute sets a specific line-ending style to be used in the working
       directory. It enables end-of-line normalization without any content
       checks, effectively setting the text attribute.

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