Skip to content

Commit d0214ee

Browse files
committed
Properly account for nested .gitignore - Fix #2
1 parent 359a7c0 commit d0214ee

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@ var fs = require('fs');
33
var path = require('path');
44
var gulpIgnore = require('gulp-ignore');
55

6-
var appendStars = function (str) {
6+
var appendStars = function (dirname, str) {
7+
var prefix = '';
8+
if (dirname) {
9+
prefix = dirname + '/';
10+
}
711
return [
8-
str + '**',
9-
str + '/**'
12+
prefix + str + '**',
13+
prefix + str + '/**'
1014
];
1115
};
1216

1317
module.exports = function (gitignorePath) {
1418
gitignorePath = path.resolve(gitignorePath || '.gitignore');
19+
var dirname = path.dirname(path.relative(process.cwd(), gitignorePath));
20+
if (dirname === '.') {
21+
dirname = '';
22+
}
1523

1624
var contents = fs.readFileSync(gitignorePath, 'utf8');
1725
var ignoredFiles = contents.split('\n')
1826
.map(str => str.trim())
1927
.filter(Boolean) // ignore empty lines
20-
.map(appendStars)
28+
.map(str => appendStars(dirname, str))
2129
.reduce((m, paths) => m.concat(paths), []);
2230

2331
return gulpIgnore.exclude(ignoredFiles);

test/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('gulp-exclude-gitignore', function () {
2626

2727
this.stub.withArgs(path.resolve('.gitignore'), 'utf8').returns(contents);
2828
this.stub.withArgs(path.resolve('custom.gitignore'), 'utf8').returns('bar\n');
29+
this.stub.withArgs(path.resolve('sub/.gitignore'), 'utf8').returns('a.txt\n');
2930
});
3031

3132
afterEach(function () {
@@ -81,4 +82,22 @@ describe('gulp-exclude-gitignore', function () {
8182
stream.write(fakeFile('bar/c.txt'));
8283
stream.end();
8384
});
85+
86+
it('correctly account for nested .gitignore', function (done) {
87+
var stream = excludeGitignore('sub/.gitignore');
88+
89+
var filePaths = [];
90+
stream.on('data', function (file) {
91+
filePaths.push(file.relative);
92+
});
93+
94+
stream.on('finish', function () {
95+
assert.deepEqual(filePaths, ['a.txt']);
96+
done();
97+
});
98+
99+
stream.write(fakeFile('a.txt'));
100+
stream.write(fakeFile('sub/a.txt'));
101+
stream.end();
102+
});
84103
});

0 commit comments

Comments
 (0)