Private/Get-CpmfUipsGitMetadata.ps1

function Get-CpmfUipsGitMetadata {
<#
.SYNOPSIS
    Returns repository URL, branch and short commit for a project directory.
 
.DESCRIPTION
    Repository metadata is per-repo and computed fresh at pack time rather than
    stored in shared publisher identity. This helper is best-effort: a project
    outside a git repository, a missing git executable, or a detached/remote-less
    checkout all yield an empty hashtable rather than an error.
 
    Keys, when available: RepositoryUrl, RepositoryBranch, RepositoryCommit.
 
.NOTES
    $LASTEXITCODE does not exist until the session has run a native command, and
    reading an unset variable is a terminating error under Set-StrictMode -Version
    Latest, which this module sets. Every access here goes through Get-Variable so
    a fresh session does not fail on the first pack.
 
.PARAMETER Path
    Directory inside the git repository (typically the project directory).
#>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory)]
        [string]$Path
    )

    $result = @{}

    # A failed lookup here is expected and harmless, but git's exit code would
    # otherwise leak out of the module and look like a pack failure to a CI
    # script that checks $LASTEXITCODE.
    $priorExitCode = (Get-Variable -Name LASTEXITCODE -Scope Global -ValueOnly -ErrorAction SilentlyContinue)

    if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
        Write-Verbose '[GitMetadata] git not found on PATH — skipping repository metadata.'
        return $result
    }

    $queries = [ordered]@{
        RepositoryUrl    = @('remote', 'get-url', 'origin')
        RepositoryBranch = @('rev-parse', '--abbrev-ref', 'HEAD')
        RepositoryCommit = @('rev-parse', '--short', 'HEAD')
    }

    foreach ($key in $queries.Keys) {
        try {
            $value    = (& git -C $Path @($queries[$key]) 2>$null | Select-Object -First 1)
            $exitCode = (Get-Variable -Name LASTEXITCODE -Scope Global -ValueOnly -ErrorAction SilentlyContinue)
            if ($exitCode -eq 0 -and -not [string]::IsNullOrWhiteSpace($value)) {
                $result[$key] = $value.Trim()
            }
        } catch {
            Write-Verbose "[GitMetadata] $key lookup failed: $($_.Exception.Message)"
        }
    }

    if ($result.Count -eq 0) {
        Write-Verbose "[GitMetadata] No repository metadata available for $Path"
    }

    if ($null -eq $priorExitCode) {
        Remove-Variable -Name LASTEXITCODE -Scope Global -ErrorAction SilentlyContinue
    } else {
        Set-Variable -Name LASTEXITCODE -Scope Global -Value $priorExitCode
    }

    return $result
}