perf(git): run git log sync with bounded count

This commit is contained in:
2026-04-28 05:36:23 +02:00
parent 80a486719e
commit 4caa1b84df
2 changed files with 78 additions and 68 deletions
+41 -28
View File
@@ -6,44 +6,57 @@ local util = require("util")
local M = {}
local LOG_FORMAT = "%h %ad {%an}%d %s"
local DEFAULT_MAX_COUNT = 1000
---@class ow.Git.LogOpts
---@field max_count integer? cap on commits to show. Nil uses the default, <= 0 means "all"
---@param opts ow.Git.LogOpts?
function M.show(opts)
opts = opts or {}
local max_count = opts.max_count
if max_count == nil then
max_count = DEFAULT_MAX_COUNT
end
function M.show()
local _, worktree = repo.resolve_cwd()
if not worktree then
log.warning("not in a git repository")
return
end
local cmd = {
"git",
"log",
"--graph",
"--all",
"--decorate",
"--date=short",
"--format=format:" .. LOG_FORMAT,
}
if max_count > 0 then
table.insert(cmd, "--max-count=" .. max_count)
end
local result = vim.system(cmd, { cwd = worktree, text = true }):wait()
if result.code ~= 0 then
log.error("git log failed: %s", vim.trim(result.stderr or ""))
return
end
local buf = git.new_scratch()
vim.b[buf].git_worktree = worktree
vim.system(
{
"git",
"log",
"--graph",
"--all",
"--decorate",
"--date=short",
"--format=format:" .. LOG_FORMAT,
},
{ cwd = worktree, text = true },
vim.schedule_wrap(function(result)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
if result.code ~= 0 then
log.error("git log failed: %s", vim.trim(result.stderr or ""))
return
end
local lines = util.split_lines(result.stdout or "")
vim.bo[buf].modifiable = true
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].modifiable = false
vim.bo[buf].modified = false
vim.bo[buf].filetype = "gitlog"
end)
vim.bo[buf].modifiable = true
vim.api.nvim_buf_set_lines(
buf,
0,
-1,
false,
util.split_lines(result.stdout or "")
)
vim.bo[buf].modifiable = false
vim.bo[buf].modified = false
vim.bo[buf].filetype = "gitlog"
end
return M