PowerShell/Scripts/sync-repo.ps1
2022-07-05 14:38:25 +02:00

43 lines
1.2 KiB
PowerShell
Executable File

<#
.SYNOPSIS
Synchronizes a Git repository
.DESCRIPTION
This PowerShell script synchronizes a Git repository by push & pull (including submodules).
.PARAMETER RepoDir
Specifies the path to the Git repository
.EXAMPLE
PS> ./sync-repo C:\MyRepo
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$RepoDir = "$PWD")
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
"⏳ Step 1/3: Checking requirements..."
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
"⏳ Step 2/3: Pushing local updates..."
& git -C "$RepoDir" push
if ($lastExitCode -ne "0") { throw "'git push' failed" }
"⏳ Step 3/3: Pulling remote updates..."
& git -C "$RepoDir" pull --all --recurse-submodules --jobs=4
if ($lastExitCode -ne "0") { throw "'git pull' failed" }
$RepoDirName = (Get-Item "$RepoDir").Name
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ synchronized Git repo 📂$RepoDirName in $Elapsed sec"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}