emcrisostomo / fswatch

A cross-platform file change monitor with multiple backends: Apple OS X File System Events, *BSD kqueue, Solaris/Illumos File Events Notification, Linux inotify, Microsoft Windows and a stat()-based backend.
https://emcrisostomo.github.io/fswatch/
GNU General Public License v3.0
5.01k stars 326 forks source link

Can't combine --include/--exclude with --event? #140

Closed joshcameron closed 7 years ago

joshcameron commented 7 years ago

I am trying to use fswatch to trigger a script which deletes .DS_Store files under my home directory as they are created/updated.

I can restrict fswatch's output to operations on .DS_Store files using --exclude=".*" --include="DS_Store". I also want to refrain from triggering my script when .DS_Store files are deleted -- especially since it's likely they are being deleted by my script.

When I add --event Created and/or --event Updated to my fswatch command line, my script starts to be called for files other than the .DS_Store files specified by my --include and --exclude flags.

I expected the --event flags would further restrict the number of times my script is invoked, instead of overriding --include and --exclude.

emcrisostomo commented 7 years ago

I'm not able to reproduce this behaviour. Can verify that fswatch is outputting the wrong events by examining its output without going through your script?

joshcameron commented 7 years ago

Looking at my command lines again today, I think I was experiencing a combination of errors on my part, only two of which fswatch could do a better job of handling. And I've misreported what I was doing in my issue above.

My main problem was a malformed regex on --include. I was using *\.DS_Store when I should have been using .*\.DS_Store. The former matched nothing, but I guess I just happened to receive no events (or not notice any) other than .DS_Storechanges in my initial testing. So I thought it was working. Later, when I realized I needed to use --exclude to blacklist every other event, I stopped getting the .DS_Store events. This happened around the same time I started trying to filter by event type, which led me to think filtering by event type was causing me to stop seeing the .DS_Store events.

My second problem was I was not using the --include and --exclude flags properly. Instead of --include="DS_Store", I was doing --include "DS_Store". I was missing the =. Likewise on the --exclude flag. This caused my arguments to --include and --exclude to be interpreted as command arguments instead of flag arguments. This was definitely still my error, but in this case fswatch could have helped me by telling me I hadn't specified any argument to --include or --exclude, and perhaps pointing me in the right direction by saying "are you missing an = after the flag?".

At some point I started using the = on my --include and --exclude flags, but since I was still having problems due to my malformed regex, I tried using -i and -e instead. This led to my third problem: I was using -i and -e, but I was still using an = after them. So my exclude flag was -e=.*. The = was interpreted as part of my regex, causing the things I wanted excluded to no longer be excluded. Again, my error, but fswatch could perhaps have helped me by complaining about the absence of a space between the -e and the regex following it.

Given that you already have people out there using fswatch, I guess the best you could do would be to issue warnings about the above two mistakes, since making them errors would break too many scripts.

Anyway, that's the full story. I am now able to use --include and --exclude with --event successfully. Thanks for the useful tool and for the prompt response to issues!

emcrisostomo commented 7 years ago

Thanks for the explanation @joshcameron.