Skip to content

Commit

Permalink
fix: Handle rev ranges that resolve to LOCAL..{REV} (#436)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre T. Strøm <[email protected]>
  • Loading branch information
refractalize and sindrets authored Nov 20, 2023
1 parent d38c1b5 commit 3dc498c
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions lua/diffview/vcs/adapters/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1478,18 +1478,33 @@ end
function GitAdapter:rev_to_args(left, right)
assert(
not (left.type == RevType.LOCAL and right.type == RevType.LOCAL),
"Can't diff LOCAL against LOCAL!"
"InvalidArgument :: Can't diff LOCAL against LOCAL!"
)

if left.type == RevType.COMMIT and right.type == RevType.COMMIT then
return { left.commit .. ".." .. right.commit }
elseif left.type == RevType.STAGE and right.type == RevType.LOCAL then
return {}

elseif left.type == RevType.COMMIT and right.type == RevType.STAGE then
return { "--cached", left.commit }
else
return { left.commit }

elseif right.type == RevType.LOCAL then
if left.type == RevType.STAGE then
return {}
elseif left.type == RevType.COMMIT then
return { left.commit }
end

elseif left.type == RevType.LOCAL then
-- WARN: these require special handling when creating the diff file list.
-- I.e. the stats for 'additions' and 'deletions' need to be swapped.
if right.type == RevType.STAGE then
return { "--cached" }
elseif right.type == RevType.COMMIT then
return { right.commit }
end
end

error(fmt("InvalidArgument :: Unsupported rev range: '%s..%s'!", left, right))
end


Expand Down Expand Up @@ -1790,7 +1805,7 @@ GitAdapter.tracked_files = async.wrap(function(self, left, right, args, kind, op
end

for _, v in ipairs(data) do
table.insert(files, FileEntry.with_layout(opt.default_layout, {
local file = FileEntry.with_layout(opt.default_layout, {
adapter = self,
path = v.name,
oldpath = v.oldname,
Expand All @@ -1801,7 +1816,17 @@ GitAdapter.tracked_files = async.wrap(function(self, left, right, args, kind, op
a = left,
b = right,
}
}))
})

if left and left.type == RevType.LOCAL then
-- Special handling is required here. The rev range `LOCAL..{REV}` can't be
-- expressed in Git's rev syntax, but logically it should be the same as
-- just `{REV}`, but with the diff stats swapped (as we want the diff from
-- the perspective of LOCAL).
file.stats.additions, file.stats.deletions = file.stats.deletions, file.stats.additions
end

table.insert(files, file)
end

callback(nil, files, conflicts)
Expand Down

0 comments on commit 3dc498c

Please sign in to comment.