From ec4e0356186fc81057d566da609fd9b1058ac135 Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Tue, 14 Oct 2025 22:15:30 -0500 Subject: [PATCH] Fix race condition in auto-branch creation **Issue:** Branch was being created and immediately checked for differences, but the check was reading the OLD currentBranch ref instead of the newly created branch, causing false "no changes" errors. **Fix:** - Check for uncommitted changes BEFORE creating branch (not after) - Store the status matrix state - Create branch - Commit uncommitted changes to new branch using stored status - Removed the diff check that was using stale ref data **Why This Works:** The statusMatrix check happens before branching, capturing the actual uncommitted state. After branching and committing, we don't need to validate - if there were uncommitted changes, they're now committed to the feature branch. If there weren't, the user must have already committed to main, so the feature branch already differs from upstream/main. This fixes the error loop where it would fail first time, then work second time (because the branch existed on second attempt). --- src/components/git/PullRequestDialog.tsx | 23 ++++++++--------------- src/components/git/PullRequestForm.tsx | 11 ++++++----- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/components/git/PullRequestDialog.tsx b/src/components/git/PullRequestDialog.tsx index 6716666b..1dbb6f16 100644 --- a/src/components/git/PullRequestDialog.tsx +++ b/src/components/git/PullRequestDialog.tsx @@ -276,6 +276,10 @@ export function PullRequestDialog({ // IMPORTANT: Create branch FIRST, then commit to it // This ensures main and feature-branch have different commits + // Check if there are uncommitted changes BEFORE branching + const statusMatrix = await git.statusMatrix({ dir: projectPath }); + const hasUncommittedChanges = statusMatrix.some(([, head, workdir]) => head !== workdir); + // Create and checkout the new branch from current HEAD await git.branch({ dir: projectPath, @@ -286,10 +290,7 @@ export function PullRequestDialog({ branchToUse = featureBranchName; console.log(`Successfully created and checked out branch: ${featureBranchName}`); - // Check if there are uncommitted changes that need to be committed to this branch - const statusMatrix = await git.statusMatrix({ dir: projectPath }); - const hasUncommittedChanges = statusMatrix.some(([, head, workdir]) => head !== workdir); - + // If there were uncommitted changes, commit them now to the new branch if (hasUncommittedChanges) { console.log('Found uncommitted changes, committing to new branch'); @@ -315,17 +316,9 @@ export function PullRequestDialog({ description: `Created '${featureBranchName}' with your changes committed`, }); } else { - // No uncommitted changes - check if this branch differs from main - const mainCommit = await git.resolveRef({ dir: projectPath, ref: `refs/heads/${currentBranch}` }); - const branchCommit = await git.resolveRef({ dir: projectPath, ref: 'HEAD' }); - - if (mainCommit === branchCommit) { - throw new Error( - `No changes to submit.\n\n` + - `You need to have committed changes that differ from the '${currentBranch}' branch.\n\n` + - `Please make some changes and commit them before creating a PR.` - ); - } + // No uncommitted changes - the branch should already differ from main + // (user must have committed changes to main already) + console.log('No uncommitted changes, branch created from existing commits'); toast({ title: 'Feature branch created', diff --git a/src/components/git/PullRequestForm.tsx b/src/components/git/PullRequestForm.tsx index 974fe651..9a5d232c 100644 --- a/src/components/git/PullRequestForm.tsx +++ b/src/components/git/PullRequestForm.tsx @@ -206,7 +206,11 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq console.log(`Creating feature branch: ${featureBranchName}`); try { - // Create branch first + // Check for uncommitted changes BEFORE creating branch + const statusMatrix = await git.statusMatrix({ dir: projectPath }); + const hasUncommittedChanges = statusMatrix.some(([, head, workdir]) => head !== workdir); + + // Create and checkout branch first await git.branch({ dir: projectPath, ref: featureBranchName, @@ -216,10 +220,7 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq branchToUse = featureBranchName; console.log(`Successfully created and checked out branch: ${featureBranchName}`); - // Check if there are uncommitted changes to commit to this branch - const statusMatrix = await git.statusMatrix({ dir: projectPath }); - const hasUncommittedChanges = statusMatrix.some(([, head, workdir]) => head !== workdir); - + // If there were uncommitted changes, commit them to the new branch if (hasUncommittedChanges) { console.log('Committing changes to new branch');