fix(#1939): fix the logic of ignore-checking, prefer to call the git check-ignore for git repos (#2383)
This commit is contained in:
@@ -72,7 +72,7 @@ end
|
|||||||
|
|
||||||
---@param abs_path string
|
---@param abs_path string
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.is_ignored(abs_path)
|
local function old_is_ignored(abs_path)
|
||||||
local project_root = Utils.get_project_root()
|
local project_root = Utils.get_project_root()
|
||||||
local gitignore_path = project_root .. "/.gitignore"
|
local gitignore_path = project_root .. "/.gitignore"
|
||||||
local gitignore_patterns, gitignore_negate_patterns = Utils.parse_gitignore(gitignore_path)
|
local gitignore_patterns, gitignore_negate_patterns = Utils.parse_gitignore(gitignore_path)
|
||||||
@@ -84,6 +84,28 @@ function M.is_ignored(abs_path)
|
|||||||
return Utils.is_ignored(rel_path, gitignore_patterns, gitignore_negate_patterns)
|
return Utils.is_ignored(rel_path, gitignore_patterns, gitignore_negate_patterns)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param abs_path string
|
||||||
|
---@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 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 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
|
||||||
|
return exit_code == 0
|
||||||
|
end
|
||||||
|
|
||||||
---@param abs_path string
|
---@param abs_path string
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.has_permission_to_access(abs_path)
|
function M.has_permission_to_access(abs_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user