Private/Get-WorktreeList.ps1
|
<#
.SYNOPSIS Returns structured information about all worktrees for a repository. .OUTPUTS Array of PSCustomObjects with: Path, Branch, Head, IsBare, IsDetached #> function Get-WorktreeList { [CmdletBinding()] [OutputType([PSCustomObject[]])] param( [string] $RepoPath = (Get-Location).Path ) $raw = git -C $RepoPath worktree list --porcelain 2>$null if ($LASTEXITCODE -ne 0) { throw "Failed to list worktrees in: $RepoPath" } $worktrees = [System.Collections.Generic.List[PSCustomObject]]::new() $current = $null foreach ($line in $raw) { if ($line -match '^worktree (.+)$') { if ($null -ne $current) { $worktrees.Add($current) } $current = [PSCustomObject]@{ Path = $Matches[1].Trim() Branch = $null Head = $null IsBare = $false IsDetached = $false } } elseif ($line -match '^HEAD (.+)$') { $current.Head = $Matches[1].Trim() } elseif ($line -match '^branch (.+)$') { $current.Branch = $Matches[1].Trim() -replace '^refs/heads/', '' } elseif ($line -eq 'bare') { $current.IsBare = $true } elseif ($line -eq 'detached') { $current.IsDetached = $true } } if ($null -ne $current) { $worktrees.Add($current) } return $worktrees.ToArray() } |