Skip to content

Commit

Permalink
feat: Enable user to get "out" of a worktree
Browse files Browse the repository at this point in the history
This is especially useful when creating or deleting a worktree, to avoid
deleting errors if the user is within the to-be deleted worktree or
unintended nested paths when creating.
  • Loading branch information
rbmarliere authored and polarmutex committed Oct 30, 2024
1 parent 3ded2f3 commit 6e1fa15
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 26 additions & 10 deletions lua/git-worktree/worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -60,24 +69,31 @@ 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
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)
vim.schedule(function()
local prev_path = change_dirs(path)
Hooks.emit(Hooks.type.SWITCH, path, prev_path)
end)
end)
end)
end
end

--- CREATE ---
Expand Down
13 changes: 11 additions & 2 deletions lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6e1fa15

Please sign in to comment.