refactor(cli): extract commit result display and extend commit return with stats
This commit is contained in:
@@ -355,20 +355,32 @@ async function main() {
|
|||||||
return;
|
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);
|
const action = await confirmCommit(message);
|
||||||
|
|
||||||
if (action === "y") {
|
if (action === "y") {
|
||||||
try {
|
try {
|
||||||
const result = await commit(message);
|
const result = await commit(message);
|
||||||
console.log(
|
printCommitResult(result, message);
|
||||||
`\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) {
|
} catch (err) {
|
||||||
console.error(
|
console.error(
|
||||||
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
|
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
|
||||||
@@ -380,15 +392,7 @@ async function main() {
|
|||||||
if (edited) {
|
if (edited) {
|
||||||
try {
|
try {
|
||||||
const result = await commit(edited);
|
const result = await commit(edited);
|
||||||
console.log(
|
printCommitResult(result, edited);
|
||||||
`\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) {
|
} catch (err) {
|
||||||
console.error(
|
console.error(
|
||||||
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
|
` ${RED}Commit failed: ${err instanceof Error ? err.message : err}${RESET}`,
|
||||||
|
|||||||
+15
-6
@@ -100,7 +100,7 @@ export async function stageFiles(paths: string[]): Promise<void> {
|
|||||||
|
|
||||||
export async function commit(
|
export async function commit(
|
||||||
message: string,
|
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], {
|
const proc = Bun.spawn(["git", "commit", "-m", message], {
|
||||||
stdout: "pipe",
|
stdout: "pipe",
|
||||||
stderr: "pipe",
|
stderr: "pipe",
|
||||||
@@ -113,10 +113,19 @@ export async function commit(
|
|||||||
throw new Error(stderr.trim() || `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 branchHashMatch = stdout.match(/\[(\S+)\s+([0-9a-f]{7,})/);
|
||||||
const hash = hashMatch?.[1] ?? "";
|
const branch = branchHashMatch?.[1] ?? "";
|
||||||
const summaryMatch = stdout.match(/(\d+\s+file[s]?\s+changed.*)/);
|
const hash = branchHashMatch?.[2] ?? "";
|
||||||
const summary = summaryMatch?.[1] ?? "";
|
|
||||||
|
|
||||||
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"),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user