* Checks out a specific branch
(branchName: string)
| 249 | * Checks out a specific branch |
| 250 | */ |
| 251 | async function checkoutBranch(branchName: string): Promise<void> { |
| 252 | // First try to checkout the branch as-is (might be local) |
| 253 | let { |
| 254 | code: checkoutCode, |
| 255 | stderr: checkoutStderr |
| 256 | } = await execFileNoThrow(gitExe(), ['checkout', branchName]); |
| 257 | |
| 258 | // If that fails, try to checkout from origin |
| 259 | if (checkoutCode !== 0) { |
| 260 | logForDebugging(`Local checkout failed, trying to checkout from origin: ${checkoutStderr}`); |
| 261 | |
| 262 | // Try to checkout the remote branch and create a local tracking branch |
| 263 | const result = await execFileNoThrow(gitExe(), ['checkout', '-b', branchName, '--track', `origin/${branchName}`]); |
| 264 | checkoutCode = result.code; |
| 265 | checkoutStderr = result.stderr; |
| 266 | |
| 267 | // If that also fails, try without -b in case the branch exists but isn't checked out |
| 268 | if (checkoutCode !== 0) { |
| 269 | logForDebugging(`Remote checkout with -b failed, trying without -b: ${checkoutStderr}`); |
| 270 | const finalResult = await execFileNoThrow(gitExe(), ['checkout', '--track', `origin/${branchName}`]); |
| 271 | checkoutCode = finalResult.code; |
| 272 | checkoutStderr = finalResult.stderr; |
| 273 | } |
| 274 | } |
| 275 | if (checkoutCode !== 0) { |
| 276 | logEvent('tengu_teleport_error_branch_checkout_failed', {}); |
| 277 | throw new TeleportOperationError(`Failed to checkout branch '${branchName}': ${checkoutStderr}`, chalk.red(`Failed to checkout branch '${branchName}'\n`)); |
| 278 | } |
| 279 | |
| 280 | // After successful checkout, ensure upstream is set |
| 281 | await ensureUpstreamIsSet(branchName); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Gets the current branch name |
no test coverage detected