(cwd: string)
| 63 | * empty when in detached-HEAD; status files use porcelain v1 (XY path). |
| 64 | */ |
| 65 | export async function gitStatus(cwd: string): Promise<GitStatus> { |
| 66 | const [branchRes, statusRes] = await Promise.all([ |
| 67 | exec(['branch', '--show-current'], cwd, gitOpts()).catch(() => null), |
| 68 | exec(['status', '--porcelain=v1'], cwd, gitOpts()), |
| 69 | ]); |
| 70 | if (statusRes.exitCode !== 0) { |
| 71 | throw new Error(`git status exited ${statusRes.exitCode}: ${statusRes.stderr.slice(0, 200)}`); |
| 72 | } |
| 73 | const branchRaw = branchRes && branchRes.exitCode === 0 ? branchRes.stdout.trim() : ''; |
| 74 | const branch = branchRaw.length > 0 ? branchRaw : null; |
| 75 | const files: GitStatusFile[] = statusRes.stdout |
| 76 | .split('\n') |
| 77 | .filter((l) => l.length > 0) |
| 78 | .map((line) => ({ |
| 79 | status: line.slice(0, 2), |
| 80 | // Porcelain emits 3-byte prefix `XY ` then the path. Rename rows have a |
| 81 | // `->` separator; for v1's simple panel we keep the original line as-is |
| 82 | // after the prefix. |
| 83 | path: line.slice(3), |
| 84 | })); |
| 85 | return { branch, clean: files.length === 0, files }; |
| 86 | } |
no test coverage detected