Skip to content

Commit

Permalink
🚀 refactor: improve diff handling and prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 10, 2024
1 parent a2b663f commit c4a5dcd
Show file tree
Hide file tree
Showing 4 changed files with 796 additions and 146 deletions.
53 changes: 17 additions & 36 deletions genaisrc/gcm.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,27 @@ script({
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(diffCmd)
if (!diff.stdout) {
/**
* 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) {
// Stage all changes and recompute diff
await host.exec("git add .")
diff = await host.exec(diffCmd)
}
if (!diff.stdout) cancel("no staged changes")
}
const diff = await git.diff({
staged: true,
askStageOnEmpty: true,
})
if (!diff) cancel("no staged changes")

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

let choice
let message
do {
// Generate commit message
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
` // TODO: add a better prompt
},
{ cache: false, temperature: 0.8 }
)
// generate a conventional commit message (https://www.conventionalcommits.org/en/v1.0.0/)
const res = await runPrompt((_) => {
_.def("GIT_DIFF", diff, { maxTokens: 20000, language: "diff" })
_.$`Generate a git conventional commit message for the changes in GIT_DIFF.
- do NOT add quotes
- maximum 50 characters
- use emojis`
})
if (res.error) throw res.error

message = res.text
Expand Down Expand Up @@ -79,9 +60,9 @@ Please generate a concise, one-line commit message for these changes.
}
// Regenerate message
if (choice === "commit" && message) {
console.log((await host.exec("git", ["commit", "-m", message])).stdout)
console.log(await git.exec(["commit", "-m", message]))
if (await host.confirm("Push changes?", { default: true }))
console.log((await host.exec("git push")).stdout)
console.log(await git.exec("push"))
break
}
} while (choice !== "commit")
} while (choice !== "commit")
Loading

0 comments on commit c4a5dcd

Please sign in to comment.