refactor(git): introduce Revision class, normalize naming, slim docs

This commit is contained in:
2026-04-30 09:44:24 +02:00
parent 775add9b15
commit d95de4bc1d
10 changed files with 244 additions and 428 deletions
+5 -12
View File
@@ -4,11 +4,10 @@ local util = require("git.util")
local M = {}
local LOG_FORMAT = "%h %ad {%an}%d %s"
local DEFAULT_MAX_COUNT = 1000
local URI_PREFIX = "gitlog://"
---@param worktree string
---@param max_count integer
---@param max_count integer?
---@return string?
local function fetch(worktree, max_count)
local cmd = {
@@ -20,7 +19,7 @@ local function fetch(worktree, max_count)
"--date=short",
"--format=format:" .. LOG_FORMAT,
}
if max_count > 0 then
if max_count then
table.insert(cmd, "--max-count=" .. max_count)
end
return util.exec(cmd, { cwd = worktree })
@@ -29,8 +28,7 @@ end
---@param buf integer
local function populate(buf)
local worktree = vim.b[buf].git_worktree
local max_count = vim.b[buf].git_log_max_count or DEFAULT_MAX_COUNT
local stdout = fetch(worktree, max_count)
local stdout = fetch(worktree, vim.b[buf].git_log_max_count)
if not stdout then
return
end
@@ -64,7 +62,6 @@ local function populate(buf)
vim.bo[buf].modified = false
end
---BufReadCmd handler for `gitlog://<worktree>` URIs.
---@param buf integer
function M.read_uri(buf)
local name = vim.api.nvim_buf_get_name(buf)
@@ -77,8 +74,6 @@ function M.read_uri(buf)
vim.bo[buf].swapfile = false
vim.bo[buf].bufhidden = "hide"
vim.bo[buf].buftype = "nofile"
-- Skip the assignment when ft is already set so re-runs don't
-- re-fire `FileType` autocmds (ftplugin reload, treesitter attach).
if vim.bo[buf].filetype ~= "gitlog" then
vim.bo[buf].filetype = "gitlog"
end
@@ -87,7 +82,7 @@ function M.read_uri(buf)
end
---@class ow.Git.LogOpts
---@field max_count integer? cap on commits to show. Nil uses the default, <= 0 means "all"
---@field max_count integer?
---@param opts ow.Git.LogOpts?
function M.open(opts)
@@ -100,7 +95,7 @@ function M.open(opts)
local buf = vim.fn.bufadd(URI_PREFIX .. worktree)
vim.b[buf].git_worktree = worktree
vim.b[buf].git_log_max_count = opts.max_count or DEFAULT_MAX_COUNT
vim.b[buf].git_log_max_count = opts.max_count
local was_loaded = vim.api.nvim_buf_is_loaded(buf)
local win = vim.fn.bufwinid(buf)
@@ -110,8 +105,6 @@ function M.open(opts)
vim.api.nvim_set_current_win(win)
end
-- `place_buf` triggers `bufload` -> `read_uri` for a fresh buffer.
-- An already-loaded buffer needs an explicit refresh.
if was_loaded then
populate(buf)
end