Files
jj.nvim/lua/jj/diff/codediff.lua
T
Nicolas GBandGitHub 1c3d38a626 feat(diff): Add the diff_history command (#97)
This new command is natively implemented by both the `codediff` and `diffview` backends allowing to easily navigate the changes between two revsets while diffing the. 

It also gives a new `:J diff_hisotyr` command that optionally takes a range of revsets formatted `<rev1>..<rev2>` otherwise it prompts the user for the range.
2026-03-12 18:03:23 +01:00

94 lines
2.2 KiB
Lua

local utils = require("jj.utils")
---@type jj.diff
local diff = require("jj.diff")
--- Givewn two changes, show their diff using codediff
--- @param left string
--- @param right string
local function diff_two_changes(left, right)
local commit_id_left = utils.get_commit_id(left)
if commit_id_left == nil then
return
end
local commit_id_right = utils.get_commit_id(right)
if commit_id_right == nil then
return
end
--- Omit the commit id when left is the current revision,
--- allowing codediff to use the actual file instead of a virtual buffer.
local commit_id_current = utils.get_current_commit_id()
if commit_id_current == commit_id_left then
vim.cmd(string.format("CodeDiff %s", commit_id_right))
return
end
vim.cmd(string.format("CodeDiff %s %s", commit_id_right, commit_id_left))
end
local function diff_two_changes_with_history(left, right)
local commit_id_left = utils.get_commit_id(left)
if commit_id_left == nil then
return
end
local commit_id_right = utils.get_commit_id(right)
if commit_id_right == nil then
return
end
-- test
vim.cmd(string.format("CodeDiff history %s..%s", commit_id_right, commit_id_left))
end
-----------------------------------------------------------------------
-- Codediff Backend
-----------------------------------------------------------------------
diff.register_backend("codediff", {
diff_current = function(opts)
if not utils.has_dependency("codediff") then
return
end
-- Extract the commit id from opts.rev
local revset = opts.rev or "@-"
local commit_id = utils.get_commit_id(revset)
if not commit_id then
return
end
vim.cmd(string.format("CodeDiff file %s", commit_id))
end,
show_revision = function(opts)
if not utils.has_dependency("codediff") then
return
end
-- When comparing a revision we always compare it to it's parent to get the diff
local right = string.format("%s-", opts.rev)
diff_two_changes(opts.rev, right)
end,
diff_revisions = function(opts)
if not utils.has_dependency("codediff") then
return
end
diff_two_changes(opts.left, opts.right)
end,
diff_history_revisions = function(opts)
if not utils.has_dependency("codediff") then
return
end
diff_two_changes_with_history(opts.left, opts.right)
end,
})