@@ -31,34 +31,39 @@ local delete_worktree = function(prompt_bufnr, force)
31
31
end
32
32
end
33
33
34
- local create_input_prompt = function (cb )
35
- --[[
36
- local window = Window.centered({
37
- width = 30,
38
- height = 1
39
- })
40
- vim.api.nvim_buf_set_option(window.bufnr, "buftype", "prompt")
41
- vim.fn.prompt_setprompt(window.bufnr, "Worktree Location: ")
42
- vim.fn.prompt_setcallback(window.bufnr, function(text)
43
- vim.api.nvim_win_close(window.win_id, true)
44
- vim.api.nvim_buf_delete(window.bufnr, {force = true})
45
- cb(text)
46
- end)
47
-
48
- vim.api.nvim_set_current_win(window.win_id)
49
- vim.fn.schedule(function()
50
- vim.nvim_command("startinsert")
51
- end)
52
- --]]
53
- --
54
-
55
- local subtree = vim .fn .input (" Path to subtree > " )
56
- cb (subtree )
34
+ local create_input_prompt = function ()
35
+ return vim .fn .input (" Path to subtree > " )
36
+ end
37
+
38
+ local use_current_worktree_as_base_prompt = function ()
39
+ return vim .fn .confirm (" Use current worktree as base?" , " &Yes\n &No" , 1 ) == 1
40
+ end
41
+
42
+ local get_base_branch = function (opts , name , branch )
43
+ local base_branch_selection_opts = opts or {}
44
+ base_branch_selection_opts .attach_mappings = function ()
45
+ actions .select_default :replace (function (prompt_bufnr , _ )
46
+ local selected_entry = action_state .get_selected_entry ()
47
+ local current_line = action_state .get_current_line ()
48
+
49
+ actions .close (prompt_bufnr )
50
+
51
+ local base_branch = selected_entry ~= nil and selected_entry .value
52
+ or current_line
53
+
54
+ git_worktree .create_worktree (name , branch , nil , base_branch )
55
+ end )
56
+
57
+ -- do we need to replace other default maps?
58
+
59
+ return true
60
+ end
61
+ require (" telescope.builtin" ).git_branches (base_branch_selection_opts )
57
62
end
58
63
59
64
local create_worktree = function (opts )
60
- opts = opts or {}
61
- opts .attach_mappings = function ()
65
+ local branch_selection_opts = opts or {}
66
+ branch_selection_opts .attach_mappings = function ()
62
67
actions .select_default :replace (function (prompt_bufnr , _ )
63
68
local selected_entry = action_state .get_selected_entry ()
64
69
local current_line = action_state .get_current_line ()
@@ -72,19 +77,29 @@ local create_worktree = function(opts)
72
77
return
73
78
end
74
79
75
- create_input_prompt (function (name )
76
- if name == " " then
77
- name = branch
80
+ local name = create_input_prompt ()
81
+ if name == " " then
82
+ name = branch
83
+ end
84
+
85
+ local has_branch = git_worktree .has_branch (branch )
86
+
87
+ if not has_branch then
88
+ if use_current_worktree_as_base_prompt () then
89
+ git_worktree .create_worktree (name , branch )
90
+ else
91
+ get_base_branch (opts , name , branch )
78
92
end
93
+ else
79
94
git_worktree .create_worktree (name , branch )
80
- end )
95
+ end
81
96
end )
82
97
83
98
-- do we need to replace other default maps?
84
99
85
100
return true
86
101
end
87
- require (" telescope.builtin" ).git_branches (opts )
102
+ require (" telescope.builtin" ).git_branches (branch_selection_opts )
88
103
end
89
104
90
105
local telescope_git_worktree = function (opts )
0 commit comments