gpt4 book ai didi

git - .gitignore 路径匹配语法中的前导星号 "**/"是否多余?

转载 作者:太空狗 更新时间:2023-10-29 12:59:37 29 4
gpt4 key购买 nike

是否有任何用法不能用不带星号的等价物代替?

Two consecutive asterisks ("**") in patterns matched against full
pathname may have special meaning:

A leading "**" followed by a slash means match in all directories.
For example, "**/foo" matches file or directory "foo" anywhere,
the same as pattern "foo". "**/foo/bar" matches file or directory
"bar" anywhere that is directly under directory "foo".

A trailing "/**" matches everything inside. For example, "abc/**"
matches all files inside directory "abc", relative to the location
of the .gitignore file, with infinite depth.

A slash followed by two consecutive asterisks then a slash matches
zero or more directories. For example, "a/**/b" matches "a/b",
"a/x/b", "a/x/y/b" and so on.

Other consecutive asterisks are considered invalid.

https://git-scm.com/docs/gitignore#_pattern_format

让我指出,我只询问前导星号/斜线冗余。因为似乎任何 **/foo/bar 都可以用简单的 foo/bar

代替

例如,我的 .gitignore 文件中有以下内容:

# NuGet Packages

# The packages folder can be ignored because of Package Restore
**/packages/*

# except build/, which is used as an MSBuild target.
!**/packages/build/

# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config

我想知道,为什么他们不能简单地写:

# NuGet Packages

# The packages folder can be ignored because of Package Restore
packages/*

# except build/, which is used as an MSBuild target.
!packages/build/

# Uncomment if necessary however generally it will be regenerated when needed
#!packages/repositories.config

https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

最佳答案

不,仅当剩余模式不包含斜线(尾随斜线除外)时,前导 **/ 才会过时。

文档位于 https://git-scm.com/docs/gitignore在我看来有点误导,正如它所说的那样

If the pattern does not contain a slash /, Git treats it as a shell glob
pattern and checks for a match against the pathname relative to the
location of the .gitignore file (relative to the toplevel of the work
tree if not from a .gitignore file).

但实际上只匹配文件或目录的名称,而不是整个路径。

因此 **/packages/build 在任何深度上都匹配 packages/build
packages/build 因为它包含一个斜杠,实际上与 /packages/build 相同,并且只匹配与 packages/build 相同级别的忽略文件。

另一方面,build 在任何深度上都匹配 build,因此实际上与 **/build 相同。
/build 仅匹配与忽略文件相同级别的 build

它始终取决于模式(如果存在则在删除尾部斜杠之后)是否包含任何斜杠。

关于git - .gitignore 路径匹配语法中的前导星号 "**/"是否多余?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761128/

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