microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/886-python-lint-fix

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

.github/skills/shared/pr-reference/scripts/shared.psm1

98lines · modecode

1# Copyright (c) Microsoft Corporation.
2# SPDX-License-Identifier: MIT
3
4function Get-RepositoryRoot {
5<#
6.SYNOPSIS
7Gets the repository root path.
8.DESCRIPTION
9Runs git rev-parse --show-toplevel to locate the repository root.
10In default mode, falls back to the current directory when git fails.
11With -Strict, throws a terminating error instead.
12.PARAMETER Strict
13When set, throws instead of falling back to the current directory.
14.OUTPUTS
15System.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
37function Resolve-DefaultBranch {
38<#
39.SYNOPSIS
40Resolves the default branch from the remote HEAD ref.
41.DESCRIPTION
42Runs git symbolic-ref refs/remotes/origin/HEAD to detect the default branch.
43Falls back to origin/main when the symbolic ref is unavailable.
44.OUTPUTS
45System.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
59function Build-PathspecExclusions {
60<#
61.SYNOPSIS
62Builds git pathspec negation patterns from extensions and path prefixes.
63.DESCRIPTION
64Accepts optional arrays of file extensions (without dots) and path prefixes,
65returning git pathspec arguments that exclude matching files.
66.PARAMETER Extensions
67File extensions to exclude (e.g., 'yml', 'json'). Leading dots are stripped.
68.PARAMETER Paths
69Path prefixes to exclude (e.g., '.github/skills/', 'docs/').
70.OUTPUTS
71System.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
98Export-ModuleMember -Function Get-RepositoryRoot, Resolve-DefaultBranch, Build-PathspecExclusions
99