Functions/Copy-HgRepository.ps1


function Copy-HgRepository
{
    <#
    .SYNOPSIS
    Clones a Mercurial repository.
 
    .DESCRIPTION
    Wraps the Mercurial clone and share commands to copy an existing repository in a new directory.
 
    If the clone/share succeeds, sets the `$PsHg?` variable to `$true`. Otherwise, sets it to `$false`. (I.e. use the `$PsHg?` variable to check if the clone succeeded or not.
     
    ALIASES
      cphg, hgclone
     
    .EXAMPLE
    Copy-HgRepository -Source C:\Projects\PsHg -Destination C:\Projects\PsHg-Spike
 
    Clones `C:\Projects\PsHg` to `C:\Projects\PsHg-Spike`.
 
    .EXAMPLE
    Copy-HgRepository -Source C:\Projects\PsHg -Destination C:\Projects\PsHg-Share -Share
 
    Demonstrates how to create a shared repository.
    #>

    [CmdletBinding(DefaultParameterSetName='Clone')]
    param(
        [Parameter(Mandatory=$true)]
        [Alias('RepoRoot')]
        [string]
        # The path of the repository to clone.
        $Source,
        
        [string]
        # The directory where the repository should be cloned/copied.
        $Destination,

        [Parameter(Mandatory=$true,ParameterSetName='Share')]
        [Switch]
        # Shares a repository instead of cloning it.
        $Share,
        
        [Parameter(ParameterSetName='Clone')]
        [Switch]
        # Use Mercurial's pull protocol to copy metadata.
        $Pull,

        [Parameter(ParameterSetName='Clone')]
        [Switch]
        # Use uncompressed transfer (fast over LAN).
        $Uncompressed,

        [Alias('NoUpdate')]
        [Switch]
        # The cloned repository will not include a working copy (only a repository).
        $NoWorkingCopy
    )
    
    Set-StrictMode -Version 'Latest'

    $noUpdateArg = ''
    if( $NoWorkingCopy )
    {
        $noUpdateArg = '--noupdate'
    }

    $pullArg = ''
    if( $Pull )
    {
        $pullArg = '--pull'
    }

    $uncompressedArg = ''
    if( $Uncompressed )
    {
        $uncompressedArg = '--uncompressed'
    }

    $Script:PsHg? = $false
    if( $PSCmdlet.ParameterSetName -eq 'Clone' )
    {
        hg clone $noUpdateArg $pullArg $uncompressedArg $Source $Destination
        $hgExitCode = $LASTEXITCODE
    }
    else
    {
        hg --config extensions.share= share $noUpdateArg $Source $Destination
        $hgExitCode = $LASTEXITCODE
    }

    if( $hgExitCode -eq 0 )
    {
        $Script:PsHg? = $true
    }
}

Set-Alias -Name 'hgclone' -Value 'Copy-HgRepository'
Set-Alias -Name 'cphg' -Value 'Copy-HgRepository'