PowerShell/Docs/sync-repo.md
2022-12-04 10:40:18 +01:00

2.2 KiB

The sync-repo.ps1 Script

This PowerShell script synchronizes a Git repository by push & pull (including submodules).

Parameters

sync-repo.ps1 [[-RepoDir] <String>] [<CommonParameters>]

-RepoDir <String>
    Specifies the path to the Git repository
    
    Required?                    false
    Position?                    1
    Default value                "$PWD"
    Accept pipeline input?       false
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./sync-repo C:\MyRepo

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Source Code

<#
.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
}

Generated by convert-ps2md.ps1 using the comment-based help of sync-repo.ps1