2025-06-16 10:34:11 +08:00
|
|
|
# Git Multi-Repository Sync Script (PowerShell)
|
|
|
|
|
|
|
|
param(
|
|
|
|
[switch]$UseRebase = $false,
|
|
|
|
[switch]$DryRun = $false
|
|
|
|
)
|
|
|
|
|
|
|
|
function Write-ColorOutput {
|
|
|
|
param(
|
|
|
|
[string]$Message,
|
|
|
|
[string]$Color = "White"
|
|
|
|
)
|
|
|
|
Write-Host $Message -ForegroundColor $Color
|
|
|
|
}
|
|
|
|
|
|
|
|
function Check-GitStatus {
|
|
|
|
$status = git status --porcelain
|
|
|
|
if ($status) {
|
|
|
|
Write-ColorOutput "Warning: Working directory is not clean. Please commit or stash your changes first!" "Yellow"
|
|
|
|
git status
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
return $true
|
|
|
|
}
|
|
|
|
|
|
|
|
function Sync-Repositories {
|
|
|
|
Write-ColorOutput "Starting repository sync..." "Cyan"
|
|
|
|
|
|
|
|
# Check working directory status
|
|
|
|
if (-not (Check-GitStatus)) {
|
|
|
|
if (-not $DryRun) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fetch latest code from upstream
|
|
|
|
Write-ColorOutput "Fetching latest code from upstream..." "Green"
|
|
|
|
if (-not $DryRun) {
|
|
|
|
git fetch upstream
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
Write-ColorOutput "Failed to fetch upstream code!" "Red"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Write-ColorOutput "[DRY RUN] git fetch upstream" "Gray"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if there are new commits
|
|
|
|
$upstreamCommits = git log HEAD..upstream/master --oneline
|
|
|
|
if ($upstreamCommits) {
|
|
|
|
Write-ColorOutput "Found new commits from upstream:" "Yellow"
|
|
|
|
Write-ColorOutput $upstreamCommits "Gray"
|
|
|
|
|
|
|
|
if ($UseRebase) {
|
|
|
|
Write-ColorOutput "Merging code using rebase..." "Green"
|
|
|
|
if (-not $DryRun) {
|
|
|
|
git rebase upstream/master
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
Write-ColorOutput "Rebase failed! Please resolve conflicts manually." "Red"
|
2025-06-16 10:39:47 +08:00
|
|
|
Write-ColorOutput "After resolving conflicts, run: git rebase --continue" "Yellow"
|
2025-06-16 10:34:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Write-ColorOutput "[DRY RUN] git rebase upstream/master" "Gray"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Write-ColorOutput "Merging code using merge..." "Green"
|
|
|
|
if (-not $DryRun) {
|
|
|
|
git merge upstream/master
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
Write-ColorOutput "Merge failed! Please resolve conflicts manually." "Red"
|
2025-06-16 10:39:47 +08:00
|
|
|
Write-ColorOutput "After resolving conflicts, run: git commit" "Yellow"
|
2025-06-16 10:34:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Write-ColorOutput "[DRY RUN] git merge upstream/master" "Gray"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-16 10:39:47 +08:00
|
|
|
Write-ColorOutput "Sync completed successfully!" "Green"
|
|
|
|
Write-ColorOutput ""
|
|
|
|
Write-ColorOutput "Next steps:" "Cyan"
|
|
|
|
Write-ColorOutput " Please manually push to your repository: git push origin master" "White"
|
|
|
|
if ($UseRebase) {
|
|
|
|
Write-ColorOutput " (If using rebase, you might need: git push origin master --force-with-lease)" "Yellow"
|
2025-06-16 10:34:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Write-ColorOutput "No new commits from upstream. Checking for local changes..." "Blue"
|
|
|
|
|
|
|
|
# Check if there are local changes to push
|
|
|
|
$localCommits = git log origin/master..HEAD --oneline
|
|
|
|
if ($localCommits) {
|
|
|
|
Write-ColorOutput "Found local changes to push:" "Yellow"
|
|
|
|
Write-ColorOutput $localCommits "Gray"
|
|
|
|
|
2025-06-16 10:39:47 +08:00
|
|
|
Write-ColorOutput ""
|
|
|
|
Write-ColorOutput "Next steps:" "Cyan"
|
|
|
|
Write-ColorOutput " Please manually push to your repository: git push origin master" "White"
|
2025-06-16 10:34:11 +08:00
|
|
|
} else {
|
|
|
|
Write-ColorOutput "Nothing to sync." "Blue"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Show-Help {
|
|
|
|
Write-ColorOutput "Git Multi-Repository Sync Script" "Cyan"
|
|
|
|
Write-ColorOutput "Usage: .\sync-repos.ps1 [options]" "White"
|
|
|
|
Write-ColorOutput ""
|
|
|
|
Write-ColorOutput "Options:" "Yellow"
|
|
|
|
Write-ColorOutput " -UseRebase Use rebase instead of merge" "White"
|
|
|
|
Write-ColorOutput " -DryRun Preview mode, don't execute actual git commands" "White"
|
|
|
|
Write-ColorOutput " -Help Show this help message" "White"
|
|
|
|
Write-ColorOutput ""
|
|
|
|
Write-ColorOutput "Examples:" "Yellow"
|
|
|
|
Write-ColorOutput " .\sync-repos.ps1 # Sync using merge" "White"
|
|
|
|
Write-ColorOutput " .\sync-repos.ps1 -UseRebase # Sync using rebase" "White"
|
|
|
|
Write-ColorOutput " .\sync-repos.ps1 -DryRun # Preview mode" "White"
|
2025-06-16 10:39:47 +08:00
|
|
|
Write-ColorOutput ""
|
|
|
|
Write-ColorOutput "Note: Script will NOT automatically push to your repository." "Yellow"
|
2025-06-16 10:34:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Main program
|
|
|
|
if ($args -contains "-Help" -or $args -contains "--help" -or $args -contains "-h") {
|
|
|
|
Show-Help
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-ColorOutput "Git Multi-Repository Sync Tool" "Cyan"
|
|
|
|
Write-ColorOutput "==============================" "Cyan"
|
|
|
|
|
|
|
|
# Show current configuration
|
|
|
|
Write-ColorOutput "Current remote repository configuration:" "Blue"
|
|
|
|
git remote -v
|
|
|
|
|
|
|
|
Write-ColorOutput ""
|
|
|
|
if ($DryRun) {
|
|
|
|
Write-ColorOutput "Preview mode - no actual git commands will be executed" "Yellow"
|
|
|
|
}
|
|
|
|
if ($UseRebase) {
|
|
|
|
Write-ColorOutput "Using rebase mode" "Yellow"
|
|
|
|
} else {
|
|
|
|
Write-ColorOutput "Using merge mode" "Yellow"
|
|
|
|
}
|
|
|
|
Write-ColorOutput ""
|
|
|
|
|
|
|
|
Sync-Repositories
|