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
+22 -18
View File
@@ -355,20 +355,32 @@ async function main() {
return;
}
function printCommitResult(
result: { branch: string; hash: string; files: number; insertions: number; deletions: number },
msg: string,
) {
console.log(`\n ${GREEN}${BOLD}✔ Committed successfully!${RESET}`);
const id = result.branch && result.hash
? `${YELLOW}[${result.branch} ${result.hash}]${RESET}`
: result.hash
? `${YELLOW}${result.hash}${RESET}`
: "";
console.log(` ${id} ${msg}`);
const parts: string[] = [];
if (result.files > 0) parts.push(`${YELLOW}${result.files} file${result.files > 1 ? "s" : ""} changed${RESET}`);
if (result.insertions > 0) parts.push(`${GREEN}${result.insertions} insertion${result.insertions > 1 ? "s" : ""}(+)${RESET}`);
if (result.deletions > 0) parts.push(`${RED}${result.deletions} deletion${result.deletions > 1 ? "s" : ""}(-)${RESET}`);
if (parts.length > 0) console.log(` ${parts.join(", ")}`);
}
const action = await confirmCommit(message);
if (action === "y") {
try {
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}`);
}
printCommitResult(result, message);
} catch (err) {
console.error(
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
@@ -380,15 +392,7 @@ async function main() {
if (edited) {
try {
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}`);
}
printCommitResult(result, edited);
} catch (err) {
console.error(
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
+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"),
};
}