fix(diff): correctly relativize paths for diffing current buffer (#108)

This commit is contained in:
Lars Hansen
2026-05-06 09:34:26 +02:00
committed by GitHub
parent a2d194d49e
commit f5d11a3098
5 changed files with 19 additions and 21 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ local function get_annotate_target()
return nil, nil, "Invalid jj:// buffer name"
end
local normalized, err = utils.normalize_repo_path(filename)
local normalized, err = utils.normalize_relative_path(filename)
if not normalized then
return nil, nil, err or "Could not normalize file path"
end
+1 -1
View File
@@ -56,7 +56,7 @@ diff.register_backend("diffview", {
end
local raw_path = opts.path or jj_path or "%"
local path, err = utils.normalize_repo_path(raw_path)
local path, err = utils.normalize_relative_path(raw_path)
if not path then
utils.notify(err or "Could not resolve file path for Diffview", vim.log.levels.ERROR)
return
+1 -1
View File
@@ -21,7 +21,7 @@ local function open_revision(rev, path)
end
local change_id = ids[1]
local rel_path, err = utils.normalize_repo_path(path)
local rel_path, err = utils.normalize_relative_path(path)
if not rel_path then
utils.notify(err or "Could not resolve path", vim.log.levels.ERROR)
return
+3 -3
View File
@@ -18,7 +18,7 @@ local parser = require("jj.core.parser")
--- Fetch file content from jj synchronously.
--- Returns lines with blank lines preserved; trailing empty line removed.
--- @param rev string The revision (change ID or other revset)
--- @param path string Repository-relative path
--- @param path string Cwd-relative path
--- @return string[] lines
--- @return boolean had_eol Whether the content had a trailing newline
--- @return boolean ok Whether the command succeeded
@@ -46,7 +46,7 @@ M.get_file_content = get_file_content
function M.read_target(opts)
local revision = opts and opts.rev or "@"
local raw_path = opts and opts.path or "%"
local path, normalize_err = utils.normalize_repo_path(raw_path)
local path, normalize_err = utils.normalize_relative_path(raw_path)
if not path then
utils.notify(normalize_err or "Could not normalize path", vim.log.levels.ERROR)
return
@@ -127,7 +127,7 @@ M.write_revision_file = write_revision_file
function M.open_target(opts)
local revision = opts.rev or "@"
local raw_path = opts.path or "%"
local path, normalize_err = utils.normalize_repo_path(raw_path)
local path, normalize_err = utils.normalize_relative_path(raw_path)
if not path then
utils.notify(normalize_err or "Could not normalize path", vim.log.levels.ERROR)
return
+13 -15
View File
@@ -161,18 +161,17 @@ function M.parse_jj_uri(name)
return name:match("^jj://([^/]+)/(.+)$")
end
--- Normalize a user-provided file path to a repository-relative path for jj commands.
--- Normalize a user-provided file path to a cwd-relative path for jj commands.
---
--- Rules:
--- - `%` resolves to the current buffer absolute file path.
--- - Absolute paths are converted to repository-relative paths.
--- - Relative paths are kept relative.
--- - Absolute paths are converted to cwd-relative paths.
--- - Relative paths are kept relative (already cwd-relative).
---
--- @param path string|nil Path provided by the user (`%`, absolute, or relative)
--- @param root? string Repository root (defaults to current jj repo root)
--- @return string|nil normalized_path Repository-relative normalized path
--- @return string|nil normalized_path Cwd-relative normalized path
--- @return string|nil err Error message when normalization fails
function M.normalize_repo_path(path, root)
function M.normalize_relative_path(path)
path = vim.trim(path or "")
if path == "" then
return nil, "Path is empty"
@@ -191,18 +190,16 @@ function M.normalize_repo_path(path, root)
path = vim.fs.normalize(path)
root = root or M.get_jj_root()
if not root or root == "" then
return nil, "Not in a jj repository"
end
root = vim.fs.normalize(root)
local is_absolute = vim.startswith(path, "/") or path:match("^%a:/") ~= nil
if is_absolute then
local rel = M.relpath(root, path)
local cwd = vim.uv.cwd()
if not cwd or cwd == "" then
return nil, "Could not determine current working directory"
end
cwd = vim.fs.normalize(cwd)
local rel = M.relpath(cwd, path)
if not rel then
return nil, "Path is outside repository root"
return nil, "Path is outside current working directory"
end
path = rel
end
@@ -685,6 +682,7 @@ function M.get_describe_text(revset)
return text
end
---
--- Get the commit id from a given revision. Returns nil and notifies an error if multiple commit_ids are found for a single revset
--- @param revset string The revset to extract the commit id from