Functions/New-HgBranch.ps1


function New-HgBranch
{
    <#
    .SYNOPSIS
    Creates a new branch.
     
    .DESCRIPTION
    Attempts to create a new branch and set the currently 'active' branch to it. If a branch already exists with the given name, an error occurs.
 
    Note that "the branch will not exist in the repository until the next commit".
     
    ALIASES
      nhgb
     
    .EXAMPLE
    New-HgBranch MyNewBranch
     
    Creates the branch MyNewBranch and set's it as 'active'.
     
    .LINK
    Test-HgBranch
     
    .LINK
    Get-HgBranch
    #>

    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory=$true,Position=0)]
        [string]
        # The branch's name.
        $Name,
        
        [Parameter()]
        [string]
        # The path to the repository where the branch should be created. Defaults to the current directory.
        $RepoRoot = (Resolve-Path .)
    )
    
    if( (Test-HgBranch -Name $Name -RepoRoot $RepoRoot) )
    {
        Write-Warning "Branch $Name already exists in repository $RepoRoot."
    }
    else
    {
        Push-Location $RepoRoot
        try
        {
            if( $pscmdlet.ShouldProcess( $RepoRoot, "create branch $Name" ) )
            {
                hg branch $Name | Write-Verbose
                if( -not $LastExitCode )
                {
                    # We don't use Get-HgBranch because it doesn't report branches that haven't been committed to.
                    $parent = Get-HgParent
                    New-Object 'PsHg.BranchInfo' $Name,$parent.ID.Revision,$parent.ID.NodeID,$false,$false
                }
            }
        }
        finally
        {
            Pop-Location
        }
    }   
}

Set-Alias -Name 'Open-HgBranch' -Value 'New-HgBranch'
Set-Alias -Name 'nhgb' -Value 'New-HgBranch'