简体   繁体   English

如何定义hg忽略异常?

[英]How to define hg ignore exceptions?

gitignore has the ability to define ignore exception by using optional prefix ! gitignore能够通过使用可选前缀来定义忽略异常 , for example , in order not to ignore particular file foo.dll : 例如 ,为了不忽略特定文件foo.dll

*.dll        # Exclude all dlls
!foo.dll     # Except for foo.dll 

How can we define such exceptions in hgignore ? 我们怎样才能在hgignore定义这样的异常?

Mercurial only allows exceptions to ignore patterns to be encoded in the same rule that defines the ignore pattern (usually via (?!...) , (?<!...) or similar regular expression constructs), even though its matching engine is considerably more powerful (and can in principle match on file size and other things specified in hg help filesets ). Mercurial只允许异常忽略在定义忽略模式的同一规则中编码的模式(通常通过(?!...)(?<!...)或类似的正则表达式构造),即使它的匹配引擎功能强大得多(原则上可以匹配文件大小和hg help filesets集中指定的其他内容)。

This has two reasons, composability and performance. 这有两个原因,可组合性和性能。

Composability 组合性

When you have an include/exclude system for patterns, then order matters. 如果您有模式的包含/排除系统,那么订购很重要。 Mercurial has various sources for ignore files: The .hgignore file in the repository, as well as any ui.ignore and ui.ignore.* option in a hgrc file that Mercurial processes, and every file included by a include: directive from the above list. Mercurial有各种忽略文件的来源:存储库中的hgrc文件,以及Mercurial处理的.hgignore文件中的任何ui.ignoreui.ignore.*选项,以及上面的include:指令包含的每个文件名单。 Allowing rules to interact with each other across various ignore files in surprising ways can lead to surprising situations that are difficult to debug. 允许规则以令人惊讶的方式跨各种忽略文件相互交互可能导致难以调试的令人惊讶的情况。 Alternatively, one kind of pattern (inclusion or exclusion) could be prioritized over others, but that poses problems of its own. 或者,一种模式(包含或排除)可以优先于其他模式,但这会产生其自身的问题。

Performance 性能

Mercurial uses ignore files to prune directory tree walks. Mercurial使用忽略文件来修剪目录树遍历。 If a directory matches an ignore rule, it is skipped in its entirety (such as a build output directory with thousands of temporary files). 如果目录与忽略规则匹配,则会完全跳过该目录(例如,包含数千个临时文件的构建输出目录)。 When you have an ignore pattern that matches a directory, but also an exception that matches a file in the directory, your options are to either not prune the directory walk or to disregard the exception for the directory (Git chooses to do the latter). 如果您具有与目录匹配的忽略模式,但也是与目录中的文件匹配的异常,则您可以选择不修剪目录walk或忽略目录的异常(Git选择执行后者)。 Not pruning the directory walk is inefficient, disregarding exceptions within the directory severely limits the number of use cases that aren't already handled well by regular expressions. 不修剪目录遍历是低效的,忽略目录中的异常严重限制了正则表达式尚未处理的用例数量。

Solution & Workarounds 解决方案和解决方法

A major difference between Mercurial and Git is that Mercurial provides the option to use Python's extended regular expressions in addition to glob patterns (Git uses glob patterns only). Mercurial和Git之间的一个主要区别是Mercurial提供了除了glob模式之外还使用Python的扩展正则表达式的选项(Git仅使用glob模式)。 Regular expressions eliminate the need for exceptions for most (but not all) use cases. 正则表达式消除了大多数(但不是全部)用例的异常需求。

These cases are usually handled adequately via a re:(?!exception)pattern line in your .hgignore file (and in some cases, other advanced regular expression patterns). 这些情况通常通过.hgignore文件中的re:(?!exception)pattern行充分处理(在某些情况下,还有其他高级正则表达式模式)。 Note that the (?x) meta pattern, which ignores unescaped whitespace and comments in a regular expression, can be useful to make complex expressions more readable. 请注意, (?x)元模式忽略了未转义的空白和正则表达式中的注释,可以使复杂表达式更具可读性。 For example, the following two are equivalent: 例如,以下两个是等效的:

re:(?x) (^|/) (?!foobar) foo # do not ignore files starting with `foobar`.
re:(^|/)(?!foobar)foo

This works reasonably well for ignore files, since file patterns rarely include whitespace or hash symbols. 这对于忽略文件非常有效,因为文件模式很少包含空格或哈希符号。 Note also that you can switch to regular expression syntax on a line-by-line basis by prefixing it with re: or regexp: , even if you normally use syntax: glob . 另请注意,即使您通常使用syntax: glob ,也可以通过在re:regexp:前面添加前缀来逐行切换到正则表达式语法。

If that is insufficient, is possible to modify the existing behavior via extensions if no other option reasonably applies. 如果这不够,可以通过扩展修改现有行为,如果没有合理适用的其他选项。 You can override mercurial.dirstate.dirstate.{_ignore,_dirignore} or you can automatically add -I/-X options to the relevant commands based on (say) an .hginclexcl file. 您可以覆盖mercurial.dirstate.dirstate.{_ignore,_dirignore}或者您可以根据(例如) .hginclexcl文件自动将-I/-X选项添加到相关命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM