refactor(git): unify around the Repo abstraction

This commit is contained in:
2026-05-02 22:45:44 +02:00
parent 8bd674622e
commit be1d7ace50
14 changed files with 671 additions and 586 deletions
+14 -19
View File
@@ -62,8 +62,8 @@ end
---@param buf integer
---@param rev ow.Git.Revision
local function uri_split(opts, buf, rev)
local worktree = vim.b[buf].git_worktree or select(2, repo.current_repo())
if not worktree then
local r = repo.find(buf)
if not r then
util.warning("git URI buffer has no worktree")
return
end
@@ -76,7 +76,7 @@ local function uri_split(opts, buf, rev)
if opts.rev and opts.rev:find(":", 1, true) then
local content = util.exec(
{ "git", "cat-file", "-p", opts.rev },
{ cwd = worktree, silent = true }
{ cwd = r.worktree, silent = true }
)
if not content then
util.warning("invalid rev: %s", opts.rev)
@@ -84,7 +84,7 @@ local function uri_split(opts, buf, rev)
end
place_pair(
buf,
object.buf_for(worktree, Revision.parse(opts.rev), content),
object.buf_for(r, Revision.parse(opts.rev), content),
false,
opts.vertical
)
@@ -92,7 +92,7 @@ local function uri_split(opts, buf, rev)
end
if not opts.rev then
local worktree_path = vim.fs.joinpath(worktree, rev.path)
local worktree_path = vim.fs.joinpath(r.worktree, rev.path)
if not vim.uv.fs_stat(worktree_path) then
util.warning("worktree file does not exist: %s", rev.path)
return
@@ -118,18 +118,13 @@ local function uri_split(opts, buf, rev)
local other_rev, left = m[1], m[2]
local content = util.exec(
{ "git", "cat-file", "-p", other_rev:format() },
{ cwd = worktree, silent = true }
{ cwd = r.worktree, silent = true }
)
if not content then
util.warning("invalid rev: %s", other_rev:format())
return
end
place_pair(
buf,
object.buf_for(worktree, other_rev, content),
left,
opts.vertical
)
place_pair(buf, object.buf_for(r, other_rev, content), left, opts.vertical)
end
---@class ow.Git.SplitOpts
@@ -141,7 +136,7 @@ function M.split(opts)
local cur_buf = vim.api.nvim_get_current_buf()
local cur_path = vim.api.nvim_buf_get_name(cur_buf)
local cur_rev = Revision.from_uri(cur_path)
local cur_rev = require("git.object").parse_uri(cur_path)
if cur_rev then
return uri_split(opts, cur_buf, cur_rev)
end
@@ -154,13 +149,13 @@ function M.split(opts)
util.warning("cannot diff this buffer (not a worktree file)")
return
end
local _, worktree, cur_path = repo.resolve(cur_path)
if not worktree then
cur_path = vim.fn.resolve(cur_path)
local r = repo.resolve(cur_path)
if not r then
util.warning("not in a git repository")
return
end
---@cast cur_path -nil
local rel = vim.fs.relpath(worktree, cur_path)
local rel = vim.fs.relpath(r.worktree, cur_path)
local rev
if not opts.rev then
@@ -172,13 +167,13 @@ function M.split(opts)
end
local content = util.exec(
{ "git", "cat-file", "-p", rev:format() },
{ cwd = worktree, silent = true }
{ cwd = r.worktree, silent = true }
)
if not content then
util.warning("invalid rev: %s", rev:format())
return
end
local buf = require("git.object").buf_for(worktree, rev, content)
local buf = require("git.object").buf_for(r, rev, content)
place_pair(buf, cur_buf, true, opts.vertical)
end