Using negative patterns for Subversion's svn:ignore property

Using Subversion's svn:ignore versioned property is pretty straight forward. Use a line-delimited list of patterns you want Subversion to not interest itself in. Here is an example to tell Subversion to ignore all files ending in .abc and .def:

svn propset svn:ignore "*.abc
*.def" .
property 'svn:ignore' set on '.'

(Note: On Windows, multi-line commands like the one above do not work. You need to use "svn propedit svn:ignore ." and then use the editor to add the two lines to the property value.) Like I said earlier, pretty straight forward. But what if you wanted to say something like: Hey Subversion...will you please ignore all files not ending in .xyz? Looking up the svn:ignore property in the Subversion book, linked to above, you will not find any mention of negation syntax. So it's not as simple as throwing in a "!*.xyz" into your svn:ignore property value. Well, after doing a little digging in the Subversion sources, and the mention in the Subversion book, you see that Subversion uses the fnmatch function to do its bidding. After looking around the internet for more information on what exactly fnmatch supports, I started playing around and I found a juicy little tidbit that will do exactly this. Here is an example:
svn propset svn:ignore "*[!x][!y][!z]
*.xyz?*" .
property 'svn:ignore' set on '.'

Yes...it's a little verbose but it works. The idea is to take any character of the extension, enclose it in square brackets behind an exclamation symbol. Just to make sure this sinks in, if you wanted to ignore all but .html files for a directory, here is an example:
svn propset svn:ignore "*[!h][!t][!m][!l]
*.html?*" .
property 'svn:ignore' set on '.'

There you have it. You can use this negation approach anywhere in the pattern, not just the end as the examples have shown. Maybe I can convince Mike Pilato to include this in the book...

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Your pattern doesn't work.

Your pattern doesn't work. It fails to ignore, for example, a file named "blah.xxx".

I guess you could use a set of patterns like this:

svn propset svn:ignore "???
??
?
*[!.]???
*[!x]??
*[!y]?
*[!z]" .

The idea is to exclude any filename that has less than four characters, or that doesn't have a "." in the fourth character from the end, or doesn't have an "x" in the third character from the end, or ...

But this is starting to get quite excruciating.