* Create a new worktree with a new branch. * * - Validates branch name * - Checks branch does not already exist * - Creates the worktree base directory if needed
(branchName: string, startPoint?: string)
| 235 | * - Creates the worktree base directory if needed |
| 236 | */ |
| 237 | async create(branchName: string, startPoint?: string): Promise<string> { |
| 238 | // Validate branch name |
| 239 | const validation = isValidBranchName(branchName); |
| 240 | if (!validation.valid) { |
| 241 | throw new InvalidBranchNameError(branchName, validation.reason); |
| 242 | } |
| 243 | |
| 244 | // Check branch does not already exist |
| 245 | const exists = await this.checkBranchExists(branchName); |
| 246 | if (exists) { |
| 247 | throw new BranchExistsError(branchName); |
| 248 | } |
| 249 | |
| 250 | const worktreePath = path.join(this.worktreeBaseDir, branchName); |
| 251 | await fs.mkdir(this.worktreeBaseDir, { recursive: true }); |
| 252 | |
| 253 | try { |
| 254 | const args = ['worktree', 'add', '-b', branchName, worktreePath]; |
| 255 | if (startPoint) { |
| 256 | args.push(startPoint); |
| 257 | } |
| 258 | await execGit(this.repoPath, args); |
| 259 | } catch (err) { |
| 260 | // Wrap with more context |
| 261 | if (err instanceof GitError) { |
| 262 | throw new GitError( |
| 263 | `Failed to create worktree for branch '${branchName}': ${err.message}`, |
| 264 | 'WORKTREE_CREATE_FAILED' |
| 265 | ); |
| 266 | } |
| 267 | throw err; |
| 268 | } |
| 269 | |
| 270 | return worktreePath; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Remove a worktree. If the worktree does not exist, this is a no-op. |
no test coverage detected