PowerShell/scripts/pull-repos.ps1

61 lines
2.1 KiB
PowerShell
Raw Normal View History

2023-10-31 13:03:45 +01:00
<#
2021-09-29 08:28:20 +02:00
.SYNOPSIS
Pulls updates into Git repos
2021-07-13 21:10:02 +02:00
.DESCRIPTION
This PowerShell script pulls updates into all Git repositories in a folder (including submodules).
.PARAMETER parentDir
2021-10-16 16:50:10 +02:00
Specifies the path to the parent folder
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./pull-repos C:\MyRepos
2024-02-29 08:23:27 +01:00
(1) Searching for Git executable... git version 2.43.0
(2) Checking parent folder... 33 subfolders
(3/35) Pulling into 📂base256unicode...
...
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-04-14 07:39:34 +02:00
.NOTES
Author: Markus Fleschutz | License: CC0
2021-01-20 16:11:38 +01:00
#>
param([string]$parentDir = "$PWD")
2021-02-18 20:17:55 +01:00
2021-02-13 10:26:14 +01:00
try {
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2021-03-19 17:43:28 +01:00
Write-Host "⏳ (1) Searching for Git executable...`t`t" -NoNewline
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
Write-Host "⏳ (2) Checking parent folder...`t`t" -NoNewline
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access folder: $parentDir" }
$folders = (Get-ChildItem "$parentDir" -attributes Directory)
$numFolders = $folders.Count
$parentDirName = (Get-Item "$parentDir").Name
Write-Host "$numFolders subfolders"
2021-02-28 18:49:53 +01:00
[int]$step = 3
[int]$failed = 0
foreach ($folder in $folders) {
$folderName = (Get-Item "$folder").Name
Write-Host "⏳ ($step/$($numFolders + 2)) Pulling into 📂$folderName...`t`t" -NoNewline
2021-02-28 18:49:53 +01:00
& git -C "$folder" pull --recurse-submodules --jobs=4
if ($lastExitCode -ne "0") { $failed++; write-warning "'git pull' in 📂$folderName failed" }
2021-06-16 08:38:33 +02:00
& git -C "$folder" submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' in 📂$folder failed with exit code $lastExitCode" }
$step++
2021-01-20 16:11:38 +01:00
}
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-02-29 08:23:27 +01:00
if ($failed -eq 0) {
2024-06-13 11:51:19 +02:00
"✔️ Updated $numFolders repositories under 📂$parentDirName in $($elapsed)s."
2024-02-29 08:23:27 +01:00
exit 0 # success
} else {
2024-06-13 11:51:19 +02:00
"⚠️ Updated $numFolders repositories under 📂$parentDirName in $($elapsed)s but $failed failed!"
2024-02-29 08:23:27 +01:00
exit 1
}
2021-01-20 16:11:38 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-01-20 16:11:38 +01:00
exit 1
2022-11-05 15:53:36 +01:00
}