无需插件,PowerShell 秒变智能:用 Ollama 自动生成 Git 提交信息

在软件开发过程中,编写清晰且符合规范的 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...

php 复制代码
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

php 复制代码
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

相关推荐
YBN娜10 分钟前
Vue实现登录功能
前端·javascript·vue.js
阳光开朗大男孩 = ̄ω ̄=11 分钟前
CSS——选择器、PxCook软件、盒子模型
前端·javascript·css
minDuck15 分钟前
ruoyi-vue集成tianai-captcha验证码
java·前端·vue.js
小政爱学习!36 分钟前
封装axios、环境变量、api解耦、解决跨域、全局组件注入
开发语言·前端·javascript
魏大帅。41 分钟前
Axios 的 responseType 属性详解及 Blob 与 ArrayBuffer 解析
前端·javascript·ajax
花花鱼1 小时前
vue3 基于element-plus进行的一个可拖动改变导航与内容区域大小的简单方法
前端·javascript·elementui
k09331 小时前
sourceTree回滚版本到某次提交
开发语言·前端·javascript
EricWang13581 小时前
[OS] 项目三-2-proc.c: exit(int status)
服务器·c语言·前端
September_ning1 小时前
React.lazy() 懒加载
前端·react.js·前端框架
web行路人1 小时前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架