refactor(git): consolidate sync subprocess pattern via util.system_sync
This commit is contained in:
@@ -441,4 +441,29 @@ function M.split_lines(content)
|
||||
return lines
|
||||
end
|
||||
|
||||
---Run a system command synchronously and return stdout on success. On
|
||||
---non-zero exit, logs stderr via `log.error` and returns nil. Pass
|
||||
---`opts.silent` to suppress the auto-log when failure is expected (e.g.
|
||||
---probe-style commands like `git rev-parse` against a possibly-missing
|
||||
---ref).
|
||||
---@param cmd string[]
|
||||
---@param opts { cwd: string?, stdin: string?, silent: boolean? }?
|
||||
---@return string?
|
||||
function M.system_sync(cmd, opts)
|
||||
opts = opts or {}
|
||||
local result = vim.system(cmd, {
|
||||
cwd = opts.cwd,
|
||||
stdin = opts.stdin,
|
||||
text = true,
|
||||
}):wait()
|
||||
if result.code ~= 0 then
|
||||
if not opts.silent then
|
||||
local label = cmd[2] and (cmd[1] .. " " .. cmd[2]) or cmd[1] or "?"
|
||||
log.error("%s failed: %s", label, vim.trim(result.stderr or ""))
|
||||
end
|
||||
return nil
|
||||
end
|
||||
return result.stdout or ""
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user