Skip to content

feat: Add git worktree support for non-devcontainer projects #104

@athal7

Description

@athal7

Summary

Extend the plugin to support git worktrees as an alternative to devcontainer clones for projects that don't use devcontainers. This provides the same isolated branch workflow with session state management.

Motivation

  • Not all projects use devcontainers, but still benefit from isolated branch work
  • Worktrees are simpler (local filesystem) but need similar session management
  • Users ask "set up a worktree for feature-x" and expect the agent to track it
  • Without session state, the agent may forget the worktree context after compaction

Proposed Design

New /worktree command

/worktree <branch>     # Create worktree, set as active session
/worktree off          # Clear active worktree
/worktree              # Show current status

New worktree tool

Similar to devcontainer tool but:

  • Creates worktree in standard location: ~/.local/share/opencode/worktree/<repo>/<branch>/
  • Copies gitignored secrets from main repo (e.g., credentials, local env files)
  • Runs direnv allow for environment setup

Bash interception

When session has an active worktree:

  • Add workdir parameter to bash commands (vs wrapping with devcontainer exec)
  • Same shouldRunOnHost logic for git commands etc.
  • HOST: escape hatch works the same way

Session state

{
  type: "worktree",  // vs "devcontainer"
  branch: "feature-x",
  workspace: "~/.local/share/opencode/worktree/<repo>/<branch>",
  repoName: "<repo>",
  mainRepo: "/path/to/main/repo",  // for worktree removal
}

Worktree setup steps

  1. Validate: in git repo, not already in a worktree
  2. Create: git worktree add <path> -b <branch> (or without -b for existing)
  3. Copy secrets: Find gitignored-but-tracked files, copy from main repo
  4. Environment: direnv allow in new worktree

Port & Database isolation

Projects using worktrees can configure their .envrc to derive PORT and database settings from the worktree name to avoid conflicts when running multiple branches simultaneously. The plugin could:

  • Report the computed PORT after setup (if detectable via source .envrc && echo $PORT)
  • Suggest adding isolation if multiple worktrees detected without it

Additional scope

Workspace cleanup

Add a /workspaces or /workspaces cleanup command for managing stale workspaces:

  • List worktrees and devcontainer clones with last access time, branch, uncommitted changes
  • Identify safe-to-delete vs active workspaces
  • Handle proper worktree removal (must run from main repo)
  • Clean up associated Docker resources for devcontainers

Plugin rebrand

Consider renaming to opencode-workspaces to reflect the broader scope (devcontainers + worktrees + cleanup).

Implementation notes

  • Reuse existing session management, job tracking infrastructure
  • Worktree creation is fast (no container build), so might not need background jobs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions