2022-12-01 06:38:45 +08:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
|
2022-11-21 06:04:56 +08:00
|
|
|
local M = {}
|
|
|
|
|
2023-02-16 00:07:57 +08:00
|
|
|
---@type table<uv.uv_process_t, true>
|
2022-12-31 17:38:03 +08:00
|
|
|
M.running = {}
|
|
|
|
|
|
|
|
M.signals = {
|
|
|
|
"HUP",
|
|
|
|
"INT",
|
|
|
|
"QUIT",
|
|
|
|
"ILL",
|
|
|
|
"TRAP",
|
|
|
|
"ABRT",
|
|
|
|
"BUS",
|
|
|
|
"FPE",
|
|
|
|
"KILL",
|
|
|
|
"USR1",
|
|
|
|
"SEGV",
|
|
|
|
"USR2",
|
|
|
|
"PIPE",
|
|
|
|
"ALRM",
|
|
|
|
"TERM",
|
|
|
|
"CHLD",
|
|
|
|
"CONT",
|
|
|
|
"STOP",
|
|
|
|
"TSTP",
|
|
|
|
"TTIN",
|
|
|
|
"TTOU",
|
|
|
|
"URG",
|
|
|
|
"XCPU",
|
|
|
|
"XFSZ",
|
|
|
|
"VTALRM",
|
|
|
|
"PROF",
|
|
|
|
"WINCH",
|
|
|
|
"IO",
|
|
|
|
"PWR",
|
|
|
|
"EMT",
|
|
|
|
"SYS",
|
|
|
|
"INFO",
|
|
|
|
}
|
|
|
|
|
2022-12-01 06:38:45 +08:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
local uv = vim.loop
|
|
|
|
|
|
|
|
---@class ProcessOpts
|
|
|
|
---@field args string[]
|
|
|
|
---@field cwd? string
|
|
|
|
---@field on_line? fun(string)
|
|
|
|
---@field on_exit? fun(ok:boolean, output:string)
|
|
|
|
---@field timeout? number
|
2023-02-06 16:16:49 +08:00
|
|
|
---@field env? table<string,string>
|
2022-11-21 06:04:56 +08:00
|
|
|
|
2022-12-01 06:38:45 +08:00
|
|
|
---@param opts? ProcessOpts
|
2022-12-31 17:38:03 +08:00
|
|
|
---@param cmd string
|
2022-11-21 06:04:56 +08:00
|
|
|
function M.spawn(cmd, opts)
|
|
|
|
opts = opts or {}
|
2022-12-01 06:44:10 +08:00
|
|
|
opts.timeout = opts.timeout or (Config.options.git and Config.options.git.timeout * 1000)
|
2022-12-01 06:38:45 +08:00
|
|
|
|
2023-02-16 00:07:57 +08:00
|
|
|
---@type table<string, string>
|
2023-02-06 16:16:49 +08:00
|
|
|
local env = vim.tbl_extend("force", {
|
|
|
|
GIT_SSH_COMMAND = "ssh -oBatchMode=yes",
|
|
|
|
}, uv.os_environ(), opts.env or {})
|
|
|
|
env.GIT_DIR = nil
|
|
|
|
env.GIT_TERMINAL_PROMPT = "0"
|
2022-11-21 06:04:56 +08:00
|
|
|
|
2023-02-16 00:07:57 +08:00
|
|
|
---@type string[]
|
2023-02-06 16:16:49 +08:00
|
|
|
local env_flat = {}
|
|
|
|
for k, v in pairs(env) do
|
|
|
|
env_flat[#env_flat + 1] = k .. "=" .. v
|
2022-11-21 06:04:56 +08:00
|
|
|
end
|
|
|
|
|
2023-02-16 00:07:57 +08:00
|
|
|
local stdout = assert(uv.new_pipe())
|
|
|
|
local stderr = assert(uv.new_pipe())
|
2022-11-21 06:04:56 +08:00
|
|
|
|
|
|
|
local output = ""
|
2023-02-16 00:07:57 +08:00
|
|
|
---@type uv.uv_process_t
|
2022-11-21 06:04:56 +08:00
|
|
|
local handle = nil
|
|
|
|
|
2023-02-16 00:07:57 +08:00
|
|
|
---@type uv.uv_timer_t
|
2022-12-01 06:38:45 +08:00
|
|
|
local timeout
|
|
|
|
local killed = false
|
|
|
|
if opts.timeout then
|
2023-02-16 00:07:57 +08:00
|
|
|
timeout = assert(uv.new_timer())
|
2022-12-01 06:38:45 +08:00
|
|
|
timeout:start(opts.timeout, 0, function()
|
2022-12-31 17:38:03 +08:00
|
|
|
if M.kill(handle) then
|
2022-12-01 06:38:45 +08:00
|
|
|
killed = true
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
handle = uv.spawn(cmd, {
|
2022-11-21 06:04:56 +08:00
|
|
|
stdio = { nil, stdout, stderr },
|
|
|
|
args = opts.args,
|
|
|
|
cwd = opts.cwd,
|
2023-02-06 16:16:49 +08:00
|
|
|
env = env_flat,
|
2022-12-01 06:38:45 +08:00
|
|
|
}, function(exit_code, signal)
|
2022-12-31 17:38:03 +08:00
|
|
|
M.running[handle] = nil
|
2022-12-01 06:38:45 +08:00
|
|
|
if timeout then
|
|
|
|
timeout:stop()
|
|
|
|
timeout:close()
|
|
|
|
end
|
2022-11-21 06:04:56 +08:00
|
|
|
handle:close()
|
|
|
|
stdout:close()
|
|
|
|
stderr:close()
|
2023-02-16 00:07:57 +08:00
|
|
|
local check = assert(uv.new_check())
|
2022-11-21 06:04:56 +08:00
|
|
|
check:start(function()
|
|
|
|
if not stdout:is_closing() or not stderr:is_closing() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
check:stop()
|
|
|
|
if opts.on_exit then
|
|
|
|
output = output:gsub("[^\r\n]+\r", "")
|
2022-12-01 06:38:45 +08:00
|
|
|
if killed then
|
|
|
|
output = output .. "\n" .. "Process was killed because it reached the timeout"
|
2022-12-31 17:38:03 +08:00
|
|
|
elseif signal ~= 0 then
|
|
|
|
output = output .. "\n" .. "Process was killed with SIG" .. M.signals[signal]
|
2022-12-01 06:38:45 +08:00
|
|
|
end
|
2022-11-21 06:04:56 +08:00
|
|
|
|
|
|
|
vim.schedule(function()
|
2022-12-01 06:38:45 +08:00
|
|
|
opts.on_exit(exit_code == 0 and signal == 0, output)
|
2022-11-21 06:04:56 +08:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
if not handle then
|
|
|
|
if opts.on_exit then
|
|
|
|
opts.on_exit(false, "Failed to spawn process " .. cmd .. " " .. vim.inspect(opts))
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2022-12-31 17:38:03 +08:00
|
|
|
M.running[handle] = true
|
2022-11-21 06:04:56 +08:00
|
|
|
|
2022-11-28 14:36:32 +08:00
|
|
|
---@param data? string
|
2022-11-21 06:04:56 +08:00
|
|
|
local function on_output(err, data)
|
|
|
|
assert(not err, err)
|
|
|
|
|
|
|
|
if data then
|
|
|
|
output = output .. data:gsub("\r\n", "\n")
|
|
|
|
local lines = vim.split(vim.trim(output:gsub("\r$", "")):gsub("[^\n\r]+\r", ""), "\n")
|
|
|
|
|
|
|
|
if opts.on_line then
|
|
|
|
vim.schedule(function()
|
|
|
|
opts.on_line(lines[#lines])
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-01 06:38:45 +08:00
|
|
|
uv.read_start(stdout, on_output)
|
|
|
|
uv.read_start(stderr, on_output)
|
2022-11-21 06:04:56 +08:00
|
|
|
|
|
|
|
return handle
|
|
|
|
end
|
|
|
|
|
2022-12-31 17:38:03 +08:00
|
|
|
function M.kill(handle)
|
|
|
|
if handle and not handle:is_closing() then
|
|
|
|
M.running[handle] = nil
|
|
|
|
uv.process_kill(handle, "sigint")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.abort()
|
|
|
|
for handle in pairs(M.running) do
|
|
|
|
M.kill(handle)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-21 17:12:39 +08:00
|
|
|
---@param cmd string[]
|
2022-12-24 18:27:29 +08:00
|
|
|
---@param opts? {cwd:string, env:table}
|
2022-12-21 17:12:39 +08:00
|
|
|
function M.exec(cmd, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
---@type string[]
|
|
|
|
local lines
|
|
|
|
local job = vim.fn.jobstart(cmd, {
|
|
|
|
cwd = opts.cwd,
|
|
|
|
pty = false,
|
2022-12-24 18:27:29 +08:00
|
|
|
env = opts.env,
|
2022-12-21 17:12:39 +08:00
|
|
|
stdout_buffered = true,
|
|
|
|
on_stdout = function(_, _lines)
|
|
|
|
lines = _lines
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
vim.fn.jobwait({ job })
|
|
|
|
return lines
|
|
|
|
end
|
|
|
|
|
2022-11-21 06:04:56 +08:00
|
|
|
return M
|