Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 9 additions & 26 deletions lua/nvim-tree/explorer/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 28 additions & 1 deletion lua/nvim-tree/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down