Private/Add-InitialWorktrees.ps1
|
<#
.SYNOPSIS Creates worktrees for existing branches in a bare repository. .DESCRIPTION For each branch name in -Worktrees, checks for a local branch first, then a remote-tracking branch. Skips with a warning if neither exists. Used by Invoke-GitCloneBare and Invoke-GitInit to reduce duplication. #> function Add-InitialWorktrees { [CmdletBinding()] param( [Parameter(Mandatory)] [string] $RepoPath, [Parameter(Mandatory)] [AllowEmptyCollection()] [string[]] $Worktrees, [Parameter(Mandatory)] [string] $Destination, [switch] $SkipSafeDirectory ) foreach ($wt in $Worktrees) { $wtPath = Join-Path $Destination $wt $localBranch = git -C $RepoPath branch --list $wt 2>$null $remoteBranch = git -C $RepoPath branch -r --list "origin/$wt" 2>$null if ($localBranch) { git -C $RepoPath worktree add $wtPath $wt } elseif ($remoteBranch) { git -C $RepoPath worktree add --track -b $wt $wtPath "origin/$wt" } else { Write-Warning "Branch '$wt' not found locally or on origin — skipping worktree" continue } if ($LASTEXITCODE -ne 0) { Write-Warning "Failed to create worktree for branch '$wt'" } else { if (-not $SkipSafeDirectory) { git config --global --add safe.directory $wtPath } Write-Host "Worktree created: $wtPath" -ForegroundColor Green } } } |