Skip to content

Commit

Permalink
Respect .git/info/exclude (resolves #613)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed May 10, 2024
1 parent 2f75272 commit 29a0bdc
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions packages/knip/src/util/globby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import picomatch from 'picomatch';
import { GLOBAL_IGNORE_PATTERNS, ROOT_WORKSPACE_NAME } from '../constants.js';
import { timerify } from './Performance.js';
import { debugLogObject } from './debug.js';
import { isFile } from './fs.js';
import { dirname, join, relative, toPosix } from './path.js';

const walk = promisify(_walk);
Expand Down Expand Up @@ -73,41 +74,46 @@ async function parseFindGitignores(options: Options): Promise<Gitignores> {
return false;
};

const entryFilter = (entry: Entry) => {
if (entry.dirent.isFile() && entry.name === '.gitignore') {
gitignoreFiles.push(entry.path);

const dir = dirname(toPosix(entry.path));
const base = relative(options.cwd, dir);
const dirIgnores = base === '' ? ['.git', ...GLOBAL_IGNORE_PATTERNS] : [];
const dirUnignores = [];

for (const rule of parseGitignoreFile(entry.path)) {
const [p, ext] = rule.patterns;
if (rule.negated) {
if (base === '') {
if (!unignores.includes(ext)) dirUnignores.push(...rule.patterns);
} else {
if (!unignores.includes(ext.startsWith('**/') ? ext : `**/${ext}`)) {
dirUnignores.push(join(base, p), join(base, ext));
}
const addFile = (filePath: string) => {
gitignoreFiles.push(filePath);

const dir = dirname(toPosix(filePath));
const base = relative(options.cwd, dir);
const dirIgnores = base === '' ? ['.git', ...GLOBAL_IGNORE_PATTERNS] : [];
const dirUnignores = [];

for (const rule of parseGitignoreFile(filePath)) {
const [p, ext] = rule.patterns;
if (rule.negated) {
if (base === '') {
if (!unignores.includes(ext)) dirUnignores.push(...rule.patterns);
} else {
if (!unignores.includes(ext.startsWith('**/') ? ext : `**/${ext}`)) {
dirUnignores.push(join(base, p), join(base, ext));
}
}
} else {
if (base === '') {
if (!ignores.includes(ext)) dirIgnores.push(...rule.patterns);
} else {
if (base === '') {
if (!ignores.includes(ext)) dirIgnores.push(...rule.patterns);
} else {
if (!ignores.includes(ext.startsWith('**/') ? ext : `**/${ext}`)) {
dirIgnores.push(join(base, p), join(base, ext));
}
if (!ignores.includes(ext.startsWith('**/') ? ext : `**/${ext}`)) {
dirIgnores.push(join(base, p), join(base, ext));
}
}
}
}

ignores.push(...dirIgnores);
unignores.push(...dirUnignores);
cachedIgnores.set(dir, { ignores: dirIgnores, unignores: dirUnignores });
matchers.push(...dirIgnores.map(ignore => _picomatch(ignore, pmOptions)));
};

ignores.push(...dirIgnores);
unignores.push(...dirUnignores);
cachedIgnores.set(dir, { ignores: dirIgnores, unignores: dirUnignores });
matchers.push(...dirIgnores.map(ignore => _picomatch(ignore, pmOptions)));
if (isFile('.git/info/exclude')) addFile('.git/info/exclude');

const entryFilter = (entry: Entry) => {
if (entry.dirent.isFile() && entry.name === '.gitignore') {
addFile(entry.path);
return true;
}
return false;
Expand Down

0 comments on commit 29a0bdc

Please sign in to comment.