Functions/Test-HgChangeset.ps1


function Test-HgChangeset
{
    <#
    .SYNOPSIS
    Tests if a revision exists in a Mercurial repository.
     
    .DESCRIPTION
    The revision to test can be a branch name, revision number, node ID, or revset query. Basically anything that can be passed to the `-r` parameter of Mercurial's `log` command.
     
    ALIASES
      thgc
       
    .EXAMPLE
    Test-HgChangeset -Revision StableBranch
     
    Returns `True` if the `StableBranch` exists in the current directory's repository.
     
    .EXAMPLE
    Test-HgChangeset -Revision StableBranch -RepoRoot C:\Projects\psHg
     
    Returns `True` if the `StableBranch` exists in the C:\Projects\psHg repository.
     
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        # The revision to merge. Can be a branch, tag, node, ID, or revset query.
        $Revision,
        
        [string]
        # The path to the repository to merge. Defaults to the current directory.
        $RepoRoot = (Resolve-HgRoot .)
    )
    
    if( -not $RepoRoot )
    {
        return $false
    }
    
    $preCount = $Global:Error.Count
    $result = hg log --template '{node}' -l1 -r $Revision -R $RepoRoot 2> $null
    if( $Global:Error.Count -gt $preCount )
    {
        $Global:Error.RemoveAt(0)
    }

    if( $result )
    {
        return $true
    }
    
    return $false
    
}    

Set-Alias -Name 'Test-HgRevision' -Value 'Test-HgChangeset'
Set-Alias -Name 'thgc' -Value 'Test-HgChangeset'