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

Fixes #38

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
28 changes: 26 additions & 2 deletions lua/git-worktree/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ end

function M.has_branch(branch, opts, cb)
local found = false
local args = { 'branch', '--format=%(refname:short)' }
local args = { 'branch', '--format=%(refname)' }
opts = opts or {}
for _, opt in ipairs(opts) do
args[#args + 1] = opt
Expand All @@ -123,7 +123,8 @@ function M.has_branch(branch, opts, cb)
command = 'git',
args = args,
on_stdout = function(_, data)
found = found or data == branch
local current = data:match('^refs/heads/(.+)') or data:match('^refs/remotes/(.+)')
found = found or current == branch
end,
cwd = vim.loop.cwd(),
}
Expand Down Expand Up @@ -277,6 +278,29 @@ function M.parse_head(path)
return table.concat(stdout, '')
end

--- @param path string
--- @return string|nil
function M.current_branch(path)
local job = Job:new {
command = 'git',
args = { 'branch', '--show-current' },
cwd = path,
on_start = function()
Log.debug('git branch --show-current')
end,
}

local stdout, code = job:sync()
if code ~= 0 then
Log.error(
'Error in getting current branch: code:' .. tostring(code) .. ' out: ' .. table.concat(stdout, '') .. '.'
)
return nil
end

return table.concat(stdout, '')
end

--- @param branch string
--- @return Job|nil
function M.delete_branch_job(branch)
Expand Down
2 changes: 1 addition & 1 deletion lua/git-worktree/worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function M.delete(path, force, opts)
opts = {}
end

local branch = Git.parse_head(path)
local branch = Git.current_branch(path)

Git.has_worktree(path, nil, function(found)
if not found then
Expand Down
Loading