Skip to content

Commit 95a9e75

Browse files
committed
feat: Add option to select base branch with telescope
1 parent 0e11330 commit 95a9e75

File tree

2 files changed

+75
-30
lines changed

2 files changed

+75
-30
lines changed

lua/git-worktree/init.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,43 @@ local function has_branch(branch, cb)
307307
:start()
308308
end
309309

310+
-- Has branch function to use outside of this file
311+
-- Using the existing one did not work for some reason, got weird erros :)
312+
M.has_branch = function(branch)
313+
local found = false
314+
Job
315+
:new({
316+
"git",
317+
"branch",
318+
on_stdout = function(_, data)
319+
-- remove marker on current branch
320+
data = data:gsub("*", "")
321+
data = vim.trim(data)
322+
found = found or data == branch
323+
end,
324+
cwd = git_worktree_root,
325+
})
326+
:sync()
327+
328+
return found
329+
end
330+
310331
local function create_worktree(
311332
path,
312333
branch,
313334
upstream,
314335
found_branch,
315336
base_branch
316337
)
338+
has_branch(base_branch, function(found)
339+
if not found then
340+
status:status(
341+
"Valid base branch was not defined, using current worktree"
342+
)
343+
base_branch = nil
344+
end
345+
end)
346+
317347
local current_branch_job = Job:new({
318348
"git",
319349
"branch",

lua/telescope/_extensions/git_worktree.lua

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,39 @@ local delete_worktree = function(prompt_bufnr, force)
3131
end
3232
end
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)
5762
end
5863

5964
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()
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)
88103
end
89104

90105
local telescope_git_worktree = function(opts)

0 commit comments

Comments
 (0)