(cwd: string, maxFiles: number = 20)
| 353 | * @returns Git status string or null if not a git repository |
| 354 | */ |
| 355 | export async function getGitStatus(cwd: string, maxFiles: number = 20): Promise<string | null> { |
| 356 | try { |
| 357 | const isInstalled = await checkGitInstalled() |
| 358 | if (!isInstalled) { |
| 359 | return null |
| 360 | } |
| 361 | |
| 362 | const isRepo = await checkGitRepo(cwd) |
| 363 | if (!isRepo) { |
| 364 | return null |
| 365 | } |
| 366 | |
| 367 | // Use porcelain v1 format with branch info |
| 368 | const { stdout } = await execAsync("git status --porcelain=v1 --branch", { cwd }) |
| 369 | |
| 370 | if (!stdout.trim()) { |
| 371 | return null |
| 372 | } |
| 373 | |
| 374 | const lines = stdout.trim().split("\n") |
| 375 | |
| 376 | // First line is always branch info (e.g., "## main...origin/main") |
| 377 | const branchLine = lines[0] |
| 378 | const fileLines = lines.slice(1) |
| 379 | |
| 380 | // Build output with branch info and limited file entries |
| 381 | const output: string[] = [branchLine] |
| 382 | |
| 383 | if (maxFiles > 0 && fileLines.length > 0) { |
| 384 | const filesToShow = fileLines.slice(0, maxFiles) |
| 385 | output.push(...filesToShow) |
| 386 | |
| 387 | // Add truncation notice if needed |
| 388 | if (fileLines.length > maxFiles) { |
| 389 | output.push(`... ${fileLines.length - maxFiles} more files`) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return output.join("\n") |
| 394 | } catch (error) { |
| 395 | console.error("Error getting git status:", error) |
| 396 | return null |
| 397 | } |
| 398 | } |
no test coverage detected