fix(#1931): do not execute git status in git ignored directories (#2326)

* fix(#1931): do not execute git status in git ignored directories

* fix(#1931): reload.refresh_node is always asynchronous
This commit is contained in:
Alexander Courtis 2023-07-23 17:12:49 +10:00 committed by GitHub
parent 3b62c6bf2c
commit 273c1700eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 28 deletions

View File

@ -205,7 +205,6 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufWritePost", {
callback = function()
if opts.auto_reload_on_write and not opts.filesystem_watchers.enable then
log.line("dev", "BufWritePost reloading")
reloaders.reload_explorer()
end
end,

View File

@ -20,18 +20,12 @@ local function update_status(nodes_by_path, node_ignored, status)
end
end
-- TODO always use callback once async/await is available
local function reload_and_get_git_project(path, callback)
local project_root = git.get_project_root(path)
if callback then
git.reload_project(project_root, path, function()
callback(project_root, git.get_project(project_root) or {})
end)
else
git.reload_project(project_root, path)
return project_root, git.get_project(project_root) or {}
end
git.reload_project(project_root, path, function()
callback(project_root, git.get_project(project_root) or {})
end)
end
local function update_parent_statuses(node, project, root)
@ -149,32 +143,21 @@ end
---Refresh contents and git status for a single node
---@param node table
---@param callback function
function M.refresh_node(node, callback)
if type(node) ~= "table" then
if callback then
callback()
end
return
callback()
end
local parent_node = utils.get_parent_of_group(node)
if callback then
reload_and_get_git_project(node.absolute_path, function(project_root, project)
require("nvim-tree.explorer.reload").reload(parent_node, project)
update_parent_statuses(parent_node, project, project_root)
callback()
end)
else
-- TODO use callback once async/await is available
local project_root, project = reload_and_get_git_project(node.absolute_path)
reload_and_get_git_project(node.absolute_path, function(project_root, project)
require("nvim-tree.explorer.reload").reload(parent_node, project)
update_parent_statuses(parent_node, project, project_root)
end
callback()
end)
end
function M.setup(opts)

View File

@ -37,6 +37,25 @@ local function reload_git_status(project_root, path, project, git_status)
project.dirs = git_utils.file_status_to_dir_status(project.files, project_root)
end
--- Is this path in a known ignored directory?
--- @param path string
--- @param project table git status
--- @return boolean
local function path_ignored_in_project(path, project)
if not path or not project then
return false
end
if project and project.files then
for file, status in pairs(project.files) do
if status == "!!" and vim.startswith(path, file) then
return true
end
end
end
return false
end
function M.reload()
if not M.config.git.enable then
return {}
@ -58,7 +77,7 @@ function M.reload_project(project_root, path, callback)
return
end
if path and path:find(project_root, 1, true) ~= 1 then
if path and (path:find(project_root, 1, true) ~= 1 or path_ignored_in_project(path, project)) then
if callback then
callback()
end
@ -107,6 +126,14 @@ function M.get_project_root(cwd)
return nil
end
-- short-circuit any known ignored paths
for root, project in pairs(M.projects) do
if project and path_ignored_in_project(cwd, project) then
M.cwd_to_project_root[cwd] = root
return root
end
end
local toplevel = git_utils.get_toplevel(cwd)
for _, disabled_for_dir in ipairs(M.config.git.disable_for_dirs) do
local toplevel_norm = vim.fn.fnamemodify(toplevel, ":p")