Proposal: make gogit checkout worktree-aware — switch a branch held by another worktree with a single command

Open
#91 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
git, go
Domain
cli

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

bug enhancement

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:

  1. In the main worktree it silently breaks the invariant. gogit checkout --branch feature succeeds with Switched to branch 'feature' even while another worktree still holds feature:

    /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-worktrees produces — two worktrees that can commit independently to the same branch — without any warning.

  2. Inside a linked worktree it does not work at all. Running gogit checkout --branch master inside the worktree directory (where .git is 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 .git file pointer so gogit checkout works 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/*/HEAD files 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:

  1. gogit checkout --branch feature in main → clear error with the other worktree's path (no silent double checkout)
  2. gogit checkout --branch feature --detach-other in main → switches here; the other worktree is left detached at the same commit
  3. gogit checkout --branch feature --worktree ../wt2 → new linked worktree created and reported
  4. gogit checkout --branch master inside 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from LinuxSuRen/gogit

All issues in LinuxSuRen/gogit

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.