Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/main/services/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ export class GitHubService {
{ cwd: projectPath }
);
} catch (fetchError) {
// Fallback: try gh pr checkout with detach to avoid working tree conflicts
// Fallback: use gh pr checkout to create/sync the local PR branch.
console.warn(
'Fetch-based PR branch creation failed, falling back to gh pr checkout:',
fetchError
Expand All @@ -915,11 +915,23 @@ export class GitHubService {

try {
await this.execGH(
`gh pr checkout ${JSON.stringify(String(prNumber))} --branch ${JSON.stringify(safeBranch)} --force --detach`,
`gh pr checkout ${JSON.stringify(String(prNumber))} --branch ${JSON.stringify(safeBranch)} --force`,
{ cwd: projectPath }
);
// Some gh/fork combinations can leave HEAD detached without a local branch ref.
// Ensure the requested local branch exists for downstream worktree creation.
try {
await execAsync(
`git show-ref --verify --quiet ${JSON.stringify(`refs/heads/${safeBranch}`)}`,
{ cwd: projectPath }
);
} catch {
await execAsync(`git branch --force ${JSON.stringify(safeBranch)} HEAD`, {
cwd: projectPath,
});
}
} catch (error) {
console.error('Failed to checkout pull request branch via gh:', error);
console.error('Failed during PR branch checkout or local-ref creation:', error);
throw error;
} finally {
if (previousRef && previousRef !== safeBranch) {
Expand Down
Loading