perf(git): minor performance cleanups

This commit is contained in:
2026-04-27 13:27:22 +02:00
parent 5966454462
commit 20dc6cc3c9
4 changed files with 51 additions and 30 deletions
+5 -1
View File
@@ -2,8 +2,12 @@ local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
vim.keymap.set("n", "<CR>", function() vim.keymap.set("n", "<CR>", function()
local worktree = vim.b.git_worktree local worktree = vim.b.git_worktree
-- 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 = worktree local sha = worktree
and vim.api.nvim_get_current_line():match("(%x%x%x%x%x%x%x+)") and vim.api
.nvim_get_current_line()
:match("^[*|/\\_ ]*(%x%x%x%x%x%x%x+)")
if sha then if sha then
require("git.show").open_commit(worktree, sha) require("git.show").open_commit(worktree, sha)
else else
+27 -10
View File
@@ -18,16 +18,8 @@ local SPLIT_HANDLERS = {
---@type string[]? ---@type string[]?
local cached_cmds local cached_cmds
---@return string[] ---@param result vim.SystemCompleted
local function git_cmds() local function populate_cached_cmds(result)
if cached_cmds then
return cached_cmds
end
local result = vim.system(
{ "git", "--list-cmds=main,others,alias" },
{ text = true }
)
:wait()
cached_cmds = {} cached_cmds = {}
if result.code == 0 then if result.code == 0 then
for line in (result.stdout or ""):gmatch("[^\r\n]+") do for line in (result.stdout or ""):gmatch("[^\r\n]+") do
@@ -37,7 +29,31 @@ local function git_cmds()
end end
table.sort(cached_cmds) table.sort(cached_cmds)
end end
end
---Prime `cached_cmds` asynchronously so the first `:G <Tab>` doesn't block.
local function prefetch_cmds()
vim.system(
{ "git", "--list-cmds=main,others,alias" },
{ text = true },
function(result)
vim.schedule(function()
populate_cached_cmds(result)
end)
end
)
end
---@return string[]
local function git_cmds()
if cached_cmds then
return cached_cmds return cached_cmds
end
populate_cached_cmds(
vim.system({ "git", "--list-cmds=main,others,alias" }, { text = true })
:wait()
)
return cached_cmds or {}
end end
---@param content string ---@param content string
@@ -204,6 +220,7 @@ local function complete(arg_lead, cmd_line, _)
end end
function M.setup() function M.setup()
prefetch_cmds()
vim.api.nvim_create_user_command("G", function(opts) vim.api.nvim_create_user_command("G", function(opts)
M.run(opts.fargs) M.run(opts.fargs)
end, { end, {
+5 -9
View File
@@ -23,7 +23,9 @@ local function attach_index_writer(buf, worktree, path)
return return
end end
local sha = vim.trim(hash.stdout or "") local sha = vim.trim(hash.stdout or "")
local mode = "100644" local mode = vim.b[buf].git_index_mode
if not mode then
mode = "100644"
local ls = vim.system( local ls = vim.system(
{ "git", "ls-files", "-s", "--", path }, { "git", "ls-files", "-s", "--", path },
{ cwd = worktree, text = true } { cwd = worktree, text = true }
@@ -34,6 +36,8 @@ local function attach_index_writer(buf, worktree, path)
mode = m mode = m
end end
end end
vim.b[buf].git_index_mode = mode
end
local upd = vim.system({ local upd = vim.system({
"git", "git",
"update-index", "update-index",
@@ -116,14 +120,6 @@ end
---@param abs_path string ---@param abs_path string
---@return integer ---@return integer
function M.load_file_buf(abs_path) function M.load_file_buf(abs_path)
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if
vim.api.nvim_buf_is_loaded(buf)
and vim.api.nvim_buf_get_name(buf) == abs_path
then
return buf
end
end
local buf = vim.fn.bufadd(abs_path) local buf = vim.fn.bufadd(abs_path)
vim.fn.bufload(buf) vim.fn.bufload(buf)
return buf return buf
+6 -2
View File
@@ -149,6 +149,7 @@ local function do_refresh(repo)
format(code) format(code)
end end
end end
local dirty = false
for buf in pairs(repo.buffers) do for buf in pairs(repo.buffers) do
if not vim.api.nvim_buf_is_valid(buf) then if not vim.api.nvim_buf_is_valid(buf) then
repo.buffers[buf] = nil repo.buffers[buf] = nil
@@ -156,10 +157,13 @@ local function do_refresh(repo)
local status = statuses[vim.api.nvim_buf_get_name(buf)] local status = statuses[vim.api.nvim_buf_get_name(buf)]
if vim.b[buf].git_status ~= status then if vim.b[buf].git_status ~= status then
vim.b[buf].git_status = status vim.b[buf].git_status = status
dirty = true
end
end
end
if dirty then
vim.cmd.redrawstatus({ bang = true }) vim.cmd.redrawstatus({ bang = true })
end end
end
end
vim.api.nvim_exec_autocmds("User", { vim.api.nvim_exec_autocmds("User", {
pattern = "GitRefresh", pattern = "GitRefresh",
data = { gitdir = repo.gitdir, worktree = repo.worktree }, data = { gitdir = repo.gitdir, worktree = repo.worktree },