Proposal: make gogit checkout worktree-aware — switch a branch held by another worktree with a single command
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start in cmd/checkout.go around runE (the remotes[0] panic at line 100) and trace how the checkout target is opened. Reproduce both behaviors from the body: silent double-checkout in the main worktree and the panic inside a linked worktree, then inspect how .git/worktrees/*/HEAD can be read directly or whether the go-git bump in #76 helps. Done means acceptance scenarios 1-4 pass and the default error names the holding worktree before switching.
Written by the indexing model from the issue text.
Description
Background
With git worktree, a branch may only be checked out in one worktree at a time. Switching to a branch that another worktree holds is refused by native git:
$ git checkout feature
fatal: 'feature' is already checked out at '/path/to/wt-feature'
Getting unblocked today takes multiple manual steps: walk to the other worktree and git switch --detach, come back and retry — or use git checkout --ignore-other-worktrees, which drops the safety and leaves two worktrees on the same branch (risk of divergent commits), or git worktree add yet another directory.
gogit checkout aims to be the friendly front door for branch/tag/PR checkouts, so it would be great if it could resolve this situation with a single command.
What gogit does today (verified on master 198a18f)
Both behaviors are problematic:
-
In the main worktree it silently breaks the invariant.
gogit checkout --branch featuresucceeds withSwitched to branch 'feature'even while another worktree still holdsfeature:/tmp/wt-test/main 4971cf4 [feature] /tmp/wt-test/wt-feature 4971cf4 [feature]go-git does not implement the per-worktree exclusivity check, so gogit creates exactly the state that
--ignore-other-worktreesproduces — two worktrees that can commit independently to the same branch — without any warning. -
Inside a linked worktree it does not work at all. Running
gogit checkout --branch masterinside the worktree directory (where.gitis a file, not a directory) panics:panic: runtime error: index out of range [0] with length 0 github.com/linuxsuren/gogit/cmd.(*checkoutOption).runE(...) cmd/checkout.go:100(that line is the
remotes[0]access also tracked in #86; in a linked worktree go-git opens the worktree gitdir without the main config, so the remote list is empty).
Proposal
Make gogit checkout worktree-aware — one command, safe by default:
-
Detection: before switching, read
.git/worktrees/*/HEAD(or the go-git equivalent) to find whether the target branch is already checked out elsewhere. -
Default: fail with an actionable message instead of silently breaking the invariant, e.g.
branch 'feature' is already checked out at /path/to/wt-feature hint: retry with --detach-other to detach it there and switch here, or use --worktree to open it in a new worktree -
One-command escape hatches:
--detach-other(name up for discussion): detach HEAD in the holding worktree (same commit, working tree untouched), then switch here — the safe version of--ignore-other-worktrees;--worktree <dir>: create and enter a linked worktree for the target branch in one step (git worktree add <dir> <branch>equivalent), which is the workflow git actually recommends.
-
Fix running inside a linked worktree: resolve the
.gitfile pointer sogogit checkoutworks from a worktree directory, not only from the main checkout.
Implementation notes
- go-git v5.4.2 has very limited worktree support; the go-git bump pending in #76 may help, otherwise the
.git/worktrees/*/HEADfiles can be read directly. - #86 (
remotes[0]panic) needs fixing regardless for worktree dirs.
Acceptance scenarios
Using a repo with main on master and a linked worktree on feature:
gogit checkout --branch featureinmain→ clear error with the other worktree's path (no silent double checkout)gogit checkout --branch feature --detach-otherinmain→ switches here; the other worktree is left detached at the same commitgogit checkout --branch feature --worktree ../wt2→ new linked worktree created and reportedgogit checkout --branch masterinside a linked worktree → works instead of panicking
- Dominant language
- Go
- Stars
- 19
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from LinuxSuRen/gogit
-
Docs: replace the real-looking access token in README examples and fix build status help textsPossibly taken @linuxsuren-fs claimed this 6 days ago. Opendocumentation good first issue
Difficulty 1/5 Under an hour Newbie friendliness 88/100
LinuxSuRen/gogit#90 ·
-
Bug: --token file://… content is not trimmed, trailing newline breaks authenticationPossibly taken @linuxsuren-fs claimed this 6 days ago. Openbug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
LinuxSuRen/gogit#87 ·
-
Bug: gogit checkout robustness: remotes[0] panic, wrong error message, empty version output, non-idempotent 0444 writesPossibly taken @linuxsuren-fs claimed this 6 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
LinuxSuRen/gogit#86 ·
-
Bug: gogit checkout fails on repositories whose default branch is not masterPossibly taken @linuxsuren-fs claimed this 6 days ago. Openbug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
LinuxSuRen/gogit#85 ·
-
Bug: DingTalk notification builds JSON by string concatenation, echoes the token, leaks resp.Body and swallows errorsPossibly taken @linuxsuren-fs claimed this 6 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
LinuxSuRen/gogit#84 ·
All issues in LinuxSuRen/gogit
Similar issues
-
A failed hydrate call can be dropped when the row's wait is ready tooPossibly taken @turbot-agent-fleet claimed this today. Openagent:ready
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
turbot/steampipe-plugin-sdk#1018 · 2 comments ·
Maintainers usually reply within 1 day
-
enhancement needs-review
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
router-for-me/CLIProxyAPI#7976 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
uselagoon/lagoon-cli#501 · 1 reaction ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100