mirror of
https://github.com/zoriya/jj.nvim.git
synced 2026-08-15 23:53:18 +00:00
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.
This commit is contained in:
@@ -28,6 +28,21 @@ local function diff_two_changes(left, right)
|
||||
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
|
||||
-----------------------------------------------------------------------
|
||||
@@ -67,4 +82,12 @@ diff.register_backend("codediff", {
|
||||
|
||||
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,
|
||||
})
|
||||
|
||||
@@ -24,6 +24,21 @@ local function diff_two_changes(left, right)
|
||||
vim.cmd(string.format("DiffviewOpen %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("DiffviewFileHistory --range=%s..%s", commit_id_right, commit_id_left))
|
||||
end
|
||||
|
||||
-- Register the diffview backend
|
||||
diff.register_backend("diffview", {
|
||||
diff_current = function(opts)
|
||||
@@ -59,4 +74,11 @@ diff.register_backend("diffview", {
|
||||
|
||||
diff_two_changes(opts.left, opts.right)
|
||||
end,
|
||||
diff_history_revisions = function(opts)
|
||||
if not utils.has_dependency("diffview") then
|
||||
return
|
||||
end
|
||||
|
||||
diff_two_changes_with_history(opts.left, opts.right)
|
||||
end,
|
||||
})
|
||||
|
||||
+12
-1
@@ -28,6 +28,7 @@ local utils = require("jj.utils")
|
||||
---@field diff_current? fun(opts: jj.diff.current_opts)
|
||||
---@field show_revision? fun(opts: jj.diff.revision_opts)
|
||||
---@field diff_revisions? fun(opts: jj.diff.revisions_opts)
|
||||
---@field diff_history_revisions? fun(opts: jj.diff.revisions_opts)
|
||||
|
||||
---@class jj.diff.config
|
||||
---@field backend? jj.diff.backend
|
||||
@@ -99,7 +100,7 @@ end
|
||||
-----------------------------------------------------------------------
|
||||
|
||||
--- Single dispatcher (canonical entry point)
|
||||
---@param kind "current"|"revision"|"revisions"
|
||||
---@param kind "current"|"revision"|"revisions"|"history"
|
||||
---@param opts table
|
||||
function M.open(kind, opts)
|
||||
opts = opts or {}
|
||||
@@ -120,6 +121,10 @@ function M.open(kind, opts)
|
||||
return backend.diff_revisions(opts)
|
||||
end
|
||||
return backends.native.diff_revisions(opts)
|
||||
elseif kind == "history" then
|
||||
if backend.diff_history_revisions then
|
||||
return backend.diff_history_revisions(opts)
|
||||
end
|
||||
else
|
||||
utils.notify("[Diff] unknown diff kind: " .. tostring(kind), vim.log.levels.ERROR)
|
||||
end
|
||||
@@ -143,6 +148,12 @@ function M.diff_revisions(opts)
|
||||
return M.open("revisions", opts)
|
||||
end
|
||||
|
||||
-- Diff between two revisions with history mode active
|
||||
--- @param opts jj.diff.revisions_opts
|
||||
function M.diff_history_revisions(opts)
|
||||
return M.open("history", opts)
|
||||
end
|
||||
|
||||
---
|
||||
-----------------------------------------------------------------------
|
||||
-- BACKWARDS COMPATIBLE API
|
||||
|
||||
@@ -124,4 +124,10 @@ diff.register_backend("native", {
|
||||
local cmd = string.format("jj diff -f %s -t %s --quiet --no-pager", opts.left, opts.right)
|
||||
terminal.run_floating(cmd, require("jj.cmd").floating_keymaps())
|
||||
end,
|
||||
diff_history_revisions = function(_)
|
||||
utils.notify(
|
||||
"Diffing revisions with history mode is not supported on the `native` backend.",
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
end,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user