should ignore p1

This commit is contained in:
Dallin Romney 2025-03-07 09:52:31 -08:00
parent 71eb67d2ab
commit 66e4db7f7c
4 changed files with 139 additions and 2 deletions

View file

@ -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();

View file

@ -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);
});
});

View file

@ -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<typeof ignore>;
dirname: string;
}
export async function shouldIgnore(
fileUri: string,
rootDirUri: string,
ide: IDE,
ignoreFiles: string[] = [".gitignore", ".continueignore"],
): Promise<boolean> {
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<IgnoreContext[]> {
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("/");
}

View file

@ -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)) {