Functions/Test-HgHead.ps1


function Test-HgHead
{
    <#
    .SYNOPSIS
    Tests if there are multiple heads in a repository or in a branch.
 
    .DESCRIPTION
    Returns `True` if a repository (or branch) contains multiple heads. `False` otherwise. Repositories with more than one branch will always return `True`. If you want to know if a specific branch has multiple heads, pass its name to the `Revision` parameter.
     
    ALIASES
      thgh
 
    .EXAMPLE
    Test-HgHeads
 
    Returns `True` if the repository in the current directory has multiple heads. `False` otherwise.
 
    .EXAMPLE
    Test-HgHeads -RepoRoot C:\Projects\PsHg -Revision stable
 
    Returns `True` if the `stable` branch in `C:\Projects\PsHg` has multiple heads. `False` otherwise.
    #>

    [CmdletBinding()]
    param(
        [string]
        # The revision whose branch should be tested for multiple heads.
        $Revision,
        
        [string]
        # The path to a repository.
        $RepoRoot = (Get-Location)
    )
    
    $rRepoRoot = Resolve-HgRoot -Path $RepoRoot
    if( -not $rRepoRoot )
    {
        return
    }

    $heads = @()
    if( $Revision )
    {
        $heads = @( hg log -r ('(head() or heads(all())) and branch({0}) and not secret()' -f $Revision) --template '{node}\n' -R $rRepoRoot)
    }
    else
    {
        $heads = @( hg log -r '(head() or heads(all())) and not secret()' --template '{node}\n' -R $rRepoRoot )
    }

    return ($heads.Length -gt 1)
}

Set-Alias -Name 'Test-HgHeads' -Value 'Test-HgHead'
Set-Alias -Name 'thgh' -Value 'Test-HgHead'