Skip to content

[idea] :WorktreePrune — detect and remove merged/stale git worktrees #340

Description

@stanfish06

What

lua/config/worktree.lua covers listing/switching (M.list, bound to :Worktrees/<leader>sw) and diffing against a base branch (M.diff_base, :WorktreeDiff/<leader>gm), but there's no cleanup path. In a worktree-heavy flow, worktrees for branches that are long since merged accumulate, and removing one today means dropping to a shell for git worktree remove — a context switch the rest of this module is specifically designed to avoid.

Where

lua/config/worktree.lua — a new M.prune() alongside the existing list_worktrees()/default_base_branch() helpers (currently lines 10-52), surfaced the same way M.list surfaces its picker (lua/config/worktree.lua:69-122, fzf-lua with a vim.ui.select fallback).

Why it matters

default_base_branch() (worktree.lua:41-52) already encodes "what's the trunk branch to compare against" for diffing; the exact same information identifies prune candidates — a worktree whose branch is fully merged into that base is almost always done being worked on. Reusing that logic rather than introducing a new concept keeps the module's surface small, and gives the existing :Worktrees picker an actual way to shrink the list it shows instead of only ever growing it.

Recommended action

Add M.prune() / :WorktreePrune that cross-references git worktree list --porcelain output (already parsed by list_worktrees()) against git branch --merged <base>, lists the matches through the same fzf-lua/vim.ui.select pattern M.list uses, and on confirmation runs git worktree remove — mirroring the confirm-then-act shape already used for killing a server in lua/config/server.lua's kill_entry().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions