PowerShell/Scripts/sync-repo.ps1

43 lines
1.2 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-01-30 10:49:30 +01:00
Synchronizes a Git repository
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script synchronizes a Git repository by push & pull (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
2021-09-27 08:35:45 +02:00
PS> ./sync-repo C:\MyRepo
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-06-09 16:12:40 +02:00
Author: Markus Fleschutz | License: CC0
2021-04-18 11:01:38 +02:00
#>
2021-07-15 15:51:22 +02:00
param([string]$RepoDir = "$PWD")
2021-04-18 11:01:38 +02:00
try {
2022-03-25 07:28:12 +01:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2021-04-27 15:26:00 +02:00
2022-03-25 07:28:12 +01:00
"⏳ Step 1/3: Checking requirements..."
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
2021-04-18 11:01:38 +02:00
2022-03-25 07:28:12 +01:00
& git --version
2021-04-18 11:01:38 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2022-03-25 07:28:12 +01:00
"⏳ Step 2/3: Pushing local updates..."
& git -C "$RepoDir" push
2021-04-18 11:01:38 +02:00
if ($lastExitCode -ne "0") { throw "'git push' failed" }
2022-03-25 07:28:12 +01:00
"⏳ Step 3/3: Pulling remote updates..."
& git -C "$RepoDir" pull --all --recurse-submodules --jobs=4
2021-04-27 15:26:00 +02:00
if ($lastExitCode -ne "0") { throw "'git pull' failed" }
2022-03-25 07:28:12 +01:00
$RepoDirName = (Get-Item "$RepoDir").Name
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"✔️ synchronized Git repo 📂$RepoDirName in $Elapsed sec"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-04-18 11:01:38 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-04-18 11:01:38 +02:00
exit 1
}