Skip to content

Commit

Permalink
Introduce shallow gitignore parsing for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Jan 28, 2018
1 parent 9026678 commit fcc95cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 26 additions & 6 deletions gitignore.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';
const fs = require('fs');
const path = require('path');
const findUp = require('find-up');
const glob = require('glob');
const gitIgnore = require('ignore');
const multimatch = require('multimatch');
const pify = require('pify');
const slash = require('slash');

Expand Down Expand Up @@ -68,20 +70,38 @@ const normalizeOpts = opts => {
return {ignore, cwd};
};

const PASS_THROUGH = () => false;

module.exports = o => {
const opts = normalizeOpts(o);
const rootIgnore = path.join(opts.cwd, '.gitignore');

if (opts.ignore.length > 0 && multimatch(rootIgnore, opts.ignore)) {
return Promise.resolve(PASS_THROUGH);
}

if (!fs.existsSync(rootIgnore)) {
return Promise.resolve(PASS_THROUGH);
}

return globP('**/.gitignore', {ignore: opts.ignore, cwd: opts.cwd})
.then(paths => Promise.all(paths.map(file => getFile(file, opts.cwd))))
.then(files => reduceIgnore(files))
return getFile('.gitignore', opts.cwd)
.then(file => reduceIgnore([file]))
.then(ignores => getIsIgnoredPredecate(ignores, opts.cwd));
};

module.exports.sync = o => {
const opts = normalizeOpts(o);
const rootIgnore = path.join(opts.cwd, '.gitignore');

if (opts.ignore.length > 0 && multimatch(rootIgnore, opts.ignore)) {
return PASS_THROUGH;
}

if (!fs.existsSync(rootIgnore)) {
return PASS_THROUGH;
}

const paths = glob.sync('**/.gitignore', {ignore: opts.ignore, cwd: opts.cwd});
const files = paths.map(file => getFileSync(file, opts.cwd));
const ignores = reduceIgnore(files);
const file = getFileSync('.gitignore', opts.cwd);
const ignores = reduceIgnore([file]);
return getIsIgnoredPredecate(ignores, opts.cwd);
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@
"dependencies": {
"array-union": "^1.0.1",
"dir-glob": "^2.0.0",
"find-up": "^2.1.0",
"glob": "^7.1.2",
"ignore": "^3.3.5",
"multimatch": "^2.1.0",
"pify": "^3.0.0",
"slash": "^1.0.0"
},
Expand Down

0 comments on commit fcc95cf

Please sign in to comment.