Description
When opening a git worktree directory directly in the OpenCode desktop app, the branch displayed in the UI shows the main repository's branch (e.g. dibble-dev) instead of the worktree's actual branch (e.g. i847761/PRD-manage-family).
Reproduction
- Have a git repo at e.g.
/Projects/my-app on branch main
- Create a worktree:
git worktree add /Projects/worktrees/my-app-feature feature-branch
- Open the worktree directory
/Projects/worktrees/my-app-feature in the OpenCode desktop app
- Observe the branch indicator shows
main instead of feature-branch
Root Cause
The worktree's .git file is a pointer:
gitdir: /Projects/my-app/.git/worktrees/my-app-feature
OpenCode appears to follow git rev-parse --git-common-dir (which returns /Projects/my-app/.git) and reads HEAD from there — getting the main repo's branch. It should instead read the worktree-specific HEAD at .git/worktrees/my-app-feature/HEAD.
Additionally, the project table in OpenCode's SQLite database (~/.local/share/opencode/opencode.db) stores the worktree column as the main repo path rather than the actual worktree directory, even when the worktree directory is opened directly.
Evidence from the database:
project.worktree = /Projects/my-app (main repo — incorrect)
session.directory = /Projects/worktrees/my-app-feature (correct)
Running git rev-parse --abbrev-ref HEAD from each directory confirms:
/Projects/my-app → main
/Projects/worktrees/my-app-feature → feature-branch
Suggested Fix
Use git rev-parse --abbrev-ref HEAD executed from the actual working directory (or session directory) rather than resolving through --git-common-dir. The project.worktree column should also be set to the worktree directory when one is opened directly.
Environment
- OpenCode desktop app (macOS)
- Git worktrees created via
git worktree add
Description
When opening a git worktree directory directly in the OpenCode desktop app, the branch displayed in the UI shows the main repository's branch (e.g.
dibble-dev) instead of the worktree's actual branch (e.g.i847761/PRD-manage-family).Reproduction
/Projects/my-appon branchmaingit worktree add /Projects/worktrees/my-app-feature feature-branch/Projects/worktrees/my-app-featurein the OpenCode desktop appmaininstead offeature-branchRoot Cause
The worktree's
.gitfile is a pointer:OpenCode appears to follow
git rev-parse --git-common-dir(which returns/Projects/my-app/.git) and readsHEADfrom there — getting the main repo's branch. It should instead read the worktree-specificHEADat.git/worktrees/my-app-feature/HEAD.Additionally, the
projecttable in OpenCode's SQLite database (~/.local/share/opencode/opencode.db) stores theworktreecolumn as the main repo path rather than the actual worktree directory, even when the worktree directory is opened directly.Evidence from the database:
Running
git rev-parse --abbrev-ref HEADfrom each directory confirms:Suggested Fix
Use
git rev-parse --abbrev-ref HEADexecuted from the actual working directory (or session directory) rather than resolving through--git-common-dir. Theproject.worktreecolumn should also be set to the worktree directory when one is opened directly.Environment
git worktree add