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

wip: feat: Make foam respect the .gitignore file #1413

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
26 changes: 25 additions & 1 deletion packages/foam-vscode/src/services/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEmpty } from 'lodash';
import { isEmpty, map, compact, filter, split, startsWith } from 'lodash';
import {
EndOfLine,
FileType,
Expand All @@ -25,6 +25,8 @@ import {
IDataStore,
IMatcher,
} from '../core/services/datastore';
import * as path from 'path';
import { Logger } from '../core/utils/log';

interface SelectionInfo {
document: TextDocument;
Expand Down Expand Up @@ -201,6 +203,28 @@ export async function createMatcherAndDataStore(excludes: string[]): Promise<{
const excludePatterns = new Map<string, string[]>();
workspace.workspaceFolders.forEach(f => excludePatterns.set(f.name, []));

// Read .gitignore files and add patterns to excludePatterns
for (const folder of workspace.workspaceFolders) {
const gitignoreUri = Uri.joinPath(folder.uri, '.gitignore');
try {
await workspace.fs.stat(gitignoreUri); // Check if the file exists
const gitignoreContent = await workspace.fs.readFile(gitignoreUri); // Read the file content
const patterns = map(
filter(
split(Buffer.from(gitignoreContent).toString('utf-8'), '\n'),
line => line && !startsWith(line, '#')
),
line => line.trim()
);
excludePatterns.get(folder.name).push(...compact(patterns));

Logger.info(`Excluded patterns from ${gitignoreUri.path}: ${patterns}`);
} catch (error) {
// .gitignore file does not exist, continue
Logger.error(`Error reading .gitignore file: ${error}`);
}
}

for (const exclude of excludes) {
const tokens = exclude.split('/');
const matchesFolder = workspace.workspaceFolders.find(
Expand Down