From 595d1aca592daf2e0fd6b23ac9f8ba6b6a4f9f91 Mon Sep 17 00:00:00 2001 From: "Ricardo B. Marliere" Date: Sun, 21 Jul 2024 08:47:22 -0300 Subject: [PATCH 1/7] fix: make setbranch_job more flexible towards --set-upstream-to User should be able to define any upstream string using --set-upstream-to. --- lua/git-worktree/git.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/git-worktree/git.lua b/lua/git-worktree/git.lua index c161fad..bff4d2b 100644 --- a/lua/git-worktree/git.lua +++ b/lua/git-worktree/git.lua @@ -195,7 +195,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, From 6601378cf799e3cc82d3ff8cdcf4191bde7706d5 Mon Sep 17 00:00:00 2001 From: "Ricardo B. Marliere" Date: Sun, 21 Jul 2024 08:50:45 -0300 Subject: [PATCH 2/7] feat: add ability of specifying upstream in Telescope extension --- lua/telescope/_extensions/git_worktree.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index d3e2137..16fecc7 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -107,7 +107,8 @@ 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 @@ -128,11 +129,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) From 33765f29cea23a43e188f9274e80de128617594a Mon Sep 17 00:00:00 2001 From: "Ricardo B. Marliere" Date: Sun, 21 Jul 2024 08:51:41 -0300 Subject: [PATCH 3/7] fix: minor spelling mistake in worktree.lua --- lua/git-worktree/worktree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/git-worktree/worktree.lua b/lua/git-worktree/worktree.lua index 4e85288..dd5804a 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 From a8ffda84d9f82dfc7b1abd9fb0b604c45d47c478 Mon Sep 17 00:00:00 2001 From: "Ricardo B. Marliere" Date: Sun, 21 Jul 2024 08:53:46 -0300 Subject: [PATCH 4/7] fix: rename create_worktree to telescope_create_worktree --- lua/telescope/_extensions/git_worktree.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index 16fecc7..d9a7ba9 100644 --- a/lua/telescope/_extensions/git_worktree.lua +++ b/lua/telescope/_extensions/git_worktree.lua @@ -114,7 +114,7 @@ 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, _) @@ -238,6 +238,6 @@ end return require('telescope').register_extension { exports = { git_worktree = telescope_git_worktree, - create_git_worktree = create_worktree, + create_git_worktree = telescope_create_worktree, }, } From 93446362a4d623c49ac34b61ffd8c736a5f9fa27 Mon Sep 17 00:00:00 2001 From: "Ricardo B. Marliere" Date: Sun, 21 Jul 2024 08:52:12 -0300 Subject: [PATCH 5/7] fix: unknown reference to Log.devel() --- lua/git-worktree/worktree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/git-worktree/worktree.lua b/lua/git-worktree/worktree.lua index dd5804a..1229298 100644 --- a/lua/git-worktree/worktree.lua +++ b/lua/git-worktree/worktree.lua @@ -135,7 +135,7 @@ function M.create(path, branch, upstream) rebase:after(function() if rebase.code ~= 0 then - Log.devel("Rebase failed, but that's ok.") + Log.debug("Rebase failed, but that's ok.") end vim.schedule(function() From 6aa50d39935d3a44c3874ab198175fa8a8db0ad4 Mon Sep 17 00:00:00 2001 From: "Ricardo B. Marliere" Date: Sat, 20 Jul 2024 15:59:15 -0300 Subject: [PATCH 6/7] fix: use proper config in confirm_deletion --- lua/telescope/_extensions/git_worktree.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/telescope/_extensions/git_worktree.lua b/lua/telescope/_extensions/git_worktree.lua index d3e2137..2842875 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 From 764a57aa1711af457eff9278c869b9c26fc5318e Mon Sep 17 00:00:00 2001 From: "Ricardo B. Marliere" Date: Sun, 21 Jul 2024 14:11:42 -0300 Subject: [PATCH 7/7] feat: set --track when creating worktree, if there is an upstream --- lua/git-worktree/git.lua | 19 ++++++++++++--- lua/git-worktree/worktree.lua | 46 +++++------------------------------ 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/lua/git-worktree/git.lua b/lua/git-worktree/git.lua index bff4d2b..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, diff --git a/lua/git-worktree/worktree.lua b/lua/git-worktree/worktree.lua index 1229298..eae8c52 100644 --- a/lua/git-worktree/worktree.lua +++ b/lua/git-worktree/worktree.lua @@ -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.debug("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