PowerShell/scripts/sync-repo.ps1

49 lines
1.7 KiB
PowerShell
Raw Normal View History

2024-10-01 15:24:16 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2023-04-12 11:35:57 +02:00
Synchronizes a repo
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2023-09-01 17:53:03 +02:00
This PowerShell script synchronizes a local Git repository by pull and push (including submodules).
2023-09-01 13:25:04 +02:00
.PARAMETER path
2024-10-28 13:46:33 +01:00
Specifies the path to the Git repository (current working directory by default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2024-10-28 13:46:33 +01:00
PS> ./sync-repo.ps1 C:\Repos\curl
2023-09-01 13:25:04 +02:00
(1/4) Searching for Git executable... git version 2.42.0.windows.1
2024-10-28 13:46:33 +01:00
(2/4) Checking local repository... C:\Repos\curl
2023-09-01 13:25:04 +02:00
(3/4) Pulling remote updates... Already up to date.
(4/4) Pushing local updates... Everything up-to-date
2024-10-28 13:46:33 +01:00
Synced the 📂curl repo in 5s.
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
#>
2023-09-01 13:25:04 +02:00
param([string]$path = "$PWD")
2021-04-18 11:01:38 +02:00
try {
2024-10-28 13:46:33 +01:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2021-04-27 15:26:00 +02:00
2022-12-04 11:31:38 +01:00
Write-Host "⏳ (1/4) Searching for Git executable... " -noNewline
& git --version
2023-04-12 11:35:57 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2021-04-18 11:01:38 +02:00
2024-10-28 13:46:33 +01:00
Write-Host "⏳ (2/4) Checking local repository... $path"
2023-09-01 13:25:04 +02:00
if (!(Test-Path "$path" -pathType container)) { throw "Can't access folder: $path" }
2021-04-18 11:01:38 +02:00
2023-09-01 13:25:04 +02:00
Write-Host "⏳ (3/4) Pulling remote updates... " -noNewline
2024-10-28 13:46:33 +01:00
& git -C "$path" pull --all --recurse-submodules
2023-09-01 13:25:04 +02:00
if ($lastExitCode -ne "0") { throw "'git pull --all --recurse-submodes' failed" }
2021-04-18 11:01:38 +02:00
2023-09-01 13:25:04 +02:00
Write-Host "⏳ (4/4) Pushing local updates... " -noNewline
2024-10-28 13:46:33 +01:00
& git -C "$path" push
2023-09-01 13:25:04 +02:00
if ($lastExitCode -ne "0") { throw "'git push' failed" }
2021-04-27 15:26:00 +02:00
2024-10-28 13:46:33 +01:00
$pathName = (Get-Item "$path").Name
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✅ Synced the 📂$pathName repo in $($elapsed)s."
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
}