Skip to content

Commit a7a9208

Browse files
committed
feat: Add option to select base branch with telescope
1 parent 82be462 commit a7a9208

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
@@ -327,13 +327,43 @@ local function has_branch(branch, cb)
327327
:start()
328328
end
329329

330+
-- Has branch function to use outside of this file
331+
-- Using the existing one did not work for some reason, got weird erros :)
332+
M.has_branch = function(branch)
333+
local found = false
334+
Job
335+
:new({
336+
"git",
337+
"branch",
338+
on_stdout = function(_, data)
339+
-- remove marker on current branch
340+
data = data:gsub("*", "")
341+
data = vim.trim(data)
342+
found = found or data == branch
343+
end,
344+
cwd = git_worktree_root,
345+
})
346+
:sync()
347+
348+
return found
349+
end
350+
330351
local function create_worktree(
331352
path,
332353
branch,
333354
upstream,
334355
found_branch,
335356
base_branch
336357
)
358+
has_branch(base_branch, function(found)
359+
if not found then
360+
status:status(
361+
"Valid base branch was not defined, using current worktree"
362+
)
363+
base_branch = nil
364+
end
365+
end)
366+
337367
local current_branch_job = Job:new({
338368
"git",
339369
"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)