Skip to content

Commit

Permalink
Added commit message automation script using GenAI in the project's h…
Browse files Browse the repository at this point in the history
…usky hook configuration
  • Loading branch information
pelikhan committed Sep 4, 2024
1 parent 8a9e4da commit 1622457
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node packages/cli/built/genaiscript.cjs run commit-msg $1
28 changes: 28 additions & 0 deletions genaisrc/commit-msg.genai.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const msg = env.files[0]
const msgContent = msg.content
?.split(/\n/g)
.filter((l) => l && !/^# /.test(l))
.join("\n")
if (!msgContent) cancel("commit message already exists")

// Check for staged changes and stage all changes if none are staged
let diff = await host.exec("git", ["diff", "--cached"])
if (!diff.stdout) cancel("no staged changes")
// 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`
},
{ cache: false, temperature: 0.8 }
)

if (res.error) throw res.error
const message = res.text
if (!message) cancel("no message generated")
await workspace.writeText(msg.filename, message)

0 comments on commit 1622457

Please sign in to comment.