From 66e4db7f7c6f6be7a2ae59f50db2301a6dc38bde Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 7 Mar 2025 09:52:31 -0800 Subject: [PATCH] should ignore p1 --- core/core.ts | 4 ++ core/indexing/shouldIgnore.test.ts | 53 ++++++++++++++++++++ core/indexing/shouldIgnore.ts | 80 ++++++++++++++++++++++++++++++ core/test/testDir.ts | 4 +- 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 core/indexing/shouldIgnore.test.ts create mode 100644 core/indexing/shouldIgnore.ts diff --git a/core/core.ts b/core/core.ts index 180ce8642..dd05484d3 100644 --- a/core/core.ts +++ b/core/core.ts @@ -829,6 +829,10 @@ export class Core { // Codebase indexing on("index/forceReIndex", async ({ data }) => { + const { config } = await this.configHandler.loadConfig(); + if (config?.disableIndexing) { + return; // TODO silent in case of commands? + } if (data?.shouldClearIndexes) { const codebaseIndexer = await this.codebaseIndexerPromise; await codebaseIndexer.clearIndexes(); diff --git a/core/indexing/shouldIgnore.test.ts b/core/indexing/shouldIgnore.test.ts new file mode 100644 index 000000000..31981c134 --- /dev/null +++ b/core/indexing/shouldIgnore.test.ts @@ -0,0 +1,53 @@ +// Generated by continue + +// Import modules +import { describe, expect, test } from "@jest/globals"; +import { testIde } from "../test/fixtures"; +import { + TEST_DIR, + addToTestDir, + setUpTestDir, + tearDownTestDir, +} from "../test/testDir"; +import { shouldIgnore } from "./shouldIgnore"; + +// Test for the shouldIgnore function +describe("shouldIgnore", () => { + beforeEach(() => { + setUpTestDir(); + }); + + afterEach(() => { + tearDownTestDir(); + }); + + test("should return true if a file is ignored by .gitignore", async () => { + addToTestDir([ + ["/ignored-file.txt", "content"], + ["/kept-file.txt", "content"], + ["/.gitignore", "ignored-file.txt\nignored-folder/"], + "/ignored-folder/", + ["/ignored-folder/file.txt", "content"], + ]); + const result1 = await shouldIgnore( + TEST_DIR + "/ignored-file.txt", + TEST_DIR, + testIde, + ); + expect(result1).toBe(true); + + const result2 = await shouldIgnore( + TEST_DIR + "/ignored-folder", + TEST_DIR, + testIde, + ); + expect(result2).toBe(true); + + const result3 = await shouldIgnore( + TEST_DIR + "/kept-file.txt", + TEST_DIR, + testIde, + ); + expect(result3).toBe(false); + }); +}); diff --git a/core/indexing/shouldIgnore.ts b/core/indexing/shouldIgnore.ts new file mode 100644 index 000000000..5af63394b --- /dev/null +++ b/core/indexing/shouldIgnore.ts @@ -0,0 +1,80 @@ +import ignore from "ignore"; +import { FileType, IDE } from "../index"; +import { getUriPathBasename } from "../util/uri"; +import { getGlobalContinueIgArray, gitIgArrayFromFile } from "./ignore"; + +interface IgnoreContext { + ignore: ReturnType; + dirname: string; +} + +export async function shouldIgnore( + fileUri: string, + rootDirUri: string, + ide: IDE, + ignoreFiles: string[] = [".gitignore", ".continueignore"], +): Promise { + let currentDir = fileUri; + + while (currentDir !== rootDirUri && currentDir.startsWith(rootDirUri)) { + const dirEntries = await ide.listDir(currentDir); + const dirs = dirEntries + .filter(([_, uriType]) => uriType === FileType.File) + .map(([uri, _]) => uri); + + const ignoreContexts = await getIgnoreContextsInDir( + currentDir, + dirs, + ignoreFiles, + ide, + ); + + const relativePath = fileUri.substring(currentDir.length + 1); + + for (const context of ignoreContexts) { + if (context.ignore.ignores(relativePath)) { + return true; + } + } + + currentDir = getParentDir(currentDir); + } + + return false; +} + +async function getIgnoreContextsInDir( + dirUri: string, + entries: string[], + ignoreFiles: string[], + ide: IDE, +): Promise { + const ignoreEntries = entries.filter((entryUri) => + ignoreFiles.includes(getUriPathBasename(entryUri)), + ); + const patterns = await Promise.all( + ignoreEntries.map(async (entryUri) => { + const fileContent = await ide.readFile(entryUri); + return gitIgArrayFromFile(fileContent); + }), + ); + + const globalPatterns = getGlobalContinueIgArray(); + + return [ + ...patterns.map((patterns) => ({ + ignore: ignore().add(patterns), + dirname: dirUri, + })), + { + ignore: ignore().add(globalPatterns), + dirname: "/", + }, + ]; +} + +function getParentDir(uri: string): string { + const splitUri = uri.split("/"); + splitUri.pop(); + return splitUri.join("/"); +} diff --git a/core/test/testDir.ts b/core/test/testDir.ts index 9b0473b46..0e34f509b 100644 --- a/core/test/testDir.ts +++ b/core/test/testDir.ts @@ -1,7 +1,7 @@ import fs from "fs"; import os from "os"; import path from "path"; -import { localPathToUri, localPathOrUriToPath } from "../util/pathToUri"; +import { localPathOrUriToPath, localPathToUri } from "../util/pathToUri"; // Want this outside of the git repository so we can change branches in tests export const TEST_DIR_PATH = path.join(os.tmpdir(), "testWorkspaceDir"); @@ -26,7 +26,7 @@ export function tearDownTestDir() { "index/index.ts" creates an empty index/index.ts ["index/index.ts", "hello"] creates index/index.ts with contents "hello" */ -export function addToTestDir(pathsOrUris: (string | string[])[]) { +export function addToTestDir(pathsOrUris: (string | [string, string])[]) { // Allow tests to use URIs or local paths const paths = pathsOrUris.map((val) => { if (Array.isArray(val)) {