96 lines
3.1 KiB
TypeScript
Executable File
96 lines
3.1 KiB
TypeScript
Executable File
#!/usr/bin/env runts -- --allow-all
|
|
|
|
import {createInterface} from "node:readline/promises";
|
|
import {stdin, stdout} from "node:process";
|
|
import {execCommandShell, log, ProcessBar, term,} from "https://script.hatter.ink/@61/deno-commons-mod.ts";
|
|
import {summarizeGitStatusDiff} from "https://script.hatter.ink/@0/deno-ai-mod.ts";
|
|
import {getGitLocalRev, getGitRemoteRev, getGitStatus,} from "https://script.hatter.ink/@2/deno-git-mod.ts";
|
|
|
|
async function checkRev(): Promise<boolean> {
|
|
const localRev = await getGitLocalRev();
|
|
const remoteRev = await new ProcessBar("Checking rev").call(
|
|
async (): Promise<string> => {
|
|
return await getGitRemoteRev();
|
|
},
|
|
);
|
|
if (localRev === remoteRev) {
|
|
console.log(term.green(`Check rev successfully, rev: ${localRev}`));
|
|
} else {
|
|
throw `Check rev failed, local rev: ${localRev} vs remote rev: ${remoteRev}`;
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
await checkRev(); // check local rev <--> remote rev equals
|
|
const gitStatus = await getGitStatus();
|
|
if (gitStatus === null) {
|
|
log.warn("No git status found");
|
|
return;
|
|
}
|
|
log.info("Git status:", gitStatus);
|
|
|
|
const summary = await new ProcessBar("AI summarizing").call(
|
|
async (): Promise<string> => {
|
|
return await summarizeGitStatusDiff();
|
|
},
|
|
);
|
|
if (summary != null) {
|
|
log.success(`AI summarized git commit message: ${summary}`);
|
|
}
|
|
|
|
let emptyCount = 0;
|
|
let message = "empty message";
|
|
const readline = createInterface({ input: stdin, output: stdout });
|
|
do {
|
|
if (emptyCount++ === 3) {
|
|
readline.close();
|
|
console.log("Too many empty messages, then exit");
|
|
return;
|
|
}
|
|
message = await readline.question(
|
|
"Input your commit message (Enter for AI summary) > ",
|
|
);
|
|
if (message.trim().length == 0) {
|
|
message = summary ?? "";
|
|
}
|
|
} while (message.length == 0);
|
|
readline.close();
|
|
|
|
const gitCommitArgs: string[] = [];
|
|
gitCommitArgs.push("commit");
|
|
for (const file of gitStatus.modified) {
|
|
gitCommitArgs.push(file);
|
|
}
|
|
for (const file of gitStatus.deleted) {
|
|
gitCommitArgs.push(file);
|
|
}
|
|
for (const file of gitStatus.untracked) {
|
|
gitCommitArgs.push(file);
|
|
}
|
|
gitCommitArgs.push("-m");
|
|
gitCommitArgs.push(message);
|
|
|
|
if (gitStatus.untracked.length > 0) {
|
|
const gitAddArgs = ["add"];
|
|
for (const file of gitStatus.untracked) {
|
|
gitAddArgs.push(file);
|
|
}
|
|
log.info(">>>>>", ["git", gitAddArgs]);
|
|
await execCommandShell("git", gitAddArgs);
|
|
}
|
|
log.info(">>>>>", ["git", gitCommitArgs]);
|
|
await execCommandShell("git", gitCommitArgs);
|
|
log.info(">>>>>", ["git", "push"]);
|
|
await new ProcessBar("Git pushing").call(async (): Promise<void> => {
|
|
await execCommandShell("git", ["push"]);
|
|
});
|
|
}
|
|
|
|
main().catch((err) => {
|
|
log.error(err);
|
|
process.exit(0);
|
|
}).then(() => process.exit(0));
|
|
|
|
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260406T232253+08:00.MEUCIQDmyDRTnp6ofB1pCeV/
|
|
// WP/RH6N+6MmJRjdp72t2sdDnoAIgEeExNIF/K3IwhrNZNWIRTv+AVjKzKKS3dKUTYfGbwTI=
|