diff --git a/lua/git-worktree/git.lua b/lua/git-worktree/git.lua index c161fad..c629a55 100644 --- a/lua/git-worktree/git.lua +++ b/lua/git-worktree/git.lua @@ -108,11 +108,17 @@ function M.toplevel_dir() return table.concat(stdout, '') end -function M.has_branch(branch, cb) +function M.has_branch(branch, remotes, cb) local found = false + + local args = { 'branch' } + if remotes then + table.insert(args, '--remotes') + end + local job = Job:new { command = 'git', - args = { 'branch' }, + args = args, on_stdout = function(_, data) -- remove markere on current branch data = data:gsub('*', '') @@ -131,8 +137,10 @@ end --- @param path string --- @param branch string --- @param found_branch boolean +--- @param upstream string +--- @param found_upstream boolean --- @return Job -function M.create_worktree_job(path, branch, found_branch) +function M.create_worktree_job(path, branch, found_branch, upstream, found_upstream) local worktree_add_cmd = 'git' local worktree_add_args = { 'worktree', 'add' } @@ -145,6 +153,11 @@ function M.create_worktree_job(path, branch, found_branch) table.insert(worktree_add_args, branch) end + if found_upstream then + table.insert(worktree_add_args, '--track') + table.insert(worktree_add_args, upstream) + end + return Job:new { command = worktree_add_cmd, args = worktree_add_args, @@ -195,7 +208,7 @@ end --- @return Job function M.setbranch_job(path, branch, upstream) local set_branch_cmd = 'git' - local set_branch_args = { 'branch', string.format('--set-upstream-to=%s/%s', upstream, branch) } + local set_branch_args = { 'branch', branch, string.format('--set-upstream-to=%s', upstream) } return Job:new { command = set_branch_cmd, args = set_branch_args, diff --git a/lua/git-worktree/worktree.lua b/lua/git-worktree/worktree.lua index 4e85288..eae8c52 100644 --- a/lua/git-worktree/worktree.lua +++ b/lua/git-worktree/worktree.lua @@ -78,7 +78,7 @@ end --- CREATE --- ---crerate a worktree +--create a worktree ---@param path string ---@param branch string ---@param upstream? string @@ -97,7 +97,7 @@ function M.create(path, branch, upstream) return end - Git.has_branch(branch, function(found_branch) + Git.has_branch(branch, false, function(found_branch) Config = require('git-worktree.config') local worktree_path if Path:new(path):is_absolute() then @@ -106,52 +106,18 @@ function M.create(path, branch, upstream) worktree_path = Path:new(vim.loop.cwd(), path):absolute() end - -- create_worktree(path, branch, upstream, found_branch) - local create_wt_job = Git.create_worktree_job(path, branch, found_branch) + Git.has_branch(upstream, true, function(found_upstream) + local create_wt_job = Git.create_worktree_job(path, branch, found_branch, upstream, found_upstream) - if upstream ~= nil then - local fetch = Git.fetchall_job(path, branch, upstream) - local set_branch = Git.setbranch_job(path, branch, upstream) - local set_push = Git.setpush_job(path, branch, upstream) - local rebase = Git.rebase_job(path) - - create_wt_job:and_then_on_success(fetch) - fetch:and_then_on_success(set_branch) - - if Config.autopush then - -- These are "optional" operations. - -- We have to figure out how we want to handle these... - set_branch:and_then(set_push) - set_push:and_then(rebase) - set_push:after_failure(failure('create_worktree', set_branch.args, worktree_path, true)) - else - set_branch:and_then(rebase) - end - - create_wt_job:after_failure(failure('create_worktree', create_wt_job.args, vim.loop.cwd())) - fetch:after_failure(failure('create_worktree', fetch.args, worktree_path)) - - set_branch:after_failure(failure('create_worktree', set_branch.args, worktree_path, true)) - - rebase:after(function() - if rebase.code ~= 0 then - Log.devel("Rebase failed, but that's ok.") - end - - vim.schedule(function() - Hooks.emit(Hooks.type.CREATE, path, branch, upstream) - M.switch(path) - end) - end) - else create_wt_job:after(function() vim.schedule(function() Hooks.emit(Hooks.type.CREATE, path, branch, upstream) M.switch(path) end) end) - end - create_wt_job:start() + + create_wt_job:start() + end) end) end) end diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index d3e2137..6303c7a 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -7,6 +7,7 @@ local action_set = require('telescope.actions.set') local action_state = require('telescope.actions.state') local conf = require('telescope.config').values local git_worktree = require('git-worktree') +local Config = require('git-worktree.config') local force_next_deletion = false @@ -70,7 +71,7 @@ end -- @param forcing boolean: whether the deletion is forced -- @return boolean: whether the deletion is confirmed local confirm_deletion = function(forcing) - if not git_worktree._config.confirm_telescope_deletions then + if not Config.confirm_telescope_deletions then return true end @@ -107,13 +108,14 @@ end -- @return nil local create_input_prompt = function(cb) local subtree = vim.fn.input('Path to subtree > ') - cb(subtree) + local upstream = vim.fn.input('Upstream > ') + cb(subtree, upstream) end -- Create a worktree -- @param opts table: the options for the telescope picker (optional) -- @return nil -local create_worktree = function(opts) +local telescope_create_worktree = function(opts) opts = opts or {} opts.attach_mappings = function() actions.select_default:replace(function(prompt_bufnr, _) @@ -128,11 +130,14 @@ local create_worktree = function(opts) return end - create_input_prompt(function(name) + create_input_prompt(function(name, upstream) if name == '' then name = branch end - git_worktree.create_worktree(name, branch) + if upstream == '' then + upstream = branch + end + git_worktree.create_worktree(name, branch, upstream) end) end) @@ -234,6 +239,6 @@ end return require('telescope').register_extension { exports = { git_worktree = telescope_git_worktree, - create_git_worktree = create_worktree, + create_git_worktree = telescope_create_worktree, }, }