Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Set --track when creating a worktree #20

Closed
wants to merge 8 commits into from
19 changes: 16 additions & 3 deletions lua/git-worktree/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('*', '')
Expand All @@ -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' }

Expand All @@ -145,6 +153,11 @@ function M.create_worktree_job(path, branch, found_branch)
table.insert(worktree_add_args, branch)
end

if found_upstream then
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it only makes sense to use --track when the branch does not exist and -b is used in git-worktree add.

table.insert(worktree_add_args, '--track')
table.insert(worktree_add_args, upstream)
end

return Job:new {
command = worktree_add_cmd,
args = worktree_add_args,
Expand Down
46 changes: 6 additions & 40 deletions lua/git-worktree/worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading