Skip to content

Instantly share code, notes, and snippets.

@itsolver
Forked from karpathy/add_to_zshrc.sh
Last active September 11, 2024 03:46
Show Gist options
  • Save itsolver/c239a35b4346631768bfc223aaf6fb80 to your computer and use it in GitHub Desktop.
Save itsolver/c239a35b4346631768bfc223aaf6fb80 to your computer and use it in GitHub Desktop.
Git Commit Message AI
# -----------------------------------------------------------------------------
# AI-powered Git Commit Functions for PowerShell
# To use this script:
# 1. Copy the entire content of this script
# 2. Open your PowerShell profile (use $PROFILE to find its location)
# 3. Paste this script at the end of your profile
# 4. Restart PowerShell or run '. $PROFILE' to reload your profile
# 5. Install the 'llm' CLI utility: pip install llm
# 6. Install the Claude 3.5 Sonnet model: llm install llm-claude-3
# 7. You can now use the 'gcmsg' command to commit without pushing
# and 'gcmsgp' command to commit and push in any Git repository
# Note: 'gcm' is already taken in PowerShell, so we use 'gcmsg' instead
function Generate-CommitMessage {
try {
$diff = git diff --cached
$encodedDiff = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($diff))
$prompt = @'
Below is a Base64 encoded diff of all staged changes, coming from the command:
***
git diff --cached
***
Generate a concise, one-line commit message for these changes. Return ONLY the commit message, without any additional text or formatting.
'@
$commitMessage = $encodedDiff | llm -m claude-3.5-sonnet $prompt
return $commitMessage.Trim()
}
catch {
Write-Host "Error generating commit message: $($Error[0].Message)"
return $null
}
}
function Global:gcmsg {
Write-Host 'Generating AI-powered commit message...'
$commitMessage = Generate-CommitMessage
if ($null -eq $commitMessage -or $commitMessage -eq '') {
Write-Host 'Failed to generate a valid commit message. Aborting.'
return
}
Write-Host "Commit message: $commitMessage"
# Commit changes
if (git commit -m "$commitMessage") {
Write-Host 'Changes committed successfully!'
}
else {
Write-Host 'Error committing changes. Please check your staged changes and try again.'
}
}
function Global:gcmsgp {
Write-Host 'Generating AI-powered commit message...'
$commitMessage = Generate-CommitMessage
if ($null -eq $commitMessage -or $commitMessage -eq '') {
Write-Host 'Failed to generate a valid commit message. Aborting.'
return
}
Write-Host "Commit message: $commitMessage"
# Commit changes
if (git commit -m "$commitMessage") {
Write-Host 'Changes committed successfully!'
# Push changes
Write-Host 'Pushing changes...'
if (git push) {
Write-Host 'Changes pushed successfully!'
}
else {
Write-Host 'Error pushing changes. Please push manually.'
}
}
else {
Write-Host 'Error committing changes. Please check your staged changes and try again.'
}
}
Set-Alias -Name gcmsg -Value Global:gcmsg
Set-Alias -Name gcmsgp -Value Global:gcmsgp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment