From 42e2bac8fb54d11e19300997dffbbe4e151323cd Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Mon, 30 Mar 2026 12:35:14 +0100 Subject: [PATCH] Add `worktree list` subcommand --- worktree/_worktree | 2 ++ worktree/worktree.plugin.zsh | 25 ++++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/worktree/_worktree b/worktree/_worktree index 950400a..4604099 100644 --- a/worktree/_worktree +++ b/worktree/_worktree @@ -28,6 +28,8 @@ _worktree() { (cmd) local commands; commands=( 'add:Create a worktree for a branch' + 'list:List managed worktrees' + 'ls:List managed worktrees' 'remove:Remove a worktree by branch' 'rm:Remove a worktree by branch' ) diff --git a/worktree/worktree.plugin.zsh b/worktree/worktree.plugin.zsh index da5bb3c..84563c8 100644 --- a/worktree/worktree.plugin.zsh +++ b/worktree/worktree.plugin.zsh @@ -1,9 +1,9 @@ worktree() { if [[ "$1" == "" ]]; then - echo "usage: worktree {add,remove} " + echo "usage: worktree {add,list,remove} []" return 1 elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then - echo "usage: worktree {add,remove} + echo "usage: worktree {add,list,remove} [] 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 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 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 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 }')