91 lines
2.7 KiB
PowerShell
91 lines
2.7 KiB
PowerShell
# 快速推送代码到 GitHub
|
|
# 用途:使用配置文件中的信息自动推送代码
|
|
|
|
param(
|
|
[string]$CommitMessage = "Update: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
|
|
[string]$ConfigFile = ".github-config",
|
|
[switch]$Force
|
|
)
|
|
|
|
Write-Host "=== 推送代码到 GitHub ===" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
# 检查配置文件
|
|
if (-not (Test-Path $ConfigFile)) {
|
|
Write-Host "错误: 配置文件 $ConfigFile 不存在" -ForegroundColor Red
|
|
Write-Host "请先运行: .\scripts\setup-github-from-config.ps1" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# 读取配置
|
|
$config = @{}
|
|
Get-Content $ConfigFile | ForEach-Object {
|
|
if ($_ -match '^\s*([^#=]+?)\s*=\s*(.+?)\s*$') {
|
|
$key = $matches[1].Trim()
|
|
$value = $matches[2].Trim()
|
|
$config[$key] = $value
|
|
}
|
|
}
|
|
|
|
$repoUrl = $config['GITHUB_REPO_URL']
|
|
$defaultBranch = $config['GITHUB_DEFAULT_BRANCH']
|
|
if (-not $defaultBranch) {
|
|
$defaultBranch = "main"
|
|
}
|
|
|
|
# 检查 Git 状态
|
|
Write-Host "检查 Git 状态..." -ForegroundColor Yellow
|
|
$status = git status --porcelain 2>$null
|
|
if ($status) {
|
|
Write-Host "发现更改的文件:" -ForegroundColor Cyan
|
|
git status --short
|
|
Write-Host ""
|
|
|
|
# 添加所有更改
|
|
Write-Host "添加所有更改..." -ForegroundColor Yellow
|
|
git add .
|
|
Write-Host "✓ 文件已添加到暂存区" -ForegroundColor Green
|
|
|
|
# 提交
|
|
Write-Host "提交更改..." -ForegroundColor Yellow
|
|
Write-Host "提交信息: $CommitMessage" -ForegroundColor Gray
|
|
git commit -m $CommitMessage
|
|
Write-Host "✓ 更改已提交" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "✓ 没有需要提交的更改" -ForegroundColor Green
|
|
}
|
|
|
|
# 检查远程仓库
|
|
$currentRemote = git remote get-url origin 2>$null
|
|
if (-not $currentRemote) {
|
|
Write-Host "错误: 未配置远程仓库" -ForegroundColor Red
|
|
Write-Host "请先运行: .\scripts\setup-github-from-config.ps1" -ForegroundColor Yellow
|
|
exit 1
|
|
}
|
|
|
|
# 推送
|
|
Write-Host ""
|
|
Write-Host "推送到 GitHub..." -ForegroundColor Yellow
|
|
Write-Host "远程仓库: $currentRemote" -ForegroundColor Gray
|
|
Write-Host "分支: $defaultBranch" -ForegroundColor Gray
|
|
Write-Host ""
|
|
|
|
if ($Force) {
|
|
Write-Host "⚠ 使用 --force 推送(危险操作)" -ForegroundColor Red
|
|
git push -u origin $defaultBranch --force
|
|
} else {
|
|
git push -u origin $defaultBranch
|
|
}
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host ""
|
|
Write-Host "✓ 代码已成功推送到 GitHub" -ForegroundColor Green
|
|
Write-Host ""
|
|
Write-Host "查看仓库: $repoUrl" -ForegroundColor Cyan
|
|
Write-Host "GitHub Actions: $repoUrl/actions" -ForegroundColor Cyan
|
|
} else {
|
|
Write-Host ""
|
|
Write-Host "✗ 推送失败,请检查错误信息" -ForegroundColor Red
|
|
exit 1
|
|
}
|