Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh gcm script #7396

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions genaisrc/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
genaiscript.d.ts -diff merge=ours linguist-generated
99 changes: 52 additions & 47 deletions genaisrc/gcm.genai.mts
Original file line number Diff line number Diff line change
@@ -1,82 +1,87 @@
// https://microsoft.github.io/genaiscript/guides/auto-git-commit-message/
import { select, input, confirm } from "@inquirer/prompts"
/**
* git commit flow with auto-generated commit message
*/
script({
title: "git commit message",
description: "Generate a commit message for all staged changes",
})

// TODO: update this diff command to match your workspace
const diffCmd = "git diff --cached -- . :!**/genaiscript.d.ts"

// Check for staged changes and stage all changes if none are staged
let diff = await host.exec("git", ["diff", "--cached"])
let diff = await host.exec(diffCmd)
if (!diff.stdout) {
const stage = await confirm({
message: "No staged changes. Stage all changes?",
/**
* Ask user to stage all changes if none are staged
*/
const stage = await host.confirm("No staged changes. Stage all changes?", {
default: true,
})
if (stage) {
await host.exec("git", ["add", "."])
diff = await host.exec("git", [
"diff",
"--cached",
"--",
".",
":!**/genaiscript.d.ts",
])
// Stage all changes and recompute diff
await host.exec("git add .")
diff = await host.exec(diffCmd)
}
if (!diff.stdout) cancel("no staged changes")
}

// show diff in the console
console.log(diff.stdout)

let choice
let message
do {
// Generate commit message
message = (
await runPrompt(
(_) => {
_.def("GIT_DIFF", diff, { maxTokens: 20000 })
_.$`GIT_DIFF is a diff of all staged changes, coming from the command:
const res = await runPrompt(
(_) => {
_.def("GIT_DIFF", diff, { maxTokens: 20000 })
_.$`GIT_DIFF is a diff of all staged changes, coming from the command:
\`\`\`
git diff --cached
\`\`\`
Please generate a concise, one-line commit message for these changes.
- do NOT add quotes`
},
{ cache: false, temperature: 0.8 }
)
).text
- do NOT add quotes
` // TODO: add a better prompt
},
{ cache: false, temperature: 0.8 }
)
if (res.error) throw res.error

message = res.text
if (!message) {
console.log("No message generated, did you configure the LLM model?")
break
}

// Prompt user for commit message
choice = await select({
message,
choices: [
{
name: "commit",
value: "commit",
description: "accept message and commit",
},
{
name: "edit",
value: "edit",
description: "edit message and commit",
},
{
name: "regenerate",
value: "regenerate",
description: "regenerate message",
},
],
})
choice = await host.select(message, [
{
value: "commit",
description: "accept message and commit",
},
{
value: "edit",
description: "edit message and commit",
},
{
value: "regenerate",
description: "regenerate message",
},
])

// Handle user choice
if (choice === "edit") {
message = await input({
message: "Edit commit message",
message = await host.input("Edit commit message", {
required: true,
})
choice = "commit"
}
// Regenerate message
if (choice === "commit" && message) {
console.log((await host.exec("git", ["commit", "-m", message])).stdout)
if (await confirm({ message: "Push changes?", default: true }))
console.log((await host.exec("git", ["push"])).stdout)
if (await host.confirm("Push changes?", { default: true }))
console.log((await host.exec("git push")).stdout)
break
}
} while (choice !== "commit")
Loading
Loading