diff --git a/gitnexus/src/cli/analyze.ts b/gitnexus/src/cli/analyze.ts index b086cf599..d46f0fcea 100644 --- a/gitnexus/src/cli/analyze.ts +++ b/gitnexus/src/cli/analyze.ts @@ -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; diff --git a/gitnexus/src/storage/git.ts b/gitnexus/src/storage/git.ts index 817b5717c..6670565ca 100644 --- a/gitnexus/src/storage/git.ts +++ b/gitnexus/src/storage/git.ts @@ -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; + } +}; +