Modules/businessdev.ALbuild.Core/Private/Get-BcHighestClaimedVersion.ps1

function Get-BcHighestClaimedVersion {
    <#
    .SYNOPSIS
        Returns the highest build/<Major>.<Minor>.* version already claimed on the remote.
    .DESCRIPTION
        Internal helper for Invoke-BcBuildVersionStamp. Build branches are never merged back, so the
        version in app.json lags behind the versions already claimed as build/* branches. Reading the
        highest existing branch for the target Major.Minor lets the stamp start just above it instead
        of re-probing from the app.json version one push at a time every build.
 
        Read-only and failure-tolerant: a missing remote, no matching branches, or a git error returns
        $null, and the caller keeps its computed start version (the claim loop still covers conflicts).
    .PARAMETER RepositoryRoot
        The git working tree.
    .PARAMETER Remote
        The remote to query.
    .PARAMETER BranchPrefix
        Build branch name prefix (e.g. 'build/').
    .PARAMETER Major
        Major version to match.
    .PARAMETER Minor
        Minor version to match.
    #>

    [CmdletBinding()]
    [OutputType([version])]
    param(
        [Parameter(Mandatory)] [string] $RepositoryRoot,
        [Parameter(Mandatory)] [string] $Remote,
        [Parameter(Mandatory)] [string] $BranchPrefix,
        [Parameter(Mandatory)] [int] $Major,
        [Parameter(Mandatory)] [int] $Minor
    )

    $ls = Invoke-BcGit -RepositoryRoot $RepositoryRoot -Arguments @('ls-remote', '--heads', $Remote, "$BranchPrefix*") -AllowFailure
    if (-not $ls.Success -or [string]::IsNullOrWhiteSpace($ls.StdOut)) { return $null }

    # Each line is "<sha>\trefs/heads/<BranchPrefix><version>"; keep the versions for this Major.Minor.
    $refPrefix = "refs/heads/$BranchPrefix"
    $highest = $null
    foreach ($line in ($ls.StdOut -split "`r?`n")) {
        $trimmed = $line.Trim()
        if (-not $trimmed) { continue }
        $idx = $trimmed.IndexOf($refPrefix)
        if ($idx -lt 0) { continue }
        $name = $trimmed.Substring($idx + $refPrefix.Length).Trim()
        $parsed = $null
        if (-not [version]::TryParse($name, [ref] $parsed)) { continue }
        if ($parsed.Major -ne $Major -or $parsed.Minor -ne $Minor) { continue }
        if ($null -eq $highest -or $parsed -gt $highest) { $highest = $parsed }
    }
    return $highest
}