From 6d007698f9e75ee5a5037a3f49eced36956549f4 Mon Sep 17 00:00:00 2001 From: Mplan Date: Tue, 9 Jun 2026 17:38:00 +0800 Subject: [PATCH] feat(cli): show commit hash and summary on success --- index.ts | 24 ++++++++++++++++++++---- src/git.ts | 20 ++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/index.ts b/index.ts index 23973df..6269926 100644 --- a/index.ts +++ b/index.ts @@ -359,8 +359,16 @@ async function main() { if (action === "y") { try { - await commit(message); - console.log(`\n ${GREEN}Committed successfully!${RESET}`); + const result = await commit(message); + console.log( + `\n ${GREEN}${BOLD}✔ Committed successfully!${RESET}`, + ); + if (result.hash) { + console.log(` ${YELLOW}${result.hash}${RESET} ${message}`); + } + if (result.summary) { + console.log(` ${DIM}${result.summary}${RESET}`); + } } catch (err) { console.error( ` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`, @@ -371,8 +379,16 @@ async function main() { const edited = await editMessage(message); if (edited) { try { - await commit(edited); - console.log(`\n ${GREEN}Committed successfully!${RESET}`); + const result = await commit(edited); + console.log( + `\n ${GREEN}${BOLD}✔ Committed successfully!${RESET}`, + ); + if (result.hash) { + console.log(` ${YELLOW}${result.hash}${RESET} ${edited}`); + } + if (result.summary) { + console.log(` ${DIM}${result.summary}${RESET}`); + } } catch (err) { console.error( ` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`, diff --git a/src/git.ts b/src/git.ts index 3c21578..914c8a1 100644 --- a/src/git.ts +++ b/src/git.ts @@ -98,13 +98,25 @@ export async function stageFiles(paths: string[]): Promise { await Bun.$`git add -- ${paths}`; } -export async function commit(message: string): Promise { +export async function commit( + message: string, +): Promise<{ hash: string; summary: string }> { const proc = Bun.spawn(["git", "commit", "-m", message], { - stdout: "inherit", - stderr: "inherit", + stdout: "pipe", + stderr: "pipe", }); const exitCode = await proc.exited; + const stdout = await new Response(proc.stdout).text(); + const stderr = await new Response(proc.stderr).text(); + if (exitCode !== 0) { - throw new Error(`git commit failed (exit code ${exitCode})`); + 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] ?? ""; + + return { hash, summary }; }