PowerShell/Scripts/pull-repo.ps1

46 lines
1.5 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-08-10 15:16:54 +02:00
Pulls Git repository updates
2021-10-04 21:29:23 +02:00
.DESCRIPTION
This PowerShell script pulls updates for a local Git repository (including submodules).
2021-10-16 16:50:10 +02:00
.PARAMETER RepoDir
Specifies the path to the Git repository
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> ./pull-repo
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
#>
2021-07-15 15:51:22 +02:00
param([string]$RepoDir = "$PWD")
2021-03-19 17:47:07 +01:00
try {
2021-04-28 07:41:12 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2022-08-10 15:16:54 +02:00
"⏳ Step 1/4 - Searching for Git executable..."
& 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
2022-08-10 15:16:54 +02:00
"⏳ Step 2/4 - Checking Git repository..."
2022-03-29 08:05:30 +02:00
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access folder: $RepoDir" }
$Result = (git -C "$RepoDir" status)
if ("$Result" -match "HEAD detached at ") { throw "Not on a branch, so nothing to pull (in detached HEAD state)" }
2021-06-08 14:03:12 +02:00
2022-08-10 15:16:54 +02:00
"⏳ Step 3/4 - Pulling updates..."
2022-03-28 16:11:13 +02:00
& git -C "$RepoDir" pull --recurse-submodules --jobs=4
if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" }
2021-03-19 17:47:07 +01:00
2022-08-10 15:16:54 +02:00
"⏳ Step 4/4 - Updating submodules... "
2022-03-28 16:11:13 +02:00
& git -C "$RepoDir" 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
2022-03-28 16:11:13 +02:00
$RepoDirName = (Get-Item "$RepoDir").Name
2021-06-08 14:03:12 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2022-05-02 16:47:17 +02:00
"✔️ pulled updates for 📂$RepoDirName repo in $Elapsed sec"
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
}