-->

Weird negative lookahead handling in hgignore

2019-08-03 13:49发布

问题:

Out of a huge source tree, i want just one subfolder be tracked by Mercurial.

specific/component/subfolder

In any decent regex tool (Regex Coach, regexpal.com), the following is absolutely sufficient (and understandable)

^(?!specific/component/subfolder).+

meanwhile .hgignore insists on having

^(?!extras).+
^extras/(?!extensions).+
^extras/extensions/(?!sharing).+

as soon as i put slash inside the lookahead group, nothing gets through.

Before calling this a bug (perhaps of yet another homegrown regex parser?) i ask the collective inteligence :-)

Credits for indirect clue on what the heck hgignore wants go to

Honing the .hgignore file using a negative lookahead

回答1:

Just don't use negative lookahead in .hgignore. Really. Every time someone suggests it here in Stack Overflow I chime in letting them know that it doesn't work well because it's not an intended feature. If Mercurial's .hgignore file was supposed to be a whitelist system it would have a real whitelist syntax. It's a blacklist and should be used as such.

The negative lookahead syntax almost-works precisely because it's not a homegrown regex parser -- it's the one built into python and going out of the way do disable negative lookahead seems like overkill, but maybe was the better choice.

Two solutions better than using the never-going-to-be-satisfying-to-you negative look ahead handling:

  • just move the repository one level lower -- down into the folder you actually want to track
  • -or- blacklist everything and then 'hg add' the files you want track -- add overrides ignore

Seriously. This wasn't an intended feature and it's never going to work well but can't be removed for backwards compatibility reasons.