Skip to content

Commit 6e1fa15

Browse files
rbmarlierepolarmutex
authored andcommitted
feat: Enable user to get "out" of a worktree
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.
1 parent 3ded2f3 commit 6e1fa15

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

lua/git-worktree/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local M = {}
2323
local Worktree = require('git-worktree.worktree')
2424

2525
--Switch the current worktree
26-
---@param path string
26+
---@param path string?
2727
function M.switch_worktree(path)
2828
Worktree.switch(path)
2929
end

lua/git-worktree/worktree.lua

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ local function get_absolute_path(path)
1414
end
1515

1616
local function change_dirs(path)
17+
if path == nil then
18+
local out = vim.fn.systemlist('git rev-parse --git-common-dir')
19+
if vim.v.shell_error ~= 0 then
20+
Log.error('Could not parse common dir')
21+
return
22+
end
23+
path = out[1]
24+
end
25+
1726
Log.info('changing dirs: %s ', path)
1827
local worktree_path = get_absolute_path(path)
1928
local previous_worktree = vim.loop.cwd()
@@ -25,7 +34,7 @@ local function change_dirs(path)
2534
Log.debug('Changing to directory %s', worktree_path)
2635
vim.cmd(cmd)
2736
else
28-
Log.error('Could not chang to directory: %s', worktree_path)
37+
Log.error('Could not change to directory: %s', worktree_path)
2938
end
3039

3140
if Config.clearjumps_on_change then
@@ -60,24 +69,31 @@ local M = {}
6069
--- SWITCH ---
6170

6271
--Switch the current worktree
63-
---@param path string
72+
---@param path string?
6473
function M.switch(path)
65-
Git.has_worktree(path, function(found)
66-
Log.debug('test')
67-
if not found then
68-
Log.error('worktree does not exists, please create it first %s ', path)
74+
if path == nil then
75+
change_dirs(path)
76+
-- TODO: do we need to send an event when getting out of a tree?
77+
-- vim.schedule(function()
78+
-- local prev_path = change_dirs(path)
79+
-- Hooks.emit(Hooks.type.SWITCH, path, prev_path)
80+
-- end)
81+
else
82+
if path == vim.loop.cwd() then
83+
return
6984
end
7085
Git.has_worktree(path, nil, function(found)
7186
if not found then
7287
Log.error('Worktree does not exists, please create it first %s ', path)
7388
return
7489
end
7590

76-
vim.schedule(function()
77-
local prev_path = change_dirs(path)
78-
Hooks.emit(Hooks.type.SWITCH, path, prev_path)
91+
vim.schedule(function()
92+
local prev_path = change_dirs(path)
93+
Hooks.emit(Hooks.type.SWITCH, path, prev_path)
94+
end)
7995
end)
80-
end)
96+
end
8197
end
8298

8399
--- CREATE ---

lua/telescope/_extensions/git_worktree.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ local force_next_deletion = false
1616
-- @return string: the path of the selected worktree
1717
local get_worktree_path = function(prompt_bufnr)
1818
local selection = action_state.get_selected_entry(prompt_bufnr)
19+
if selection == nil then
20+
return
21+
end
1922
return selection.path
2023
end
2124

@@ -25,9 +28,11 @@ end
2528
local switch_worktree = function(prompt_bufnr)
2629
local worktree_path = get_worktree_path(prompt_bufnr)
2730
actions.close(prompt_bufnr)
28-
if worktree_path ~= nil then
29-
git_worktree.switch_worktree(worktree_path)
31+
if worktree_path == nil then
32+
vim.print("No worktree selected")
33+
return
3034
end
35+
git_worktree.switch_worktree(worktree_path)
3136
end
3237

3338
-- Toggle the forced deletion of the next worktree
@@ -93,6 +98,8 @@ local delete_worktree = function(prompt_bufnr)
9398
return
9499
end
95100

101+
git_worktree.switch_worktree(nil)
102+
96103
local worktree_path = get_worktree_path(prompt_bufnr)
97104
actions.close(prompt_bufnr)
98105
if worktree_path ~= nil then
@@ -139,6 +146,8 @@ end
139146
-- @param opts table: the options for the telescope picker (optional)
140147
-- @return nil
141148
local create_worktree = function(opts)
149+
git_worktree.switch_worktree(nil)
150+
142151
opts = opts or {}
143152
-- TODO: Parse this as an user option.
144153
-- opts.pattern = 'refs/heads' -- only show local branches

0 commit comments

Comments
 (0)