refactor(cli): extract commit result display and extend commit return with stats

This commit is contained in:
2026-06-09 17:38:23 +08:00
parent 6d007698f9
commit 647d1096ba
2 changed files with 37 additions and 24 deletions
+15 -6
View File
@@ -100,7 +100,7 @@ export async function stageFiles(paths: string[]): Promise<void> {
export async function commit(
message: string,
): Promise<{ hash: string; summary: string }> {
): Promise<{ branch: string; hash: string; files: number; insertions: number; deletions: number }> {
const proc = Bun.spawn(["git", "commit", "-m", message], {
stdout: "pipe",
stderr: "pipe",
@@ -113,10 +113,19 @@ export async function commit(
throw new Error(stderr.trim() || `git commit failed (exit code ${exitCode})`);
}
const hashMatch = stdout.match(/\[\w[^\s]*\s+([0-9a-f]{7,})/);
const hash = hashMatch?.[1] ?? "";
const summaryMatch = stdout.match(/(\d+\s+file[s]?\s+changed.*)/);
const summary = summaryMatch?.[1] ?? "";
const branchHashMatch = stdout.match(/\[(\S+)\s+([0-9a-f]{7,})/);
const branch = branchHashMatch?.[1] ?? "";
const hash = branchHashMatch?.[2] ?? "";
return { hash, summary };
const filesMatch = stdout.match(/(\d+)\s+file/);
const insertionsMatch = stdout.match(/(\d+)\s+insertion/);
const deletionsMatch = stdout.match(/(\d+)\s+deletion/);
return {
branch,
hash,
files: parseInt(filesMatch?.[1] ?? "0"),
insertions: parseInt(insertionsMatch?.[1] ?? "0"),
deletions: parseInt(deletionsMatch?.[1] ?? "0"),
};
}