Skip to content

Commit

Permalink
fix: Disallow nil passing to create_dirs()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rbmarliere committed Dec 17, 2024
1 parent bac72c2 commit c2ee4b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
33 changes: 10 additions & 23 deletions lua/git-worktree/worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ local function get_absolute_path(path)
end

local function change_dirs(path)
if path == nil then
local out = vim.fn.systemlist('git rev-parse --git-common-dir')
if vim.v.shell_error ~= 0 then
Log.error('Could not parse common dir')
return
end
path = out[1]
end

Log.info('changing dirs: %s ', path)
local worktree_path = get_absolute_path(path)
local previous_worktree = vim.loop.cwd()
Expand Down Expand Up @@ -72,24 +63,20 @@ local M = {}
--Switch the current worktree
---@param path string?
function M.switch(path)
if path == nil then
change_dirs(path)
else
if path == vim.loop.cwd() then
if path == vim.loop.cwd() then
return
end
Git.has_worktree(path, nil, function(found)
if not found then
Log.error('Worktree does not exists, please create it first %s ', path)
return
end
Git.has_worktree(path, nil, function(found)
if not found then
Log.error('Worktree does not exists, please create it first %s ', path)
return
end

vim.schedule(function()
local prev_path = change_dirs(path)
Hooks.emit(Hooks.type.SWITCH, path, prev_path)
end)
vim.schedule(function()
local prev_path = change_dirs(path)
Hooks.emit(Hooks.type.SWITCH, path, prev_path)
end)
end
end)
end

--- CREATE ---
Expand Down
19 changes: 8 additions & 11 deletions lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ local force_next_deletion = false
-- @return string: the path of the selected worktree
local get_worktree_path = function(prompt_bufnr)
local selection = action_state.get_selected_entry(prompt_bufnr)
if selection == nil then
return
end
return selection.path
end

Expand Down Expand Up @@ -118,12 +115,14 @@ local delete_worktree = function(prompt_bufnr)
return
end

git_worktree.switch_worktree(nil)

local worktree_path = get_worktree_path(prompt_bufnr)
local selected = get_worktree_path(prompt_bufnr)
actions.close(prompt_bufnr)
if worktree_path ~= nil then
git_worktree.delete_worktree(worktree_path, force_next_deletion, {
if selected ~= nil then
local current = vim.loop.cwd()
if current == selected then
git_worktree.switch_worktree(Git.gitroot_dir())
end
git_worktree.delete_worktree(selected, force_next_deletion, {
on_failure = delete_failure_handler,
on_success = delete_success_handler,
})
Expand All @@ -137,8 +136,7 @@ local create_input_prompt = function(opts, cb)
opts = opts or {}
opts.pattern = nil -- show all branches that can be tracked

local prefix = opts.prefix or ''
local path = vim.fn.input('Path to subtree > ', prefix .. opts.branch)
local path = vim.fn.input('Path to subtree > ', Git.gitroot_dir() .. '/' .. opts.branch)
if path == '' then
Log.error('No worktree path provided')
return
Expand Down Expand Up @@ -184,7 +182,6 @@ end
-- @param opts table: the options for the telescope picker (optional)
-- @return nil
local telescope_create_worktree = function(opts)
git_worktree.switch_worktree(nil)
opts = opts or {}

local create_branch = function(prompt_bufnr, _)
Expand Down

0 comments on commit c2ee4b3

Please sign in to comment.