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

@@ -84,40 +84,72 @@ function M.func(input, opts)
if search_cmd == "" then return "", "No search command found" end
---execute the search command
local cmd = ""
local cmd = {}
if search_cmd:find("rg") then
cmd = string.format("%s --files-with-matches --hidden", search_cmd)
cmd = { search_cmd, "--files-with-matches", "--hidden" }
if input.case_sensitive then
cmd = string.format("%s --case-sensitive", cmd)
table.insert(cmd, "--case-sensitive")
else
cmd = string.format("%s --ignore-case", cmd)
table.insert(cmd, "--ignore-case")
end
if input.include_pattern then cmd = string.format("%s --glob '%s'", cmd, input.include_pattern) end
if input.exclude_pattern then cmd = string.format("%s --glob '!%s'", cmd, input.exclude_pattern) end
cmd = string.format("%s '%s' %s", cmd, input.query, abs_path)
if input.include_pattern then
table.insert(cmd, "--glob")
table.insert(cmd, input.include_pattern)
end
if input.exclude_pattern then
table.insert(cmd, "--glob")
table.insert(cmd, "!" .. input.exclude_pattern)
end
table.insert(cmd, input.query)
table.insert(cmd, abs_path)
elseif search_cmd:find("ag") then
cmd = string.format("%s --nocolor --nogroup --hidden", search_cmd)
if input.case_sensitive then cmd = string.format("%s --case-sensitive", cmd) end
if input.include_pattern then cmd = string.format("%s --ignore '!%s'", cmd, input.include_pattern) end
if input.exclude_pattern then cmd = string.format("%s --ignore '%s'", cmd, input.exclude_pattern) end
cmd = string.format("%s '%s' %s", cmd, input.query, abs_path)
cmd = { search_cmd, "--nocolor", "--nogroup", "--hidden" }
if input.case_sensitive then table.insert(cmd, "--case-sensitive") end
if input.include_pattern then
table.insert(cmd, "--ignore")
table.insert(cmd, "!" .. input.include_pattern)
end
if input.exclude_pattern then
table.insert(cmd, "--ignore")
table.insert(cmd, input.exclude_pattern)
end
table.insert(cmd, input.query)
table.insert(cmd, abs_path)
elseif search_cmd:find("ack") then
cmd = string.format("%s --nocolor --nogroup --hidden", search_cmd)
if input.case_sensitive then cmd = string.format("%s --smart-case", cmd) end
if input.exclude_pattern then cmd = string.format("%s --ignore-dir '%s'", cmd, input.exclude_pattern) end
cmd = string.format("%s '%s' %s", cmd, input.query, abs_path)
cmd = { search_cmd, "--nocolor", "--nogroup", "--hidden" }
if input.case_sensitive then table.insert(cmd, "--smart-case") end
if input.exclude_pattern then
table.insert(cmd, "--ignore-dir")
table.insert(cmd, input.exclude_pattern)
end
table.insert(cmd, input.query)
table.insert(cmd, abs_path)
elseif search_cmd:find("grep") then
cmd = string.format("cd %s && git ls-files -co --exclude-standard | xargs %s -rH", abs_path, search_cmd, abs_path)
if not input.case_sensitive then cmd = string.format("%s -i", cmd) end
if input.include_pattern then cmd = string.format("%s --include '%s'", cmd, input.include_pattern) end
if input.exclude_pattern then cmd = string.format("%s --exclude '%s'", cmd, input.exclude_pattern) end
cmd = string.format("%s '%s'", cmd, input.query)
local files =
vim.system({ "git", "-C", abs_path, "ls-files", "-co", "--exclude-standard" }, { text = true }):wait().stdout
cmd = { "grep", "-rH" }
if not input.case_sensitive then table.insert(cmd, "-i") end
if input.include_pattern then
table.insert(cmd, "--include")
table.insert(cmd, input.include_pattern)
end
if input.exclude_pattern then
table.insert(cmd, "--exclude")
table.insert(cmd, input.exclude_pattern)
end
table.insert(cmd, input.query)
if files ~= "" then
for _, path in ipairs(vim.split(files, "\n")) do
if not path:match("^%s*$") then table.insert(cmd, vim.fs.joinpath(abs_path, path)) end
end
else
table.insert(cmd, abs_path)
end
end
Utils.debug("cmd", cmd)
if on_log then on_log("Running command: " .. cmd) end
local result = vim.fn.system(cmd)
Utils.debug("cmd", table.concat(cmd, " "))
if on_log then on_log("Running command: " .. table.concat(cmd, " ")) end
local result = vim.system(cmd, { text = true }):wait().stdout or ""
local filepaths = vim.split(result, "\n")
return vim.json.encode(filepaths), nil

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

View File

@@ -389,20 +389,20 @@ function M.git_diff(input, opts)
if not project_root then return nil, "Not in a git repository" end
-- Check if we're in a git repository
local git_dir = vim.fn.system("git rev-parse --git-dir"):gsub("\n", "")
local git_dir = vim.system({ "git", "rev-parse", "--git-dir" }, { text = true }):wait().stdout:gsub("\n", "")
if git_dir == "" then return nil, "Not a git repository" end
-- Get the diff
local scope = input.scope or ""
local cmd = string.format("git diff --cached %s", scope)
if on_log then on_log("Running command: " .. cmd) end
local diff = vim.fn.system(cmd)
local cmd = { "git", "diff", "--cached", scope }
if on_log then on_log("Running command: " .. table.concat(cmd, " ")) end
local diff = vim.system(cmd, { text = true }):wait().stdout
if diff == "" then
-- If there's no staged changes, get unstaged changes
cmd = string.format("git diff %s", scope)
if on_log then on_log("No staged changes. Running command: " .. cmd) end
diff = vim.fn.system(cmd)
cmd = { "git", "diff", scope }
if on_log then on_log("No staged changes. Running command: " .. table.concat(cmd, " ")) end
diff = vim.system(cmd, { text = true }):wait().stdout
end
if diff == "" then return nil, "No changes detected" end
@@ -421,20 +421,23 @@ function M.git_commit(input, opts)
if not project_root then return false, "Not in a git repository" end
-- Check if we're in a git repository
local git_dir = vim.fn.system("git rev-parse --git-dir"):gsub("\n", "")
local git_dir = vim.system({ "git", "rev-parse", "--git-dir" }, { text = true }):wait().stdout:gsub("\n", "")
if git_dir == "" then return false, "Not a git repository" end
-- First check if there are any changes to commit
local status = vim.fn.system("git status --porcelain")
local status = vim.system({ "git", "status", "--porcelain" }, { text = true }):wait().stdout
if status == "" then return false, "No changes to commit" end
-- Get git user name and email
local git_user = vim.fn.system("git config user.name"):gsub("\n", "")
local git_email = vim.fn.system("git config user.email"):gsub("\n", "")
local git_user = vim.system({ "git", "config", "user.name" }, { text = true }):wait().stdout:gsub("\n", "")
local git_email = vim.system({ "git", "config", "user.email" }, { text = true }):wait().stdout:gsub("\n", "")
-- Check if GPG signing is available and configured
local has_gpg = false
local signing_key = vim.fn.system("git config --get user.signingkey"):gsub("\n", "")
local signing_key = vim
.system({ "git", "config", "--get", "user.signingkey" }, { text = true })
:wait().stdout
:gsub("\n", "")
if signing_key ~= "" then
-- Try to find gpg executable based on OS
@@ -449,8 +452,7 @@ function M.git_commit(input, opts)
if gpg_cmd ~= "" then
-- Verify GPG is working
local _ = vim.fn.system(string.format('"%s" --version', gpg_cmd))
has_gpg = vim.v.shell_error == 0
has_gpg = vim.system({ gpg_cmd, "--version" }, { text = true }):wait().code == 0
end
end
@@ -486,11 +488,11 @@ function M.git_commit(input, opts)
end
-- Stage changes if scope is provided
if input.scope then
local stage_cmd = string.format("git add %s", input.scope)
if on_log then on_log("Staging files: " .. stage_cmd) end
local stage_result = vim.fn.system(stage_cmd)
if vim.v.shell_error ~= 0 then
on_complete(false, "Failed to stage files: " .. stage_result)
local stage_cmd = { "git", "add", input.scope }
if on_log then on_log("Staging files: " .. table.concat(stage_cmd, " ")) end
local stage_result = vim.system(stage_cmd, { text = true }):wait()
if stage_result.code ~= 0 then
on_complete(false, "Failed to stage files: " .. stage_result.stderr)
return
end
end
@@ -501,16 +503,16 @@ function M.git_commit(input, opts)
if has_gpg then table.insert(cmd_parts, "-S") end
for _, line in ipairs(commit_msg_lines) do
table.insert(cmd_parts, "-m")
table.insert(cmd_parts, '"' .. line .. '"')
table.insert(cmd_parts, line)
end
local cmd = table.concat(cmd_parts, " ")
-- Execute git commit
if on_log then on_log("Running command: " .. cmd) end
local result = vim.fn.system(cmd)
local result = vim.system(cmd_parts, { text = true }):wait()
if vim.v.shell_error ~= 0 then
on_complete(false, "Failed to commit: " .. result)
if result.code ~= 0 then
on_complete(false, "Failed to commit: " .. result.stderr)
return
end