feat(object): use <commit>:<path> URIs when entering a file from a commit dump
This commit is contained in:
+64
-11
@@ -337,10 +337,45 @@ local function side_buf(r, blob, path)
|
||||
end
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
---@param commit string
|
||||
---@param parent boolean
|
||||
---@param path string
|
||||
---@return integer?
|
||||
local function commit_side_buf(r, commit, parent, path)
|
||||
local rev = parent and (commit .. "^") or commit
|
||||
local sha = r:rev_parse(rev, false)
|
||||
if not sha then
|
||||
return nil
|
||||
end
|
||||
if not r:rev_parse(sha .. ":" .. path, false) then
|
||||
return nil
|
||||
end
|
||||
return M.buf_for(r, Revision.new({ base = sha, path = path }))
|
||||
end
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
---@param commit string?
|
||||
---@param parent boolean
|
||||
---@param blob string?
|
||||
---@param path string
|
||||
local function load_side(r, blob, path)
|
||||
local buf = side_buf(r, blob, path)
|
||||
---@return integer?
|
||||
local function side(r, commit, parent, blob, path)
|
||||
if commit then
|
||||
local buf = commit_side_buf(r, commit, parent, path)
|
||||
if buf then
|
||||
return buf
|
||||
end
|
||||
end
|
||||
return side_buf(r, blob, path)
|
||||
end
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
---@param commit string?
|
||||
---@param parent boolean
|
||||
---@param blob string?
|
||||
---@param path string
|
||||
local function load_side(r, commit, parent, blob, path)
|
||||
local buf = side(r, commit, parent, blob, path)
|
||||
if not buf then
|
||||
util.error("no content for %s", path)
|
||||
return
|
||||
@@ -350,14 +385,15 @@ local function load_side(r, blob, path)
|
||||
end
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
---@param commit string?
|
||||
---@param section ow.Git.DiffSection
|
||||
local function open_section(r, section)
|
||||
local function open_section(r, commit, section)
|
||||
if not section.blob_a or not section.blob_b then
|
||||
util.error("no index line, cannot determine blob SHAs")
|
||||
return
|
||||
end
|
||||
local left = side_buf(r, section.blob_a, section.path_a)
|
||||
local right = side_buf(r, section.blob_b, section.path_b)
|
||||
local left = side(r, commit, true, section.blob_a, section.path_a)
|
||||
local right = side(r, commit, false, section.blob_b, section.path_b)
|
||||
if left and right then
|
||||
vim.cmd.normal({ "m'", bang = true })
|
||||
vim.api.nvim_set_current_buf(right)
|
||||
@@ -377,6 +413,21 @@ local function open_section(r, section)
|
||||
vim.api.nvim_set_current_buf(buf)
|
||||
end
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
---@param buf integer
|
||||
---@return string?
|
||||
local function commit_in_buf(r, buf)
|
||||
local name = vim.api.nvim_buf_get_name(buf)
|
||||
if not vim.startswith(name, M.URI_PREFIX) then
|
||||
return nil
|
||||
end
|
||||
local rev = M.parse_uri(name)
|
||||
if not rev or not rev.base or rev.path then
|
||||
return nil
|
||||
end
|
||||
return r:rev_parse(rev.base .. "^{commit}", false)
|
||||
end
|
||||
|
||||
---@class ow.Git.Object.OpenOpts
|
||||
---@field split (false|"above"|"below"|"left"|"right")?
|
||||
|
||||
@@ -423,7 +474,7 @@ function M.open_under_cursor()
|
||||
line:match("^%d+ (%w+) (%x+)\t(.+)$")
|
||||
if entry_sha then
|
||||
if entry_type == "blob" then
|
||||
load_side(r, entry_sha, entry_name --[[@as string]])
|
||||
load_side(r, nil, false, entry_sha, entry_name --[[@as string]])
|
||||
else
|
||||
M.open(r, entry_sha, { split = false })
|
||||
end
|
||||
@@ -435,24 +486,26 @@ function M.open_under_cursor()
|
||||
return false
|
||||
end
|
||||
|
||||
local commit = commit_in_buf(r, vim.api.nvim_get_current_buf())
|
||||
|
||||
if line:match("^diff %-%-git ") then
|
||||
open_section(r, section)
|
||||
open_section(r, commit, section)
|
||||
return true
|
||||
end
|
||||
if line:match("^%-%-%- ") then
|
||||
load_side(r, section.blob_a, section.path_a)
|
||||
load_side(r, commit, true, section.blob_a, section.path_a)
|
||||
return true
|
||||
end
|
||||
if line:match("^%+%+%+ ") then
|
||||
load_side(r, section.blob_b, section.path_b)
|
||||
load_side(r, commit, false, section.blob_b, section.path_b)
|
||||
return true
|
||||
end
|
||||
local prefix = line:sub(1, 1)
|
||||
if prefix == "+" then
|
||||
load_side(r, section.blob_b, section.path_b)
|
||||
load_side(r, commit, false, section.blob_b, section.path_b)
|
||||
return true
|
||||
elseif prefix == "-" then
|
||||
load_side(r, section.blob_a, section.path_a)
|
||||
load_side(r, commit, true, section.blob_a, section.path_a)
|
||||
return true
|
||||
end
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user