microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
.github/skills/shared/pr-reference/scripts/shared.psm1
98lines · 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 | function Resolve-DefaultBranch { |
| 38 | <# |
| 39 | .SYNOPSIS |
| 40 | Resolves the default branch from the remote HEAD ref. |
| 41 | .DESCRIPTION |
| 42 | Runs git symbolic-ref refs/remotes/origin/HEAD to detect the default branch. |
| 43 | Falls back to origin/main when the symbolic ref is unavailable. |
| 44 | .OUTPUTS |
| 45 | System.String |
| 46 | #> |
| 47 | [OutputType([string])] |
| 48 | param() |
| 49 | |
| 50 | $symRef = & git symbolic-ref refs/remotes/origin/HEAD 2>$null |
| 51 | if ($LASTEXITCODE -eq 0 -and $symRef) { |
| 52 | # Strip refs/remotes/ prefix to get origin/<branch> |
| 53 | return ($symRef.Trim() -replace '^refs/remotes/', '') |
| 54 | } |
| 55 | |
| 56 | return 'origin/main' |
| 57 | } |
| 58 | |
| 59 | function Build-PathspecExclusions { |
| 60 | <# |
| 61 | .SYNOPSIS |
| 62 | Builds git pathspec negation patterns from extensions and path prefixes. |
| 63 | .DESCRIPTION |
| 64 | Accepts optional arrays of file extensions (without dots) and path prefixes, |
| 65 | returning git pathspec arguments that exclude matching files. |
| 66 | .PARAMETER Extensions |
| 67 | File extensions to exclude (e.g., 'yml', 'json'). Leading dots are stripped. |
| 68 | .PARAMETER Paths |
| 69 | Path prefixes to exclude (e.g., '.github/skills/', 'docs/'). |
| 70 | .OUTPUTS |
| 71 | System.String[] |
| 72 | #> |
| 73 | [OutputType([string[]])] |
| 74 | param( |
| 75 | [Parameter()] |
| 76 | [string[]]$Extensions = @(), |
| 77 | |
| 78 | [Parameter()] |
| 79 | [string[]]$Paths = @() |
| 80 | ) |
| 81 | |
| 82 | $specs = @() |
| 83 | foreach ($ext in $Extensions) { |
| 84 | $clean = $ext.TrimStart('.') |
| 85 | if ($clean) { |
| 86 | $specs += ":!*.$clean" |
| 87 | } |
| 88 | } |
| 89 | foreach ($p in $Paths) { |
| 90 | $clean = $p.TrimEnd('/') |
| 91 | if ($clean) { |
| 92 | $specs += ":!$clean/**" |
| 93 | } |
| 94 | } |
| 95 | return $specs |
| 96 | } |
| 97 | |
| 98 | Export-ModuleMember -Function Get-RepositoryRoot, Resolve-DefaultBranch, Build-PathspecExclusions |