Skip to content

Commit db45bab

Browse files
committed
fix(action): git_switch fallback to worktree cd
1 parent e114b7f commit db45bab

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lua/fzf-lua/actions.lua

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lua/fzf-lua/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,8 @@ M._action_to_helpstr = {
10821082
[actions.git_branch_del] = "git-branch-del",
10831083
[actions.git_switch] = "git-switch",
10841084
[actions.git_worktree_cd] = "change-directory",
1085+
[actions.git_worktree_add] = "git-worktree-add",
1086+
[actions.git_worktree_del] = "git-worktree-del",
10851087
[actions.git_checkout] = "git-checkout",
10861088
[actions.git_reset] = "git-reset",
10871089
[actions.git_stage] = "git-stage",

0 commit comments

Comments
 (0)