You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log(chalk.blue(`Switching main worktree back to "${originalBranch}" before creating worktree...`));
86
+
awaitexeca("git",["checkout",originalBranch]);
87
+
}elseif(currentBranchAfterGh!==originalBranch){
88
+
console.log(chalk.yellow(`Current branch is ${currentBranchAfterGh}, not ${prBranchName}. Assuming gh handled checkout correctly.`));
89
+
// If gh failed but left us on a different branch, still try to go back
90
+
awaitexeca("git",["checkout",originalBranch]);
91
+
}
92
+
}catch(checkoutError: any){
93
+
console.warn(chalk.yellow(`⚠️ Warning: Failed to switch main worktree back to original branch "${originalBranch}" after gh checkout. Please check manually.`));
console.error(chalk.red(`Failed to create worktree for branch "${prBranchName}" at ${resolvedPath}:`),worktreeError.stderr||worktreeError.message);
138
-
// Common error: branch checked out elsewhere (shouldn't happen with this flow ideally, but check)
139
-
if(worktreeError.stderr?.includes("is already checked out at")){
140
-
console.error(chalk.red(`Branch "${prBranchName}" might be checked out in another worktree.`));
150
+
// The "already checked out" error should ideally not happen with the new flow.
151
+
// Handle other potential worktree add errors.
152
+
console.error(chalk.red(`❌ Failed to create worktree for branch "${prBranchName}" at ${resolvedPath}:`),worktreeError.stderr||worktreeError.message);
153
+
// Suggest checking if the branch exists locally if it fails
154
+
if(worktreeError.stderr?.includes("fatal:")){
155
+
console.error(chalk.cyan(` Suggestion: Verify branch "${prBranchName}" exists locally ('git branch') and the path "${resolvedPath}" is valid and empty.`));
141
156
}
142
-
throwworktreeError;// Rethrow to trigger finally block and cleanup
157
+
throwworktreeError;// Rethrow to trigger main catch block and cleanup
143
158
}
144
159
145
160
// 8. (Optional) Install dependencies
@@ -179,17 +194,21 @@ export async function prWorktreeHandler(
179
194
}
180
195
process.exit(1);
181
196
}finally{
182
-
// 10. Switch back to the original branch in the main worktree
197
+
// 10. Ensure we are back on the original branch in the main worktree
198
+
// This is now mostly a safeguard, as we attempted to switch back earlier.
183
199
if(originalBranch){
184
200
try{
185
201
constcurrentBranchNow=awaitgetCurrentBranch();
186
202
if(currentBranchNow!==originalBranch){
187
-
console.log(chalk.blue(`Switching main worktree back to "${originalBranch}"...`));
203
+
console.log(chalk.blue(`Ensuring main worktree is back on "${originalBranch}"...`));
188
204
awaitexeca("git",["checkout",originalBranch]);
189
205
}
190
206
}catch(checkoutError: any){
191
-
console.warn(chalk.yellow(`⚠️ Warning: Failed to switch main worktree back to original branch "${originalBranch}". Please check manually.`));
console.warn(chalk.yellow(`⚠️ Warning: Final check failed to switch main worktree back to original branch "${originalBranch}". Please check manually.`));
0 commit comments