Skip to content

Commit 4dd1fa8

Browse files
committed
Fix SSH trunk branch: ensure trunk is checked out before creating branch
The previous fix used 'git checkout -b branch trunk' which failed because after git clone from bundle, the trunk branch exists as a ref but may not be properly resolved. This caused new branches to be created from the wrong base. New approach: 1. Try to checkout the branch if it already exists 2. If not found, first checkout the trunk branch explicitly 3. Then create the new branch from HEAD (which is now at trunk) This ensures the trunk branch is properly resolved and checked out before creating the new branch from it.
1 parent 94d934a commit 4dd1fa8

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

src/runtime/SSHRuntime.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,50 @@ export class SSHRuntime implements Runtime {
688688
initLogger.logStep("Files synced successfully");
689689

690690
// 2. Checkout branch remotely
691-
// If branch exists, check it out; otherwise create it from the specified trunk branch
691+
// If branch doesn't exist, we need to ensure the trunk branch is checked out locally first
692+
// to properly resolve it when creating the new branch
692693
initLogger.logStep(`Checking out branch: ${branchName}`);
693-
const checkoutCmd = `(git checkout ${shescape.quote(branchName)} 2>/dev/null || git checkout -b ${shescape.quote(branchName)} ${shescape.quote(trunkBranch)})`;
694694

695-
const checkoutStream = await this.exec(checkoutCmd, {
696-
cwd: workspacePath, // Use the full workspace path for git operations
697-
timeout: 300, // 5 minutes for git checkout (can be slow on large repos)
695+
// First, try to checkout the branch if it already exists
696+
const tryCheckout = await this.exec(
697+
`git checkout ${shescape.quote(branchName)} 2>/dev/null || echo "branch_not_found"`,
698+
{
699+
cwd: workspacePath,
700+
timeout: 60,
701+
}
702+
);
703+
const checkoutResult = await streamToString(tryCheckout.stdout);
704+
705+
// If branch doesn't exist, ensure trunk is available then create from it
706+
if (checkoutResult.includes("branch_not_found")) {
707+
// Ensure the trunk branch ref is available (checkout to resolve it)
708+
await this.exec(`git checkout ${shescape.quote(trunkBranch)}`, {
709+
cwd: workspacePath,
710+
timeout: 60,
711+
});
712+
713+
// Now create the new branch from the checked-out trunk
714+
const createStream = await this.exec(`git checkout -b ${shescape.quote(branchName)}`, {
715+
cwd: workspacePath,
716+
timeout: 60,
717+
});
718+
719+
const exitCode = await createStream.exitCode;
720+
if (exitCode !== 0) {
721+
const stderr = await streamToString(createStream.stderr);
722+
initLogger.logStderr(`Failed to create branch from trunk: ${stderr}`);
723+
initLogger.logComplete(-1);
724+
return {
725+
success: false,
726+
error: `Failed to create branch from trunk: ${stderr}`,
727+
};
728+
}
729+
}
730+
731+
// At this point, we should be on the correct branch
732+
const checkoutStream = await this.exec(`git branch --show-current`, {
733+
cwd: workspacePath,
734+
timeout: 10,
698735
});
699736

700737
const [stdout, stderr, exitCode] = await Promise.all([

0 commit comments

Comments
 (0)