@@ -31,34 +31,39 @@ local delete_worktree = function(prompt_bufnr, force)
3131 end
3232end
3333
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 )
5762end
5863
5964local 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 ()
6267 actions .select_default :replace (function (prompt_bufnr , _ )
6368 local selected_entry = action_state .get_selected_entry ()
6469 local current_line = action_state .get_current_line ()
@@ -72,19 +77,29 @@ local create_worktree = function(opts)
7277 return
7378 end
7479
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 )
7892 end
93+ else
7994 git_worktree .create_worktree (name , branch )
80- end )
95+ end
8196 end )
8297
8398 -- do we need to replace other default maps?
8499
85100 return true
86101 end
87- require (" telescope.builtin" ).git_branches (opts )
102+ require (" telescope.builtin" ).git_branches (branch_selection_opts )
88103end
89104
90105local telescope_git_worktree = function (opts )
0 commit comments