hgignore:帮助忽略除某些文件之外的所有文件

发布于 2024-08-07 10:44:51 字数 227 浏览 10 评论 0原文

我需要一个 .hgdontignore 文件:-) 来包含某些文件并排除目录中的其他所有文件。基本上我只想包含特定目录中的 .jar 文件,而不包含其他文件。我该怎么做?我对正则表达式语法不太熟练。或者我可以用 glob 语法来做到这一点吗? (为了便于阅读,我更喜欢这样)

作为示例位置,假设我想排除 foo/bar/ 下除 foo/bar/*.jar 之外的所有文件。

I need an .hgdontignore file :-) to include certain files and exclude everything else in a directory. Basically I want to include only the .jar files in a particular directory and nothing else. How can I do this? I'm not that skilled in regular expression syntax. Or can I do it with glob syntax? (I prefer that for readability)

Just as an example location, let's say I want to exclude all files under foo/bar/ except for foo/bar/*.jar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骄兵必败 2024-08-14 10:44:51

Michael 的答案很好,但另一种选择是排除:

foo/bar/**

然后手动添加 .jar 文件。您始终可以添加被忽略规则排除的文件,并且它会覆盖忽略。您只需要记住添加您将来创建的任何罐子即可。

The answer from Michael is a fine one, but another option is to just exclude:

foo/bar/**

and then manually add the .jar files. You can always add files that are excluded by an ignore rule and it overrides the ignore. You just have to remember to add any jars you create in the future.

残龙傲雪 2024-08-14 10:44:51

为此,您需要使用以下正则表达式:

foo/bar/.+?\.(?!jar).+

解释

您正在告诉它要忽略什么,因此该表达式正在搜索您想要的内容。

  1. 您查找名称(包括相对目录)包含 (foo/bar/) 的任何文件,
  2. 然后查找句点之前的任何字符 ( .+?\. == 匹配任意时间的一个或多个字符,直到到达句点字符)
  3. 然后确保它没有“jar”结尾(?!jar)(这称为否定前瞻
  4. 最后你抓住它确实有的结尾(.+)

正则表达式很容易搞乱,所以我强烈建议您使用 Regex Buddy 之类的工具来帮助您构建它们,它会分解正则表达式。翻译成简单的英语,这真的很有帮助

编辑

Jason S,你抓住了我,它确实错过了这些文件。

这个更正的正则表达式适用于您列出的每个示例:

foo/bar/(?!.*\.jar$).+

它找到:

  • foo/bar/baz.txt
  • foo/bar/baz
  • foo/bar/jar
  • foo/bar/baz.jar.txt
  • foo/bar/baz .jar.
  • foo/bar
  • /baz.txt。

但是没有找到

  • foo/bar/baz.jar

新解释

这表示在“foo/bar/”中查找文件,如果“.jar”后面有零个或多个字符,并且没有更多字符($ 表示行尾),则不匹配,如果不是这种情况,则匹配任何后续字符。

To do this, you'll need to use this regular expression:

foo/bar/.+?\.(?!jar).+

Explanation

You are telling it what to ignore, so this expression is searching for things you don't want.

  1. You look for any file whose name (including relative directory) includes (foo/bar/)
  2. You then look for any characters that precede a period ( .+?\. == match one or more characters of any time until you reach the period character)
  3. You then make sure it doesn't have the "jar" ending (?!jar) (This is called a negative look ahead
  4. Finally you grab the ending it does have (.+)

Regular expressions are easy to mess up, so I strongly suggest that you get a tool like Regex Buddy to help you build them. It will break down a regex into plain English which really helps.

EDIT

Hey Jason S, you caught me, it does miss those files.

This corrected regex will work for every example you listed:

foo/bar/(?!.*\.jar$).+

It finds:

  • foo/bar/baz.txt
  • foo/bar/baz
  • foo/bar/jar
  • foo/bar/baz.jar.txt
  • foo/bar/baz.jar.
  • foo/bar/baz.
  • foo/bar/baz.txt.

But does not find

  • foo/bar/baz.jar

New Explanation

This says look for files in "foo/bar/" , then do not match if there are zero or more characters followed by ".jar" and then no more characters ($ means end of the line), then, if that isn't the case, match any following characters.

瑕疵 2024-08-14 10:44:51

任何想要使用负向先行(或正则表达式语法中的 ?!)或任何类型的反向引用机制的人都应该知道,Mercurial 将从 google 的 RE2 回退到 Python 的re 模块进行匹配。

RE2 是一个非回溯引擎,保证运行时与大小呈线性关系输入的。如果性能对你来说很重要,也就是说,如果你有一个大的存储库,你应该考虑坚持使用 Re2 支持的更简单的模式,这就是为什么我认为 Ryan 提供的解决方案

Anyone that wants to use negative lookaheads (or ?! in regex syntax) or any kind of back-referencing mechanism should be aware that Mercurial will fall back from google's RE2 to Python's re module for matching.

RE2 is a non-backtracking engine that guarantees a run-time linear with the size of the input. If performance is important to you, that is if you have a big repository, you should consider sticking to more simple patterns that Re2 supports, which is why I think that the solution offered by Ryan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文