Skip to main content

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:
  1. Backend API - Git operations and worktree management
  2. Frontend UI - Session creation and worktree visualization
  3. 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
Worktree Routes (web/src/server/routes/worktrees.ts)
  • GET /api/worktrees - List all worktrees with stats and follow mode status
  • POST /api/worktrees - Create new worktree
  • DELETE /api/worktrees/:branch - Remove worktree
  • POST /api/worktrees/switch - Switch branch and enable follow mode
  • POST /api/worktrees/follow - Enable/disable follow mode for a branch
Git Routes (web/src/server/routes/git.ts)
  • GET /api/git/repo-info - Get repository information
  • POST /api/git/event - Process git hook events (internal use)
  • GET /api/git/follow - Check follow mode status for a repository
  • GET /api/git/notifications - Get pending notifications

Key Functions

Git Operations

All Git operations use Node.js child_process.execFile for security:

Follow Mode Implementation

Follow mode uses Git hooks and git config for state management:
  1. State Storage: Git config vibetunnel.followWorktree
  2. Git Hooks: Installed in BOTH main repo and worktree
    • post-checkout: Detects branch switches
    • post-commit: Detects new commits
    • post-merge: Detects merge operations
  3. Event Processing: Hooks execute vt git event command
  4. 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
WorktreeManager (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:
  1. No Worktree Selected:
  2. Worktree Selected:

UI Updates

Dynamic labels based on context:
Help text explaining behavior:

Git Hook Integration

Hook Installation

Automatic hook installation on repository access:

Hook Script

The hook implementation uses the vt command:
The 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):
Frontend (web/src/client/services/git-service.ts):
The UI helper fields are computed dynamically in the worktree routes based on the current repository path and are not stored in the backend data model.

Session with Git Info

Error Handling

Common Errors

  1. Uncommitted Changes
  2. Branch Already Checked Out
  3. 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

  1. 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)
  2. 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
  3. 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

Missing Features from Spec

  1. Worktree Path Customization
    • Current: Auto-generated paths only
    • Spec: Should allow custom path input
    • Impact: Users cannot organize worktrees in custom locations
  2. 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
  3. 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

Testing

Unit Tests

  • worktrees.test.ts - Route handlers
  • git-hooks.test.ts - Hook installation
  • session-create-form.test.ts - UI logic

Integration Tests

  • worktree-workflows.test.ts - Full workflows
  • follow-mode.test.ts - Follow mode scenarios

E2E Tests

  • Create worktree via UI
  • Switch branches with warnings
  • Follow mode synchronization

Implementation Summary

✅ Fully Implemented

  1. 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
  2. Follow Mode - Complete implementation
    • Git config storage (vibetunnel.followBranch)
    • Automatic branch synchronization via hooks
    • UI controls in WorktreeManager and SessionCreateForm
  3. 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

  1. Path Generation - Simplified version only
    • Auto-generates paths as ${repoPath}-${branchSlug}
    • No user customization option
    • Works for basic use cases
  2. 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

  1. Advanced Worktree Creation UI
    • Custom path input field
    • Path validation and suggestions
    • Preview of final worktree location
  2. WorktreeManager Creation UI
    • No worktree creation in management view
    • Must use SessionCreateForm or terminal
  3. Worktree Templates/Presets
    • No saved worktree configurations
    • No quick-create from templates