Skip to content

Commit

Permalink
feat: Add option to select base branch with telescope
Browse files Browse the repository at this point in the history
  • Loading branch information
Juksuu committed Nov 22, 2021
1 parent 82be462 commit a7a9208
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 30 deletions.
30 changes: 30 additions & 0 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,43 @@ local function has_branch(branch, cb)
:start()
end

-- Has branch function to use outside of this file
-- Using the existing one did not work for some reason, got weird erros :)
M.has_branch = function(branch)
local found = false
Job
:new({
"git",
"branch",
on_stdout = function(_, data)
-- remove marker on current branch
data = data:gsub("*", "")
data = vim.trim(data)
found = found or data == branch
end,
cwd = git_worktree_root,
})
:sync()

return found
end

local function create_worktree(
path,
branch,
upstream,
found_branch,
base_branch
)
has_branch(base_branch, function(found)
if not found then
status:status(
"Valid base branch was not defined, using current worktree"
)
base_branch = nil
end
end)

local current_branch_job = Job:new({
"git",
"branch",
Expand Down
75 changes: 45 additions & 30 deletions lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,39 @@ local delete_worktree = function(prompt_bufnr, force)
end
end

local create_input_prompt = function(cb)
--[[
local window = Window.centered({
width = 30,
height = 1
})
vim.api.nvim_buf_set_option(window.bufnr, "buftype", "prompt")
vim.fn.prompt_setprompt(window.bufnr, "Worktree Location: ")
vim.fn.prompt_setcallback(window.bufnr, function(text)
vim.api.nvim_win_close(window.win_id, true)
vim.api.nvim_buf_delete(window.bufnr, {force = true})
cb(text)
end)
vim.api.nvim_set_current_win(window.win_id)
vim.fn.schedule(function()
vim.nvim_command("startinsert")
end)
--]]
--

local subtree = vim.fn.input("Path to subtree > ")
cb(subtree)
local create_input_prompt = function()
return vim.fn.input("Path to subtree > ")
end

local use_current_worktree_as_base_prompt = function()
return vim.fn.confirm("Use current worktree as base?", "&Yes\n&No", 1) == 1
end

local get_base_branch = function(opts, name, branch)
local base_branch_selection_opts = opts or {}
base_branch_selection_opts.attach_mappings = function()
actions.select_default:replace(function(prompt_bufnr, _)
local selected_entry = action_state.get_selected_entry()
local current_line = action_state.get_current_line()

actions.close(prompt_bufnr)

local base_branch = selected_entry ~= nil and selected_entry.value
or current_line

git_worktree.create_worktree(name, branch, nil, base_branch)
end)

-- do we need to replace other default maps?

return true
end
require("telescope.builtin").git_branches(base_branch_selection_opts)
end

local create_worktree = function(opts)
opts = opts or {}
opts.attach_mappings = function()
local branch_selection_opts = opts or {}
branch_selection_opts.attach_mappings = function()
actions.select_default:replace(function(prompt_bufnr, _)
local selected_entry = action_state.get_selected_entry()
local current_line = action_state.get_current_line()
Expand All @@ -72,19 +77,29 @@ local create_worktree = function(opts)
return
end

create_input_prompt(function(name)
if name == "" then
name = branch
local name = create_input_prompt()
if name == "" then
name = branch
end

local has_branch = git_worktree.has_branch(branch)

if not has_branch then
if use_current_worktree_as_base_prompt() then
git_worktree.create_worktree(name, branch)
else
get_base_branch(opts, name, branch)
end
else
git_worktree.create_worktree(name, branch)
end)
end
end)

-- do we need to replace other default maps?

return true
end
require("telescope.builtin").git_branches(opts)
require("telescope.builtin").git_branches(branch_selection_opts)
end

local telescope_git_worktree = function(opts)
Expand Down

0 comments on commit a7a9208

Please sign in to comment.