From 887af8e9757fc85412b66efe04d03370c834f698 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Tue, 14 Apr 2026 14:26:21 +0100 Subject: [PATCH] Extend worktree remove to support multiple branches --- worktree/_worktree | 2 +- worktree/worktree.plugin.zsh | 52 +++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/worktree/_worktree b/worktree/_worktree index 4604099..91932ab 100644 --- a/worktree/_worktree +++ b/worktree/_worktree @@ -42,7 +42,7 @@ _worktree() { _arguments '1:: :__worktree_branches' ;; (rm|remove) - _arguments '1:: :__worktree_active_branches' + _arguments '*:: :__worktree_active_branches' ;; esac ;; diff --git a/worktree/worktree.plugin.zsh b/worktree/worktree.plugin.zsh index 84563c8..1dfa470 100644 --- a/worktree/worktree.plugin.zsh +++ b/worktree/worktree.plugin.zsh @@ -1,9 +1,9 @@ worktree() { if [[ "$1" == "" ]]; then - echo "usage: worktree {add,list,remove} []" + echo "usage: worktree {add,list,remove} [...]" return 1 elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then - echo "usage: worktree {add,list,remove} [] + echo "usage: worktree {add,list,remove} [...] Manage git worktrees by branch name." return 0 @@ -57,27 +57,37 @@ Manage git worktrees by branch name." print -P "%F{red}error:%f missing argument" return 1 fi - local wt_path - wt_path=$(git worktree list --porcelain | awk -v b="$branch" \ - '/^worktree /{ sub(/^worktree /, ""); p=$0 } /^branch refs\/heads\//{ sub(/^branch refs\/heads\//, ""); if($0==b) print p }') - if [[ -z "$wt_path" ]]; then - print -P "%F{red}error:%f no worktree for branch: $branch" - return 1 - fi - if [[ -f "$wt_path/BUILD" ]] || [[ -f "$wt_path/WORKSPACE" ]]; then - if (( $+commands[bazel] )); then - bazel --output_base="$wt_path" clean --expunge - elif [[ -x "$wt_path/bazelw" ]]; then - "$wt_path/bazelw" clean --expunge - fi - fi - git worktree remove "$wt_path" || return 1 + local branches=("${@:2}") local root=$(git rev-parse --show-toplevel) - local parent=${wt_path:h} - while [[ "$parent" != "${root:h}" ]] && [[ -d "$parent" ]]; do - rmdir "$parent" 2>/dev/null || break - parent=${parent:h} + local wt_list + wt_list=$(git worktree list --porcelain) + local failed=0 + local wt_path + for branch in "${branches[@]}"; do + wt_path=$(echo "$wt_list" | awk -v b="$branch" \ + '/^worktree /{ sub(/^worktree /, ""); p=$0 } /^branch refs\/heads\//{ sub(/^branch refs\/heads\//, ""); if($0==b) print p }') + if [[ -z "$wt_path" ]]; then + print -P "%F{yellow}warning:%f no worktree for branch: $branch" + continue + fi + if [[ -f "$wt_path/BUILD" ]] || [[ -f "$wt_path/WORKSPACE" ]]; then + if (( $+commands[bazel] )); then + bazel --output_base="$wt_path" clean --expunge + elif [[ -x "$wt_path/bazelw" ]]; then + "$wt_path/bazelw" clean --expunge + fi + fi + if ! git worktree remove "$wt_path"; then + failed=1 + continue + fi + local parent=${wt_path:h} + while [[ "$parent" != "${root:h}" ]] && [[ -d "$parent" ]]; do + rmdir "$parent" 2>/dev/null || break + parent=${parent:h} + done done + return $failed ;; *) print -P "%F{red}error:%f unknown command: $cmd"