gitignore-parser icon indicating copy to clipboard operation
gitignore-parser copied to clipboard

Pattern matching is too greedy

Open johnhaley81 opened this issue 10 years ago • 2 comments

var parser = require('gitignore-parser').compile('package');

parser.accepts('package.json'); // false

I would think that package.json should still be accepted since git CLI would not ignore that file with that .gitignore.

johnhaley81 avatar Apr 07 '15 16:04 johnhaley81

I found the same thing. This logic is incorrect:

    accepts: function (input) {
      if (input[0] === '/') input = input.slice(1);
      return negatives[0].test(input) || !positives[0].test(input);
    },

It should be something like (but write tests to verify since I only tested my use case):

    accepts: function (input) {
      if (input[0] === '/') input = input.slice(1);
      if (positives[0].test(input)) return false;
      return negatives[0].test(input));
    },

I found it through doing something like and testing node_modules/backbone/package.json:

node_modules/
!node_modules/backbone/*

This library will list things in the backbone directory, but git would ignore the files based on the first condition before getting to the second one.

kmalakoff avatar Nov 19 '15 19:11 kmalakoff

I tried to port gitignore tests from libgit2 to to this module and stopped. The problem is that actually implementing gitignore to be fully compliant is very complex.

I've moved over to using the following module: https://github.com/kaelzhang/node-ignore

kmalakoff avatar Nov 20 '15 00:11 kmalakoff