From 88b611519cdb9d98adb80c9fc553b3ca182c016f Mon Sep 17 00:00:00 2001 From: David Poblador i Garcia Date: Tue, 10 Feb 2026 08:41:06 +0100 Subject: [PATCH] fix: resolve branch name from worktree when target is a path When wt-rm or wt-destroy receive an absolute path, the branch name passed to git branch -d/-D was the full path instead of the actual branch name. Now resolves the branch from git worktree list --porcelain before removal. Co-Authored-By: Claude Opus 4.6 --- template/justfile | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/template/justfile b/template/justfile index 353ffb0..b15b2a9 100644 --- a/template/justfile +++ b/template/justfile @@ -56,6 +56,12 @@ wt-rm target: exit 1 fi + # Resolve the branch name from the worktree before removing it + RESOLVED=$(realpath "$TARGET" 2>/dev/null || echo "$TARGET") + BRANCH=$(git worktree list --porcelain | awk -v path="$RESOLVED" \ + '/^worktree /{wt=substr($0,10)} /^branch /{if(wt==path){b=substr($0,8); sub(/^refs\/heads\//,"",b); print b}}') + [ -z "$BRANCH" ] && BRANCH="$TARGET" + git worktree remove "$TARGET" # Remove empty parent directories left after worktree removal @@ -65,11 +71,11 @@ wt-rm target: 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" + if ! git branch -d "$BRANCH" 2>/dev/null; then + echo "Worktree removed but branch '$BRANCH' has unmerged changes." + echo " To delete it anyway: git branch -D $BRANCH" else - echo "Removed worktree and branch '$TARGET'" + echo "Removed worktree and branch '$BRANCH'" fi # Remove a worktree and delete its local and remote branches (accepts branch name or folder path) @@ -84,6 +90,12 @@ wt-destroy target: exit 1 fi + # Resolve the branch name from the worktree before removing it + RESOLVED=$(realpath "$TARGET" 2>/dev/null || echo "$TARGET") + BRANCH=$(git worktree list --porcelain | awk -v path="$RESOLVED" \ + '/^worktree /{wt=substr($0,10)} /^branch /{if(wt==path){b=substr($0,8); sub(/^refs\/heads\//,"",b); print b}}') + [ -z "$BRANCH" ] && BRANCH="$TARGET" + git worktree remove "$TARGET" --force # Remove empty parent directories left after worktree removal @@ -93,13 +105,13 @@ wt-destroy target: DIR="$(dirname "$DIR")" done - git branch -D "$TARGET" + git branch -D "$BRANCH" - if git remote | grep -q . && git ls-remote --exit-code --heads origin "$TARGET" >/dev/null 2>&1; then - git push origin --delete "$TARGET" - echo "Removed worktree, local branch, and remote branch '$TARGET'" + if git remote | grep -q . && git ls-remote --exit-code --heads origin "$BRANCH" >/dev/null 2>&1; then + git push origin --delete "$BRANCH" + echo "Removed worktree, local branch, and remote branch '$BRANCH'" else - echo "Removed worktree and local branch '$TARGET' (no remote branch found)" + echo "Removed worktree and local branch '$BRANCH' (no remote branch found)" fi # List all active git worktrees