# TSP智能助手 - 自动推送脚本 (PowerShell版本) # 使用方法: .\auto_push.ps1 [提交信息] param( [string]$CommitMessage = "", [switch]$Force = $false, [switch]$NoConfirm = $false ) # 设置控制台编码 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 # 颜色输出函数 function Write-ColorOutput { param( [string]$Message, [string]$Color = "White" ) Write-Host $Message -ForegroundColor $Color } function Write-Step { param( [int]$Step, [int]$Total, [string]$Message ) Write-ColorOutput "[$Step/$Total] $Message" "Cyan" } function Show-GitStatus { Write-ColorOutput "`n📋 当前Git状态:" "Yellow" $status = git status --porcelain if ($status) { $status | ForEach-Object { $line = $_ if ($line.StartsWith("??")) { Write-ColorOutput " + $($line.Substring(3))" "Green" } elseif ($line.StartsWith(" M")) { Write-ColorOutput " ~ $($line.Substring(3))" "Yellow" } elseif ($line.StartsWith(" D")) { Write-ColorOutput " - $($line.Substring(3))" "Red" } else { Write-ColorOutput " $line" "White" } } } else { Write-ColorOutput " 工作区干净,无更改" "Green" } } function Get-CommitMessage { if ($CommitMessage) { return $CommitMessage } # 尝试从最近提交生成智能消息 $lastCommit = git log --oneline -1 2>$null if ($lastCommit) { $suggested = "feat: 自动提交更新 - $(Get-Date -Format 'yyyy-MM-dd HH:mm')" } else { $suggested = "feat: 初始提交 - $(Get-Date -Format 'yyyy-MM-dd HH:mm')" } Write-ColorOutput "`n💡 建议的提交信息: $suggested" "Cyan" $custom = Read-Host "请输入自定义提交信息 (直接回车使用建议)" if ($custom) { return $custom } else { return $suggested } } function Test-GitRepository { try { git status --porcelain >$null 2>&1 return $LASTEXITCODE -eq 0 } catch { return $false } } function Show-CommitStats { Write-ColorOutput "`n📊 提交统计:" "Yellow" $commitHash = git log --oneline -1 | Select-String -Pattern "^\w+" | ForEach-Object { $_.Matches[0].Value } $filesChanged = git diff --cached --numstat | Measure-Object | Select-Object -ExpandProperty Count $insertions = git diff --cached --numstat | ForEach-Object { $parts = $_ -split '\s+' if ($parts[0] -match '^\d+$') { [int]$parts[0] } else { 0 } } | Measure-Object -Sum | Select-Object -ExpandProperty Sum $deletions = git diff --cached --numstat | ForEach-Object { $parts = $_ -split '\s+' if ($parts[1] -match '^\d+$') { [int]$parts[1] } else { 0 } } | Measure-Object -Sum | Select-Object -ExpandProperty Sum Write-ColorOutput " 提交哈希: $commitHash" "White" Write-ColorOutput " 文件变更: $filesChanged 个文件" "White" Write-ColorOutput " 新增行数: $insertions 行" "Green" Write-ColorOutput " 删除行数: $deletions 行" "Red" Write-ColorOutput " 净增加: $($insertions - $deletions) 行" "Cyan" } # 主程序开始 Write-ColorOutput "========================================" "Magenta" Write-ColorOutput "TSP智能助手 - 自动推送脚本 v2.0" "Magenta" Write-ColorOutput "========================================" "Magenta" # 检查Git仓库 Write-Step 1 4 "检查Git仓库状态" if (-not (Test-GitRepository)) { Write-ColorOutput "❌ Git未初始化或不在Git仓库中" "Red" Write-ColorOutput "请确保在正确的Git仓库目录中运行此脚本" "Yellow" Read-Host "按任意键退出" exit 1 } Write-ColorOutput "✅ Git仓库状态正常" "Green" # 显示状态 Show-GitStatus # 检查是否有更改 $hasChanges = git status --porcelain | Measure-Object | Select-Object -ExpandProperty Count if ($hasChanges -eq 0) { Write-ColorOutput "`n✅ 工作区干净,无需提交" "Green" Read-Host "按任意键退出" exit 0 } # 确认操作 if (-not $NoConfirm) { Write-ColorOutput "`n❓ 是否继续推送?" "Yellow" $confirm = Read-Host "输入 'y' 继续,其他键取消" if ($confirm -ne 'y') { Write-ColorOutput "操作已取消" "Yellow" Read-Host "按任意键退出" exit 0 } } # 添加文件 Write-Step 2 4 "添加所有更改到暂存区" try { git add . Write-ColorOutput "✅ 文件已添加到暂存区" "Green" } catch { Write-ColorOutput "❌ 添加文件失败: $($_.Exception.Message)" "Red" Read-Host "按任意键退出" exit 1 } # 生成提交信息 Write-Step 3 4 "生成提交信息" $finalCommitMessage = Get-CommitMessage Write-ColorOutput "提交信息: $finalCommitMessage" "Cyan" # 提交更改 try { git commit -m $finalCommitMessage Write-ColorOutput "✅ 提交成功" "Green" } catch { Write-ColorOutput "❌ 提交失败: $($_.Exception.Message)" "Red" Read-Host "按任意键退出" exit 1 } # 推送到远程 Write-Step 4 4 "推送到远程仓库" try { git push origin main Write-ColorOutput "✅ 推送成功" "Green" } catch { Write-ColorOutput "❌ 推送失败: $($_.Exception.Message)" "Red" Write-ColorOutput "请检查网络连接和远程仓库权限" "Yellow" Read-Host "按任意键退出" exit 1 } # 显示结果 Write-ColorOutput "`n========================================" "Magenta" Write-ColorOutput "✅ 推送完成!" "Green" Write-ColorOutput "========================================" "Magenta" Show-CommitStats Write-ColorOutput "`n🌐 远程仓库状态:" "Yellow" git status --short Write-ColorOutput "`n🎉 所有操作完成!" "Green" if (-not $NoConfirm) { Read-Host "按任意键退出" }