Functions/Copy-HgChangeset.ps1


function Copy-HgChangeset
{
    <#
    .SYNOPSIS
    Copies a single changeset from one branch to another.
 
    .DESCRIPTION
    This function first verifies that you are either in an Hg repository or that the path you pass in to the repository is valid. It will then verify that the Revision passed in exists and try to graft it to the current branch. If this succeeds the changeset is returned. If this fails an error is written.
 
    .OUTPUT
    PsHg.ChangesetInfo
 
    .EXAMPLE
    Copy-HgChangeset -Revision 'df34b97be6c4'
 
    Demonstrates how to copy/graft the revision specified by 'df34b97be6c4' onto the current branch.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string[]]
        # The revision to copy/graft.
        $Revision,

        [string]
        # The path to the repository. Defaults to current directory.
        $RepoRoot = (Get-Location).Path
    )

    Set-StrictMode -Version 'Latest'

    $script:PsHg? = $false

    $repoRoot = Resolve-HgRoot -Path $RepoRoot
    if( -not $repoRoot )
    {
        Write-Error ("Hg root could not be found in '{0}'." -f $RepoRoot)
        return
    }

    foreach( $rev in $Revision ) 
    {
        $changeset = Find-HgChangeset -Revision $rev -RepoRoot $repoRoot
        if( -not $changeset )
        {
            Write-Error ("Revision '{0}' not found in '{1}'." -f $rev, $repoRoot)
            return
        }

        hg graft --rev $rev --repository $repoRoot
        $hgExitCode = $LASTEXITCODE
        if( $hgExitCode )
        {
            Write-Error ("Grafting revision '{0}' failed. See console output for details." -f $rev)
            return
        }
        else
        {
            $script:PsHg? = $true
        }
    }
}