mei
mei

Reputation: 33

Ignore all files except one or more files from a specific file type in Mercurial?

Identical question to gitignore: Ignore all files in folder hierarchy except one specific filetype, but for Mercurial.

I want .hgignore to ignore all files except one or more files from a specific file type, for instance only allow files of the *.java and *.class file type to be tracked, in the current directory as well as sub directories.

For example, in the following directory:

A.java
a.txt
a.png
A.class
B/C.java
B/G/H/h.png
B/C.class
D/E/F.java

I only want the following files to be included (including the structure of the directories):

A.java
A.class
B/C.java
B/C.class
D/E/F.java

(it does not matter if the directory containing h.png will be created or not.)

I've tried applying and changing the same principles as mentioned in the question above (but for Mercurial instead), as well as trying to apply principles from the question How can I ignore all directories except one using .hgignore?, without any luck.

EDIT: I tried the following with the mentioned directory tree above

$ hg stat
? A.class
? A.java
? B/C.class
? B/C.java
? B/G/H/h.png
? D/E/F.java
? a.png
? a.txt
$ hg add -I "**.{class, java}"
adding A.class
adding A.java
adding B/C.class
adding B/C.java
adding D/E/F.java

which gave me the wanted result. How do I create a .hgignore or config file that applies the -I "**.{class, java}" for each add or commit (and similar queries)?

Upvotes: 3

Views: 3840

Answers (2)

bug
bug

Reputation: 565

Try this:

syntax: regexp
.*\.(?!java$|class$)

or better yet:

syntax: regexp
.*\.(?!java$|.*\.java$|class$|.*\.class$)

which will also match weird stuff like "a..java" or "a.java.java".

EDIT: Here's a revised solution incorporating Søren's tips and some further tweaking on my part. I hope the false positives e.g. that something like "ajava" or "java" didn't get ignored, are fixed now.

syntax: regexp
\.(?!java$|class$)[^.]+$
(?<!\.)java$
(?<!\.)class$

Upvotes: 3

Lazy Badger
Lazy Badger

Reputation: 97375

If ignored patterns are countable, why not use list of glob patterns? In your case for ignoring everywhere in repo-tree all *.class and *.java (if I understand task correctly) .hgrc will be

syntax: glob

**.class
**.java

If you want ignore all files with a lot of (or possible unknown at all) extensions except some you can try to use filesets (hg help filesets) in files-definition. Ignoring all extensions except *.png for your case will be smth. like (TBT!)

"set:not **.png"

Upvotes: 0

Related Questions