sourcebot/packages/backend/src/zoekt.ts

45 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-11-01 17:51:14 +00:00
import { exec } from "child_process";
import { AppContext, GitRepository, LocalRepository, Settings } from "./types.js";
2024-11-01 17:51:14 +00:00
const ALWAYS_EXCLUDED_DIRS = ['.git', '.hg', '.svn'];
export const indexGitRepository = async (repo: GitRepository, settings: Settings, ctx: AppContext) => {
2024-11-07 02:28:10 +00:00
const revisions = [
'HEAD',
...repo.branches ?? [],
...repo.tags ?? [],
];
const command = `zoekt-git-index -allow_missing_branches -index ${ctx.indexPath} -file_limit ${settings.maxFileSize} -branches ${revisions.join(',')} ${repo.path}`;
2024-11-07 02:28:10 +00:00
2024-11-01 17:51:14 +00:00
return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
2024-11-07 02:28:10 +00:00
exec(command, (error, stdout, stderr) => {
2024-11-01 17:51:14 +00:00
if (error) {
reject(error);
return;
}
resolve({
stdout,
stderr
});
})
});
}
export const indexLocalRepository = async (repo: LocalRepository, settings: Settings, ctx: AppContext, signal?: AbortSignal) => {
2024-11-01 17:51:14 +00:00
const excludedDirs = [...ALWAYS_EXCLUDED_DIRS, repo.excludedPaths];
const command = `zoekt-index -index ${ctx.indexPath} -file_limit ${settings.maxFileSize} -ignore_dirs ${excludedDirs.join(',')} ${repo.path}`;
2024-11-01 17:51:14 +00:00
return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
exec(command, { signal }, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve({
stdout,
stderr
});
})
});
}