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).
This commit is contained in:
shakespeare.diy committed 2025-10-14 22:15:30 -05:00
1 parent 428c6b0e2e
commit ec4e035618
2 files changed
+14 -20

No files matched your search

+8 -15
View File
@@ -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',
+6 -5
View File
@@ -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');