Functions/Update-HgRepository.ps1


function Update-HgRepository
{
    <#
    .SYNOPSIS
    Updates a Mercurial repository.
     
    .DESCRIPTOIN
    By default, will update to the latest head of the repository's current branch. Use the `Revision` parameter to update to a specific revision.
 
    When running this command non-interactively, make sure to set the `NonInteractive` switch.
 
    If the update succeeds, sets the `$PsHg?` variable to `$true`. If the udpate fails, or you need to resolve files after the update, `$PsHg?` is set to `$false`. (I.e. use the `$PsHg?` variable to check if the update succeeds or fails.)
     
    ALIASES
      uphg, hgup
       
    .EXAMPLE
    Update-HgRepository
     
    Updates to the head of the repository's current branch.
     
    .EXAMPLE
    Update-HgRepository FeatureBranch
     
    Updates to the head of the FeatureBranch branch.
     
    .EXAMPLE
    Update-HgRepository 3984
     
    Updates to revision 3984
     
    .EXAMPLE
    Update-HgRepository -Clean
     
    Updates to the tip of the current branch, discarding any uncommitted changes.
    #>

    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Position=0)]
        [string]
        # The revision to update to. Defaults to the head of the repository's current branch.
        $Revision,
        
        [Switch]
        # Discards uncommitted changes, with no backup.
        $Clean,

        [Switch]
        # Update across branches if there are no uncommitted changes.
        $Check,

        [Switch]
        # Running non-interactively, so return an object representing the result of the update. Using this can cause the update to hang because Mercurial sends prompts to stdout, which this function get captures and doesn't show you.
        $NonInteractive,
        
        [Parameter()]
        [string]
        # The path to the repository to update. Defaults to the current directory.
        $RepoRoot = (Resolve-Path .)
    )
    
    $rRepoRoot = Resolve-HgRoot -Path $RepoRoot
    if( -not $rRepoRoot )
    {
        return
    }
    
    $revisionArg = ''
    if( $Revision )
    {
        $revisionArg = $Revision
    }
    
    $cleanArg = ''
    if( $Clean )
    {
        $cleanArg = '--clean'
    }

    $checkArg = ''
    if( $Check )
    {
        $checkArg = '--check'
    }

    $nonInteractiveArg = ''
    if( $NonInteractive )
    {
        $nonInteractiveArg = '--noninteractive'
    }
    
    $revisionText = if( $Revision ) { $Revision } else { '.' }
    $Script:PsHg? = $true
    if( $pscmdlet.ShouldProcess( $RepoRoot, "update to $revisionText" ) )
    {
        hg update $cleanArg $checkArg $revisionArg -R $rRepoRoot $nonInteractiveArg
        $hgExitCode = $LASTEXITCODE
        if( $hgExitCode -ne 0 )
        {
            $Script:PsHg? = $false
            $revDescription = ''
            if( $Revision )
            {
                $revDescription = ' to revision ''{0}''' -f $Revision
            }
            Write-Error -Message ('Failed to update repository ''{0}''{1}.' -f $RepoRoot,$revDescription)
        }
    }
}

Set-Alias -Name 'hgup' -Value 'Update-HgRepository'
Set-Alias -Name 'udhg' -Value 'Update-HgRepository'