feat: analyze command auto-finds git root when run from subdirectory

This commit is contained in:
abhigyanpatwari 2026-02-04 02:30:16 +05:30
parent 0c99dbabb1
commit e2215c1ff9
2 changed files with 28 additions and 2 deletions

View file

@ -11,7 +11,7 @@ import { initKuzu, loadGraphToKuzu, getKuzuStats, executeQuery, executeWithReuse
import { buildBM25Index, exportBM25Index } from '../core/search/bm25-index.js';
import { runEmbeddingPipeline } from '../core/embeddings/embedding-pipeline.js';
import { getStoragePaths, saveMeta, loadMeta, addToGitignore } from '../storage/repo-manager.js';
import { getCurrentCommit, isGitRepo } from '../storage/git.js';
import { getCurrentCommit, isGitRepo, getGitRoot } from '../storage/git.js';
import { generateAIContextFiles } from './ai-context.js';
export interface AnalyzeOptions {
@ -23,9 +23,22 @@ export const analyzeCommand = async (
inputPath?: string,
options?: AnalyzeOptions
) => {
const repoPath = path.resolve(inputPath || '.');
const spinner = ora('Checking repository...').start();
// If path provided, use it directly. Otherwise, find git root from cwd.
let repoPath: string;
if (inputPath) {
repoPath = path.resolve(inputPath);
} else {
const gitRoot = getGitRoot(process.cwd());
if (!gitRoot) {
spinner.fail('Not inside a git repository');
process.exitCode = 1;
return;
}
repoPath = gitRoot;
}
if (!isGitRepo(repoPath)) {
spinner.fail('Not a git repository');
process.exitCode = 1;

View file

@ -25,5 +25,18 @@ export const getStatusPorcelain = (repoPath: string): string => {
}
};
/**
* Find the git repository root from any path inside the repo
*/
export const getGitRoot = (fromPath: string): string | null => {
try {
return execSync('git rev-parse --show-toplevel', { cwd: fromPath })
.toString()
.trim();
} catch {
return null;
}
};