切換語言為:簡體

無需外掛,PowerShell 秒變智慧:用 Ollama 自動生成 Git 提交資訊

  • 爱糖宝
  • 2024-09-28
  • 2051
  • 0
  • 0

在軟件開發過程中,編寫清晰且符合規範的 Git 提交資訊至關重要。爲了提高效率和規範性,可以利用 Ollama 和 PowerShell 指令碼自動生成提交資訊。本文將介紹如何實現這一功能。

前置條件

  • git

  • ollama,並 pull model , 比如這裏使用 codegeex4:latest

1. 新增 PowerShell 函式

執行notepad $PROFILE 新增加以下內容,其中 model codegeex4:latest,你可以改成其它 model (qwen2.5-coder、deepseek-coder-v2),請參考ollama.com/library?q=c…

function gco {
    # Function to generate commit message
    function Generate-CommitMessage {
        $diff = git diff --cached
        $message = $diff | & ollama run codegeex4:latest "
Below 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, conforming to the Conventional Commits specification."
        return $message
    }

    # Function to read user input
    function Read-Input {
        param (
            [string]$Prompt
        )
        Write-Host -NoNewline $Prompt
        return Read-Host
    }

    # Main script
    Write-Host "Generating..."
    $commitMessage = Generate-CommitMessage

    while ($true) {
        Write-Host "`nProposed commit message:"
        Write-Host $commitMessage

        $choice = Read-Input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "

        switch ($choice.ToLower()) {
            'a' {
                if ($commitMessage) {
                    git commit -m "$commitMessage"
                    if ($?) {
                        Write-Host "Changes committed successfully!"
                        return 0
                    } else {
                        Write-Host "Commit failed. Please check your changes and try again."
                        return 1
                    }
                } else {
                    Write-Host "Commit message cannot be empty. Please try again."
                }
            }
            'e' {
                $commitMessage = Read-Input "Enter your commit message: "
                if ($commitMessage) {
                    git commit -m "$commitMessage"
                    if ($?) {
                        Write-Host "Changes committed successfully with your message!"
                        return 0
                    } else {
                        Write-Host "Commit failed. Please check your message and try again."
                        return 1
                    }
                } else {
                    Write-Host "Commit message cannot be empty. Please try again."
                }
            }
            'r' {
                Write-Host "Regenerating commit message..."
                $commitMessage = Generate-CommitMessage
            }
            'c' {
                Write-Host "Commit cancelled."
                return 1
            }
            default {
                Write-Host "Invalid choice. Please try again."
            }
        }
    }
}

除此之外,你還可以使用其它網路上的大語言模型 api 端點,比如使用 github modelgpt-4o-mini

function Invoke-ChatCompletion {
    param (
        [string]$GitHubToken = "你的 GitHub 令牌填寫在此處",
        [string]$UserMessage,
        [string]$SystemMessage = "You are a helpful assistant."
    )

    # 設定請求頭
    $headers = @{
        "Content-Type" = "application/json"
        "Authorization" = "Bearer $GitHubToken"
    }

    # 建立請求體
    $body = @{
        "messages" = @(
            @{
                "role" = "system"
                "content" = $SystemMessage
            },
            @{
                "role" = "user"
                "content" = $UserMessage
            }
        )
        "temperature" = 1.0
        "top_p" = 1.0
        "max_tokens" = 1000
        "model" = "gpt-4o-mini"
    }

    # 將請求體轉換為 JSON 格式
    $bodyJson = $body | ConvertTo-Json

    try {
        # 傳送 POST 請求
        $response = Invoke-RestMethod -Uri "https://models.inference.ai.azure.com/chat/completions" -Method Post -Headers $headers -Body $bodyJson

        # 輸出響應中的 content 內容
        $content = $response.choices[0].message.content
        Write-Output $content
    } catch {
        # 輸出錯誤資訊
        Write-Error $_.Exception.Message
    }
}

function Generate-CommitMessage {
    $diff = git diff --cached
    $userMessage = @"
Below 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, conforming to the Conventional Commits specification.
$diff
"@

    # 呼叫 Invoke-ChatCompletion 函式生成提交資訊
    $commitMessage = Invoke-ChatCompletion -GitHubToken "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" -UserMessage $userMessage
    return $commitMessage
}

function Read-Input {
    param (
        [string]$Prompt
    )
    Write-Host -NoNewline $Prompt
    return Read-Host
}

function gco {
    Write-Host "Generating..."
    $commitMessage = Generate-CommitMessage

    while ($true) {
        Write-Host "`nProposed commit message:"
        Write-Host $commitMessage

        $choice = Read-Input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "

        switch ($choice.ToLower()) {
            'a' {
                if ($commitMessage) {
                    git commit -m "$commitMessage"
                    if ($?) {
                        Write-Host "Changes committed successfully!"
                        return 0
                    } else {
                        Write-Host "Commit failed. Please check your changes and try again."
                        return 1
                    }
                } else {
                    Write-Host "Commit message cannot be empty. Please try again."
                }
            }
            'e' {
                $commitMessage = Read-Input "Enter your commit message: "
                if ($commitMessage) {
                    git commit -m "$commitMessage"
                    if ($?) {
                        Write-Host "Changes committed successfully with your message!"
                        return 0
                    } else {
                        Write-Host "Commit failed. Please check your message and try again."
                        return 1
                    }
                } else {
                    Write-Host "Commit message cannot be empty. Please try again."
                }
            }
            'r' {
                Write-Host "Regenerating commit message..."
                $commitMessage = Generate-CommitMessage
            }
            'c' {
                Write-Host "Commit cancelled."
                return 1
            }
            default {
                Write-Host "Invalid choice. Please try again."
            }
        }
    }
}

2. 執行,生成 git commit message

在 PowerShell 執行. $PROFILE 重新載入配置檔案,在專案目錄執行 gco

0則評論

您的電子郵件等資訊不會被公開,以下所有項目均必填

OK! You can skip this field.