refine: replace vim.fn.system with vim.system (#2863)

This commit is contained in:
XuJiawei
2025-12-13 18:58:10 +08:00
committed by GitHub
parent bbf6d8f0d9
commit 42028811e4
8 changed files with 138 additions and 106 deletions

View File

@@ -129,21 +129,17 @@ end
---@return boolean
function M.is_ignored(abs_path)
local project_root = Utils.get_project_root()
local cmd =
string.format("git -C %s check-ignore %s", vim.fn.shellescape(project_root), vim.fn.shellescape(abs_path))
local exit_code = vim
.system({ "git", "-C", vim.fn.shellescape(project_root), "check-ignore", vim.fn.shellescape(abs_path) }, { text = true })
:wait().code
local result = vim.fn.system(cmd)
local exit_code = vim.v.shell_error
-- If command failed or git is not available, fall back to old method
-- If command failed or git is not available or not a git repository, fall back to old method
if exit_code ~= 0 and exit_code ~= 1 then return old_is_ignored(abs_path) end
-- Check if result indicates this is not a git repository
if result:sub(1, 26) == "fatal: not a git repository" then return old_is_ignored(abs_path) end
-- git check-ignore returns:
-- - exit code 0 and outputs the path if the file is ignored
-- - exit code 1 and no output if the file is not ignored
-- - exit code 0 and outputs the path to stdout if the file is ignored
-- - exit code 1 and no output to stdout if the file is not ignored
-- - exit code 128 and outputs the error info to stderr not stdout
return exit_code == 0
end