Skip to content

Commit c58e31e

Browse files
rbmarlierepolarmutex
authored andcommitted
fix: Disallow nil passing to create_dirs()
To tackle deletion and creation path issues, commit 6e1fa15 ("feat: Enable user to get "out" of a worktree") added the possibility of passing nil to change_dirs(). But the problems can be solved in a better way: When deleting, check if the selection is the current tree then change dir to gitroot_dir() if so. When creating, add gitroot_dir() as prefix.
1 parent 4197927 commit c58e31e

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed

lua/git-worktree/worktree.lua

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ local function get_absolute_path(path)
1414
end
1515

1616
local function change_dirs(path)
17-
if path == nil then
18-
local out = vim.fn.systemlist('git rev-parse --git-common-dir')
19-
if vim.v.shell_error ~= 0 then
20-
Log.error('Could not parse common dir')
21-
return
22-
end
23-
path = out[1]
24-
end
25-
2617
Log.info('changing dirs: %s ', path)
2718
local worktree_path = get_absolute_path(path)
2819
local previous_worktree = vim.loop.cwd()
@@ -72,24 +63,20 @@ local M = {}
7263
--Switch the current worktree
7364
---@param path string?
7465
function M.switch(path)
75-
if path == nil then
76-
change_dirs(path)
77-
else
78-
if path == vim.loop.cwd() then
66+
if path == vim.loop.cwd() then
67+
return
68+
end
69+
Git.has_worktree(path, nil, function(found)
70+
if not found then
71+
Log.error('Worktree does not exists, please create it first %s ', path)
7972
return
8073
end
81-
Git.has_worktree(path, nil, function(found)
82-
if not found then
83-
Log.error('Worktree does not exists, please create it first %s ', path)
84-
return
85-
end
8674

87-
vim.schedule(function()
88-
local prev_path = change_dirs(path)
89-
Hooks.emit(Hooks.type.SWITCH, path, prev_path)
90-
end)
75+
vim.schedule(function()
76+
local prev_path = change_dirs(path)
77+
Hooks.emit(Hooks.type.SWITCH, path, prev_path)
9178
end)
92-
end
79+
end)
9380
end
9481

9582
--- CREATE ---

lua/telescope/_extensions/git_worktree.lua

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ local force_next_deletion = false
1818
-- @return string: the path of the selected worktree
1919
local get_worktree_path = function(prompt_bufnr)
2020
local selection = action_state.get_selected_entry(prompt_bufnr)
21-
if selection == nil then
22-
return
23-
end
2421
return selection.path
2522
end
2623

@@ -118,12 +115,14 @@ local delete_worktree = function(prompt_bufnr)
118115
return
119116
end
120117

121-
git_worktree.switch_worktree(nil)
122-
123-
local worktree_path = get_worktree_path(prompt_bufnr)
118+
local selected = get_worktree_path(prompt_bufnr)
124119
actions.close(prompt_bufnr)
125-
if worktree_path ~= nil then
126-
git_worktree.delete_worktree(worktree_path, force_next_deletion, {
120+
if selected ~= nil then
121+
local current = vim.loop.cwd()
122+
if current == selected then
123+
git_worktree.switch_worktree(Git.gitroot_dir())
124+
end
125+
git_worktree.delete_worktree(selected, force_next_deletion, {
127126
on_failure = delete_failure_handler,
128127
on_success = delete_success_handler,
129128
})
@@ -137,8 +136,7 @@ local create_input_prompt = function(opts, cb)
137136
opts = opts or {}
138137
opts.pattern = nil -- show all branches that can be tracked
139138

140-
local prefix = opts.prefix or ''
141-
local path = vim.fn.input('Path to subtree > ', prefix .. opts.branch)
139+
local path = vim.fn.input('Path to subtree > ', Git.gitroot_dir() .. '/' .. opts.branch)
142140
if path == '' then
143141
Log.error('No worktree path provided')
144142
return
@@ -184,7 +182,6 @@ end
184182
-- @param opts table: the options for the telescope picker (optional)
185183
-- @return nil
186184
local telescope_create_worktree = function(opts)
187-
git_worktree.switch_worktree(nil)
188185
opts = opts or {}
189186

190187
local create_branch = function(prompt_bufnr, _)

0 commit comments

Comments
 (0)