Functions/Remove-HgItem.ps1


function Remove-HgItem
{
    <#
    .SYNOPSIS
    Removes an item from a Mercurial repository.
     
    .DESCRIPTION
    Given a path, removes the item at that location from its Mercurial repository. Don't forget to commit!
     
    ALIASES
      rhgi, hgremove
       
    .EXAMPLE
    Remove-HgItem C:\Projects\psHg\Test\Test-RemoveHgItem.ps1
     
    Removes the Test\Test-RemoveHgItem.ps1 file from the psHg repository.
     
    .EXAMPLE
    Remove-HgItem C:\Projects\psHg\Test
     
    Removes the Test directory from the psHg repository.
    #>

    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory=$true)]
        [string]
        # The files to remove from their repositories.
        $Path,
        
        [Switch]
        # Forces the removal if the item even if it is added or modified.
        $Force
    )
    
    $repoRoot = Resolve-HgRoot -Path $Path
    $repoPath = Resolve-HgPath -Path $Path
    
    if( $pscmdlet.ShouldProcess( $repoRoot, "remove $repoPath" ) )
    {
        Push-Location $repoRoot
        try
        {
            Write-Verbose "Removing $repoPath from repository $Path."
            $forceArg = ''
            if( $Force )
            {
                $forceArg = '--force'
            }
            $errMsg = hg remove $repoPath -R $repoRoot $forceArg 2>&1
            if( $LastExitCode )
            {
                Write-Error $errMsg
            }
        }
        finally
        {
            Pop-Location
        }
    }
}

Set-Alias -Name 'hgremove' -Value 'Remove-HgItem'
Set-Alias -Name 'rhgi' -Value 'Remove-HgItem'