perf(git): piggyback sidebar status on the indicator's git status call
This commit is contained in:
+13
-1
@@ -142,17 +142,24 @@ end
|
||||
|
||||
---@param repo ow.Git.Repo
|
||||
local function do_refresh(repo)
|
||||
-- `--branch` adds the `## branch...upstream [ahead/behind]` line that
|
||||
-- the sidebar parses; the per-buffer indicator only needs the XY +
|
||||
-- path lines, so it ignores `##` lines below. Running with `--branch`
|
||||
-- lets the sidebar reuse this single subprocess via the GitRefresh
|
||||
-- data payload instead of spawning its own.
|
||||
vim.system({
|
||||
"git",
|
||||
"-c",
|
||||
"core.quotePath=false",
|
||||
"status",
|
||||
"--porcelain=v1",
|
||||
"--branch",
|
||||
}, { cwd = repo.worktree, text = true }, function(obj)
|
||||
vim.schedule(function()
|
||||
local statuses = {}
|
||||
if obj.code == 0 then
|
||||
for line in (obj.stdout or ""):gmatch("[^\r\n]+") do
|
||||
if line:sub(1, 2) ~= "##" then
|
||||
local code = line:sub(1, 2)
|
||||
local path_part = line:sub(4)
|
||||
local arrow = path_part:find(" -> ", 1, true)
|
||||
@@ -162,6 +169,7 @@ local function do_refresh(repo)
|
||||
statuses[vim.fs.joinpath(repo.worktree, path_part)] =
|
||||
format(code)
|
||||
end
|
||||
end
|
||||
else
|
||||
log.warning("git status failed: %s", vim.trim(obj.stderr or ""))
|
||||
end
|
||||
@@ -182,7 +190,11 @@ local function do_refresh(repo)
|
||||
end
|
||||
vim.api.nvim_exec_autocmds("User", {
|
||||
pattern = "GitRefresh",
|
||||
data = { gitdir = repo.gitdir, worktree = repo.worktree },
|
||||
data = {
|
||||
gitdir = repo.gitdir,
|
||||
worktree = repo.worktree,
|
||||
porcelain_stdout = obj.code == 0 and obj.stdout or nil,
|
||||
},
|
||||
})
|
||||
end)
|
||||
end)
|
||||
|
||||
+65
-29
@@ -142,18 +142,12 @@ local function parse_branch_line(line)
|
||||
return info
|
||||
end
|
||||
|
||||
---@param worktree string
|
||||
---@param callback fun(branch: ow.Git.BranchInfo, groups: table<string, ow.Git.StatusEntry[]>)
|
||||
local function fetch_status(worktree, callback)
|
||||
vim.system({
|
||||
"git",
|
||||
"-c",
|
||||
"core.quotePath=false",
|
||||
"status",
|
||||
"--porcelain=v1",
|
||||
"--branch",
|
||||
}, { cwd = worktree, text = true }, function(obj)
|
||||
vim.schedule(function()
|
||||
---Parse `git status --porcelain=v1 --branch` output into a (branch, groups)
|
||||
---pair. `Unpushed` and `Unpulled` start empty here; ahead/behind commits are
|
||||
---filled in by a follow-up `git log` once we know the upstream is set.
|
||||
---@param stdout string
|
||||
---@return ow.Git.BranchInfo, table<string, ow.Git.StatusEntry[]>
|
||||
local function parse_porcelain(stdout)
|
||||
local branch = { ahead = 0, behind = 0 }
|
||||
local groups = {
|
||||
Untracked = {},
|
||||
@@ -163,11 +157,7 @@ local function fetch_status(worktree, callback)
|
||||
Unpushed = {},
|
||||
Unpulled = {},
|
||||
}
|
||||
if obj.code ~= 0 then
|
||||
log.error("git status failed: %s", vim.trim(obj.stderr or ""))
|
||||
end
|
||||
if obj.code == 0 then
|
||||
for line in (obj.stdout or ""):gmatch("[^\r\n]+") do
|
||||
for line in stdout:gmatch("[^\r\n]+") do
|
||||
if line:sub(1, 2) == "##" then
|
||||
branch = parse_branch_line(line)
|
||||
else
|
||||
@@ -215,7 +205,17 @@ local function fetch_status(worktree, callback)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return branch, groups
|
||||
end
|
||||
|
||||
---Run the ahead/behind `git log` calls for any non-zero counters and call
|
||||
---`callback(branch, groups)` once they all finish (or immediately when
|
||||
---there's nothing to fetch).
|
||||
---@param worktree string
|
||||
---@param branch ow.Git.BranchInfo
|
||||
---@param groups table<string, ow.Git.StatusEntry[]>
|
||||
---@param callback fun(branch: ow.Git.BranchInfo, groups: table<string, ow.Git.StatusEntry[]>)
|
||||
local function enrich_with_log(worktree, branch, groups, callback)
|
||||
local fetches = {}
|
||||
if branch.upstream and branch.ahead > 0 then
|
||||
table.insert(
|
||||
@@ -240,16 +240,11 @@ local function fetch_status(worktree, callback)
|
||||
"log",
|
||||
"--format=%h %s",
|
||||
f.range,
|
||||
}, { cwd = worktree, text = true }, function(
|
||||
log_obj
|
||||
)
|
||||
}, { cwd = worktree, text = true }, function(log_obj)
|
||||
vim.schedule(function()
|
||||
if log_obj.code == 0 then
|
||||
for line in
|
||||
(log_obj.stdout or ""):gmatch("[^\r\n]+")
|
||||
do
|
||||
local sha, subject =
|
||||
line:match("^(%S+)%s+(.+)$")
|
||||
for line in (log_obj.stdout or ""):gmatch("[^\r\n]+") do
|
||||
local sha, subject = line:match("^(%S+)%s+(.+)$")
|
||||
if sha then
|
||||
table.insert(groups[f.section], {
|
||||
section = f.section,
|
||||
@@ -272,6 +267,46 @@ local function fetch_status(worktree, callback)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
---Build the (branch, groups) tuple for the sidebar. When `prefetched_stdout`
|
||||
---is provided (typical case: dispatched via the `User GitRefresh` autocmd
|
||||
---that already ran `git status --porcelain=v1 --branch` for the indicator),
|
||||
---we skip the duplicate subprocess. Otherwise the sidebar fetches its own.
|
||||
---@param worktree string
|
||||
---@param prefetched_stdout string?
|
||||
---@param callback fun(branch: ow.Git.BranchInfo, groups: table<string, ow.Git.StatusEntry[]>)
|
||||
local function fetch_status(worktree, prefetched_stdout, callback)
|
||||
if prefetched_stdout then
|
||||
local branch, groups = parse_porcelain(prefetched_stdout)
|
||||
enrich_with_log(worktree, branch, groups, callback)
|
||||
return
|
||||
end
|
||||
vim.system({
|
||||
"git",
|
||||
"-c",
|
||||
"core.quotePath=false",
|
||||
"status",
|
||||
"--porcelain=v1",
|
||||
"--branch",
|
||||
}, { cwd = worktree, text = true }, function(obj)
|
||||
vim.schedule(function()
|
||||
if obj.code ~= 0 then
|
||||
log.error("git status failed: %s", vim.trim(obj.stderr or ""))
|
||||
local branch = { ahead = 0, behind = 0 }
|
||||
local groups = {
|
||||
Untracked = {},
|
||||
Unstaged = {},
|
||||
Staged = {},
|
||||
Unmerged = {},
|
||||
Unpushed = {},
|
||||
Unpulled = {},
|
||||
}
|
||||
callback(branch, groups)
|
||||
return
|
||||
end
|
||||
local branch, groups = parse_porcelain(obj.stdout or "")
|
||||
enrich_with_log(worktree, branch, groups, callback)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
@@ -367,7 +402,8 @@ local function fingerprint(branch, groups)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
local function refresh(bufnr)
|
||||
---@param prefetched_stdout string? porcelain output from a piggybacked GitRefresh
|
||||
local function refresh(bufnr, prefetched_stdout)
|
||||
local s = state[bufnr]
|
||||
if not s then
|
||||
return
|
||||
@@ -384,7 +420,7 @@ local function refresh(bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
fetch_status(s.worktree, function(branch, groups)
|
||||
fetch_status(s.worktree, prefetched_stdout, function(branch, groups)
|
||||
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||
return
|
||||
end
|
||||
@@ -874,7 +910,7 @@ local function open(worktree)
|
||||
group = group,
|
||||
callback = function(args)
|
||||
if args.data and args.data.gitdir == gitdir then
|
||||
refresh(bufnr)
|
||||
refresh(bufnr, args.data.porcelain_stdout)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user