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

fix: Stop using "local/" for upstream tracking disambiguation #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/git-worktree/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function M.create_worktree_job(path, branch, found_branch, upstream, found_upstr
table.insert(worktree_add_args, branch)
table.insert(worktree_add_args, path)

if found_upstream and branch ~= upstream then
if found_upstream then
table.insert(worktree_add_args, '--track')
table.insert(worktree_add_args, upstream)
end
Expand Down
65 changes: 26 additions & 39 deletions lua/git-worktree/worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ end
---@param branch string
---@param upstream? string
function M.create(path, branch, upstream)
-- if upstream == nil then
-- if Git.has_origin() then
-- upstream = 'origin'
-- end
-- end

-- M.setup_git_info()
local schedule = function(_path, _branch, _found_branch, _upstream, _found_upstream)
local create_wt_job = Git.create_worktree_job(_path, _branch, _found_branch, _upstream, _found_upstream)
create_wt_job:after_success(function()
vim.schedule(function()
Hooks.emit(Hooks.type.CREATE, path, branch, upstream)
M.switch(path)
end)
end)
create_wt_job:start()
end

Git.has_worktree(path, branch, function(found)
if found then
Expand All @@ -115,44 +118,28 @@ function M.create(path, branch, upstream)

if branch == '' then
-- detached head
local create_wt_job = Git.create_worktree_job(path, nil, false, nil, false)
create_wt_job:after(function()
vim.schedule(function()
Hooks.emit(Hooks.type.CREATE, path, branch, upstream)
M.switch(path)
end)
schedule(path, nil, false, nil, false)
return
end

if upstream == nil then
Git.has_branch(branch, nil, function(found_branch)
schedule(path, branch, found_branch, nil, false)
end)
create_wt_job:start()
return
end

Git.has_branch(branch, { '--remotes' }, function(found_remote_branch)
Log.debug('Found remote branch %s? %s', branch, found_remote_branch)
if found_remote_branch then
upstream = branch
branch = 'local/' .. branch
Git.has_branch(upstream, { '--all' }, function(found_upstream)
if found_upstream and branch == upstream then
-- if a remote branch, default to `basename $branch` like git does
branch = branch:match('([^/]+)$')
end
Git.has_branch(branch, nil, function(found_branch)
Log.debug('Found branch %s? %s', branch, found_branch)
Git.has_branch(upstream, { '--all' }, function(found_upstream)
Log.debug('Found upstream %s? %s', upstream, found_upstream)

local create_wt_job = Git.create_worktree_job(path, branch, found_branch, upstream, found_upstream)

if found_branch and found_upstream and branch ~= upstream then
local set_remote = Git.setbranch_job(path, branch, upstream)
create_wt_job:and_then_on_success(set_remote)
end

create_wt_job:after(function()
vim.schedule(function()
Hooks.emit(Hooks.type.CREATE, path, branch, upstream)
M.switch(path)
end)
end)

create_wt_job:start()
end)
if found_upstream and found_branch then
Log.error('Branch "%s" already exists', branch)
return
end
schedule(path, branch, found_branch, upstream, found_upstream)
end)
end)
end)
Expand Down
5 changes: 4 additions & 1 deletion lua/telescope/_extensions/git_worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ local create_input_prompt = function(opts, cb)
local re = string.format('git branch --remotes --list %s', opts.branch)
local remote_branch = vim.fn.systemlist(re)
if #remote_branch == 1 then
cb(path, nil)
cb(path, opts.branch)
return
end

Expand Down Expand Up @@ -191,6 +191,9 @@ local telescope_create_worktree = function(opts)
-- if current_line is still not enough to filter everything but user
-- still wants to use it as the new branch name, without selecting anything
local branch = action_state.get_current_line()
-- if branch == "" then
-- TODO:detached head should list tags
-- end
actions.close(prompt_bufnr)
opts.branch = branch
create_input_prompt(opts, function(path, upstream)
Expand Down
Loading