perf(git): minor performance cleanups
This commit is contained in:
+5
-1
@@ -2,8 +2,12 @@ local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
|
||||
|
||||
vim.keymap.set("n", "<CR>", function()
|
||||
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
|
||||
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
|
||||
require("git.show").open_commit(worktree, sha)
|
||||
else
|
||||
|
||||
+27
-10
@@ -18,16 +18,8 @@ local SPLIT_HANDLERS = {
|
||||
---@type string[]?
|
||||
local cached_cmds
|
||||
|
||||
---@return string[]
|
||||
local function git_cmds()
|
||||
if cached_cmds then
|
||||
return cached_cmds
|
||||
end
|
||||
local result = vim.system(
|
||||
{ "git", "--list-cmds=main,others,alias" },
|
||||
{ text = true }
|
||||
)
|
||||
:wait()
|
||||
---@param result vim.SystemCompleted
|
||||
local function populate_cached_cmds(result)
|
||||
cached_cmds = {}
|
||||
if result.code == 0 then
|
||||
for line in (result.stdout or ""):gmatch("[^\r\n]+") do
|
||||
@@ -37,7 +29,31 @@ local function git_cmds()
|
||||
end
|
||||
table.sort(cached_cmds)
|
||||
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
|
||||
end
|
||||
populate_cached_cmds(
|
||||
vim.system({ "git", "--list-cmds=main,others,alias" }, { text = true })
|
||||
:wait()
|
||||
)
|
||||
return cached_cmds or {}
|
||||
end
|
||||
|
||||
---@param content string
|
||||
@@ -204,6 +220,7 @@ local function complete(arg_lead, cmd_line, _)
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
prefetch_cmds()
|
||||
vim.api.nvim_create_user_command("G", function(opts)
|
||||
M.run(opts.fargs)
|
||||
end, {
|
||||
|
||||
+5
-9
@@ -23,7 +23,9 @@ local function attach_index_writer(buf, worktree, path)
|
||||
return
|
||||
end
|
||||
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(
|
||||
{ "git", "ls-files", "-s", "--", path },
|
||||
{ cwd = worktree, text = true }
|
||||
@@ -34,6 +36,8 @@ local function attach_index_writer(buf, worktree, path)
|
||||
mode = m
|
||||
end
|
||||
end
|
||||
vim.b[buf].git_index_mode = mode
|
||||
end
|
||||
local upd = vim.system({
|
||||
"git",
|
||||
"update-index",
|
||||
@@ -116,14 +120,6 @@ end
|
||||
---@param abs_path string
|
||||
---@return integer
|
||||
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)
|
||||
vim.fn.bufload(buf)
|
||||
return buf
|
||||
|
||||
+6
-2
@@ -149,6 +149,7 @@ local function do_refresh(repo)
|
||||
format(code)
|
||||
end
|
||||
end
|
||||
local dirty = false
|
||||
for buf in pairs(repo.buffers) do
|
||||
if not vim.api.nvim_buf_is_valid(buf) then
|
||||
repo.buffers[buf] = nil
|
||||
@@ -156,10 +157,13 @@ local function do_refresh(repo)
|
||||
local status = statuses[vim.api.nvim_buf_get_name(buf)]
|
||||
if vim.b[buf].git_status ~= status then
|
||||
vim.b[buf].git_status = status
|
||||
dirty = true
|
||||
end
|
||||
end
|
||||
end
|
||||
if dirty then
|
||||
vim.cmd.redrawstatus({ bang = true })
|
||||
end
|
||||
end
|
||||
end
|
||||
vim.api.nvim_exec_autocmds("User", {
|
||||
pattern = "GitRefresh",
|
||||
data = { gitdir = repo.gitdir, worktree = repo.worktree },
|
||||
|
||||
Reference in New Issue
Block a user