microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
.github/skills/shared/pr-reference/scripts/shared.psm1
37lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | |
| 4 | function Get-RepositoryRoot { |
| 5 | <# |
| 6 | .SYNOPSIS |
| 7 | Gets the repository root path. |
| 8 | .DESCRIPTION |
| 9 | Runs git rev-parse --show-toplevel to locate the repository root. |
| 10 | In default mode, falls back to the current directory when git fails. |
| 11 | With -Strict, throws a terminating error instead. |
| 12 | .PARAMETER Strict |
| 13 | When set, throws instead of falling back to the current directory. |
| 14 | .OUTPUTS |
| 15 | System.String |
| 16 | #> |
| 17 | [OutputType([string])] |
| 18 | param( |
| 19 | [switch]$Strict |
| 20 | ) |
| 21 | |
| 22 | if ($Strict) { |
| 23 | $repoRoot = (& git rev-parse --show-toplevel).Trim() |
| 24 | if (-not $repoRoot) { |
| 25 | throw "Unable to determine repository root." |
| 26 | } |
| 27 | return $repoRoot |
| 28 | } |
| 29 | |
| 30 | $root = & git rev-parse --show-toplevel 2>$null |
| 31 | if ($LASTEXITCODE -eq 0 -and $root) { |
| 32 | return $root.Trim() |
| 33 | } |
| 34 | return $PWD.Path |
| 35 | } |
| 36 | |
| 37 | Export-ModuleMember -Function Get-RepositoryRoot |
| 38 | |