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
+30 -25
View File
@@ -1,3 +1,4 @@
local git = require("git")
local log = require("log")
local repo = require("git.repo")
local util = require("util")
@@ -13,32 +14,36 @@ function M.show()
return
end
local result = vim.system({
"git",
"log",
"--graph",
"--all",
"--decorate",
"--date=short",
"--format=format:" .. LOG_FORMAT,
}, { cwd = worktree, text = true }):wait()
if result.code ~= 0 then
log.error("git log failed: %s", result.stderr or "")
return
end
local lines = util.split_lines(result.stdout or "")
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.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].modifiable = false
vim.bo[buf].modified = false
local buf = git.new_scratch()
vim.b[buf].git_worktree = worktree
vim.bo[buf].filetype = "gitlog"
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)
)
end
return M