From 97791ed21a6fdc9e96e7640064dd51e29fd0359d Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Tue, 14 Oct 2025 15:47:50 -0500 Subject: [PATCH] Fix merge not actually committing merged changes The merge dialog was showing success but the commits weren't appearing in git log because the merge result wasn't being committed properly. Issues fixed: 1. Fast-forward merges now use git.fastForward() and checkout to update working directory 2. Three-way merges now properly create a merge commit with two parents using the merged tree 3. Added git.checkout() after merge to update working directory to match new HEAD 4. Set toast duration to Infinity for merge failures so users can read the error The key issue was that git.merge() returns a tree object but doesn't create a commit - we need to explicitly call git.commit() with the merged tree and both parent commits to create the actual merge commit. --- src/components/git/MergeDialog.tsx | 103 ++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 25 deletions(-) diff --git a/src/components/git/MergeDialog.tsx b/src/components/git/MergeDialog.tsx index 4965ae01..bb0dc623 100644 --- a/src/components/git/MergeDialog.tsx +++ b/src/components/git/MergeDialog.tsx @@ -209,48 +209,100 @@ export function MergeDialog({ setError(null); try { - // Get commit IDs + // Check if can fast-forward const sourceOid = await git.resolveRef({ dir: projectPath, ref: sourceBranch }); const targetOid = await git.resolveRef({ dir: projectPath, ref: currentBranch }); - // Perform the merge - const result = await git.merge({ - fs: git['fs'], + const canFastForward = await git.isDescendent({ dir: projectPath, - ours: targetOid, - theirs: sourceOid, - author: { - name: 'shakespeare.diy', - email: 'assistant@shakespeare.diy', - }, - message: `Merge branch '${sourceBranch}' into ${currentBranch}`, - }); + oid: sourceOid, + ancestor: targetOid, + }).catch(() => false); + + if (canFastForward) { + // Fast-forward merge: just update the current branch ref + await git.fastForward({ + dir: projectPath, + ref: currentBranch, + singleBranch: true, + }); + + // Update working directory to match the new HEAD + await git.checkout({ + dir: projectPath, + ref: currentBranch, + force: true, + }); - if (result.alreadyMerged) { toast({ - title: 'Already merged', - description: 'The branches are already merged', + title: 'Fast-forward merge successful', + description: `Successfully fast-forwarded ${currentBranch} to ${sourceBranch}`, }); } else { + // Three-way merge + const result = await git.merge({ + fs: git['fs'], + dir: projectPath, + ours: currentBranch, + theirs: sourceBranch, + author: { + name: 'shakespeare.diy', + email: 'assistant@shakespeare.diy', + }, + message: `Merge branch '${sourceBranch}' into ${currentBranch}`, + }); + + if (result.alreadyMerged) { + toast({ + title: 'Already merged', + description: 'The branches are already merged', + }); + setMergeComplete(true); + onMergeComplete?.(); + return; + } + + // The merge result contains the new tree OID + // We need to create a commit with this tree + const mergeCommitOid = await git.commit({ + dir: projectPath, + message: `Merge branch '${sourceBranch}' into ${currentBranch}`, + author: { + name: 'shakespeare.diy', + email: 'assistant@shakespeare.diy', + }, + parent: [targetOid, sourceOid], // Two parents for merge commit + tree: result.tree, + }); + + console.log('Created merge commit:', mergeCommitOid); + + // Update working directory to match the merge + await git.checkout({ + dir: projectPath, + ref: currentBranch, + force: true, + }); + toast({ title: 'Merge successful', description: `Successfully merged ${sourceBranch} into ${currentBranch}`, }); - - setMergeComplete(true); - - // Trigger rebuild - setTimeout(() => { - const newSearchParams = new URLSearchParams(searchParams); - newSearchParams.set('build', ''); - setSearchParams(newSearchParams); - }, 500); } + setMergeComplete(true); + + // Trigger rebuild + setTimeout(() => { + const newSearchParams = new URLSearchParams(searchParams); + newSearchParams.set('build', ''); + setSearchParams(newSearchParams); + }, 500); + onMergeComplete?.(); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Unknown error'; - + if (errorMessage.includes('conflict') || errorMessage.includes('CONFLICT')) { setError('Merge conflicts detected. Please resolve conflicts manually using the terminal.'); } else { @@ -261,6 +313,7 @@ export function MergeDialog({ title: 'Merge failed', description: errorMessage, variant: 'destructive', + duration: Infinity, }); } finally { setIsMerging(false);