From f523710e95bf2837d323005f6eb7300c7e3c4ed0 Mon Sep 17 00:00:00 2001 From: guanghechen <42513619+guanghechen@users.noreply.github.com> Date: Tue, 1 Jul 2025 17:19:17 +0800 Subject: [PATCH] fix(#1939): fix the logic of ignore-checking, prefer to call the git check-ignore for git repos (#2383) --- lua/avante/llm_tools/helpers.lua | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lua/avante/llm_tools/helpers.lua b/lua/avante/llm_tools/helpers.lua index a7db08f..8d89651 100644 --- a/lua/avante/llm_tools/helpers.lua +++ b/lua/avante/llm_tools/helpers.lua @@ -72,7 +72,7 @@ end ---@param abs_path string ---@return boolean -function M.is_ignored(abs_path) +local function old_is_ignored(abs_path) local project_root = Utils.get_project_root() local gitignore_path = project_root .. "/.gitignore" 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) 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 ---@return boolean function M.has_permission_to_access(abs_path)