@@ -825,9 +825,12 @@ M.git_switch = function(selected, opts)
825825 cmd = path .git_cwd ({ " git" , " switch" }, opts )
826826 end
827827 -- remove anything past space
828- local branch = selected [1 ]:match (" [ ^ ]+" )
828+ local marker , branch = selected [1 ]:match (" %s-([%+%*]?)%s+([ ^ ]+) " )
829829 -- do nothing for active branch
830- if branch :find (" %*" ) ~= nil then return end
830+ if marker == " *" then
831+ utils .warn (" already on bramch '%s'" , branch )
832+ return
833+ end
831834 if branch :find (" ^remotes/" ) then
832835 if opts .remotes == " detach" then
833836 table.insert (cmd , " --detach" )
@@ -837,6 +840,12 @@ M.git_switch = function(selected, opts)
837840 end
838841 table.insert (cmd , branch )
839842 local output , rc = utils .io_systemlist (cmd )
843+ if marker == " +" then
844+ assert (rc ~= 0 ) -- should err with worktree path
845+ local worktree_path = output [1 ]:match ([[ ([^']+)'$]] )
846+ utils .warn (" '%s' is a worktree, changing directory to '%s'" , branch , worktree_path )
847+ return M .git_worktree_cd ({ worktree_path }, opts )
848+ end
840849 if rc ~= 0 then
841850 utils .error (unpack (output ))
842851 else
@@ -911,10 +920,11 @@ M.git_worktree_add = function(selected, opts)
911920 if type (branch ) ~= " string" or # branch == 0 then
912921 utils .warn (" Branch name cannot be empty, use prompt for input." )
913922 else
914- local worktree_path = " ../ " .. branch
923+ local worktree_path = path . join ({ " .." , branch })
915924
916- local check_cmd = path .git_cwd ({ " git" , " show-ref" , " --verify" , " --quiet" , " refs/heads/" .. branch }, opts )
917- local _ , check_rc = utils .io_systemlist (check_cmd )
925+ local cmd_branch_check = path .git_cwd (
926+ { " git" , " show-ref" , " --verify" , " --quiet" , " refs/heads/" .. branch }, opts )
927+ local _ , check_rc = utils .io_systemlist (cmd_branch_check )
918928
919929 local cmd_add
920930 if check_rc == 0 then
@@ -925,7 +935,7 @@ M.git_worktree_add = function(selected, opts)
925935
926936 local output , rc = utils .io_systemlist (cmd_add )
927937 if rc ~= 0 then
928- utils .error (output and unpack ( output ) or " git worktree add failed " )
938+ utils .error (" git worktree add failed, %s " , output and output [ # output ] or " nil " )
929939 else
930940 utils .info (" Created worktree '%s' at '%s'." , branch , worktree_path )
931941 end
0 commit comments