-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit-CliScripts.ps1
More file actions
305 lines (293 loc) · 12.4 KB
/
Git-CliScripts.ps1
File metadata and controls
305 lines (293 loc) · 12.4 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# List the branch and state/status from upstream
$upstreamRemote = "upstream"; `
Get-ChildItem -Directory `
| Where-Object Name -NotIn @("_myprojects", "NetduinoSamples") `
| ForEach-Object { `
Set-Location $_; `
$currentBranch = (git branch --show-current); `
$commitsAhead = (git rev-list $currentBranch --not $upstreamRemote/$currentBranch --count); `
$commitsBehind = (git rev-list $upstreamRemote/$currentBranch --not $currentBranch --count); `
Write-Host "$($_.Name): $(git branch --show-current) ↓$commitsAhead ↑$commitsBehind"; `
Set-Location .. `
}
$upstreamRemote = "upstream"; `
Get-ChildItem -Directory `
| Where-Object Name -NotIn @("_myprojects", "NetduinoSamples") `
| ForEach-Object { `
Set-Location $_; `
$currentBranch = (git branch --show-current); `
$commitsAhead = (git rev-list $currentBranch --not $upstreamRemote/$currentBranch --count); `
$commitsBehind = (git rev-list $upstreamRemote/$currentBranch --not $currentBranch --count); `
# Write-Host "$($_.Name): $(git branch --show-current) ↓$commitsAhead ↑$commitsBehind"; `
Set-Location .. `
}
# Get the latest dev commits for each repo.
# Multi-line commands are marked with the continuation character for ease of use in a CLI, but are not needed for a full script file.
$upstreamRemoteDefault = "upstream";
$upstreamDevelopBranchDefault = "develop";
# Start with a list of all the repos we want to check.
$repoNames = @(
"DefCon-Badge-2022",
"Meadow.CLI",
"Meadow.Contracts",
"Meadow.Core",
"Meadow.Core.Samples",
"Meadow.Foundation",
"Meadow.Foundation.Grove",
"Meadow.Foundation.Featherwings",
"Meadow.Foundation.MikroBus",
"Meadow.Linux",
"Meadow.Logging",
#...
);
# Create a list of all repo default overrides to the defaults.
# NOTE: Not using [PSCustomObject] because `Add-Member -NotePropertyMembers` doesn't work with those objects.
$repoCustomDetails = @(
@{
RepoName = "DefCon-Badge-2022";
UpstreamDevelopBranch = "main";
},
#...
);
# Build up a list of repo details using the defaults.
$repoDetailList = $repoNames
| ForEach-Object {
[PSCustomObject]@{
RepoName = $_;
RepoUrl = "https://github.com/WildernessLabs/$_.git";
UpstreamRemoteName = $upstreamRemoteDefault;
UpstreamDevelopBranch = $upstreamDevelopBranchDefault;
LocalDirectory = $_;
}
};
# Merge any custom repo overrides with the bulk-generated defaults.
$repoCustomDetails
| ForEach-Object {
$repoName = $_.RepoName;
$repoDefaults = $repoDetailList | Where-Object { $_.RepoName -eq $repoName };
if ($repoDefaults -ne $null) {
$repoDefaults | Add-Member -NotePropertyMembers $_ -PassThru -Force;
}
} | Out-Null; # Disregard normal console output from this command.
Write-Host "Current state:";
$repoDetailList | Select-Object -First 3 #| Select-Object -Skip 10 #| Select-Object -First 2
| ForEach-Object {
if (Test-Path -Path $_.LocalDirectory) {
Set-Location $_.LocalDirectory;
$currentBranch = (git branch --show-current);
Write-Host "$($_.RepoName) [$($currentBranch)]: " -ForegroundColor White -NoNewline;
if ($currentBranch -eq $_.UpstreamDevelopBranch) {
(git fetch $($_.UpstreamRemoteName) | Out-Null);
$repoDevelopSha = (git rev-parse --short $_.UpstreamDevelopBranch);
$repoCurrentBranchSha = (git rev-parse --short HEAD);
if ($repoDevelopSha -ne $repoCurrentBranchSha) {
$branchAheadCount = $(git rev-list --left-only --count $_.UpstreamDevelopBranch $_.UpstreamRemoteName/$_.UpstreamDevelopBranch);
$branchBehindCount = $(git rev-list --right-right --count $_.UpstreamDevelopBranch $_.UpstreamRemoteName/$_.UpstreamDevelopBranch);
Write-Host "↑$($branchAheadCount), ↓$($branchBehindCount)" -ForegroundColor Yellow;
}
else {
Write-Host "up-to-date" -ForegroundColor Green;
}
}
else {
Write-Host "not on [$($_.UpstreamDevelopBranch)]" -ForegroundColor Yellow;
}
Set-Location ..;
}
}
Write-Host "Continue with updates? (Y|[N])" -NoNewline;
$continueAfterStatus = (Read-Host).ToUpper();
Write-Host $continueAfterStatus
if ($continueAfterStatus -ne "Y" && $continueAfterStatus -ne "y") {
Return
}
$repoDetailList
| ForEach-Object {
# Try to get the latest changes from all upstream repos.
Write-Host "Repo: $($_.RepoName)";
if (Test-Path -Path $_.LocalDirectory) {
Set-Location $_.LocalDirectory;
$currentBranch = (git branch --show-current);
if ($currentBranch -eq $_.UpstreamDevelopBranch) {
# Found expected dev branch currently checked out; proceeding...
$expectedRemoteName = $_.UpstreamRemoteName;
$availableRemoteNames = git remote;
if ($LASTEXITCODE -eq 1) {
# External `git` call failed (git remote).
Write-Warning "`tError getting repo remotes: $($_.RepoName).";
$availableRemoteNames = @();
}
$hasExpectedRemote = ($availableRemoteNames | Where-Object { $_ -eq $expectedRemoteName }).Count -eq 1;
Write-Host $hasExpectedRemote;
if (!$hasExpectedRemote) {
# Didn't find expected Git remote.
Write-Warning "`tRemote not available: $($_.UpstreamRemoteName).";
Write-Warning "`tSkipped update: $($_.RepoName).";
} else {
Write-Host "`tPulling latest from $($_.UpstreamRemoteName)/$($_.UpstreamDevelopBranch).";
git pull $($_.UpstreamRemoteName) $($_.UpstreamDevelopBranch);
if ($LASTEXITCODE -eq 1) {
# External `git` call failed (git pull).
Write-Warning "`tError pulling latest upstream changes: $($_.RepoName) $($_.UpstreamRemoteName)/$($_.UpstreamDevelopBranch).";
} else {
# External `git` call succeeded (git pull).
Write-Host "`tUpdated with latest upstream changes: $($_.RepoName) $($_.UpstreamRemoteName)/$($_.UpstreamDevelopBranch).";
}
}
} else {
# Not on the desired dev branch. Requesting an update to the dev branch without impacting current branch.
Write-Warning "`tRepo $($_.RepoName) not on expected dev branch ($($currentBranch) vs. $($_.UpstreamDevelopBranch)).`n`tAttempting to update adjacent dev branch.";
git fetch $($_.UpstreamRemoteName) $($_.UpstreamDevelopBranch):$($_.UpstreamDevelopBranch);
if ($LASTEXITCODE -eq 1) {
# External `git` call failed (git fetch).
Write-Warning "`tError updating adjacent dev branch: $($_.RepoName) $($_.UpstreamRemoteName)/$($_.UpstreamDevelopBranch).`n`t`tMake sure your dev branch isn't ahead of the upstream dev branch.";
} else {
Write-Host "`tUpdated adjacent dev branch: $($_.RepoName) $($_.UpstreamDevelopBranch).`n`t`tYour current branch was unaffected ($($currentBranch)).";
}
}
Set-Location ..
} else {
# Repo not found; cloning new...
Write-Host "`tRepo folder not found: cloning new copy...";
git clone --branch $($_.UpstreamDevelopBranch) --origin $($_.UpstreamRemoteName) $($_.RepoUrl) $($_.LocalDirectory);
if ($LASTEXITCODE -eq 1) {
# External `git` call failed (git clone).
Write-Warning "`tError cloning repo: $($_.RepoName)";
} else {
# External `git` call succeeded (git clone).
Write-Host "`tCloned new repo: $($_.RepoName)";
}
}
};
# Original version that generated code to then customize manually before running
$repoDetails = @( `
[PSCustomObject]@{ `
Name = "DefCon-Badge-2022"; `
RepoUrl = "https://github.com/WildernessLabs/DefCon-Badge-2022.git"; `
DevBranch = "main"; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.CLI"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.CLI.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Contracts"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Contracts.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Core"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Core.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Core.Samples"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Core.Samples.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Foundation"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Foundation.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Foundation.Grove"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Foundation.Grove.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Foundation.Featherwings"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Foundation.Featherwings.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Foundation.MikroBus"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Foundation.MikroBus.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Logging"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Logging.git"; `
DevBranch = "main"; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Project.Samples"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Project.Samples.git"; `
DevBranch = "b6.5"; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.ProjectLab.Samples"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.ProjectLab.Samples.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow.Units"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow.Units.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "Meadow_Samples"; `
RepoUrl = "https://github.com/WildernessLabs/Meadow_Samples.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
}, `
[PSCustomObject]@{ `
Name = "VSCode_Meadow_Extension"; `
RepoUrl = "https://github.com/WildernessLabs/VSCode_Meadow_Extension.git"; `
DevBranch = $upstreamDevelopBranchDefault; `
UpstreamRemoteName = $upstreamRemoteDefault; `
} `
);
$repoDetails `
# | Select-Object -First 1
| ForEach-Object { `
Set-Location $_.Name; `
Write-Host "Repo: $(Get-Location)"; `
# TODO: Get current branch
$currentBranch = (git branch --show-current); `
if ($currentBranch -eq $_.DevBranch) { `
git pull $_.UpstreamRemoteName $_.DevBranch; `
} else { `
Write-Information "Repo $_.Name not on expected dev branch"; `
} `
Write-Host " Branch: $currentBranch"; `
# TODO: If currently on $_.DevBranch, pull latest from $_DevBranch
# TODO: Else display some output
Set-Location .. `
}
# Generate the initial array of repo details.
$repoNames = @( `
"DefCon-Badge-2022", `
"Meadow.CLI", `
"Meadow.Contracts", `
"Meadow.Core", `
"Meadow.Core.Samples", `
"Meadow.Foundation", `
"Meadow.Foundation.Grove", `
"Meadow.Foundation.Featherwings", `
"Meadow.Foundation.MikroBus", `
"Meadow.Logging", `
"Meadow.Project.Samples", `
"Meadow.ProjectLab.Samples", `
"Meadow.Units", `
"Meadow_Samples", `
"VSCode_Meadow_Extension" `
);
$repoNames | ForEach-Object { " [PSCustomObject]@{ ```n RepoName = ""$_""; ```n RepoUrl = ""https://github.com/WildernessLabs/$_.git""; ```n DevBranch = `$upstreamDevelopBranchDefault; ```n UpstreamRemote = `$upstreamRemoteDefault; ```n }, ``" } | Set-Clipboard
# Add members to existing object.
# TODO: Hoping to start with a repo array of names and defaults and override only the members in a modifications array of objects.
[PSCustomObject]@{ x = 5; y = "d" } | Add-Member -NotePropertyMembers @{ z = 3; a = "e" } -PassThru