Functions/Test-HgIncomingChangeset.ps1


function Test-HgIncomingChangeset
{
    <#
    .SYNOPSIS
    Tests if there are incoming changesets for a repository.
 
    .DESCRIPTION
    Incoming changes can be dectected across the entire repository or on specific branches via the `Branch` parameter. If there are any incoming changesets, returns `True`. `False` otherwise.
     
    ALIASES
      thginc
 
    .EXAMPLE
    Test-HgIncomingChangesets
 
    Returns `True` if there are any incoming changesets in the repository in the current working directory. `False` if there are no incoming changesets.
 
    .EXAMPLE
    Test-HgINcomingChangesets -Branch Stable
 
    Returns `True` if there are any incoming changesets in the `Stable` branch in the current directory's repository. `False` if there are no incoming changesets.
 
    .EXAMPLE
    Test-HgIncomingChangesets -RepoRoot C:\Projects\PsHg
 
    Returns `True` if there are any incoming changesets in the `C:\Projects\PsHg` repository. `False` if there are no incoming changesets.
    #>

    [CmdletBinding()]
    param(
        [string]
        # Tests for incoming chnagesets on a specific branch.
        [Alias('Branch')]
        $Revision,
        
        [string]
        # The source/remote repository to check for incoming changes.
        $Source,
        
        [string]
        $RepoRoot = (Get-Location)
    )
    
    $rRepoRoot = Resolve-HgRoot -Path $RepoRoot
    if( -not $rRepoRoot )
    {
        return
    }
    
    Push-Location $rRepoRoot
    try
    {
        if( -not $Source -and -not (Test-HgDefaultPath -Incoming) )
        {
            Write-Warning ('Repository {0}''s default path setting not found.' -f $rRepoRoot)
            return
        }
        
        $revisionArg = ''
        if( $Revision )
        {
            $revisionArg = '-r{0}' -f $Revision
        }

        $preErrCount = $Global:Error.Count
        try
        {
            [string[]]$incoming = hg incoming --template '{node|short}\n' $revisionArg $Source 2>$null |
                                    Where-Object { $_ -match '^[0-9a-f]{12}$' }
            return ( $incoming.Count -gt 0 )
        }
        finally
        {
            $numErrors = $Global:Error.Count - $preErrCount
            for( $idx = 0; $idx -lt $numErrors; ++$idx )
            {
                $Global:Error.RemoveAt(0)
            }
        }
    }
    finally
    {
        Pop-Location
    }
}

Set-Alias -Name 'Test-HgIncomingChangesets' -Value 'Test-HgIncomingChangeset'
Set-Alias -Name 'thginc' -Value 'Test-HgIncomingChangeset'