hgignore:帮助忽略除某些文件之外的所有文件
我需要一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Michael 的答案很好,但另一种选择是排除:
然后手动添加 .jar 文件。您始终可以添加被忽略规则排除的文件,并且它会覆盖忽略。您只需要记住添加您将来创建的任何罐子即可。
The answer from Michael is a fine one, but another option is to just exclude:
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.
为此,您需要使用以下正则表达式:
解释
您正在告诉它要忽略什么,因此该表达式正在搜索您不想要的内容。
正则表达式很容易搞乱,所以我强烈建议您使用 Regex Buddy 之类的工具来帮助您构建它们,它会分解正则表达式。翻译成简单的英语,这真的很有帮助
编辑
嘿Jason S,你抓住了我,它确实错过了这些文件。
这个更正的正则表达式适用于您列出的每个示例:
它找到:
但是没有找到
新解释
这表示在“foo/bar/”中查找文件,如果“.jar”后面有零个或多个字符,并且没有更多字符($ 表示行尾),则不匹配,如果不是这种情况,则匹配任何后续字符。
To do this, you'll need to use this regular expression:
Explanation
You are telling it what to ignore, so this expression is searching for things you don't want.
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:
It finds:
But does not find
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.
任何想要使用负向先行(或正则表达式语法中的
?!
)或任何类型的反向引用机制的人都应该知道,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'sRE2
to Python'sre
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.