Skip to content

Worktree creation fails: git worktree remove --force on non-existent path causes fatal error #304

@breskeby

Description

@breskeby

Bug

When creating a new worktree via the Supacode UI or supacode repo worktree-new, the operation always fails with "Git command failed" for repositories where no worktrees have been previously created under ~/.supacode/repos/<repo>/.

Root Cause

Before calling the bundled wt sw script to create a worktree, Supacode's Swift code runs a pre-cleanup step:

git -C <repo> worktree remove --force ~/.supacode/repos/<repo>/<branch>/

This fails with exit code 128 when the target path doesn't exist (which is always the case for a brand-new worktree):

fatal: '~/.supacode/repos/<repo>/<branch>/' is not a working tree

Supacode treats this as a fatal error and aborts — the wt sw command is never invoked.

How I diagnosed this

I enabled [trace2] eventTarget in ~/.gitconfig to capture all git commands Supacode executes. The sequence for every worktree creation attempt is:

  1. git check-ref-format --branch <name> → exit 0 ✓
  2. git rev-parse --is-bare-repository → exit 0 ✓
  3. git worktree remove --force <target-path>/exit 128
  4. Supacode aborts. wt sw is never called.

Running the wt sw command directly from a terminal works perfectly:

wt --base-dir ~/.supacode/repos/<repo>/ sw --from origin/main <branch>

Expected behavior

git worktree remove --force should be allowed to fail silently when the target path doesn't exist or isn't a registered worktree. It's a cleanup step — if there's nothing to clean up, that's fine.

Suggested fix

Either:

  • Skip the worktree remove call if the target path doesn't exist
  • Ignore exit code 128 from worktree remove when the error message contains "is not a working tree"

Reproduction

  1. Add https://github.com/elastic/elasticsearch as a repository in Supacode (large repo, ~41,500 files, open source)
  2. Try creating a new worktree from the UI with any branch name based on origin/main
  3. Observe "Git command failed" error — the wt sw command is never executed

Environment

  • Supacode v0.8.5 (build 136000)
  • macOS 26.4 (25E246), Apple Silicon
  • Git: Apple Git 2.50.1 (/Applications/Xcode.app/Contents/Developer/usr/bin/git)

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