From 297e51f01de8df7fa695ce5df29d523e1ce3dfab Mon Sep 17 00:00:00 2001 From: David Poblador i Garcia Date: Mon, 9 Feb 2026 19:33:53 +0100 Subject: [PATCH] fix: clean up empty parent directories after worktree removal When using nested branch names like `fix/issue-x`, removing the worktree leaves empty parent directories behind. Add a loop to both wt-rm and wt-destroy that walks up from the target's parent, removing empty directories until it hits the project root or a non-empty directory. Co-Authored-By: Claude Opus 4.6 --- template/justfile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/template/justfile b/template/justfile index a81ca3e..353ffb0 100644 --- a/template/justfile +++ b/template/justfile @@ -58,6 +58,13 @@ wt-rm target: git worktree remove "$TARGET" + # Remove empty parent directories left after worktree removal + DIR="$(dirname "$TARGET")" + while [ "$DIR" != "." ] && [ -d "$DIR" ] && [ -z "$(ls -A "$DIR")" ]; do + rmdir "$DIR" + DIR="$(dirname "$DIR")" + done + if ! git branch -d "$TARGET" 2>/dev/null; then echo "Worktree removed but branch '$TARGET' has unmerged changes." echo " To delete it anyway: git branch -D $TARGET" @@ -79,6 +86,13 @@ wt-destroy target: git worktree remove "$TARGET" --force + # Remove empty parent directories left after worktree removal + DIR="$(dirname "$TARGET")" + while [ "$DIR" != "." ] && [ -d "$DIR" ] && [ -z "$(ls -A "$DIR")" ]; do + rmdir "$DIR" + DIR="$(dirname "$DIR")" + done + git branch -D "$TARGET" if git remote | grep -q . && git ls-remote --exit-code --heads origin "$TARGET" >/dev/null 2>&1; then