Functions/Get-HgMergeResult.ps1


function Get-HgMergeResult
{
    <#
    .SYNOPSIS
    Gets the result of the currently in-progress merge.
 
    .DESCRIPTION
    `Get-HgMergeResult` gets the state of an uncommitted merge in a repository.
 
    One of the members of the return object is the exit code of the merge. This exit code is only valid if you call `Get-HgMergeResult` after running Mercurial's `merge` command and before running any other command.
 
    .EXAMPLE
    Get-HgMergeResult
 
    Demonstrates how to get the result/state of the current merge from the repository in the current directory.
    #>

    [CmdletBinding()]
    [OutputType([Pshg.MergeResult])]
    param(
        [string]
        # Path to the repository to operate on.
        $RepoRoot = (Get-Location).Path
    )

    Set-StrictMode -Version 'Latest'

    $RepoRoot = Resolve-HgRoot -Path $RepoRoot
    if( -not $RepoRoot )
    {
        return
    }

    Push-Location -Path $RepoRoot -StackName 'PsHg'
    
    try
    {
        $parents = Get-HgParent | Measure-Object
        if( $parents.Count -eq 1 )
        {
            Write-Error ('No merge found in {0}. Are you sure you called `hg merge`?' -f $RepoRoot)
            return
        }

        $mergeExitCode = $LASTEXITCODE
        [string[]]$resolveOutput = hg resolve --list 
        if( -not $resolveOutput )
        {
            $resolveOutput = @() 
        }

        $unresolved = $resolveOutput | Where-Object { $_ -like 'U *' } | Measure-Object 
        $merged = $resolveOutput | Where-Object { $_ -like 'R *' } | Measure-Object

        $status = Get-HgUncommittedChangeset -Modified -Removed
        $modified = $status | Where-Object { $_.Modified } | Measure-Object 
        $removed = $status | Where-Object { $_.Removed } | Measure-Object

        New-Object 'PsHg.MergeResult' -ArgumentList ($modified.Count - $merged.Count - $unresolved.Count), $merged.Count, $removed.Count, $unresolved.Count, $mergeExitCode
    }
    finally
    {
        Pop-Location -StackName 'PsHg'
    }
}