Skip to content

Fetch-Repo

Fetches updates into a Git repo

param([string]$path = "$PWD")

try {
	$stopWatch = [system.diagnostics.stopwatch]::startNew()

	Write-Host "? (1/3) Searching for Git executable...    " -noNewline
	& git --version
	if ($lastExitCode -ne 0) { throw "Can't execute 'git' - make sure Git is installed and available" }

	Write-Host "? (2/3) Checking local repository...       $path"
	if (!(Test-Path "$path" -pathType container)) { throw "Can't access folder: $path" }
	$repoDirName = (Get-Item "$path").Name

	Write-Host "? (3/3) Fetching updates (including submodules)..."
	& git -C "$path" fetch --all --recurse-submodules --tags --prune --prune-tags --force --quiet
	if ($lastExitCode -ne 0) { throw "'git fetch --all' failed with exit code $lastExitCode" }
	
	[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
	"? Updates fetched into ??$repoDirName repo in $($elapsed)s."
	exit 0 # success
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the file path to the local Git repository (default is working directory).

An interactive directory of PowerShell scripts.