Add worktree list subcommand
This commit is contained in:
@@ -28,6 +28,8 @@ _worktree() {
|
|||||||
(cmd)
|
(cmd)
|
||||||
local commands; commands=(
|
local commands; commands=(
|
||||||
'add:Create a worktree for a branch'
|
'add:Create a worktree for a branch'
|
||||||
|
'list:List managed worktrees'
|
||||||
|
'ls:List managed worktrees'
|
||||||
'remove:Remove a worktree by branch'
|
'remove:Remove a worktree by branch'
|
||||||
'rm:Remove a worktree by branch'
|
'rm:Remove a worktree by branch'
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
worktree() {
|
worktree() {
|
||||||
if [[ "$1" == "" ]]; then
|
if [[ "$1" == "" ]]; then
|
||||||
echo "usage: worktree {add,remove} <branch>"
|
echo "usage: worktree {add,list,remove} [<branch>]"
|
||||||
return 1
|
return 1
|
||||||
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
|
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."
|
Manage git worktrees by branch name."
|
||||||
return 0
|
return 0
|
||||||
@@ -12,11 +12,6 @@ Manage git worktrees by branch name."
|
|||||||
local cmd=$1
|
local cmd=$1
|
||||||
local branch=$2
|
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
|
if ! git rev-parse --git-dir &>/dev/null; then
|
||||||
print -P "%F{red}error:%f not a git repository"
|
print -P "%F{red}error:%f not a git repository"
|
||||||
return 1
|
return 1
|
||||||
@@ -31,6 +26,10 @@ Manage git worktrees by branch name."
|
|||||||
|
|
||||||
case $cmd in
|
case $cmd in
|
||||||
add)
|
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 root=$(git rev-parse --show-toplevel)
|
||||||
local wt_dest=${root:h}/${root:t}@${branch}
|
local wt_dest=${root:h}/${root:t}@${branch}
|
||||||
if ! git show-ref --verify --quiet refs/heads/"$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
|
_autoenv_authorized $wt_dest/.exit yes
|
||||||
fi
|
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)
|
rm|remove)
|
||||||
|
if [[ -z "$branch" ]]; then
|
||||||
|
print -P "%F{red}error:%f missing <branch> argument"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
local wt_path
|
local wt_path
|
||||||
wt_path=$(git worktree list --porcelain | awk -v b="$branch" \
|
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 }')
|
'/^worktree /{ sub(/^worktree /, ""); p=$0 } /^branch refs\/heads\//{ sub(/^branch refs\/heads\//, ""); if($0==b) print p }')
|
||||||
|
|||||||
Reference in New Issue
Block a user