feat: 添加远程仓库配置功能,支持自动检查和设置upstream及origin仓库,优化脚本使用体验
This commit is contained in:
parent
f574739ae0
commit
7f9e881472
135
自动同步仓库.ps1
135
自动同步仓库.ps1
@ -3,9 +3,14 @@
|
||||
|
||||
param(
|
||||
[switch]$UseRebase = $false,
|
||||
[switch]$DryRun = $false
|
||||
[switch]$DryRun = $false,
|
||||
[switch]$SetupRemotes = $false
|
||||
)
|
||||
|
||||
# 远程仓库配置常量
|
||||
$UPSTREAM_URL = "http://192.168.189.2:8418/amr.core/web-amr"
|
||||
$ORIGIN_URL = "http://192.168.189.2:8418/xudan/web-map"
|
||||
|
||||
# 设置控制台编码以正确显示中文
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
@ -21,6 +26,91 @@ function Write-ColorOutput {
|
||||
Write-Host $Message -ForegroundColor $Color
|
||||
}
|
||||
|
||||
function Check-RemoteExists {
|
||||
param([string]$RemoteName)
|
||||
$remotes = git remote
|
||||
return $remotes -contains $RemoteName
|
||||
}
|
||||
|
||||
function Setup-RemoteRepositories {
|
||||
Write-ColorOutput "配置远程仓库..." "Cyan"
|
||||
|
||||
# 检查并配置upstream远程仓库
|
||||
if (-not (Check-RemoteExists "upstream")) {
|
||||
Write-ColorOutput "添加upstream远程仓库: $UPSTREAM_URL" "Green"
|
||||
if (-not $DryRun) {
|
||||
git remote add upstream $UPSTREAM_URL
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-ColorOutput "添加upstream远程仓库失败!" "Red"
|
||||
return $false
|
||||
}
|
||||
} else {
|
||||
Write-ColorOutput "[预览模式] git remote add upstream $UPSTREAM_URL" "Gray"
|
||||
}
|
||||
} else {
|
||||
$currentUpstream = git remote get-url upstream
|
||||
Write-ColorOutput "upstream远程仓库已存在: $currentUpstream" "Blue"
|
||||
|
||||
if ($UPSTREAM_URL -ne $currentUpstream) {
|
||||
Write-ColorOutput "更新upstream远程仓库URL为: $UPSTREAM_URL" "Yellow"
|
||||
if (-not $DryRun) {
|
||||
git remote set-url upstream $UPSTREAM_URL
|
||||
} else {
|
||||
Write-ColorOutput "[预览模式] git remote set-url upstream $UPSTREAM_URL" "Gray"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# 检查并配置origin远程仓库
|
||||
if (-not (Check-RemoteExists "origin")) {
|
||||
Write-ColorOutput "添加origin远程仓库: $ORIGIN_URL" "Green"
|
||||
if (-not $DryRun) {
|
||||
git remote add origin $ORIGIN_URL
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-ColorOutput "添加origin远程仓库失败!" "Red"
|
||||
return $false
|
||||
}
|
||||
} else {
|
||||
Write-ColorOutput "[预览模式] git remote add origin $ORIGIN_URL" "Gray"
|
||||
}
|
||||
} else {
|
||||
$currentOrigin = git remote get-url origin
|
||||
Write-ColorOutput "origin远程仓库已存在: $currentOrigin" "Blue"
|
||||
|
||||
if ($ORIGIN_URL -ne $currentOrigin) {
|
||||
Write-ColorOutput "更新origin远程仓库URL为: $ORIGIN_URL" "Yellow"
|
||||
if (-not $DryRun) {
|
||||
git remote set-url origin $ORIGIN_URL
|
||||
} else {
|
||||
Write-ColorOutput "[预览模式] git remote set-url origin $ORIGIN_URL" "Gray"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Write-ColorOutput "远程仓库配置完成!" "Green"
|
||||
return $true
|
||||
}
|
||||
|
||||
function Check-RemoteConfiguration {
|
||||
$hasUpstream = Check-RemoteExists "upstream"
|
||||
$hasOrigin = Check-RemoteExists "origin"
|
||||
|
||||
if (-not $hasUpstream -or -not $hasOrigin) {
|
||||
Write-ColorOutput "远程仓库配置不完整!" "Red"
|
||||
if (-not $hasUpstream) {
|
||||
Write-ColorOutput " 缺少upstream远程仓库(上游仓库)" "Yellow"
|
||||
}
|
||||
if (-not $hasOrigin) {
|
||||
Write-ColorOutput " 缺少origin远程仓库(个人仓库)" "Yellow"
|
||||
}
|
||||
Write-ColorOutput ""
|
||||
Write-ColorOutput "正在自动配置远程仓库..." "Cyan"
|
||||
return Setup-RemoteRepositories
|
||||
}
|
||||
|
||||
return $true
|
||||
}
|
||||
|
||||
function Check-GitStatus {
|
||||
$status = git status --porcelain
|
||||
if ($status) {
|
||||
@ -34,6 +124,11 @@ function Check-GitStatus {
|
||||
function Sync-Repositories {
|
||||
Write-ColorOutput "开始同步镜像仓库..." "Cyan"
|
||||
|
||||
# 检查并自动配置远程仓库
|
||||
if (-not (Check-RemoteConfiguration)) {
|
||||
return
|
||||
}
|
||||
|
||||
# 检查工作目录状态
|
||||
if (-not (Check-GitStatus)) {
|
||||
if (-not $DryRun) {
|
||||
@ -118,19 +213,24 @@ function Sync-Repositories {
|
||||
|
||||
function Show-Help {
|
||||
Write-ColorOutput "Git多仓库同步脚本" "Cyan"
|
||||
Write-ColorOutput "用法: .\sync-repos.ps1 [选项]" "White"
|
||||
Write-ColorOutput "用法: .\自动同步仓库.ps1 [选项]" "White"
|
||||
Write-ColorOutput ""
|
||||
Write-ColorOutput "选项:" "Yellow"
|
||||
Write-ColorOutput " -UseRebase 使用rebase代替merge进行合并" "White"
|
||||
Write-ColorOutput " -DryRun 预览模式,不执行实际的git命令" "White"
|
||||
Write-ColorOutput " -Help 显示此帮助信息" "White"
|
||||
Write-ColorOutput " -UseRebase 使用rebase代替merge进行合并" "White"
|
||||
Write-ColorOutput " -DryRun 预览模式,不执行实际的git命令" "White"
|
||||
Write-ColorOutput " -SetupRemotes 强制重新配置远程仓库" "White"
|
||||
Write-ColorOutput ""
|
||||
Write-ColorOutput "示例:" "Yellow"
|
||||
Write-ColorOutput " .\sync-repos.ps1 # 使用merge同步" "White"
|
||||
Write-ColorOutput " .\sync-repos.ps1 -UseRebase # 使用rebase同步" "White"
|
||||
Write-ColorOutput " .\sync-repos.ps1 -DryRun # 预览模式" "White"
|
||||
Write-ColorOutput " .\自动同步仓库.ps1 # 使用merge同步" "White"
|
||||
Write-ColorOutput " .\自动同步仓库.ps1 -UseRebase # 使用rebase同步" "White"
|
||||
Write-ColorOutput " .\自动同步仓库.ps1 -DryRun # 预览模式" "White"
|
||||
Write-ColorOutput " .\自动同步仓库.ps1 -SetupRemotes # 强制配置远程仓库" "White"
|
||||
Write-ColorOutput ""
|
||||
Write-ColorOutput "注意: 脚本不会自动推送到你的仓库,需要手动执行推送命令。" "Yellow"
|
||||
Write-ColorOutput "仓库配置信息:" "Yellow"
|
||||
Write-ColorOutput " Upstream: $UPSTREAM_URL" "Gray"
|
||||
Write-ColorOutput " Origin: $ORIGIN_URL" "Gray"
|
||||
Write-ColorOutput ""
|
||||
Write-ColorOutput "注意: 脚本会自动配置远程仓库,不会自动推送到你的仓库,需要手动执行推送命令。" "Yellow"
|
||||
}
|
||||
|
||||
# 主程序
|
||||
@ -142,7 +242,22 @@ if ($args -contains "-Help" -or $args -contains "--help" -or $args -contains "-h
|
||||
Write-ColorOutput "Git多仓库同步工具" "Cyan"
|
||||
Write-ColorOutput "===================" "Cyan"
|
||||
|
||||
# 显示当前配置
|
||||
# 如果需要强制配置远程仓库
|
||||
if ($SetupRemotes) {
|
||||
if (-not (Setup-RemoteRepositories)) {
|
||||
Write-ColorOutput "远程仓库配置失败,脚本退出。" "Red"
|
||||
return
|
||||
}
|
||||
Write-ColorOutput ""
|
||||
}
|
||||
|
||||
# 显示预设的仓库配置
|
||||
Write-ColorOutput "预设仓库配置:" "Blue"
|
||||
Write-ColorOutput " Upstream: $UPSTREAM_URL" "Gray"
|
||||
Write-ColorOutput " Origin: $ORIGIN_URL" "Gray"
|
||||
Write-ColorOutput ""
|
||||
|
||||
# 显示当前实际配置
|
||||
Write-ColorOutput "当前远程仓库配置:" "Blue"
|
||||
git remote -v
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user