Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce shallow gitignore parsing for better performance #66

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ const runners = [{
run: patterns => {
globbyMaster.sync(patterns);
}
}, {
name: 'globby async gitignore (working directory)',
run: (patterns, cb) => {
globby(patterns, {gitignore: true}).then(cb.bind(null, null), cb);
}
}, {
name: 'globby async gitignore (upstream/master)',
run: (patterns, cb) => {
globbyMaster(patterns, {gitignore: true}).then(cb.bind(null, null), cb);
}
}, {
name: 'globby sync gitignore (working directory)',
run: patterns => {
globby.sync(patterns, {gitignore: true});
}
}, {
name: 'globby sync gitignore (upstream/master)',
run: patterns => {
globbyMaster.sync(patterns, {gitignore: true});
}
}, {
name: 'glob-stream',
run: (patterns, cb) => {
Expand All @@ -53,15 +73,18 @@ const benchs = [{
patterns: ['a/*', '!a/**']
}, {
name: 'multiple positive globs',
patterns: ['a/*', 'b/*']
patterns: ['a/*', 'b/*', 'c/*']
}];

before(() => {
process.chdir(__dirname);
rimraf.sync(BENCH_DIR);
fs.mkdirSync(BENCH_DIR);
process.chdir(BENCH_DIR);
['a', 'b']

fs.writeFileSync('.gitignore', 'c');

['a', 'b', 'c']
.map(dir => `${dir}/`)
.forEach(dir => {
fs.mkdirSync(dir);
Expand Down
33 changes: 25 additions & 8 deletions gitignore.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const gitIgnore = require('ignore');
const multimatch = require('multimatch');
const pify = require('pify');
const slash = require('slash');

const globP = pify(glob);
const readFileP = pify(fs.readFile);

const mapGitIgnorePatternTo = base => ignore => {
Expand Down Expand Up @@ -69,20 +68,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).length > 0) {
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).length > 0) {
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);
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dir-glob": "^2.0.0",
"glob": "^7.1.2",
"ignore": "^3.3.5",
"multimatch": "^2.1.0",
"pify": "^3.0.0",
"slash": "^1.0.0"
},
Expand Down