Skip to content

wt new/session add fail when target is the default branch already at the primary worktree #13

@ukanga

Description

@ukanga

Repro

From the repo root on main:

$ wt session add main
Creating worktree: main
Error: Failed to create worktree: Preparing worktree (checking out 'main')
fatal: 'main' is already used by worktree at '/home/me/code/wt'

$ wt new main
Error: Failed to create worktree: Preparing worktree (checking out 'main')
fatal: 'main' is already used by worktree at '/home/me/code/wt'

Why it fails

Both commands shell out to git worktree add <path> main. Git refuses because main is already checked out at the repo root (the primary worktree). The error surfaces git's internal message rather than something wt-shaped.

Expected

The default branch is, by definition, already a "worktree" — it's the primary one. The commands should recognize this and reuse the existing primary worktree path instead of trying to create a duplicate:

  • wt session add main → add a tmux window/session pointing at the repo root.
  • wt new main → either drop into a shell at the repo root, or print a friendly hint pointing the user at the primary worktree.

Suggested fix

WorktreeManager::list_worktrees() already exposes the primary worktree as the entry with empty task_id (src/worktree_manager.rs:357-364). A small helper — "is this branch checked out at the primary worktree?" — lets both call sites short-circuit:

  • ensure_worktree_path (src/session_cmd.rs:132-155): if the requested name matches the primary worktree's branch, return repo.root instead of calling create_worktree.
  • cmd_new (src/main.rs:156-211): same check; reuse repo.root instead of creating.

Generalizes naturally to "any branch already checked out at the primary worktree," not just main/master.

Open question

wt session add main is a clear win — useful, and the current failure is git-shaped rather than wt-shaped.

wt new main is less obvious. wt new semantically creates a worktree, and elsewhere in the codebase the primary worktree is explicitly not a wt worktree (pick_worktree filters out empty task_id at src/main.rs:295-298; cmd_use rejects "main" at src/main.rs:412-414). One option is to replace the git error here with a friendlier wt-level error pointing the user at wt session add main or cd <repo-root>, rather than pretending main is a wt worktree. Open to either direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions