Functions/Get-HgBranch.ps1
function Get-HgBranch { <# .SYNOPSIS Gets the current branch of a Mercurial repository. .DESCRIPTION ALIASES ghgbr, hgbranch .EXAMPLE Get-HgBranch Returns an object representing the current branch. #> [CmdletBinding()] [OutputType([PsHg.BranchInfo])] param( [Switch] # Get the current branch only. Otherwise all branches are returned. $Current, [Switch] # Get closed branches, too. $Closed, [Parameter()] [string] # The path to the repository whose branches to get. Defaults to the current directory. $RepoRoot = (Resolve-Path .) ) $rRepoRoot = Resolve-HgRoot -Path $RepoRoot -ErrorAction Stop Push-Location -Path $rRepoRoot try { $currentBranchName = hg branch if( $LastExitCode ) { Write-Error "Unable to determine current branch in repository $rRepoRoot." return } $closedArg = '' if( $Closed ) { $closedArg = '-c' } # Can't use `hg branches` because if the current branch is closed it won't show unless the user uses the -Closed parameter. How are they supposed to know? if( $Current ) { $branchTip = Get-HgChangeset -Revision $currentBranchName -Force $isClosed = $branchTip.Extras.ContainsKey('close') -and $branchTip.Extras['close'] $isInactive = $false if( -not $isClosed ) { $isInactive = $null -ne (Get-HgChangeset -Revision ('children(''{0}'') and merge() and not branch(''{0}'')' -f $currentBranchName)) } New-Object -TypeName 'PsHg.BranchInfo' -ArgumentList @( $branchTip.Branch, $branchTip.Revision, $branchTip.Node, $isClosed, $isInactive ) return } hg branches --debug $closedArg | ForEach-Object { if( $_ -notmatch "^(.*)\s+(\d+):([a-f0-9]{40})(\s+\((closed|inactive)\))?$" ) { Write-Error "Unable to parse $_" return } $closed = $false $inactive = $false if( $matches[5] ) { $closed = ($matches[5] -eq 'closed') $inactive = ($matches[5] -eq 'inactive') } New-Object PsHg.BranchInfo ($matches[1].Trim()),$matches[2],$matches[3],$closed,$inactive } | Where-Object { if( $Current ) { return ($_.Name -ceq $currentBranchName ) } return $true } } finally { Pop-Location } } Set-Alias -Name 'hgbranch' -Value 'Get-HgBranch' Set-Alias -Name 'ghgb' -Value 'Get-HgBranch' |