.gitattributes中的`* text = auto`和`* text eol = lf`有什么区别? [英] What is the differrence between `* text=auto` and `* text eol=lf` in .gitattributes?
问题描述
我一次又一次地查看 .gitattributes 的文档,但找不到清晰的文档回答这两者之间的区别是什么
I am looking again and again at the documentation of .gitattributes but I cannot find a clear answer on what is the differrence between these two:
* text=auto
* text eol=lf
text=auto
是否还打算仅与*
一起使用,或者也可以与特定扩展名一起使用?在这种情况下有什么区别?
Also is text=auto
intended only for use with *
or it can also be used with specific extensions? In such a case what is the differrence?
*.txt text=auto
*.txt text eol=lf
推荐答案
TL; DR
eol=lf
设置会覆盖所有text
设置,并且由于您已选择将其应用于所有路径,因此,如果使用eol=lf
设置,则将很重要.
TL;DR
The eol=lf
setting overrides any text
setting, and since you have chosen to apply this to every path, only the eol=lf
setting will matter, if you use that.
让我们从此开始并逐步开展工作:
Let's start with this and work outwards:
text=auto
是否还打算仅与*
一起使用,或者也可以与特定扩展名一起使用?
Also is
text=auto
intended only for use with*
or it can also be used with specific extensions?
模式可能包括扩展名. text=auto
部分是属性设置,模式会选择要应用于哪些文件的属性.
Patterns may include extensions. The text=auto
part is an attribute setting, and the patterns select which attributes to apply to which file(s).
.gitattributes
中的每一行都匹配或不匹配某些路径名,例如dir1/dir2/file.ext
或README.md
或其他名称.正如 gitattributes文档所述:
Each line in .gitattributes
matches, or does not match, some path name such as dir1/dir2/file.ext
or README.md
or whatever. As the gitattributes documentation says:
gitattributes
文件中的每一行的格式为:
Each line in
gitattributes
file is of form:
pattern attr1 attr2 ...
即,模式后面是属性列表,由空格分隔.前导和尾随空格将被忽略.以#开头的行将被忽略.以双引号开头的模式以C样式引用.当模式与所讨论的路径匹配时,该行上列出的属性将被赋予该路径.
That is, a pattern followed by an attributes list, separated by whitespaces. Leading and trailing whitespaces are ignored. Lines that begin with # are ignored. Patterns that begin with a double quote are quoted in C style. When the pattern matches the path in question, the attributes listed on the line are given to the path.
因此,*
是模式.这些样式"是与.gitignore
文件中的样式相同,但不允许使用否定模式.因此,您可以使用*.txt
和*.jpg
之类的模式来匹配文件扩展名,或者使用dir1/*
之类的模式来匹配特定目录中的文件(尽管还有另一种方法可以执行:像.gitignore
文件一样,您可以每个目录中可以包含.gitattributes
个文件,在这种情况下,它们适用于该目录及其子目录中的文件,但不适用于树中更高的路径.
Hence, *
is the pattern. These "patterns" are the same as those in .gitignore
files except that negative patterns are disallowed. Thus, you can use patterns like *.txt
and *.jpg
to match file name extensions, or patterns like dir1/*
to match files within a specific directory (although there is another way to do this: like .gitignore
files, you can have .gitattributes
files per directory, in which case they apply to files in that dierctory and its subdirectories, but not to paths higher in the tree).
现在,对于text
与text=auto
,以及对于eol=lf
与否,我们发现以下内容:
Now, for text
vs text=auto
, and for eol=lf
or not, we find the following:
对于给定路径,每个属性可以处于以下状态之一:
Each attribute can be in one of these states for a given path:
-
设置
Set
路径具有特殊值"true"的属性;这是 通过仅在列表中列出属性名称来指定 属性列表.
The path has the attribute with special value "true"; this is specified by listing only the name of the attribute in the attribute list.
未设置[详细信息已删除,但请参见下文]
Unset [details snipped, but see below]
设置为值
路径具有具有指定字符串值的属性;这是 通过列出属性名称后跟等号来指定 符号=及其值在属性列表中.
The path has the attribute with specified string value; this is specified by listing the name of the attribute followed by an equal sign = and its value in the attribute list.
未指定
没有模式与路径匹配,并且如果路径具有或 没有属性,据说路径的属性是 未指定.
No pattern matches the path, and nothing says if the path has or does not have the attribute, the attribute for the path is said to be Unspecified.
(在我看来,这最后一个词的措辞特别糟糕.它的真正含义是在与路径匹配的所有模式中,没有任何关于此属性的内容.")
(This last one's wording is particularly poor, in my opinion. It really means "of all patterns matching the path, none said anything about this attribute.")
因此,对于text
,属性为 set ,对于text=auto
,属性为 set为值.在这种情况下, value 部分为auto
.由于该模式为*
,因此它适用于所有文件.
So for text
, the attribute is set, and for text=auto
, the attribute is set to a value. The value part in this case is auto
. Since the pattern is *
, it applies to all files.
相同的逻辑适用于eol=lf
项目.如果首先,此eol=lf
以某种模式出现,其次,该模式与所讨论的文件匹配,则将eol
属性设置为一个值,并且该值为lf
.由于您建议的行是* text eol=lf
,因此这将使eol
设置为一个值,并且将text
set 设置为,但未设置为值.
This same logic applies to the eol=lf
item. If, first, this eol=lf
occurs in some pattern, and second, that pattern matches the file in question, then the eol
attribute is set to a value, and the value is lf
. Since your suggested line was * text eol=lf
, this would make eol
set to a value, and would make text
set, but not set to a value.
如果在单个.gitattributes
文件中写入两行序列:
If you write, in a single .gitattributes
file, the two line sequence:
* text=auto
* text eol=lf
第二行的text
会覆盖第一行的text
,因此将text
设置为(但不设置为值),将eol
设置为一个值,该值为lf
.两条线都匹配,第二条线覆盖了第一条线.
the second line's text
overrides the first one's, so that text
is set (but not to a value) and eol
is set to a value, with the value being lf
. Both lines matched, and the second line overrode the first.
如果您反转这两行:
* text eol=lf
* text=auto
然后再次两行都匹配,但是现在第二行仅覆盖text
设置,因此现在您已将text
设置为auto
,并且将eol
设置为lf
.
then again both lines match but now the second line only overrides the text
setting, so now you have text
set to auto
and eol
set to lf
.
此属性[
text
]启用并控制行尾规范化. ... [如果是]
This attribute [
text
] enables and controls end-of-line normalization. ... [If it is]
-
设置...启用行尾规范化并将路径标记为文本文件...
Set ... enables end-of-line normalization and marks the path as a text file ...
未设置...告诉Git在签入或签出时不要尝试任何行尾转换...
Unset ... tells Git not to attempt any end-of-line conversion upon checkin or checkout ...
设置为字符串值"auto" ...如果Git确定内容为文本...
Set to string value "auto" ... If Git decides that the content is text ...
未指定... Git使用core.autocrlf
配置变量...
Unspecified ... Git uses the core.autocrlf
configuration variable ...
(这意味着您必须追赶 git config
文档(如果未指定text
的话)以查明core.autocrlf
的作用.
(which means you have to go chase down the git config
documentation to find out what core.autocrlf
does, if you leave text
unspecified).
您已选择为每个文件设置它,或为每个文件将其设置为auto
.前者的意思是对每个文件进行转换",而后者的意思是auto
设置,它表示:嘿,Git,请为我确定文件是否为文本.如果您确定它是文本,请进行转换.
You have chosen to either set it for every file, or set it to auto
for every file. The former means "do conversion for every file" and the latter—the auto
setting—means: Hey, Git, please decide for me whether the file is text or not. If you decide that it is text, do the conversion.
在text
设置的描述下面是eol
设置的描述:
Just below the description for the text
setting is this description for the eol
setting:
此属性设置要在 工作目录.它可以实现行尾转换,而无需任何操作 内容检查,有效地设置
text
属性.
This attribute sets a specific line-ending style to be used in the working directory. It enables end-of-line conversion without any content checks, effectively setting the
text
attribute.
-
设置为字符串值"crlf" ... [由于设置了
lf
而被删除]
设置为字符串值"lf"
Set to string value "lf"
此设置强制Git将行尾标准化为LF 签入并防止在文件为 签出.
This setting forces Git to normalize line endings to LF on checkin and prevents conversion to CRLF when the file is checked out.
因此,如果为路径设置了eol=lf
-并且以*
为模式,则将其设置为每个路径-Git会将每个文件都视为文本,并执行从CR-LF的行尾到"checkin"的仅LF的行尾的转换(这是措辞不佳的:再次,转换实际上发生在git add
步骤中). Git在结帐期间不会执行任何操作(这也不是完美的措辞:在从索引提取到工作树的过程中,转换(或者在这种情况下是非转换)发生了.)
So, if you have eol=lf
set for a path—and with *
as the pattern, it will be set for every path—Git will treat every file as text, and do conversion from CR-LF line endings to LF-only line endings on "checkin" (this is badly phrased, again: the conversion actually occurs during the git add
step). Git will do nothing during checkout (this too is not perfectly phrased: the conversion—or in this case, non-conversion—happens during extraction from index to work-tree).
请注意,如果您选择*.txt
之类的模式,则仅针对与该模式匹配的路径设置这些属性 .对于其他路径,这些属性保持未设置.因此,您应该查看文档和看看这些属性未设置时会发生什么.
Note that if you choose a pattern like *.txt
, then these attributes are set only for paths that match the pattern. For other paths, these attributes remain unset. You should therefore look back at the documentation and see what happens when these attributes are unset.
您当然可以这样做:
* -text
*.txt eol=lf
第一行将在所有文件上显式取消设置 text
,而在所有文件上未指定eol
.然后,第二行将*.txt
文件的设置为值 eol=lf
,覆盖未指定的值.现在,Git将eol=lf
规则应用于名称与*.txt
匹配的所有文件,并对其余所有文件使用unspecified-eol和unset-text规则.
The first line will explicitly unset text
on all files, leaving eol
unspecified on all files. The second line then sets to a value eol=lf
for *.txt
files, overriding the unspecified value. Now Git will apply the eol=lf
rules to all files whose name matches *.txt
, and use the unspecified-eol and unset-text rules for all remaining files.
这种特殊的-text
语法是我上面摘录的内容.使用text=false
不会取消设置text
:它将text
设置为字符串值false
.这与保留text
未指定(不是专门针对 unset )具有相同的效果.使用-text
为它提供特殊的未设置设置.
This special -text
syntax is the stuff I snipped above. Using text=false
does not unset text
: it leaves text
set to the string value false
. This has the same effect as leaving text
unspecified (not specifically unset). Using -text
gives it the special unset setting.
未设置 text
和未指定 text
的区别在于,当未指定text
时,Git可能会尝试猜测-是否基于core.*
设置,例如core.autocrlf
-是否进行转换.当text
特别设置为 时,Git不会做任何猜测:对于该文件,它根本不会进行任何转换.
The difference between an unset text
and an unspecified text
is that when text
is unspecified, Git could attempt to guess—based on the core.*
settings like core.autocrlf
—whether to do conversions. When text
is specifically unset, Git will not do any guessing: it will just not do any conversion at all, for that file.
这篇关于.gitattributes中的`* text = auto`和`* text eol = lf`有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!