Skip to content

Instantly share code, notes, and snippets.

@stephane303
Created August 30, 2024 05:57
Show Gist options
  • Save stephane303/a7345a8f539f28c3ec187f5760479371 to your computer and use it in GitHub Desktop.
Save stephane303/a7345a8f539f28c3ec187f5760479371 to your computer and use it in GitHub Desktop.
ai commit
function Global:ai-commit {
# Function to generate commit message using Ollama
function Generate-CommitMessage {
$diff = git diff --cached
Write-Host "Git diff being sent:"
Write-Host $diff
#$prompt = "Below is a diff of all staged changes, coming from the command:`ngit diff --cached`nPlease generate a concise, one-line commit message for these changes.`n$diff"
$prompt = "Git diff:
$diff
Task: Please generate a concise, 3 lines commit message for these changes.
Commit message:"
Write-Host "Full prompt being sent to Ollama:"
Write-Host $prompt
$body = @{
model = "deepseek-coder-v2:16b"
prompt = $prompt
stream = $false
} | ConvertTo-Json
try {
$response = Invoke-WebRequest -Method POST -Body $body -Uri "http://localhost:11434/api/generate" -ContentType "application/json"
$result = $response.Content | ConvertFrom-Json
Write-Host "Received response from Ollama:"
Write-Host $result.response
if ([string]::IsNullOrWhiteSpace($result.response)) {
throw "Empty response from Ollama"
}
return $result.response.Trim()
}
catch {
Write-Host "Error generating commit message: $_"
return $null
}
}
# Main script
Write-Host "Generating AI-powered commit message..."
$commitMessage = Generate-CommitMessage
if ($null -eq $commitMessage) {
Write-Host "Failed to generate commit message. Exiting."
return
}
while ($true) {
Write-Host "`nProposed commit message:"
Write-Host $commitMessage
$choice = Read-Host "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel?"
switch ($choice.ToLower()) {
'a' {
if (git commit -m $commitMessage) {
Write-Host "Changes committed successfully!"
return
}
else {
Write-Host "Commit failed. Please check your changes and try again."
return
}
}
'e' {
$commitMessage = Read-Host "Enter your commit message"
if ($commitMessage -and (git commit -m $commitMessage)) {
Write-Host "Changes committed successfully with your message!"
return
}
else {
Write-Host "Commit failed. Please check your message and try again."
return
}
}
'r' {
Write-Host "Regenerating commit message..."
$commitMessage = Generate-CommitMessage
if ($null -eq $commitMessage) {
Write-Host "Failed to regenerate commit message. Please try again or cancel."
}
}
'c' {
Write-Host "Commit cancelled."
return
}
default {
Write-Host "Invalid choice. Please try again."
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment