diff --git a/src/components/git/PullRequestDialog.tsx b/src/components/git/PullRequestDialog.tsx index bac6addf..6716666b 100644 --- a/src/components/git/PullRequestDialog.tsx +++ b/src/components/git/PullRequestDialog.tsx @@ -267,11 +267,16 @@ export function PullRequestDialog({ .replace(/^-|-$/g, '') // Remove leading/trailing hyphens .substring(0, 50); // Limit length - const featureBranchName = `${sanitizedTitle}-${Date.now().toString(36).slice(-4)}`; + // Ensure we have a valid base name + const baseName = sanitizedTitle || 'feature'; + const featureBranchName = `${baseName}-${Date.now().toString(36).slice(-4)}`; console.log(`Creating feature branch: ${featureBranchName}`); try { - // Create and checkout the new branch + // IMPORTANT: Create branch FIRST, then commit to it + // This ensures main and feature-branch have different commits + + // Create and checkout the new branch from current HEAD await git.branch({ dir: projectPath, ref: featureBranchName, @@ -281,10 +286,52 @@ export function PullRequestDialog({ branchToUse = featureBranchName; console.log(`Successfully created and checked out branch: ${featureBranchName}`); - toast({ - title: 'Feature branch created', - description: `Created branch '${featureBranchName}' for your changes`, - }); + // 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 (hasUncommittedChanges) { + console.log('Found uncommitted changes, committing to new branch'); + + // Stage all changes + for (const [filepath, , workdir] of statusMatrix) { + if (workdir === 0) { + await git.remove({ dir: projectPath, filepath }); + } else if (workdir !== 0) { + await git.add({ dir: projectPath, filepath }); + } + } + + // Commit with the PR title as message + await git.commit({ + dir: projectPath, + message: title, + }); + + console.log('Committed changes to new branch'); + + toast({ + title: 'Feature branch created', + 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.` + ); + } + + toast({ + title: 'Feature branch created', + description: `Created branch '${featureBranchName}'`, + }); + } } catch (branchError) { console.error('Failed to create feature branch:', branchError); throw new Error( @@ -322,9 +369,10 @@ export function PullRequestDialog({ let headRef = branchToUse; if (forkInfo.needsFork && forkInfo.forkOwner && forkInfo.forkUrl) { // For cross-fork PRs, head ref format is "owner:branch" - headRef = `${forkInfo.forkOwner}:${currentBranch}`; + headRef = `${forkInfo.forkOwner}:${branchToUse}`; console.log(`Setting up fork workflow for ${forkInfo.forkOwner}/${forkInfo.forkRepo}`); + console.log(`Head ref will be: ${headRef}`); // Check current remotes const remotes = await git.listRemotes({ dir: projectPath }); @@ -540,8 +588,12 @@ export function PullRequestDialog({ const createGitHubPR = async (info: RemoteInfo, token: string, headRef: string): Promise => { const url = `${info.apiUrl}/repos/${info.owner}/${info.repo}/pulls`; - console.log('GitHub PR URL:', url); - console.log('GitHub PR data:', { title, body: description, head: headRef, base: targetBranch }); + console.log('Creating GitHub PR:'); + console.log(' URL:', url); + console.log(' Head:', headRef); + console.log(' Base:', targetBranch); + console.log(' Title:', title); + console.log(' Body length:', description?.length || 0); const response = await fetch(url, { method: 'POST', @@ -748,28 +800,23 @@ export function PullRequestDialog({
- {forkInfo.needsFork && forkInfo.forkOwner - ? `${remoteInfo?.owner}/${remoteInfo?.repo}:${targetBranch}` - : targetBranch - } + {targetBranch} - {forkInfo.needsFork && forkInfo.forkOwner - ? `${forkInfo.forkOwner}/${forkInfo.forkRepo}:${currentBranch}` - : currentBranch - } + {currentBranch} + {(currentBranch === 'main' || currentBranch === 'master') && ' (will create new branch)'}

{forkInfo.needsFork - ? `Merge ${currentBranch} from your fork into ${targetBranch} in ${remoteInfo?.owner}/${remoteInfo?.repo}` + ? `From your fork to ${remoteInfo?.owner}/${remoteInfo?.repo}:${targetBranch}` : `Merge ${currentBranch} into ${targetBranch}` }

{(currentBranch === 'main' || currentBranch === 'master') && (

- Note: A new branch will be created automatically + ℹ️ Feature branch will be auto-generated from PR title

)}
diff --git a/src/components/git/PullRequestForm.tsx b/src/components/git/PullRequestForm.tsx index cd1a79b7..974fe651 100644 --- a/src/components/git/PullRequestForm.tsx +++ b/src/components/git/PullRequestForm.tsx @@ -201,10 +201,12 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq .replace(/^-|-$/g, '') .substring(0, 50); - const featureBranchName = `${sanitizedTitle}-${Date.now().toString(36).slice(-4)}`; + const baseName = sanitizedTitle || 'feature'; + const featureBranchName = `${baseName}-${Date.now().toString(36).slice(-4)}`; console.log(`Creating feature branch: ${featureBranchName}`); try { + // Create branch first await git.branch({ dir: projectPath, ref: featureBranchName, @@ -214,6 +216,29 @@ 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 (hasUncommittedChanges) { + console.log('Committing changes to new branch'); + + for (const [filepath, , workdir] of statusMatrix) { + if (workdir === 0) { + await git.remove({ dir: projectPath, filepath }); + } else if (workdir !== 0) { + await git.add({ dir: projectPath, filepath }); + } + } + + await git.commit({ + dir: projectPath, + message: title, + }); + + console.log('Committed changes to new branch'); + } + toast({ title: 'Feature branch created', description: `Created branch '${featureBranchName}' for your changes`, @@ -234,6 +259,9 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq ? `${forkInfo.forkOwner}:${branchToUse}` : branchToUse; + console.log('PR head ref will be:', headRef); + console.log('PR base ref will be:', targetBranch); + if (forkInfo.needsFork && forkInfo.forkUrl) { console.log(`Pushing branch ${branchToUse} to fork ${forkInfo.forkUrl}`); @@ -279,19 +307,24 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq } const url = `${remoteInfo.apiUrl}/repos/${remoteInfo.owner}/${remoteInfo.repo}/pulls`; + + const prData = { + title, + body: description, + head: headRef, + base: targetBranch, + maintainer_can_modify: true, + }; + + console.log('Creating PR with data:', prData); + const response = await fetch(url, { method: 'POST', headers: { 'Authorization': `token ${credentials.password}`, 'Content-Type': 'application/json', }, - body: JSON.stringify({ - title, - body: description, - head: headRef, - base: targetBranch, - maintainer_can_modify: true, - }), + body: JSON.stringify(prData), }); if (!response.ok) {