Re: [RFC] worktree: add lifecycle hooks

From: Michał Papis <hidden>
Date: 2026-09-22 16:54:14

The important piece for your use case is the pre-worktree-remove hook,
not just post-operation hooks.

Post-hooks fire after the worktree is already gone. That's fine when a
tool's state lives entirely inside the worktree. It breaks when the
worktree points at state outside it — a docker container, a database,
sockets, volumes. Then the worktree itself is the registry: its config
is the only mapping from worktree to external resources, and
pre-remove is the only moment that mapping is still readable. After
removal you're guessing — hunting the disk for orphans, diffing
against remaining worktrees, turning deterministic teardown into
stale-data detection.

Real example: with AI-generated worktrees (one per task, created and
discarded faster than a human types), I was exhausting ~200GB of disk
every week and ended up writing cleanup scripts to reverse-engineer,
from what was left on disk, which containers and databases belonged to
deleted worktrees. A pre-remove hook runs the same teardown with the
config still present.

This is not about vetoing removal — pre-remove needs no power to
countermand anything. It's about doing cleanup with full information
instead of reconstructing it afterward.

One limit, which is why post hooks still matter: pre-remove only
applies to git worktree remove. For manual deletion + git worktree
prune, the tree is already gone, so pre-remove can't see it. The two
are complementary.

Cheers / Pozdrawiam,
Michal

--
Michal Papis
phone. +48 603 751 266
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help