何时在gitignore中使用前导斜杠 [英] When to use leading slash in gitignore

查看:111
本文介绍了何时在gitignore中使用前导斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更清楚地理解 .gitignore 语法,特别是 gitignores。



我看到,前导斜杠用于仅匹配相对路径名到 .gitignore 文件的位置(来自 http:// git-scm .com / docs / gitignore ):
$ b


前导斜杠匹配路径名的开头。例如,/*.c匹配cat-file.c,但不匹配mozilla-sha1 / sha1.c。



<但是,当我删除主要斜杠时会发生什么?据我所知,有两种情况:


  1. 如果该模式不包含斜线(或者它只包含尾部斜线,这意味着它应该匹配一个目录),搜索在整个目录树内执行。例如, dir / 模式将匹配< root> / dir < root> / a / dir < root> / a / b / c /.../ dir 等,其中< root> .gitignore 文件的位置。

  2. pattern包含一个斜杠,它不在尾部位置(它不是最后一个字符),那么它只与相对于 .gitignore 文件位置的路径名匹配。

以下是我检查此行为的示例:

 #目录结构:
< root>
| - dir /
| | - 测试
| - src /
| | - dir /
| | | - 测试
测试文件仅仅是因为Git不跟踪空目录。

第一次测试:

 #.gitignore 
dir /

#git status
无需提交

所以Git忽略了两个 dir 目录。这与案例1一致:该模式没有斜线(除了尾部),所以Git正在观察整个目录树,忽略与该模式匹配的所有内容。



<第二个测试:

 #.gitignore 
/ dir /

#git status
未记录文件:
src /

这里,Git忽略了 dir 目录直接位于根目录下,这要归功于该模式中的前导斜杠。



第三个测试:

 #.gitignore 
dir / *

#git status
未记录的文件:
src /

这与案例2一致:该模式内有一些斜杠,所以它被认为是从根目录开始的路径名。



现在是真正的问题了。让我们考虑这个gitignore文件:当他们忽略目录下载程序/ ,是不是他们实际上忽略了整个目录树中的每一个下载器目录?这是我自从我之前看到有关Git的工作方式后就开始思考的问题。

因此,如果我碰巧有一个自定义模块,其中包含 downloader 目录中,它会被意外忽略,以及Magento根目录中的常规内容?这是一个反复的问题,因为它实际上已经发生在我身上,产生了一个真正难以发现的bug。

因此,在Magento .gitignore 文件(我指的只是作为一个例子,btw)很多模式都包含斜杠,所以它们与从根开始的路径名正确匹配,但有一些情况,如下载程序/ 错误/ ,如果我没有弄错,可能会有危险,改为 / downloader / / errors /

<作为一个更一般的问题,当我想从根开始显式地选择一个路径名,而不是将它用于包含斜杠的模式时,我应该总是使用不含斜线的模式的前导斜杠(除了尾部)我应该总是使用主导斜线清晰?您对此有何看法?



感谢您阅读并对比较长的文章感到抱歉。 方案

你完全回答了你自己的问题。如果你仔细看看github / gitignore repo,你会发现大多数文件使用不一致的关于如何编写模式的规则;很可能大部分是由不愿意阅读文档或测试内容的人员贡献的。

所以如果这样做有帮助:你是对的,有信心。

如果您发现类似这样的合作项目存在错误,请不要犹豫提供您的知识。如果您需要进一步建立您的信心,甚至有先例


I'm trying to understand more clearly the .gitignore syntax, and in particular as far as https://github.com/github/gitignore gitignores are concerned.

I see that the leading slash is used to match only pathnames relative to the location of the .gitignore file (from http://git-scm.com/docs/gitignore):

A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

But what happens when i remove the leading slash? As far as what i understood, there are two cases:

  1. If the pattern does not contain a slash (or it contains only a trailing slash, which means that it should match a directory), the search is performed inside the entire directory tree. For example, the pattern dir/ will match <root>/dir, <root>/a/dir, <root>/a/b/c/.../dir, etc., where <root> is the location of the .gitignore file.
  2. If the pattern contains a slash, which is not in the trailing position (it's not the last character), then it is matched only with pathnames relative to the .gitignore file location.

These are the examples i made to check this behaviour:

# Directory structure:
<root>
|- dir/
|   |- test
|- src/
|   |- dir/
|   |   |- test
test file is there only because Git does not track empty directories.

First test:

# .gitignore
dir/

# git status
nothing to commit

So Git is ignoring both dir directories. This is consistent with case number 1: the pattern has no slashes (except for the trailing one), so Git is watching the entire directory tree, ignoring everything that matches the pattern.

Second test:

# .gitignore
/dir/

# git status
Untracked files:
    src/

Here, Git is ignoring only the dir directory directly beneath the root directory, thanks to the leading slash in the pattern.

Third test:

# .gitignore
dir/*

# git status
Untracked files:
    src/

This is consistent with the case number 2: the pattern has some slash inside it, so it is considered as a pathname starting from the root directory.

Now it's time for the real question. Let's consider this gitignore file: when they ignore the directory downloader/, for example, aren't they actually ignoring every single downloader directory found in the entire directory tree? This is what i'm driven to think since what i saw about Git's workings before.

So if i happen to have a custom module with a downloader directory inside of it, will it be unexpectedly ignored as well as the regular one in the root of Magento? This is a bit of a rethorical question because it actually already happened to me, producing a really hard to find bug.

So, in the Magento .gitignore file (which i'm referring to only as an example, btw) a lot of the patterns contain slashes, so they are correctly matched against pathnames starting from the root, but there are a few cases, like downloader/ or errors/ that, if i'm not mistaken, are potentially dangerous, and which should probably be changed to /downloader/ and /errors/.

As a more general question, should i always use the leading slash for patterns not containing slashes (except for the trailing one) when i want to select a pathname explicitly starting from root, and not to use it for patterns containing slashes, or should i always use the leading slash for clarity? What do you think about it?

Thank you for reading and sorry for the long post.

解决方案

You've answered your own question entirely. If you look at the github/gitignore repo more closely, you'll see most files use inconsistent rules about how patterns are written; it's very likely most were contributed by people who didn't bother to read the documentation nor test things out as you did.

So if that helps: You're right, be confident.

If you see mistakes in collaborative projects such as this, don't hesitate to contribute your knowledge. There's even some precedent if you need to build up your confidence further.

这篇关于何时在gitignore中使用前导斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆