Skip to content

GetWorktreeID fails with bare repo + worktree layout (.bare/worktrees/) #431

@atl-k

Description

@atl-k

Bug Description

GetWorktreeID in cmd/entire/cli/paths/worktree.go fails when the git repository uses the bare repo + worktree layout pattern (.bare directory instead of .git directory).

Error

failed to save changes: failed to initialize session: failed to get worktree ID: unexpected
gitdir format (no worktrees): /Users/user/repos/project/.bare/worktrees/main

Root Cause

The function hardcodes .git/worktrees/ as the only valid path marker:

const marker = ".git/worktrees/"
_, worktreeID, found := strings.Cut(gitdir, marker)
if !found {
    return "", fmt.Errorf("unexpected gitdir format (no worktrees): %s", gitdir)
}

The widely-used "bare worktree" pattern structures repos as:

project/
  .git          → file containing: "gitdir: ./.bare"
  .bare/        → actual bare git repository
    worktrees/
      main/
        gitdir  → /path/to/project/main/.git
  main/         → worktree checkout
    .git        → file containing: "gitdir: /path/to/.bare/worktrees/main"

The gitdir path is .bare/worktrees/main — no .git/worktrees/ substring exists, so strings.Cut fails.

Suggested Fix

const standardMarker = ".git/worktrees/"
_, worktreeID, found := strings.Cut(gitdir, standardMarker)
if !found {
    const bareMarker = ".bare/worktrees/"
    _, worktreeID, found = strings.Cut(gitdir, bareMarker)
}
if !found {
    return "", fmt.Errorf("unexpected gitdir format (no worktrees): %s", gitdir)
}

Or more robustly, just split on /worktrees/ and take the last component.

Environment

  • entire CLI v0.4.5
  • macOS (Darwin 25.2.0)
  • Git worktree layout created via git clone --bare + git worktree add

Related

Possibly related to #354 (also a worktree issue).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions