Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Disallow nil passing to create_dirs() #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading