Public/Enter-GitWorktree.ps1

<#
.SYNOPSIS
    Navigates to a git worktree by path or branch name.
.DESCRIPTION
    Changes the current location to the specified worktree directory.
    Use -NewWindow to open the worktree in a new terminal window instead.
 
    The -Worktree argument accepts either a full worktree path or a branch name.
.EXAMPLE
    Enter-GitWorktree main
    # Set-Location to the worktree checked out on 'main'
.EXAMPLE
    Enter-GitWorktree feature/you/login-page
    # Navigate by branch name
.EXAMPLE
    Enter-GitWorktree C:\repos\myrepo\feature\you\login-page
    # Navigate by full path
.EXAMPLE
    Enter-GitWorktree main -NewWindow
    # Open the worktree in a new terminal window
#>

function Enter-GitWorktree {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position = 0)]
        [string] $Worktree,

        # Open the worktree in a new terminal window instead of navigating the current session
        [Parameter()]
        [switch] $NewWindow,

        [Parameter()]
        [string] $RepoPath = (Get-Location).Path
    )

    $root      = Get-GitRoot -Path $RepoPath
    $worktrees = Get-WorktreeList -RepoPath $root

    # Match by exact path first, then by branch name
    $entry = $worktrees | Where-Object { -not $_.IsBare -and $_.Path -eq $Worktree }
    if (-not $entry) {
        $entry = $worktrees | Where-Object { -not $_.IsBare -and $_.Branch -eq $Worktree }
    }
    if (-not $entry) {
        throw "No worktree found matching '$Worktree'. Run 'git worktree list' to see available worktrees."
    }

    if ($NewWindow) {
        Open-WorktreeTerminal -WorktreePath $entry.Path
    }
    else {
        Set-Location $entry.Path
        Write-Host "Now in worktree : $($entry.Path)" -ForegroundColor Cyan
        Write-Host "Branch : $($entry.Branch)" -ForegroundColor Cyan
    }
}