Skip to content
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
63 changes: 47 additions & 16 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local current_worktree_path = nil
local on_change_callbacks = {}

M.setup_git_info = function()
local cwd = vim.loop.cwd()
local cwd = vim.uv.cwd()

local is_in_worktree = false

Expand Down Expand Up @@ -125,11 +125,43 @@ M.setup_git_info = function()
end

local function on_tree_change_handler(op, metadata)
-- Do nothing if not really changing
if metadata["path"] == metadata["prev_path"] then
return nil
end

if M._config.update_on_change then
if op == Enum.Operations.Switch then
local changed = M.update_current_buffer(metadata["prev_path"])
if not changed then
status:log().debug("Could not change to the file in the new worktree, running the `update_on_change_command`")
-- Try to change the buffer in each window, if a buffer cannot be
-- changed then close that window. If no buffer is left open,
-- then run update_on_change_command
local keep_open = {}
local is_keep_open_empty = true

-- Try to load buffers in each window
for _, win in ipairs(vim.api.nvim_list_wins()) do
local buffer_name = vim.api.nvim_win_call(win, function()
return M.update_current_buffer(metadata["prev_path"])
end)

if buffer_name then
keep_open[buffer_name] = true
is_keep_open_empty = false
end
end

-- Close all buffers that are not in keep_open
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local name = vim.api.nvim_buf_get_name(buf)
if not keep_open[name] then
vim.api.nvim_buf_delete(buf, {})
end
end

if is_keep_open_empty then
status
:log()
.debug("Could not change to the file in the new worktree, running the `update_on_change_command`")
vim.cmd(M._config.update_on_change_command)
end
end
Expand All @@ -150,14 +182,13 @@ local function change_dirs(path)

local previous_worktree = current_worktree_path

-- vim.loop.chdir(worktree_path)
if Path:new(worktree_path):exists() then
local cmd = string.format("%s %s", M._config.change_directory_command, worktree_path)
status:log().debug("Changing to directory " .. worktree_path)
vim.cmd(cmd)
current_worktree_path = worktree_path
else
status:error('Could not chang to directory: ' ..worktree_path)
status:error('Could not change to directory: ' ..worktree_path)
end

if M._config.clearjumps_on_change then
Expand Down Expand Up @@ -459,7 +490,7 @@ M.delete_worktree = function(path, force, opts)
opts.on_failure(e)
end

failure(cmd, vim.loop.cwd())(e)
failure(cmd, vim.uv.cwd())(e)
end)
delete:start()
end)
Expand All @@ -475,37 +506,37 @@ end

M.update_current_buffer = function(prev_path)
if prev_path == nil then
return false
return nil
end

local cwd = vim.loop.cwd()
local cwd = vim.uv.cwd()
local current_buf_name = vim.api.nvim_buf_get_name(0)
if not current_buf_name or current_buf_name == "" then
return false
return nil
end

local name = Path:new(current_buf_name):absolute()
local start, fin = string.find(name, cwd..Path.path.sep, 1, true)
if start ~= nil then
return true
return name
end

start, fin = string.find(name, prev_path, 1, true)
if start == nil then
return false
return nil
end

local local_name = name:sub(fin + 2)

local final_path = Path:new({cwd, local_name}):absolute()

if not Path:new(final_path):exists() then
return false
return nil
end

local bufnr = vim.fn.bufnr(final_path, true)
vim.api.nvim_set_current_buf(bufnr)
return true
-- Open file with edit so the buffer is loaded also
vim.cmd("edit " .. final_path)
return vim.api.nvim_buf_get_name(0)
end

M.on_tree_change = function(cb)
Expand Down
2 changes: 1 addition & 1 deletion lua/git-worktree/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

local Path = require("plenary.path")
local path = Path:new(vim.loop.cwd(), "foo", "..", "..")
local path = Path:new(vim.uv.cwd(), "foo", "..", "..")


print(path:absolute())
Expand Down
6 changes: 2 additions & 4 deletions lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,8 @@ local telescope_git_worktree = function(opts)
attach_mappings = function(_, map)
action_set.select:replace(switch_worktree)

map("i", "<c-d>", delete_worktree)
map("n", "<c-d>", delete_worktree)
map("i", "<c-f>", toggle_forced_deletion)
map("n", "<c-f>", toggle_forced_deletion)
map({"i", "n"}, "<C-d>", delete_worktree, { desc = "Delete worktree" })
map({"i", "n"}, "<C-f>", toggle_forced_deletion, { desc = "Force delete worktree" })

return true
end
Expand Down
Loading