Hgignore: helps to ignore all files, but some of them

I need a .hgdontignore file :-) to include certain files and exclude everything else in the directory. Basically I want to include only .jar files in a specific directory and nothing else. How can i do this? I am not so skilled in regular expression syntax. Or can I do this using glob syntax? (I prefer this for readability)

As an example of a place, let's say I want to exclude all files in foo/bar/ except for foo/bar/*.jar .

+42
regex mercurial hgignore
02 Oct '09 at 10:15
source share
2 answers

For this you will need to use this regular expression:

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

Explanation

You say ignore, so this expression is looking for things that you do not need.

  • You are looking for any file whose name (including the relative directory) includes (foo / bar /)
  • Then you look for the characters preceding the period (. +? \. == correspond to one or more characters of any time until you reach the period character)
  • Then you will make sure that it has no end to "jar" (?! Jar) (This is called a negative look ahead
  • Finally, you will get the ending that he has (. +)

Regular expressions are easy to spoil, so I highly recommend that you get a tool like Regex Buddy to help you create them. It will break the regular expression into plain English, which really helps.

EDIT

Hey Jason S , you caught me, he skips these files.

This fixed regular expression will work for every example you give:

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

He 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 means looking for files in "foo / bar /", then do not match if there are zero or more characters followed by ".jar" and then more characters ($ means end of line), then if it is not, match any of the following characters.

+36
Oct 02 '09 at 22:27
source share

Michael's answer is fine, but another option is simply to exclude:

 foo/bar/** 

and then manually add the .jar files. You can always add files that are excluded from the ignore rule and overrides ignore. You just have to remember to add all the banks that you create in the future.

+41
04 Oct '09 at 4:21
source share



All Articles