Functions/Receive-HgChangeset.ps1


function Receive-HgChangeset
{
    <#
    .SYNOPSIS
    Pulls changes into a repository.
     
    .DESCRIPTION
    Runs Mercurial's pull command to bring changes into a repository. Can optionally pull changes in from a specific branch via the `Branch` parameter.
     
    ALIASES
      rchgc, hgpull
       
    .OUTPUTS
    PsHg.PullResult
 
    .EXAMPLE
    Receive-HgChangesets
     
    Pulls changes into the current working directory's repository from its default source.
     
    .EXAMPLE
    Receive-HgChangesets -Revision Stable
     
    Pulls changes in just the Stable branch into the current working directory's repository.
     
    .EXAMPLE
    Receive-HgChangesets -RepoRoot C:\Projects\psHg
     
    Pulls changes into the repository at C:\Projects\psHg from its default source.
    #>

    [CmdletBinding()]
    param(
        [string]
        # The revision to pull. All ancestores are also pulled.
        [Alias('Branch')]
        $Revision,
        
        [string]
        # The remote/source repository from which changes should be pulled.
        $Source,

        [string]
        $RepoRoot = (Resolve-Path .),

        [string]
        # The bookmark to pull.
        $Bookmark
    )
    
    $rRepoRoot = (Resolve-HgRoot -Path $RepoRoot)

    if( -not $rRepoRoot )
    {
        return
    }
    
    $rRepoRootArg = '-R{0}' -f $rRepoRoot

    $revisionArg = ''
    if( $Revision )
    {
        $revisionArg = '-r{0}' -f $Revision
    }

    $bookmarkArg = ''
    if( $Bookmark )
    {
        $bookmarkArg = '-B{0}' -f $Bookmark
    }

    $Script:PsHg? = $false
    hg pull $bookmarkArg $rRepoRootArg $revisionArg $Source
    $hgExitCode = $LASTEXITCODE

    if( $hgExitCode -eq 0 )
    {
        $Script:PsHg? = $true
    }
    else
    {
        $revisionMsg = 'changes'
        if( $Revision )
        {
            $revisionMsg = 'revision ''{0}''' -f $Revision
        }

        $sourceMsg = ''
        if( $Source )
        {
            $sourceMsg = ' from ''{0}''' -f $Source
        }

        Write-Error ('Failed to pull {0} into repository ''{1}''{2}.' -f $revisionMsg,$RepoRoot,$sourceMsg)
    }
}

Set-Alias -Name 'Receive-HgChangesets' -Value 'Receive-HgChangeset'
Set-Alias -Name 'hgpull' -Value 'Receive-HgChangeset'
Set-Alias -Name 'rchgc' -Value 'Receive-HgChangeset'