Functions/Test-HgDefaultPath.ps1


function Test-HgDefaultPath
{
    <#
    .SYNOPSIS
    Tests if a repository has a default incoming/outgoing path, i.e. that you'll be able to push/pull or view incoming/outgoing changes.
     
    .DESCRIPTION
    If you try to read incoming/outgoing changes for a repository without a default path, you'll sometimes get cryptic error messages. Use this function when you want to detct if its safe to push/pull or view the incoming/outgoing changesets.
     
    .EXAMPLE
    Test-HgDefaultPath -Incoming
     
    Checks if there is a repository from which changesets are pulled and incoming changesets read.
     
    .EXAMPLE
    Test-HgDefaultPath -Outgoing
     
    Checks if there is a repository to which changesets can be pushed and outgoing changesets read.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ParameterSetName='Incoming')]
        [Switch]
        # Checks if there is a repository from which changes can be pulled and incoming changesets read.
        $Incoming,

        [Parameter(Mandatory=$true,ParameterSetName='Outgoing')]
        [Switch]
        # Checks if there is a repository to which changes can be pushed and outgoing changesets read.
        $Outgoing,
        
        [string]
        # The path to the repository.
        $RepoRoot = (Get-Location).Path
    )
    
    $RepoRoot = Resolve-HgRoot -Path $RepoRoot
    if( -not $RepoRoot )
    {
        return
    }
    
    Push-Location $RepoRoot
    try
    {
        if( (Get-HgConfig -Name paths.default) )
        {
            return $true
        }
        else
        {
            if( $pscmdlet.ParameterSetName -eq 'Outgoing' )
            {
                if( (Get-HgConfig -Name paths.default-push) )
                {
                    return $true
                }
            }
            return $false
        }
    }
    finally
    {
        Pop-Location
    }
}