From d9d789f5857f5abd366086fd1110e65cc182ddee Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Mon, 10 Aug 2026 11:58:16 +0100 Subject: [PATCH] Also print directory in worktree list --- worktree/worktree.plugin.zsh | 52 +++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/worktree/worktree.plugin.zsh b/worktree/worktree.plugin.zsh index 2283b7e..8dda8fb 100644 --- a/worktree/worktree.plugin.zsh +++ b/worktree/worktree.plugin.zsh @@ -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)