From 0a95a9131d4423e2a11b36832fdcb7edd71478a9 Mon Sep 17 00:00:00 2001 From: Kael Zhang Date: Wed, 14 Aug 2019 17:14:28 +0800 Subject: [PATCH] 5.1.3: gitignore rules 2.22.1 --- index.js | 17 +++++++++-------- package.json | 2 +- test/fixtures/cases.js | 31 ++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index f676f24..6ea9cd1 100644 --- a/index.js +++ b/index.js @@ -111,14 +111,6 @@ const REPLACERS = [ // > A leading slash matches the beginning of the pathname. // > For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c". // A leading slash matches the beginning of the pathname - - // WTF! https://git-scm.com/docs/gitignore changes, the new rules are - - // > If there is a separator at the beginning or middle (or both) - // > of the pattern, then the pattern is relative to the directory level of - // >.the particular .gitignore file itself. Otherwise the pattern may also - // > match at any level below the .gitignore level. - /^\//, () => '^' ], @@ -174,11 +166,20 @@ const REPLACERS = [ // If starts with '**', adding a '^' to the regular expression also works /^(?=[^^])/, function startingReplacer () { + // If has a slash `/` at the beginning or middle return !/\/(?!$)/.test(this) + // > Prior to 2.22.1 // > If the pattern does not contain a slash /, // > Git treats it as a shell glob pattern // Actually, if there is only a trailing slash, // git also treats it as a shell glob pattern + + // After 2.22.1 (but compatible) + // > If there is a separator at the beginning or middle (or both) + // > of the pattern, then the pattern is relative to the directory + // > level of the particular .gitignore file itself. + // > Otherwise the pattern may also match at any level below + // > the .gitignore level. ? '(?:^|\\/)' // > Otherwise, Git treats the pattern as a shell glob suitable for diff --git a/package.json b/package.json index 75d26c3..510af3c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ignore", - "version": "5.1.2", + "version": "5.1.3", "description": "Ignore is a manager and filter for .gitignore rules, the one used by eslint, gitbook and many others.", "files": [ "legacy.js", diff --git a/test/fixtures/cases.js b/test/fixtures/cases.js index d160a61..a2803c1 100644 --- a/test/fixtures/cases.js +++ b/test/fixtures/cases.js @@ -9,17 +9,26 @@ function readPatterns (file) { } const cases = [ - // [ - // '#56', - // [ - // '/*/', - // '!/foo/' - // ], - // { - // 'foo/bar.js': 0 - // }, - // true - // ], + [ + 'gitignore 2.22.1 example', + [ + 'doc/frotz/' + ], + { + 'doc/frotz/': 1, + 'a/doc/frotz/': 0 + } + ], + [ + '#56', + [ + '/*/', + '!/foo/' + ], + { + 'foo/bar.js': 0 + } + ], [ 'object prototype', [