diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index 522c889..4ebd56e 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -23,7 +23,7 @@ local M = {} local Worktree = require('git-worktree.worktree') --Switch the current worktree ----@param path string +---@param path string? function M.switch_worktree(path) Worktree.switch(path) end diff --git a/lua/git-worktree/worktree.lua b/lua/git-worktree/worktree.lua index f6ea30f..f4b2ff2 100644 --- a/lua/git-worktree/worktree.lua +++ b/lua/git-worktree/worktree.lua @@ -14,6 +14,15 @@ 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() @@ -25,7 +34,7 @@ local function change_dirs(path) Log.debug('Changing to directory %s', worktree_path) vim.cmd(cmd) else - Log.error('Could not chang to directory: %s', worktree_path) + Log.error('Could not change to directory: %s', worktree_path) end if Config.clearjumps_on_change then @@ -60,12 +69,18 @@ local M = {} --- SWITCH --- --Switch the current worktree ----@param path string +---@param path string? function M.switch(path) - Git.has_worktree(path, function(found) - Log.debug('test') - if not found then - Log.error('worktree does not exists, please create it first %s ', path) + if path == nil then + change_dirs(path) + -- TODO: do we need to send an event when getting out of a tree? + -- vim.schedule(function() + -- local prev_path = change_dirs(path) + -- Hooks.emit(Hooks.type.SWITCH, path, prev_path) + -- end) + else + if path == vim.loop.cwd() then + return end Git.has_worktree(path, nil, function(found) if not found then @@ -73,11 +88,12 @@ function M.switch(path) return end - vim.schedule(function() - local prev_path = change_dirs(path) - Hooks.emit(Hooks.type.SWITCH, path, prev_path) + vim.schedule(function() + local prev_path = change_dirs(path) + Hooks.emit(Hooks.type.SWITCH, path, prev_path) + end) end) - end) + end end --- CREATE --- diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index 5c55f35..0916349 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -16,6 +16,9 @@ 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 @@ -25,9 +28,11 @@ end local switch_worktree = function(prompt_bufnr) local worktree_path = get_worktree_path(prompt_bufnr) actions.close(prompt_bufnr) - if worktree_path ~= nil then - git_worktree.switch_worktree(worktree_path) + if worktree_path == nil then + vim.print("No worktree selected") + return end + git_worktree.switch_worktree(worktree_path) end -- Toggle the forced deletion of the next worktree @@ -93,6 +98,8 @@ local delete_worktree = function(prompt_bufnr) return end + git_worktree.switch_worktree(nil) + local worktree_path = get_worktree_path(prompt_bufnr) actions.close(prompt_bufnr) if worktree_path ~= nil then @@ -139,6 +146,8 @@ end -- @param opts table: the options for the telescope picker (optional) -- @return nil local create_worktree = function(opts) + git_worktree.switch_worktree(nil) + opts = opts or {} -- TODO: Parse this as an user option. -- opts.pattern = 'refs/heads' -- only show local branches