Add worktree list subcommand

This commit is contained in:
2026-03-30 12:35:14 +01:00
parent 7c98b592bc
commit 42e2bac8fb
2 changed files with 20 additions and 7 deletions

View File

@@ -1,9 +1,9 @@
worktree() {
if [[ "$1" == "" ]]; then
echo "usage: worktree {add,remove} <branch>"
echo "usage: worktree {add,list,remove} [<branch>]"
return 1
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "usage: worktree {add,remove} <branch>
echo "usage: worktree {add,list,remove} [<branch>]
Manage git worktrees by branch name."
return 0
@@ -12,11 +12,6 @@ Manage git worktrees by branch name."
local cmd=$1
local branch=$2
if [[ -z "$branch" ]]; then
print -P "%F{red}error:%f missing <branch> argument"
return 1
fi
if ! git rev-parse --git-dir &>/dev/null; then
print -P "%F{red}error:%f not a git repository"
return 1
@@ -31,6 +26,10 @@ Manage git worktrees by branch name."
case $cmd in
add)
if [[ -z "$branch" ]]; then
print -P "%F{red}error:%f missing <branch> argument"
return 1
fi
local root=$(git rev-parse --show-toplevel)
local wt_dest=${root:h}/${root:t}@${branch}
if ! git show-ref --verify --quiet refs/heads/"$branch" && \
@@ -45,7 +44,19 @@ Manage git worktrees by branch name."
_autoenv_authorized $wt_dest/.exit yes
fi
;;
list|ls)
local root=$(git rev-parse --show-toplevel)
local prefix="${root:h}/${root:t}@"
git worktree list --porcelain | awk -v prefix="$prefix" '
/^worktree /{ sub(/^worktree /, ""); p=$0 }
/^branch refs\/heads\//{ if(index(p, prefix)==1) { sub(/^branch refs\/heads\//, ""); print } }
'
;;
rm|remove)
if [[ -z "$branch" ]]; then
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 }')