PowerShell_Scripts/git/Get-GitRepo.ps1

31 lines
930 B
PowerShell

[CmdletBinding(SupportsShouldProcess)]param(
[Parameter(Mandatory)]
[string]$LocalPath,
[Parameter(Mandatory)]
[string]$GitOrigin,
[string]$Branch='HEAD',
[switch]$sudo
)
$local:gitPath = Join-Path 'themes' 'plextheme' | Get-Path
function doGit { & $(if($sudo){"sudo"}) git "--git-dir=`"$(Join-Path $LocalPath '.git')`"" "--work-tree=`"$LocalPath`"" $args }
$local:savedErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
$local:exists = Test-Path $LocalPath
try {
if( $exists ) {
# Update code from git repo
Write-Verbose "Pulling from [$GitOrigin] into [$LocalPath] on branch/tag '$Branch'..."
doGit fetch --quiet
} else {
git clone $GitOrigin $LocalPath
}
doGit checkout $Branch --quiet
if( $exists ) {
doGit status
}
} catch {
throw $_
} finally {
$ErrorActionPreference = $savedErrorActionPreference
}