PowerShell/scripts/pull-repo.ps1

50 lines
1.9 KiB
PowerShell
Raw Normal View History

2023-10-31 13:03:45 +01:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2024-04-17 11:23:41 +02:00
Pulls updates into a Git repo
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2024-04-17 11:23:41 +02:00
This PowerShell script pulls remote updates into a local Git repository (including submodules).
.PARAMETER pathToRepo
2022-10-31 11:14:53 +01:00
Specifies the file path to the local Git repository (default is working directory)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2024-05-13 08:35:08 +02:00
PS> ./pull-repo.ps1
2024-04-17 11:23:41 +02:00
(1/4) Searching for Git executable... git version 2.44.0.windows.1
2024-06-13 11:51:19 +02:00
(2/4) Checking local repository... C:\Repos\rust
2024-04-17 11:23:41 +02:00
(3/4) Pulling remote updates...
2023-09-06 16:43:16 +02:00
(4/4) Updating submodules...
2024-05-16 07:28:18 +02:00
Updates pulled into 📂rust repo in 14s.
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-03-28 16:11:13 +02:00
.NOTES
Author: Markus Fleschutz | License: CC0
2021-03-19 17:47:07 +01:00
#>
2024-04-17 11:23:41 +02:00
param([string]$pathToRepo = "$PWD")
2021-03-19 17:47:07 +01:00
try {
2024-04-17 11:23:41 +02:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2021-04-28 07:41:12 +02:00
2023-04-06 09:41:33 +02:00
Write-Host "⏳ (1/4) Searching for Git executable... " -noNewline
2022-08-10 15:16:54 +02:00
& git --version
2021-06-08 14:03:12 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2022-03-29 08:05:30 +02:00
2024-06-13 11:51:19 +02:00
Write-Host "⏳ (2/4) Checking local repository... $pathToRepo"
2024-04-17 11:23:41 +02:00
if (-not(Test-Path "$pathToRepo" -pathType container)) { throw "Can't access folder: $pathToRepo" }
$result = (git -C "$pathToRepo" status)
2024-05-13 08:35:08 +02:00
if ("$result" -match "HEAD detached at ") { throw "Nothing to pull due to detached HEAD state (not on a branch!)" }
2024-04-17 11:23:41 +02:00
$pathToRepoName = (Get-Item "$pathToRepo").Name
2021-06-08 14:03:12 +02:00
2024-04-17 11:23:41 +02:00
Write-Host "⏳ (3/4) Pulling remote updates..."
& git -C "$pathToRepo" pull --recurse-submodules=yes
if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" }
2021-03-19 17:47:07 +01:00
2022-12-04 11:49:19 +01:00
Write-Host "⏳ (4/4) Updating submodules... "
2024-04-17 11:23:41 +02:00
& git -C "$pathToRepo" submodule update --init --recursive
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
2021-06-16 08:38:33 +02:00
2024-04-17 11:23:41 +02:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-05-16 07:28:18 +02:00
"✔️ Updates pulled into 📂$pathToRepoName repo in $($elapsed)s."
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-03-19 17:47:07 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-19 17:47:07 +01:00
exit 1
2022-08-10 15:16:54 +02:00
}