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
+43 -10
View File
@@ -3,8 +3,31 @@ local util = require("git.util")
local M = {}
M.URI_PREFIX = "gitlog://"
local LOG_FORMAT = "%h %ad {%an}%d %s"
local URI_PREFIX = "gitlog://"
local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
---@param buf integer
local function attach_dispatch(buf)
vim.keymap.set("n", "<CR>", function()
local r = repo.find(buf)
-- Anchor past the leading graph chars (matches the leading sha
-- column, not any hex word that happens to appear later in the
-- subject).
local sha = r
and vim.api
.nvim_get_current_line()
:match("^[*|/\\_ ]*(%x%x%x%x%x%x%x+)")
if sha then
---@cast r -nil
require("git.object").open_object(r, sha, { split = false })
else
vim.api.nvim_feedkeys(cr, "n", false)
end
end, { buffer = buf, silent = true, desc = "Open commit" })
end
---@param worktree string
---@param max_count integer?
@@ -27,8 +50,12 @@ end
---@param buf integer
local function populate(buf)
local worktree = vim.b[buf].git_worktree
local stdout = fetch(worktree, vim.b[buf].git_log_max_count)
local r = repo.find(buf)
local state = r and r:state(buf)
if not r or not state then
return
end
local stdout = fetch(r.worktree, state.log_max_count)
if not stdout then
return
end
@@ -65,12 +92,16 @@ end
---@param buf integer
function M.read_uri(buf)
local name = vim.api.nvim_buf_get_name(buf)
local worktree = name:sub(#URI_PREFIX + 1)
local worktree = name:sub(#M.URI_PREFIX + 1)
if worktree == "" then
return
end
local r = repo.resolve(worktree)
if not r then
return
end
repo.attach(buf, r)
vim.b[buf].git_worktree = worktree
vim.bo[buf].swapfile = false
vim.bo[buf].bufhidden = "hide"
vim.bo[buf].buftype = "nofile"
@@ -78,6 +109,7 @@ function M.read_uri(buf)
vim.bo[buf].filetype = "gitlog"
end
attach_dispatch(buf)
populate(buf)
end
@@ -92,15 +124,16 @@ M.opt_parsers = {
---@param opts ow.Git.LogOpts?
function M.open(opts)
opts = opts or {}
local _, worktree = repo.current_repo()
if not worktree then
local r = repo.find()
if not r then
util.warning("not in a git repository")
return
end
local buf = vim.fn.bufadd(URI_PREFIX .. worktree)
vim.b[buf].git_worktree = worktree
vim.b[buf].git_log_max_count = opts.max_count
local buf = vim.fn.bufadd(M.URI_PREFIX .. r.worktree)
repo.attach(buf, r)
local state = r:state(buf) --[[@as -nil]]
state.log_max_count = opts.max_count
local was_loaded = vim.api.nvim_buf_is_loaded(buf)
local win = vim.fn.bufwinid(buf)