-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-all-repos.ps1
More file actions
163 lines (138 loc) · 6.82 KB
/
update-all-repos.ps1
File metadata and controls
163 lines (138 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# PowerShell script to sync changes from energy repository to all other repositories
# This script will push energy repository changes and pull them into other repositories
# Set target directory using current username
$currentUser = $env:USERNAME
$targetDirectory = "C:\Users\$currentUser\Code\asciimath"
$energyRepoPath = "$targetDirectory\energy"
# First, ensure energy repository changes are committed and pushed
Write-Host "Syncing changes from energy repository to all other repositories..." -ForegroundColor Green
Write-Host "Step 1: Processing energy repository..." -ForegroundColor Cyan
Push-Location $energyRepoPath
try {
# First, fetch latest changes from energy repository
Write-Host " 🔄 Fetching latest changes from energy repository..." -ForegroundColor Cyan
$fetchResult = git fetch --all 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " ✓ Fetch successful" -ForegroundColor Green
} else {
Write-Host " ⚠️ Fetch completed with warnings: $fetchResult" -ForegroundColor DarkYellow
}
# Check current branch
$currentBranch = git branch --show-current 2>$null
if (-not $currentBranch) {
$currentBranch = "main" # fallback
}
# Pull latest changes if behind
$behind = git rev-list HEAD..origin/$currentBranch --count 2>$null
if ($behind -and $behind -gt 0) {
Write-Host " 📥 Energy repository is $behind commits behind, pulling changes..." -ForegroundColor Cyan
$pullResult = git pull origin $currentBranch 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " ✓ Pull successful - updated $behind commits" -ForegroundColor Green
} else {
Write-Host " ❌ Pull failed: $pullResult" -ForegroundColor Red
}
} else {
Write-Host " ✓ Energy repository is up to date with remote" -ForegroundColor Green
}
# Check if there are uncommitted changes in energy repo
$energyStatus = git status --porcelain 2>$null
if ($energyStatus) {
Write-Host " 📝 Found uncommitted changes in energy repository, committing..." -ForegroundColor Yellow
git add . 2>$null
$commitMessage = "Auto-sync: Updated energy repository - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
git commit -m $commitMessage 2>$null
Write-Host " ✓ Committed changes to energy repository" -ForegroundColor Green
}
# Push energy repository changes
Write-Host " 📤 Pushing energy repository changes..." -ForegroundColor Cyan
$pushResult = git push 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " ✓ Successfully pushed energy repository changes" -ForegroundColor Green
} else {
Write-Host " ⚠️ Push completed with warnings: $pushResult" -ForegroundColor DarkYellow
}
} catch {
Write-Host " ❌ Error processing energy repository: $($_.Exception.Message)" -ForegroundColor Red
} finally {
Pop-Location
}
# Get list of all subdirectories (repositories) excluding energy
$repos = Get-ChildItem -Path $targetDirectory -Directory | Where-Object { $_.Name -ne "energy" }
Write-Host "`nStep 2: Updating all other repositories with latest changes..." -ForegroundColor Cyan
Write-Host "Found $($repos.Count) repositories to update" -ForegroundColor Cyan
$successCount = 0
$failCount = 0
$skippedCount = 0
foreach ($repo in $repos) {
$repoName = $repo.Name
$repoPath = $repo.FullName
Write-Host "`nProcessing repository: $repoName" -ForegroundColor Yellow
# Check if this is a git repository
if (-not (Test-Path (Join-Path $repoPath ".git"))) {
Write-Host " ⚠️ Skipping $repoName - not a git repository" -ForegroundColor DarkYellow
$skippedCount++
continue
}
# Navigate to repo directory
Push-Location $repoPath
try {
# Add safe directory configuration to prevent git ownership issues
Write-Host " 📁 Adding safe directory configuration..." -ForegroundColor Cyan
git config --global --add safe.directory $repoPath.Replace('\', '/') 2>$null
# Fetch latest changes
Write-Host " 🔄 Fetching latest changes..." -ForegroundColor Cyan
$fetchResult = git fetch --all 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " ✓ Fetch successful" -ForegroundColor Green
} else {
Write-Host " ⚠️ Fetch completed with warnings: $fetchResult" -ForegroundColor DarkYellow
}
# Check current branch
$currentBranch = git branch --show-current 2>$null
if (-not $currentBranch) {
$currentBranch = "main" # fallback
}
Write-Host " 📋 Current branch: $currentBranch" -ForegroundColor Cyan
# Check if there are any changes to pull
$status = git status --porcelain 2>$null
$behind = git rev-list HEAD..origin/$currentBranch --count 2>$null
if ($behind -and $behind -gt 0) {
Write-Host " 📥 Repository is $behind commits behind, pulling changes..." -ForegroundColor Cyan
$pullResult = git pull origin $currentBranch 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " ✓ Pull successful - updated $behind commits" -ForegroundColor Green
} else {
Write-Host " ❌ Pull failed: $pullResult" -ForegroundColor Red
$failCount++
continue
}
} else {
Write-Host " ✓ Repository is up to date" -ForegroundColor Green
}
# Check for uncommitted changes
if ($status) {
Write-Host " ⚠️ Repository has uncommitted changes:" -ForegroundColor DarkYellow
git status --short
}
$successCount++
} catch {
Write-Host " ❌ Error updating $repoName`: $($_.Exception.Message)" -ForegroundColor Red
$failCount++
} finally {
# Return to original directory
Pop-Location
}
}
Write-Host "`n🎉 Synchronization completed!" -ForegroundColor Green
Write-Host "📊 Sync Results Summary:" -ForegroundColor Cyan
Write-Host " ✓ Energy repository: Pulled latest changes, committed local changes, and pushed" -ForegroundColor Green
Write-Host " ✓ Successfully updated: $successCount repositories" -ForegroundColor Green
Write-Host " ❌ Failed to update: $failCount repositories" -ForegroundColor Red
Write-Host " ⚠️ Skipped (not git repos): $skippedCount directories" -ForegroundColor DarkYellow
Write-Host " 📁 Total processed: $($repos.Count) directories" -ForegroundColor Cyan
if ($failCount -gt 0) {
Write-Host "`n⚠️ Some repositories failed to update. Check the output above for details." -ForegroundColor DarkYellow
} else {
Write-Host "`n🎉 All repositories are now synchronized with energy repository changes!" -ForegroundColor Green
}