public/New-WtwWorktree.ps1
|
function New-WtwWorktree { <# .SYNOPSIS Create a new git worktree with workspace and color assignment. .DESCRIPTION Creates a git worktree for the given task, generates a VS Code workspace file from the repo template, assigns a unique color, and registers it in the wtw registry. .PARAMETER Task Branch or task name for the new worktree. .PARAMETER Branch Override the git branch name (defaults to the task name). .PARAMETER Repo Target repo alias if not auto-detected from cwd. .PARAMETER Open Open the workspace in the configured editor after creation. .PARAMETER NoBranch Attach to an existing branch instead of creating a new one. .PARAMETER PrettyName Human-readable display name stored in the registry and used as the Superset workspace name (e.g. "035 Context Building 🔵"). .PARAMETER FolderName Override the worktree folder suffix (default: $Task). Final folder is "${repoName}_${FolderName}". Useful for long branch names: --folder p2 → snowmain1_p2. .EXAMPLE wtw create auth Create a worktree and branch named "auth" for the current repo. .EXAMPLE wtw create "my feature name" Normalizes to my_feature_name for branch, folder, and registry key. .EXAMPLE wtw create 035-context-building-function --name "035 Context Building 🔵" Creates the worktree and registers a pretty name used for Superset. #> [CmdletBinding()] param( [Parameter(Mandatory, Position = 0)] [string] $Task, [string] $Branch, [string] $Repo, [string] $PrettyName, [string] $FolderName, [switch] $Open, [switch] $NoBranch ) $rawTask = $Task $Task = ConvertTo-WtwBranchSafeName -Name $Task if ([string]::IsNullOrWhiteSpace($Task)) { Write-Error "Task name is empty or invalid after normalization (input: '$rawTask')." return } if ($rawTask -ne $Task) { Write-Host " Normalized task/branch: $Task" -ForegroundColor DarkCyan Write-Host " (from: $rawTask)" -ForegroundColor DarkGray } $repoName, $repoEntry = Resolve-WtwRepo -RepoAlias $Repo if (-not $repoName) { return } if ($repoEntry.worktrees.PSObject.Properties.Name -contains $Task) { Write-Error "Worktree '$Task' already exists for $repoName. Use 'wtw go $Task' or 'wtw remove $Task' first." return } # Folder suffix (default: $Task). Normalized so spaces/casing don't sneak into paths. $folderSuffix = if ($FolderName) { ConvertTo-WtwBranchSafeName -Name $FolderName } else { $Task } if ($FolderName -and $folderSuffix -ne $FolderName) { Write-Host " Normalized folder name: $folderSuffix" -ForegroundColor DarkCyan Write-Host " (from: $FolderName)" -ForegroundColor DarkGray } $worktreePath = Join-Path $repoEntry.worktreeParent "${repoName}_${folderSuffix}" if (Test-Path $worktreePath) { Write-Error "Path already exists: $worktreePath" return } if (-not $Branch) { $Branch = $Task } # Create git worktree Write-Host " Creating worktree..." -ForegroundColor Cyan $mainRepo = $repoEntry.mainPath if ($NoBranch) { $result = git -C $mainRepo worktree add $worktreePath $Branch 2>&1 } else { $result = git -C $mainRepo worktree add -b $Branch $worktreePath 2>&1 } if ($LASTEXITCODE -ne 0) { Write-Error "git worktree add failed: $result" return } Write-Host " Worktree: $worktreePath" -ForegroundColor Green Write-Host " Branch: $Branch" -ForegroundColor Green # Pick color $color = New-WtwColor -RepoName $repoName -TaskName $Task Write-Host " Color: $color" -ForegroundColor Green # Generate workspace file $wsFile = $null $config = Get-WtwConfig # Use template source (.template file) if available, fall back to templateWorkspace $templatePath = if ($repoEntry.template -and (Test-Path $repoEntry.template)) { $repoEntry.template } elseif ($repoEntry.templateWorkspace -and (Test-Path $repoEntry.templateWorkspace)) { $repoEntry.templateWorkspace } else { $null } if ($config -and $templatePath) { $wsDir = $config.workspacesDir.Replace('~', $HOME) $wsDir = [System.IO.Path]::GetFullPath($wsDir) $wsFile = Join-Path $wsDir "${repoName}_${folderSuffix}.code-workspace" New-WtwWorkspaceFile ` -RepoName $repoName ` -Name "${repoName}_${folderSuffix}" ` -CodeFolderPath $worktreePath ` -TemplatePath $templatePath ` -OutputPath $wsFile ` -Color $color ` -Branch $Branch ` -WorktreePath $worktreePath ` -Managed | Out-Null Write-Host " Workspace: $wsFile" -ForegroundColor Green } else { Write-Host ' Workspace: (no template configured, skipped)' -ForegroundColor Yellow } # Register in registry $registry = Get-WtwRegistry $wtEntry = [PSCustomObject]@{ path = $worktreePath branch = $Branch workspace = $wsFile color = $color created = (Get-Date -Format 'o') prettyName = $PrettyName supersetWorkspaceId = $null } $registry.repos.$repoName.worktrees | Add-Member -NotePropertyName $Task -NotePropertyValue $wtEntry -Force Save-WtwRegistry $registry # Create Superset workspace (no-op when CLI absent or project not found) $supersetWsId = New-WtwSupersetWorkspace -RepoName $repoName -Branch $Branch -PrettyName $PrettyName if ($supersetWsId) { $registry.repos.$repoName.worktrees.$Task.supersetWorkspaceId = $supersetWsId Save-WtwRegistry $registry } if ($Open) { Open-WtwWorkspace -Name $Task -Repo $repoName } Write-Host '' Write-Host " Done! Use 'wtw go $Task' to switch." -ForegroundColor Green } |