refactor(git): convert blocking subprocess calls to async

This commit is contained in:
2026-04-27 16:02:14 +02:00
parent 00eae8dbb9
commit 6a86a75ed5
8 changed files with 338 additions and 253 deletions
+35 -28
View File
@@ -1,3 +1,4 @@
local git = require("git")
local log = require("log")
local repo = require("git.repo")
local util = require("util")
@@ -39,11 +40,7 @@ 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
vim.schedule_wrap(populate_cached_cmds)
)
end
@@ -75,33 +72,41 @@ end
---@param args string[]
---@param conf ow.Git.SplitHandler
local function run_in_split(worktree, args, conf)
vim.cmd("new")
local buf = vim.api.nvim_get_current_buf()
vim.bo[buf].buftype = "nofile"
vim.bo[buf].bufhidden = "hide"
vim.bo[buf].swapfile = false
vim.bo[buf].modifiable = false
local buf = git.new_scratch()
vim.b[buf].git_worktree = worktree
if conf.needs_ref then
local user_ref = first_positional(args, 2) or "HEAD"
local sha = repo.rev_parse(worktree, user_ref, true)
if sha then
vim.b[buf].git_ref = sha
vim.b[buf].git_parent_ref =
repo.rev_parse(worktree, user_ref .. "^", true)
local function apply_name(label)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
pcall(vim.api.nvim_buf_set_name, buf, "git://" .. label .. "/")
vim.bo[buf].filetype = conf.ft
end
pcall(
vim.api.nvim_buf_set_name,
buf,
"git://" .. (sha or user_ref) .. "/"
)
repo.rev_parse(worktree, user_ref, true, function(sha)
if not sha then
apply_name(user_ref)
return
end
repo.rev_parse(worktree, user_ref .. "^", true, function(parent)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
vim.b[buf].git_ref = sha
vim.b[buf].git_parent_ref = parent
apply_name(sha)
end)
end)
else
vim.bo[buf].filetype = conf.ft
end
vim.bo[buf].filetype = conf.ft
local cmd = { "git" }
vim.list_extend(cmd, args)
vim.system(cmd, { cwd = worktree, text = true }, function(obj)
vim.schedule(function()
vim.system(
cmd,
{ cwd = worktree, text = true },
vim.schedule_wrap(function(obj)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
@@ -124,7 +129,7 @@ local function run_in_split(worktree, args, conf)
)
end
end)
end)
)
end
---@param worktree string
@@ -132,8 +137,10 @@ end
local function run_to_messages(worktree, args)
local cmd = { "git" }
vim.list_extend(cmd, args)
vim.system(cmd, { cwd = worktree, text = true }, function(obj)
vim.schedule(function()
vim.system(
cmd,
{ cwd = worktree, text = true },
vim.schedule_wrap(function(obj)
local out = vim.trim(obj.stdout or "")
local err = vim.trim(obj.stderr or "")
local chunks = {}
@@ -156,7 +163,7 @@ local function run_to_messages(worktree, args)
vim.api.nvim_echo(chunks, true, {})
end
end)
end)
)
end
---@param args string[]