Also print directory in worktree list

This commit is contained in:
2026-08-10 11:58:16 +01:00
parent 471d336cc4
commit d9d789f585

View File

@@ -67,10 +67,54 @@ Manage git worktrees by branch name."
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 } }
'
local -a names dirs mismatched
local maxw=0
local row p b h name suffix
for row in "${(@f)$(git worktree list --porcelain | awk '
/^worktree /{ p = substr($0, 10); b = ""; h = "" }
/^HEAD /{ h = substr($0, 6) }
/^branch refs\/heads\//{ b = $0; sub(/^branch refs\/heads\//, "", b) }
/^$/{ if (p != "") { print p "\t" b "\t" h; p = "" } }
END{ if (p != "") print p "\t" b "\t" h }
')}"; do
[[ -z "$row" ]] && continue
p=${row%%$'\t'*}
b=${${row#*$'\t'}%%$'\t'*}
h=${row##*$'\t'}
[[ "${p[1,${#prefix}]}" == "$prefix" ]] || continue
suffix=${p[${#prefix}+1,-1]}
if [[ -n "$b" ]]; then
name=$b
else
name="(detached ${h[1,7]})"
fi
names+=("$name")
dirs+=("@$suffix")
if [[ "$b" == "$suffix" ]]; then
mismatched+=(0)
else
mismatched+=(1)
fi
(( ${#name} > maxw )) && maxw=${#name}
done
(( ${#names} )) || return 0
local c_dir c_warn c_off
if [[ -t 1 ]]; then
c_dir=$(print -Pn '%F{8}')
c_warn=$(print -Pn '%F{yellow}')
c_off=$(print -Pn '%f')
fi
local i c
for (( i = 1; i <= ${#names}; i++ )); do
if (( mismatched[i] )); then
c=$c_warn
else
c=$c_dir
fi
print -r -- " ${(r:$maxw:)names[i]} ${c}${dirs[i]}${c_off}"
done
;;
rm|remove)