hgignore: help ignoring all files but certain ones

本小妞迷上赌 提交于 2019-11-26 18:03:12

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1512058/hgignore-help-ignoring-all-files-but-certain-ones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!