Extend worktree remove to support multiple branches
This commit is contained in:
@@ -42,7 +42,7 @@ _worktree() {
|
|||||||
_arguments '1:: :__worktree_branches'
|
_arguments '1:: :__worktree_branches'
|
||||||
;;
|
;;
|
||||||
(rm|remove)
|
(rm|remove)
|
||||||
_arguments '1:: :__worktree_active_branches'
|
_arguments '*:: :__worktree_active_branches'
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
worktree() {
|
worktree() {
|
||||||
if [[ "$1" == "" ]]; then
|
if [[ "$1" == "" ]]; then
|
||||||
echo "usage: worktree {add,list,remove} [<branch>]"
|
echo "usage: worktree {add,list,remove} <branch> [<branch>...]"
|
||||||
return 1
|
return 1
|
||||||
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
|
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
|
||||||
echo "usage: worktree {add,list,remove} [<branch>]
|
echo "usage: worktree {add,list,remove} <branch> [<branch>...]
|
||||||
|
|
||||||
Manage git worktrees by branch name."
|
Manage git worktrees by branch name."
|
||||||
return 0
|
return 0
|
||||||
@@ -57,27 +57,37 @@ Manage git worktrees by branch name."
|
|||||||
print -P "%F{red}error:%f missing <branch> argument"
|
print -P "%F{red}error:%f missing <branch> argument"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
local wt_path
|
local branches=("${@:2}")
|
||||||
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 root=$(git rev-parse --show-toplevel)
|
local root=$(git rev-parse --show-toplevel)
|
||||||
local parent=${wt_path:h}
|
local wt_list
|
||||||
while [[ "$parent" != "${root:h}" ]] && [[ -d "$parent" ]]; do
|
wt_list=$(git worktree list --porcelain)
|
||||||
rmdir "$parent" 2>/dev/null || break
|
local failed=0
|
||||||
parent=${parent:h}
|
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
|
done
|
||||||
|
return $failed
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
print -P "%F{red}error:%f unknown command: $cmd"
|
print -P "%F{red}error:%f unknown command: $cmd"
|
||||||
|
|||||||
Reference in New Issue
Block a user