Extend worktree remove to support multiple branches
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
worktree() {
|
||||
if [[ "$1" == "" ]]; then
|
||||
echo "usage: worktree {add,list,remove} [<branch>]"
|
||||
echo "usage: worktree {add,list,remove} <branch> [<branch>...]"
|
||||
return 1
|
||||
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."
|
||||
return 0
|
||||
@@ -57,27 +57,37 @@ Manage git worktrees by branch name."
|
||||
print -P "%F{red}error:%f missing <branch> 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"
|
||||
|
||||
Reference in New Issue
Block a user