-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-GitPull.ps1
More file actions
483 lines (386 loc) · 16.1 KB
/
Copy pathInvoke-GitPull.ps1
File metadata and controls
483 lines (386 loc) · 16.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
function Invoke-GitPull
{
<#
.SYNOPSIS
Performs a git pull with rebase on one or more Git repositories.
.DESCRIPTION
Updates Git repositories by performing 'git pull --rebase' on the specified paths.
Supports multiple paths, recursive searching for Git repositories, and automatically
skips directories that are not Git repositories.
When using -Recurse, the function searches for directories containing a .git folder
and performs the pull operation on each discovered repository.
Cross-platform compatible with PowerShell 5.1+ on Windows, macOS, and Linux.
Requires Git to be installed and available in PATH.
.PARAMETER Path
The path(s) to Git repositories or directories containing Git repositories.
Defaults to the current directory.
Supports ~ for home directory expansion.
Accepts pipeline input and multiple paths.
Without -Recurse: Each path must be within a Git repository.
With -Recurse: Searches for Git repositories within each path.
.PARAMETER Recurse
Searches for Git repositories recursively within the specified Path(s) and pulls each one.
Useful for workspace directories containing multiple Git repositories.
Example use case: Update all projects in ~/Projects
.PARAMETER Depth
Maximum depth to search for Git repositories when using -Recurse.
Default is unlimited. Use this to limit search depth in large directory trees.
.PARAMETER NoRebase
Performs a regular git pull without the --rebase option.
By default, git pull --rebase is used to maintain a linear history.
.PARAMETER Prune
Adds the --prune flag to remove remote-tracking references that no longer exist on the remote.
.PARAMETER Force
Continue processing remaining repositories even if one fails.
Without this, the function stops on the first error.
.EXAMPLE
PS > Invoke-GitPull
Performs git pull --rebase on the Git repository in the current directory.
.EXAMPLE
PS > Invoke-GitPull -Path ~/Projects/MyRepo
Performs git pull --rebase on the specified repository.
.EXAMPLE
PS > Invoke-GitPull -Path ~/Projects -Recurse
Finds all Git repositories within ~/Projects and performs git pull --rebase on each one.
.EXAMPLE
PS > Invoke-GitPull -Path ~/Projects -Recurse -Depth 2
Finds Git repositories within ~/Projects up to 2 levels deep.
.EXAMPLE
PS > Invoke-GitPull -Path @('~/Project1', '~/Project2')
Performs git pull --rebase on multiple specified repositories.
.EXAMPLE
PS > Get-ChildItem ~/Projects -Directory | Invoke-GitPull
Performs git pull --rebase on each directory piped in (if it's a Git repository).
.EXAMPLE
PS > Invoke-GitPull -NoRebase
Performs a regular git pull without rebase on the current directory.
.EXAMPLE
PS > Invoke-GitPull -Path ~/Projects -Recurse -Prune
Updates all repositories and prunes deleted remote-tracking branches.
.EXAMPLE
PS > Invoke-GitPull -Path ~/Projects -Recurse -Force
Updates all repositories, continuing even if some fail.
.EXAMPLE
PS > Invoke-GitPull -Verbose
Performs git pull --rebase with detailed verbose output.
.OUTPUTS
[PSCustomObject]
Returns an object with summary information about the operation:
- RepositoriesProcessed: Number of Git repositories processed
- RepositoriesUpdated: Number of repositories successfully updated
- RepositoriesSkipped: Number of directories skipped (not Git repositories)
- RepositoriesFailed: Number of repositories that failed to update
- Results: Array of per-repository results (when multiple repositories processed)
.NOTES
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Developer/Invoke-GitPull.ps1
- Requires PowerShell 5.1 or later
- Requires Git to be installed and available in PATH
- Uses 'git pull --rebase' by default for cleaner history
- With -Recurse: Finds .git directories to locate repositories
- Non-Git directories are silently skipped (or reported with -Verbose)
.LINK
https://git-scm.com/docs/git-pull
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Developer/Invoke-GitPull.ps1
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType([PSCustomObject])]
param(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('FullName')]
[ValidateNotNullOrEmpty()]
[String[]]$Path = (Get-Location).Path,
[Parameter()]
[Switch]$Recurse,
[Parameter()]
[ValidateRange(1, [int]::MaxValue)]
[Int]$Depth,
[Parameter()]
[Switch]$NoRebase,
[Parameter()]
[Switch]$Prune,
[Parameter()]
[Switch]$Force
)
begin
{
Write-Verbose 'Starting Invoke-GitPull'
# Check if Git is available
$gitCommand = Get-Command -Name 'git' -ErrorAction SilentlyContinue
if (-not $gitCommand)
{
throw 'Git is not installed or not found in PATH. Please install Git and try again.'
}
# Statistics tracking
$stats = @{
RepositoriesProcessed = 0
RepositoriesUpdated = 0
RepositoriesSkipped = 0
RepositoriesFailed = 0
RepositoriesAlreadyUpToDate = 0
}
$results = [System.Collections.ArrayList]::new()
# Helper function to check if path is a Git repository
function Test-GitRepository
{
param([String]$TestPath)
$gitDir = Join-Path -Path $TestPath -ChildPath '.git'
return Test-Path -Path $gitDir
}
# Helper function to perform git pull on a repository
function Invoke-GitPullOnRepository
{
param(
[String]$RepoPath,
[Bool]$UseRebase,
[Bool]$UsePrune,
[Bool]$IsWhatIf
)
$repoName = Split-Path -Path $RepoPath -Leaf
Write-Verbose "Processing repository: $RepoPath"
# Build git arguments
$gitArgs = @('pull')
if ($UseRebase)
{
$gitArgs += '--rebase'
}
if ($UsePrune)
{
$gitArgs += '--prune'
}
$resultObj = [PSCustomObject]@{
Path = $RepoPath
Name = $repoName
Success = $false
HadUpdates = $false
Message = ''
Output = ''
}
if ($IsWhatIf)
{
$resultObj.Message = 'Would perform git pull'
$resultObj.Success = $true
return $resultObj
}
try
{
# Execute git pull using .NET Process class for reliable output capture
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = 'git'
$psi.Arguments = $gitArgs -join ' '
$psi.WorkingDirectory = $RepoPath
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
$psi.CreateNoWindow = $true
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $psi
$null = $proc.Start()
$stdoutContent = $proc.StandardOutput.ReadToEnd()
$stderrContent = $proc.StandardError.ReadToEnd()
$proc.WaitForExit()
$exitCode = $proc.ExitCode
if ($exitCode -eq 0)
{
$resultObj.Success = $true
$resultObj.Output = $stdoutContent.Trim()
if ([String]::IsNullOrWhiteSpace($stdoutContent) -or $stdoutContent -match 'Already up to date')
{
$resultObj.Message = 'Already up to date'
$resultObj.HadUpdates = $false
Write-Host " [OK] $repoName (already up to date)" -ForegroundColor Green
}
else
{
$resultObj.Message = 'Updated successfully'
$resultObj.HadUpdates = $true
Write-Host " [OK] $repoName (pulled updates)" -ForegroundColor Green
}
}
else
{
$resultObj.Success = $false
$resultObj.Message = 'Pull failed'
$resultObj.Output = $stderrContent.Trim()
Write-Host " [FAILED] $repoName - $($stderrContent.Trim())" -ForegroundColor Red
}
}
catch
{
$resultObj.Success = $false
$resultObj.Message = $_.Exception.Message
$resultObj.Output = $_.Exception.Message
Write-Host " [ERROR] $repoName - $($_.Exception.Message)" -ForegroundColor Red
}
return $resultObj
}
# Helper function to find Git repositories recursively
function Find-GitRepositories
{
param(
[String]$SearchPath,
[Int]$MaxDepth = -1
)
$repos = [System.Collections.ArrayList]::new()
# Check if the search path itself is a repository
if (Test-GitRepository -TestPath $SearchPath)
{
$null = $repos.Add($SearchPath)
return $repos
}
# Search for .git directories
$getChildItemParams = @{
Path = $SearchPath
Filter = '.git'
Directory = $true
Force = $true
Recurse = $true
ErrorAction = 'SilentlyContinue'
}
if ($MaxDepth -gt 0)
{
$getChildItemParams.Depth = $MaxDepth
}
$gitDirs = Get-ChildItem @getChildItemParams
foreach ($gitDir in $gitDirs)
{
$repoPath = Split-Path -Path $gitDir.FullName -Parent
$null = $repos.Add($repoPath)
}
return $repos
}
# Collection to accumulate paths from pipeline
$allPaths = [System.Collections.ArrayList]::new()
}
process
{
foreach ($currentPath in $Path)
{
$null = $allPaths.Add($currentPath)
}
}
end
{
$useRebase = -not $NoRebase.IsPresent
Write-Host 'Git Pull Operation' -ForegroundColor Cyan
Write-Host ('-' * 40) -ForegroundColor DarkGray
foreach ($currentPath in $allPaths)
{
# Normalize path (expand ~ and resolve)
$resolvedPath = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($currentPath)
if (-not (Test-Path -Path $resolvedPath))
{
Write-Warning "Path not found: $resolvedPath"
$stats.RepositoriesSkipped++
continue
}
$item = Get-Item -Path $resolvedPath
if (-not $item.PSIsContainer)
{
Write-Warning "Path is not a directory: $resolvedPath"
$stats.RepositoriesSkipped++
continue
}
if ($Recurse)
{
# Find all Git repositories in the path
$maxDepth = if ($PSBoundParameters.ContainsKey('Depth')) { $Depth } else { -1 }
$repositories = Find-GitRepositories -SearchPath $resolvedPath -MaxDepth $maxDepth
if ($repositories.Count -eq 0)
{
Write-Verbose "No Git repositories found in: $resolvedPath"
continue
}
Write-Host "Found $($repositories.Count) Git repositor$(if ($repositories.Count -eq 1) { 'y' } else { 'ies' }) in: $resolvedPath" -ForegroundColor Cyan
foreach ($repoPath in $repositories)
{
if ($PSCmdlet.ShouldProcess($repoPath, 'git pull'))
{
$result = Invoke-GitPullOnRepository -RepoPath $repoPath -UseRebase $useRebase -UsePrune $Prune.IsPresent -IsWhatIf $WhatIfPreference
$stats.RepositoriesProcessed++
if ($result.Success)
{
$stats.RepositoriesUpdated++
if (-not $result.HadUpdates)
{
$stats.RepositoriesAlreadyUpToDate++
}
}
else
{
$stats.RepositoriesFailed++
if (-not $Force)
{
Write-Error "Failed to update repository '$repoPath': $($result.Message)"
break
}
}
$null = $results.Add($result)
}
}
}
else
{
# Single repository mode
if (-not (Test-GitRepository -TestPath $resolvedPath))
{
Write-Verbose "Skipping non-Git directory: $resolvedPath"
$stats.RepositoriesSkipped++
continue
}
if ($PSCmdlet.ShouldProcess($resolvedPath, 'git pull'))
{
$result = Invoke-GitPullOnRepository -RepoPath $resolvedPath -UseRebase $useRebase -UsePrune $Prune.IsPresent -IsWhatIf $WhatIfPreference
$stats.RepositoriesProcessed++
if ($result.Success)
{
$stats.RepositoriesUpdated++
if (-not $result.HadUpdates)
{
$stats.RepositoriesAlreadyUpToDate++
}
}
else
{
$stats.RepositoriesFailed++
if (-not $Force)
{
Write-Error "Failed to update repository '$resolvedPath': $($result.Message)"
}
}
$null = $results.Add($result)
}
}
}
# Summary
Write-Host ''
Write-Host 'Summary:' -ForegroundColor Cyan
Write-Host " Repositories processed: $($stats.RepositoriesProcessed)" -ForegroundColor Gray
Write-Host " Updated successfully: $($stats.RepositoriesUpdated)" -ForegroundColor Green
if ($stats.RepositoriesAlreadyUpToDate -gt 0)
{
Write-Host " Already up to date: $($stats.RepositoriesAlreadyUpToDate)" -ForegroundColor DarkGreen
}
if ($stats.RepositoriesSkipped -gt 0)
{
Write-Host " Skipped (not Git): $($stats.RepositoriesSkipped)" -ForegroundColor Yellow
}
if ($stats.RepositoriesFailed -gt 0)
{
Write-Host " Failed: $($stats.RepositoriesFailed)" -ForegroundColor Red
}
# Return summary object
$summary = [PSCustomObject]@{
RepositoriesProcessed = $stats.RepositoriesProcessed
RepositoriesUpdated = $stats.RepositoriesUpdated
RepositoriesAlreadyUpToDate = $stats.RepositoriesAlreadyUpToDate
RepositoriesSkipped = $stats.RepositoriesSkipped
RepositoriesFailed = $stats.RepositoriesFailed
Results = @($results)
}
Write-Verbose 'Invoke-GitPull completed'
return $summary
}
}