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

Aperta
#91 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
git, go
Ambito
cli

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Go
Stelle
19
Fork
3
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di LinuxSuRen/gogit

Tutte le issue di LinuxSuRen/gogit

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.