Git Worktree Implementation Specification
This document describes the technical implementation of Git worktree support in VibeTunnel.Architecture Overview
VibeTunnel’s worktree support is built on three main components:- Backend API - Git operations and worktree management
- Frontend UI - Session creation and worktree visualization
- Git Hooks - Automatic synchronization and follow mode
Backend Implementation
Core Services
GitService (web/src/server/services/git-service.ts
)
- Not implemented as a service, Git operations are embedded in routes
- Client-side GitService exists at
web/src/client/services/git-service.ts
web/src/server/routes/worktrees.ts
)
GET /api/worktrees
- List all worktrees with stats and follow mode statusPOST /api/worktrees
- Create new worktreeDELETE /api/worktrees/:branch
- Remove worktreePOST /api/worktrees/switch
- Switch branch and enable follow modePOST /api/worktrees/follow
- Enable/disable follow mode for a branch
web/src/server/routes/git.ts
)
GET /api/git/repo-info
- Get repository informationPOST /api/git/event
- Process git hook events (internal use)GET /api/git/follow
- Check follow mode status for a repositoryGET /api/git/notifications
- Get pending notifications
Key Functions
Git Operations
All Git operations use Node.jschild_process.execFile
for security:
Follow Mode Implementation
Follow mode uses Git hooks and git config for state management:-
State Storage: Git config
vibetunnel.followWorktree
-
Git Hooks: Installed in BOTH main repo and worktree
post-checkout
: Detects branch switchespost-commit
: Detects new commitspost-merge
: Detects merge operations
-
Event Processing: Hooks execute
vt git event
command -
Synchronization Logic:
- Worktree events → Main repo syncs (branch, commits, checkouts)
- Main repo commits → Worktree syncs (commits only)
- Main repo branch switch → Auto-unfollow
Frontend Implementation
Components
SessionCreateForm (web/src/client/components/session-create-form.ts
)
- Branch/worktree selection UI
- Smart branch switching logic
- Warning displays for conflicts
web/src/client/components/worktree-manager.ts
)
- Dedicated worktree management UI
- Follow mode controls
- Worktree deletion and branch switching
- Note: Does not include UI for creating new worktrees
State Management
Branch Selection Logic
The new session dialog implements smart branch handling:-
No Worktree Selected:
-
Worktree Selected:
UI Updates
Dynamic labels based on context:Git Hook Integration
Hook Installation
Automatic hook installation on repository access:Hook Script
The hook implementation uses thevt
command:
vt git event
command:
- Sends the repository path to the server via
POST /api/git/event
- Server determines what changed by examining current git state
- Triggers branch synchronization if follow mode is enabled
- Sends notifications to connected sessions
- Runs in background to avoid blocking git operations
Follow Mode Logic
The git event handler determines sync behavior based on event source:Data Models
Worktree
The Worktree interface differs between backend and frontend: Backend (web/src/server/routes/worktrees.ts
):
web/src/client/services/git-service.ts
):
Session with Git Info
Error Handling
Common Errors
-
Uncommitted Changes
-
Branch Already Checked Out
-
Worktree Path Exists
Error Recovery
- Show user-friendly warnings
- Fallback to safe defaults
- Never lose user work
- Log detailed errors for debugging
Performance Considerations
Caching
- Worktree list cached for 5 seconds
- Branch list cached per repository
- Git status cached with debouncing
Optimization
Security
Command Injection Prevention
All Git commands use array arguments:Path Validation
Worktree Creation
Currently, worktree creation is handled through terminal commands rather than UI:UI Support Status
-
WorktreeManager (
web/src/client/components/worktree-manager.ts
)- No creation UI, only management of existing worktrees
- Provides worktree switching, deletion, and follow mode controls
- Shows worktree status (commits ahead, uncommitted changes)
-
SessionCreateForm (
web/src/client/components/session-create-form.ts
)- Has worktree creation support through the git-branch-selector component
- ✅ Creates worktrees and updates UI state properly
- ✅ Selects newly created worktree after creation
- ✅ Clears loading states and resets form on completion
- ✅ Comprehensive branch name validation
- ✅ Specific error messages for common failures
- ⚠️ Uses simplistic path generation (repo path + branch slug)
- ❌ No path customization UI
- ❌ No option to create from specific base branch in UI
-
Path Generation (
web/src/client/components/session-create-form/git-utils.ts:100-103
)- Simple approach:
${repoPath}-${branchSlug}
- Branch names sanitized to alphanumeric + hyphens/underscores
- No user customization of worktree location
- Simple approach:
Missing Features from Spec
-
Worktree Path Customization
- Current: Auto-generated paths only
- Spec: Should allow custom path input
- Impact: Users cannot organize worktrees in custom locations
-
Base Branch Selection in UI
- Current: Uses selected base branch from dropdown
- Missing: No explicit UI to choose base branch during worktree creation
- Workaround: Select base branch first, then create worktree
-
Comprehensive E2E Tests
- Unit tests exist:
worktrees.test.ts
,git-hooks.test.ts
- Integration tests exist:
worktree-workflows.test.ts
- Missing: Full E2E tests for UI worktree creation flow
- Unit tests exist:
Testing
Unit Tests
worktrees.test.ts
- Route handlersgit-hooks.test.ts
- Hook installationsession-create-form.test.ts
- UI logic
Integration Tests
worktree-workflows.test.ts
- Full workflowsfollow-mode.test.ts
- Follow mode scenarios
E2E Tests
- Create worktree via UI
- Switch branches with warnings
- Follow mode synchronization
Implementation Summary
✅ Fully Implemented
-
Backend API - All planned endpoints functional
- List, create, delete, switch, follow mode operations
- Git hook integration for automatic branch following
- Proper error handling and validation
-
Follow Mode - Complete implementation
- Git config storage (
vibetunnel.followBranch
) - Automatic branch synchronization via hooks
- UI controls in WorktreeManager and SessionCreateForm
- Git config storage (
-
Basic Worktree Creation - Functional with recent fixes
- Create new worktrees from SessionCreateForm
- Branch name validation
- UI state management
- Error handling with specific messages
⚠️ Partially Implemented
-
Path Generation - Simplified version only
- Auto-generates paths as
${repoPath}-${branchSlug}
- No user customization option
- Works for basic use cases
- Auto-generates paths as
-
Testing - Good coverage but missing E2E
- Unit tests for routes and utilities
- Integration tests for workflows
- Missing: Full E2E tests with UI interactions
❌ Not Implemented
-
Advanced Worktree Creation UI
- Custom path input field
- Path validation and suggestions
- Preview of final worktree location
-
WorktreeManager Creation UI
- No worktree creation in management view
- Must use SessionCreateForm or terminal
-
Worktree Templates/Presets
- No saved worktree configurations
- No quick-create from templates