-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-update-repos.ps1
More file actions
157 lines (137 loc) · 4.27 KB
/
Copy pathgit-update-repos.ps1
File metadata and controls
157 lines (137 loc) · 4.27 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
# Update/Clone All GitHub Repositories (Pure PowerShell 7)
# Transcribed from git-update-repos.sh
# Usage: .\git-update-repos.ps1 [-Username] "user" [-BaseDir] "path" [-UseSSH] [-NoSync] [-Commit]
param(
[string]$Username = (git config user.name 2>$null ?? "lavantien"),
[string]$BaseDir = "$HOME/dev/github",
[switch]$UseSSH,
[switch]$NoSync,
[switch]$Commit
)
# Colors
$C = @{
R = "`e[0;31m"
G = "`e[0;32m"
Y = "`e[1;33m"
B = "`e[0;34m"
C = "`e[0;36m"
N = "`e[0m"
}
function Wc { param($c, $t); Write-Host "$($c)$t$($script:C.N)" }
# Check for GitHub CLI
$ghCmd = Get-Command gh -ErrorAction SilentlyContinue
if (!$ghCmd) {
Wc $C.R "Error: GitHub CLI (gh) not found"
Wc $C.Y "Install it from: https://cli.github.com/"
Wc $C.Y "Or run your bootstrap script"
exit 1
}
# Check gh auth
$null = gh auth status 2>&1
if ($LASTEXITCODE -ne 0) {
Wc $C.R "Error: gh not authenticated. Run: gh auth login"
exit 1
}
Wc $C.C "========================================"
Wc $C.C " GitHub Repos Updater"
Wc $C.C "========================================"
Wc $C.B "User: $Username"
Wc $C.B "Directory: $BaseDir"
Wc $C.B "SSH: $UseSSH"
Wc $C.B "Sync: $(-not $NoSync)"
Wc $C.B "Auto-commit: $Commit"
Wc $C.C "========================================"
Write-Host ""
# Create base directory if needed
if (!(Test-Path $BaseDir)) {
New-Item -ItemType Directory -Path $BaseDir -Force | Out-Null
Wc $C.G "Created directory: $BaseDir"
Write-Host ""
}
Wc $C.C "Fetching repositories via GitHub CLI..."
Write-Host ""
# Fetch repos
$ReposJson = gh repo list --json name,sshUrl,url --limit 1000 2>&1
if ($LASTEXITCODE -ne 0) {
Wc $C.R "Error fetching repositories"
exit 1
}
# Parse JSON
$Repos = $ReposJson | ConvertFrom-Json
Wc $C.G "Found $($Repos.Count) repositories"
Write-Host ""
$Cloned = 0
$Updated = 0
$Skipped = 0
$Failed = 0
foreach ($Repo in $Repos) {
$RepoName = $Repo.name
$RepoPath = Join-Path $BaseDir $RepoName
# Choose URL
$CloneUrl = if ($UseSSH) { $Repo.sshUrl } else { "$($Repo.url).git" }
if (Test-Path $RepoPath) {
# Update existing repo
Write-Host -NoNewline "[$RepoName] "
Push-Location $RepoPath -ErrorAction SilentlyContinue
if ($?) {
if (git rev-parse --git-dir 2>$null) {
# Check if already up to date
$Local = git rev-parse HEAD 2>$null
$Remote = git rev-parse "@{u}" 2>$null
if ($Local -and $Remote -and $Local -eq $Remote) {
Wc $C.B "Skipped (already up to date)"
$Skipped++
} elseif ((git fetch origin 2>&1) -and (git pull 2>&1)) {
Wc $C.Y "Updated"
$Updated++
} else {
Wc $C.R "Error updating"
$Failed++
}
} else {
Wc $C.Y "Skipped (not a git repo)"
$Skipped++
}
Pop-Location
} else {
Wc $C.Y "Error accessing"
$Failed++
}
} else {
# Clone new repo
Write-Host -NoNewline "[$RepoName] "
$null = git clone $CloneUrl $RepoPath 2>&1
if ($LASTEXITCODE -eq 0) {
Wc $C.G "Cloned"
$Cloned++
} else {
Wc $C.Y "Error cloning"
$Failed++
}
}
}
# Sync system instructions
if (-not $NoSync) {
Write-Host ""
Wc $C.C "========================================"
Wc $C.C " Syncing System Instructions"
Wc $C.C "========================================"
Write-Host ""
$SyncScript = Join-Path $PSScriptRoot "sync-system-instructions.ps1"
if (Test-Path $SyncScript) {
& $SyncScript -BaseDir $BaseDir -Commit:$Commit
} else {
Wc $C.Y "Warning: Sync script not found: $SyncScript"
}
}
# Summary
Write-Host ""
Wc $C.C "========================================"
Wc $C.C " Summary"
Wc $C.C "========================================"
Wc $C.G " Cloned: $Cloned"
Wc $C.Y " Updated: $Updated"
Wc $C.B " Skipped: $Skipped"
if ($Failed -gt 0) { Wc $C.Y " Failed: $Failed" }
Wc $C.C " Total: $($Repos.Count) repositories"
Wc $C.C "========================================"