Functions/Get-HgBookmark.ps1

function Get-HgBookmark
{
    <#
    .SYNOPSIS
    Gets all the bookmarks in a repository, or a specific bookmark.
     
    .DESCRIPTION
    By default, returns all bookmarks in the repository. If given a value for the `Name` parameter, does a wildcard search for that bookmark.
     
    .OUTPUT
    PsHg.Bookmark.
     
    .EXAMPLE
    Get-HgBookmark
     
    Returns all the bookmarks in the repository.
     
    .EXAMPLE
    Get-HgBookmark -Name '*fuzz*'
     
    Returns all the bookmarks that match the wildcard `*fuzz*` (i.e. that contain the string `fuzz`).
    #>

    [CmdletBinding()]
    param(
        [Parameter()]
        [string]
        # The name of the bookmark. Wildcards supported.
        $Name,
        
        [Parameter()]
        [string]
        # The path to the repository whose bookmarks to get. Default is the current directory.
        $RepoRoot = ((Get-Location) | Select-Object -ExpandProperty 'Path')
    )
    
    hg bookmarks --debug -R $RepoRoot |
        Where-Object { $_ -match '^ (\*| ) (.*) (-?\d+):([a-f0-9]{40})$' } |
        ForEach-Object {
            $isCurrent = $Matches[1] -eq '*'
            $bookmarkName = $Matches[2].Trim()
            $revision = $Matches[3]
            $node = $Matches[4]
            New-Object 'PsHg.BookmarkInfo' $bookmarkName,$isCurrent,$revision,$node
        } |
        Where-Object {
            if( $PSBoundParameters.ContainsKey( 'Name' ) )
            {
                return $_.Name -like $Name
            }
            return $true
        }
}