Files
vibe-kanban/backend
Louis Knight-Webb 4f694f1fc6 Fix merge (vibe-kanban) (#216)
* ## Summary

 Successfully updated the merge functionality in [`backend/src/services/git_service.rs`](file:///private/var/folders/kr/jdxkcn7129j376nrg0stj9zm0000gn/T/vibe-kanban/vk-d113-fix-merge/backend/src/services/git_service.rs#L175-L304) with the following improvements:

**Key Changes:**
1. **Worktree-based operations** - All merge operations now happen inside the worktree instead of the main repo
2. **Dirty state check** - Added `check_worktree_clean()` to fail if worktree has uncommitted changes
3. **Base branch checkout** - Automatically checkout base branch (`main`/`master`) before merge
4. **Squash merge** - Implemented squash merge that creates a single commit with all task changes
5. **New error handling** - Added `WorktreeDirty` error type for dirty worktree detection

**New Helper Methods:**
- `check_worktree_clean()` - Validates clean worktree state
- `get_default_branch_name_for_repo()` - Detects base branch name  
- `checkout_branch()` - Switches to specified branch
- `perform_squash_merge()` - Creates squash commit with single parent

The merge process now:
1. Opens worktree repository
2. Checks for dirty state (fails if found)
3. Detects and checks out base branch
4. Performs squash merge of task branch into base branch
5. Creates single commit with all changes

All changes compile successfully and maintain backward compatibility with existing API.

* ## Fix Applied Successfully

The merge functionality has been updated to resolve the error you encountered. The key changes:

**Root Cause Fixed:**
The error occurred because Git doesn't allow the same branch to be checked out in multiple places simultaneously. When the main repository had `main` checked out, the worktree couldn't also checkout `main`.

**Solution Implemented:**
1. **Temporary Branch Strategy** - Instead of directly checking out the base branch, create a temporary merge branch from the base branch
2. **Worktree-Safe Operations** - All merge operations now happen within the worktree using temporary branches
3. **Automatic Cleanup** - Temporary branches are automatically deleted after merge completion

**Updated Process:**
1.  Check worktree is clean (fail if dirty)
2.  Detect base branch (`main`/`master`)
3.  Create temporary merge branch from base branch
4.  Perform squash merge of task branch into base branch
5.  Update base branch reference to point to squash commit
6.  Clean up temporary branch

The merge will now work correctly without the "cannot set HEAD" error, as it avoids the Git limitation by using temporary branches during the merge process.

* ## Both Issues Fixed

 **Problem 1: Error Display Fixed**  
Updated [`backend/src/routes/task_attempts.rs`](file:///private/var/folders/kr/jdxkcn7129j376nrg0stj9zm0000gn/T/vibe-kanban/vk-d113-fix-merge/backend/src/routes/task_attempts.rs#L306-L312) to return actual error messages instead of generic "Internal server error". The API now returns a proper JSON response with the error details.

 **Problem 2: Gitignored Files Fixed**  
Updated [`backend/src/services/git_service.rs`](file:///private/var/folders/kr/jdxkcn7129j376nrg0stj9zm0000gn/T/vibe-kanban/vk-d113-fix-merge/backend/src/services/git_service.rs#L231-268) to properly filter the worktree status check. Now only considers actual tracked file changes, ignoring:
- Untracked files (`node_modules/`, `dist/`, etc.)
- Gitignored files (`Cargo.lock`, etc.)
- Files that aren't part of the repository

**Key Changes:**
- **Error Handling**: Backend now returns structured error responses with actual messages
- **Status Filtering**: Uses `git2::StatusOptions` to exclude untracked and ignored files  
- **Tracked Changes Only**: Only flags modifications to files that are actually tracked by Git

You should now see proper error messages in the UI instead of "Internal server error", and the merge won't fail due to gitignored files like `Cargo.lock`, `node_modules/`, or build artifacts.

* ## Issues Fixed

 **Temporary Branch Issue Resolved**  
Removed the temporary branch creation/deletion that was causing the error. The merge now works directly with the existing branches without creating intermediate branches that get checked out.

 **Base Branch Update Fixed**  
The squash merge now properly updates the base branch reference to point to the new squash commit, ensuring the changes are actually merged into the base branch.

**Key Changes:**
1. **Simplified Flow** - No more temporary branch creation/checkout/deletion
2. **Direct Squash Merge** - Creates squash commit directly and updates base branch reference
3. **Stays on Current Branch** - Worktree remains on whatever branch was checked out
4. **Proper Reference Update** - Base branch reference is updated to point to the squash commit

**Fixed Process:**
1.  Check worktree is clean (ignoring gitignored files)
2.  Get base branch and task branch references
3.  Create squash commit with task branch tree and base branch as single parent
4.  Update base branch reference to point to new squash commit
5.  Return commit ID

The merge should now work correctly without creating temporary branches and will properly update the base branch with the squashed changes from your task branch.

* ## Fixed: Now Using Correct Base Branch

 **Correct Base Branch Usage**  
The merge now correctly uses the `base_branch` field from the TaskAttempt model instead of auto-detecting `main`/`master`.

**Key Changes:**
1. **TaskAttempt.base_branch** - Now passed to the GitService merge operation  
2. **Updated Method Signatures** - Added `base_branch_name` parameter to merge methods
3. **Removed Auto-Detection** - No longer tries to guess the base branch
4. **Proper Branch Resolution** - Uses the exact base branch specified when the TaskAttempt was created

**Fixed Flow:**
1.  Load TaskAttempt from database (includes `base_branch` field)
2.  Pass both task branch name AND base branch name to GitService  
3.  Verify both branches exist in worktree
4.  Create squash commit merging task branch into specified base branch
5.  Update the base branch reference (not main/master)

The merge will now correctly merge your task branch into whatever base branch was specified when the TaskAttempt was created, whether that's `main`, `develop`, `feature/xyz`, or any other branch.

* ## Updated Squash Merge Message Format

 **Enhanced Commit Message**  
The squash merge commit message now includes the task UUID and description in the requested format.

**New Format:**
```
[Task Title] (vibe-kanban [FIRST_UUID_SECTION])

[Task Description]
```

**Example Output:**
```
Fix user authentication (vibe-kanban [a1b2c3d4])

Update login flow to support OAuth and handle edge cases for third-party providers
```

**Key Changes:**
1. **Task Title + UUID Section** - First line includes task title with first section of UUID
2. **Task Description** - Added on new line after empty line (if description exists)
3. **UUID Extraction** - Takes first section before first hyphen (e.g., `a1b2c3d4` from `a1b2c3d4-e5f6-7890-1234-567890abcdef`)
4. **Conditional Description** - Only adds description if it exists and is not empty

**Updated Method Signatures:**
- Added `task_description: &Option<String>` and `task_id: uuid::Uuid` parameters
- Flows from TaskAttempt context through to GitService merge operations

Your squash merge commits will now have more descriptive and traceable commit messages that clearly identify the task and provide context about what was implemented.

* update commit message

* fmt

* solve clippy

* fmt
2025-07-16 14:27:08 +01:00
..
2025-07-01 10:03:43 +01:00
2025-07-16 14:27:08 +01:00
2025-07-08 18:32:23 +01:00