Skip to content

feat: enable merge-base option when compare working tree with specific ref #590

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion lua/diffview/vcs/adapters/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ function GitAdapter:diffview_options(argo)
local left, right = self:parse_revs(rev_arg, {
cached = argo:get_flag({ "cached", "staged" }),
imply_local = argo:get_flag("imply-local"),
merge_base = argo:get_flag("merge-base"),
})

if not (left and right) then
Expand Down Expand Up @@ -1445,10 +1446,26 @@ function GitAdapter:parse_revs(rev_arg, opt)
end
else
local hash = rev_strings[1]:gsub("^%^", "")
left = GitRev(RevType.COMMIT, hash)
if opt.cached then
left = GitRev(RevType.COMMIT, hash)
right = GitRev(RevType.STAGE, 0)
else
-- When comparing a single ref with working tree, optionally use merge-base
if opt.merge_base then
local merge_base_out, merge_base_code = self:exec_sync(
{ "merge-base", "HEAD", hash },
{ cwd = self.ctx.toplevel, fail_on_empty = true, retry = 2 }
)
if merge_base_code == 0 and #merge_base_out > 0 then
-- Use merge-base as the left side
left = GitRev(RevType.COMMIT, merge_base_out[1])
else
-- Fallback to the ref itself if merge-base fails
left = GitRev(RevType.COMMIT, hash)
end
else
left = GitRev(RevType.COMMIT, hash)
end
right = GitRev(RevType.LOCAL)
end
end
Expand Down Expand Up @@ -2120,6 +2137,7 @@ function GitAdapter:init_completion()
self.comp.open:put({ "u", "untracked-files" }, { "true", "normal", "all", "false", "no" })
self.comp.open:put({ "cached", "staged" })
self.comp.open:put({ "imply-local" })
self.comp.open:put({ "merge-base" })
self.comp.open:put({ "C" }, function(_, arg_lead)
return vim.fn.getcompletion(arg_lead, "dir")
end)
Expand Down