diff --git a/lua/nvim-tree/actions/reloaders/reloaders.lua b/lua/nvim-tree/actions/reloaders/reloaders.lua index 662f76c7abe..8c2c9683fe6 100644 --- a/lua/nvim-tree/actions/reloaders/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders/reloaders.lua @@ -8,12 +8,14 @@ local Iterator = require "nvim-tree.iterators.node-iterator" local M = {} -local function refresh_nodes(node, projects, unloaded_bufnr) +local function refresh_nodes(node, unloaded_bufnr) Iterator.builder({ node }) :applier(function(n) if n.open and n.nodes then - local project_root = git.get_project_root(n.cwd or n.link_to or n.absolute_path) - explorer_module.reload(n, projects[project_root] or {}, unloaded_bufnr) + local project = git.get_project(n.cwd or n.link_to or n.absolute_path) + if project then + explorer_module.reload(n, project, unloaded_bufnr) + end end end) :recursor(function(n) @@ -22,13 +24,15 @@ local function refresh_nodes(node, projects, unloaded_bufnr) :iterate() end -function M.reload_node_status(parent_node, projects) - local project_root = git.get_project_root(parent_node.absolute_path) - local status = projects[project_root] or {} +function M.reload_node_status(parent_node) + local project = git.get_project(parent_node.absolute_path) + if not project then + return + end for _, node in ipairs(parent_node.nodes) do - explorer_node.update_git_status(node, explorer_node.is_git_ignored(parent_node), status) + explorer_node.update_git_status(node, explorer_node.is_git_ignored(parent_node), project) if node.nodes and #node.nodes > 0 then - M.reload_node_status(node, projects) + M.reload_node_status(node) end end end @@ -42,8 +46,8 @@ function M.reload_explorer(_, unloaded_bufnr) end event_running = true - local projects = git.reload() - refresh_nodes(core.get_explorer(), projects, unloaded_bufnr) + git.reload() + refresh_nodes(core.get_explorer(), unloaded_bufnr) if view.is_visible() then renderer.draw(unloaded_bufnr) end @@ -56,8 +60,8 @@ function M.reload_git() end event_running = true - local projects = git.reload() - M.reload_node_status(core.get_explorer(), projects) + git.reload() + M.reload_node_status(core.get_explorer()) renderer.draw() event_running = false end diff --git a/lua/nvim-tree/explorer/explore.lua b/lua/nvim-tree/explorer/explore.lua index 792a9aa6a24..72e88b35810 100644 --- a/lua/nvim-tree/explorer/explore.lua +++ b/lua/nvim-tree/explorer/explore.lua @@ -57,7 +57,7 @@ local function populate_children(handle, cwd, node, git_status) end end -function M.explore(node, status) +function M.explore(node, project) local cwd = node.link_to or node.absolute_path local handle = vim.loop.fs_scandir(cwd) if not handle then @@ -66,15 +66,15 @@ function M.explore(node, status) local profile = log.profile_start("explore init %s", node.absolute_path) - populate_children(handle, cwd, node, status) + populate_children(handle, cwd, node, project) local is_root = not node.parent local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1] if M.config.group_empty and not is_root and child_folder_only then local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path - local child_status = git.load_project_status(child_cwd) + local child_project = git.get_project(child_cwd) node.group_next = child_folder_only - local ns = M.explore(child_folder_only, child_status) + local ns = M.explore(child_folder_only, child_project) node.nodes = ns or {} log.profile_end(profile) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index a47ac4d7423..93455bca9e7 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -24,8 +24,8 @@ end function Explorer:_load(node) local cwd = node.link_to or node.absolute_path - local git_status = git.load_project_status(cwd) - M.explore(node, git_status) + local project = git.get_project(cwd) + M.explore(node, project) end function Explorer:expand(node) diff --git a/lua/nvim-tree/explorer/node.lua b/lua/nvim-tree/explorer/node.lua index 3d0119be655..1ca5d6e43bd 100644 --- a/lua/nvim-tree/explorer/node.lua +++ b/lua/nvim-tree/explorer/node.lua @@ -32,7 +32,11 @@ function M.has_one_child_folder(node) return #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R") end -function M.update_git_status(node, parent_ignored, status) +function M.update_git_status(node, parent_ignored, project) + if not project then + return + end + local get_status if node.nodes then get_status = get_dir_git_status @@ -41,11 +45,11 @@ function M.update_git_status(node, parent_ignored, status) end -- status of the node's absolute path - node.git_status = get_status(parent_ignored, status, node.absolute_path) + node.git_status = get_status(parent_ignored, project, node.absolute_path) -- status of the link target, if the link itself is not dirty if node.link_to and not node.git_status then - node.git_status = get_status(parent_ignored, status, node.link_to) + node.git_status = get_status(parent_ignored, project, node.link_to) end end diff --git a/lua/nvim-tree/explorer/reload.lua b/lua/nvim-tree/explorer/reload.lua index aee26dc266c..766e1949eb9 100644 --- a/lua/nvim-tree/explorer/reload.lua +++ b/lua/nvim-tree/explorer/reload.lua @@ -21,33 +21,24 @@ local function update_status(nodes_by_path, node_ignored, status) end end -local function reload_and_get_git_project(path, callback) - local project_root = git.get_project_root(path) - - 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) +local function update_parent_statuses(node, project) while project and node do -- step up to the containing project - if node.absolute_path == root then + if node.absolute_path == project.toplevel then -- stop at the top of the tree if not node.parent then break end - root = git.get_project_root(node.parent.absolute_path) + project = git.get_project(node.parent.absolute_path) -- stop when no more projects - if not root then + if not project then break end -- update the containing project - project = git.get_project(root) - git.reload_project(root, node.absolute_path, nil) + git.reload_project(project, node.absolute_path, nil) end -- update status @@ -172,12 +163,13 @@ function M.refresh_node(node, callback) callback() end + local project = git.get_project(node.absolute_path) local parent_node = utils.get_parent_of_group(node) - reload_and_get_git_project(node.absolute_path, function(project_root, project) - require("nvim-tree.explorer.reload").reload(parent_node, project) + git.reload_project(project, node.absolute_path, function() + M.reload(parent_node, project) - update_parent_statuses(parent_node, project, project_root) + update_parent_statuses(parent_node, project) callback() end) @@ -211,11 +203,10 @@ function M.refresh_parent_nodes_for_path(path) -- refresh in order; this will expand groups as needed for _, node in ipairs(parent_nodes) do - local project_root = git.get_project_root(node.absolute_path) - local project = git.get_project(project_root) or {} + local project = git.get_project(node.absolute_path) M.reload(node, project) - update_parent_statuses(node, project, project_root) + update_parent_statuses(node, project) end log.profile_end(profile) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 8c4117b4f56..0a9cdb0e077 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -8,10 +8,17 @@ local explorer_node = require "nvim-tree.explorer.node" local M = { config = {}, - projects = {}, - cwd_to_project_root = {}, } +-- all projects keyed by toplevel +local projects_by_toplevel = {} + +-- index of paths inside projects +local projects_by_path = {} + +-- paths not inside a git project +local paths_no_project = {} + -- Files under .git that should result in a reload when changed. -- Utilities (like watchman) can also write to this directory (often) and aren't useful for us. local WATCHED_FILES = { @@ -22,7 +29,7 @@ local WATCHED_FILES = { "index", -- staging area } -local function reload_git_status(project_root, path, project, git_status) +local function reload_git_status(toplevel, path, project, git_status) if path then for p in pairs(project.files) do if p:find(path, 1, true) == 1 then @@ -34,7 +41,7 @@ local function reload_git_status(project_root, path, project, git_status) project.files = git_status end - project.dirs = git_utils.file_status_to_dir_status(project.files, project_root) + project.dirs = git_utils.file_status_to_dir_status(project.files, toplevel) end --- Is this path in a known ignored directory? @@ -56,181 +63,198 @@ local function path_ignored_in_project(path, project) return false end -function M.reload() +local function reload_tree_at(toplevel) if not M.config.git.enable then - return {} + return nil end - for project_root in pairs(M.projects) do - M.reload_project(project_root) + log.line("watcher", "git event executing '%s'", toplevel) + local root_node = utils.get_node_from_path(toplevel) + if not root_node then + return end - return M.projects -end + M.reload_project(toplevel, nil, function() + local project = projects_by_toplevel[toplevel] -function M.reload_project(project_root, path, callback) - local project = M.projects[project_root] - if not project or not M.config.git.enable then - if callback then - callback() - end - return - end + Iterator.builder(root_node.nodes) + :hidden() + :applier(function(node) + local parent_ignored = explorer_node.is_git_ignored(node.parent) + explorer_node.update_git_status(node, parent_ignored, project) + end) + :recursor(function(node) + return node.nodes and #node.nodes > 0 and node.nodes + end) + :iterate() - if path and (path:find(project_root, 1, true) ~= 1 or path_ignored_in_project(path, project)) then - if callback then - callback() - end - return - end + require("nvim-tree.renderer").draw() + end) +end - local opts = { - project_root = project_root, - path = path, - list_untracked = git_utils.should_show_untracked(project_root), +--- Create a populated git project synchronously. +--- @param toplevel string absolute +--- @param git_dir string absolute +--- @return table project +local function create_project(toplevel, git_dir) + local git_status = Runner.run { + toplevel = toplevel, + list_untracked = git_utils.should_show_untracked(toplevel), list_ignored = true, timeout = M.config.git.timeout, } - if callback then - Runner.run(opts, function(git_status) - reload_git_status(project_root, path, project, git_status) - callback() - end) - else - -- TODO use callback once async/await is available - local git_status = Runner.run(opts) - reload_git_status(project_root, path, project, git_status) - end -end + local project = { + toplevel = toplevel, + git_dir = git_dir, + files = git_status, + dirs = git_utils.file_status_to_dir_status(git_status, toplevel), + } + projects_by_toplevel[toplevel] = project -function M.get_project(project_root) - return M.projects[project_root] -end + if M.config.filesystem_watchers.enable then + log.line("watcher", "git start") -function M.get_project_root(cwd) - if not M.config.git.enable then - return nil - end + local callback = function(w) + log.line("watcher", "git event scheduled '%s'", w.toplevel) + utils.debounce("git:watcher:" .. w.toplevel, M.config.filesystem_watchers.debounce_delay, function() + if w.destroyed then + return + end + reload_tree_at(w.toplevel) + end) + end - if M.cwd_to_project_root[cwd] then - return M.cwd_to_project_root[cwd] + project.watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { + toplevel = toplevel, + }) end - if M.cwd_to_project_root[cwd] == false then - return nil + return project +end + +--- Find an project known for a path. +--- Updates paths_no_project when not a directory. +--- @param path string absolute +--- @return table|nil project +local function find_project(path) + -- known + if projects_by_path[path] then + return projects_by_path[path] end - local stat, _ = vim.loop.fs_stat(cwd) + -- ignore non-directories + local stat, _ = vim.loop.fs_stat(path) if not stat or stat.type ~= "directory" then + paths_no_project[path] = true 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") - local disabled_norm = vim.fn.fnamemodify(disabled_for_dir, ":p") - if toplevel_norm == disabled_norm then - return nil + for _, project in pairs(projects_by_toplevel) do + if project and path_ignored_in_project(path, project) then + projects_by_path[path] = project + return project end end - M.cwd_to_project_root[cwd] = toplevel - return M.cwd_to_project_root[cwd] + return nil end -local function reload_tree_at(project_root) +--- Reload all git projects +function M.reload() if not M.config.git.enable then - return nil + return {} end - log.line("watcher", "git event executing '%s'", project_root) - local root_node = utils.get_node_from_path(project_root) - if not root_node then - return + for _, project in pairs(projects_by_toplevel) do + M.reload_project(project, nil, nil) end - - M.reload_project(project_root, nil, function() - local git_status = M.get_project(project_root) - - Iterator.builder(root_node.nodes) - :hidden() - :applier(function(node) - local parent_ignored = explorer_node.is_git_ignored(node.parent) - explorer_node.update_git_status(node, parent_ignored, git_status) - end) - :recursor(function(node) - return node.nodes and #node.nodes > 0 and node.nodes - end) - :iterate() - - require("nvim-tree.renderer").draw() - end) end -function M.load_project_status(cwd) - if not M.config.git.enable then - return {} - end - - local project_root = M.get_project_root(cwd) - if not project_root then - M.cwd_to_project_root[cwd] = false - return {} +--- Reload the git project. +--- @param project table|nil +--- @param path string|nil only reload this path, NOP if this path is ignored +--- @param callback function|nil no arguments +function M.reload_project(project, path, callback) + if not project or not M.config.git.enable then + if callback then + callback() + end + return end - local status = M.projects[project_root] - if status then - return status + if path and (path:find(project.toplevel, 1, true) ~= 1 or path_ignored_in_project(path, project)) then + if callback then + callback() + end + return end - local git_status = Runner.run { - project_root = project_root, - list_untracked = git_utils.should_show_untracked(project_root), + local opts = { + toplevel = project.toplevel, + path = path, + list_untracked = git_utils.should_show_untracked(project.toplevel), list_ignored = true, timeout = M.config.git.timeout, } - local watcher = nil - if M.config.filesystem_watchers.enable then - log.line("watcher", "git start") + if callback then + Runner.run(opts, function(git_status) + reload_git_status(project.toplevel, path, project, git_status) + callback() + end) + else + -- TODO use callback once async/await is available + local git_status = Runner.run(opts) + reload_git_status(project.toplevel, path, project, git_status) + end +end - local callback = function(w) - log.line("watcher", "git event scheduled '%s'", w.project_root) - utils.debounce("git:watcher:" .. w.project_root, M.config.filesystem_watchers.debounce_delay, function() - if w.destroyed then - return - end - reload_tree_at(w.project_root) - end) - end +--- Retrieve the project containing a path, creating and populating if necessary. +--- @param path string absolute +--- @return table|nil project +function M.get_project(path) + if not M.config.git.enable or paths_no_project[path] then + return nil + end - local git_dir = vim.env.GIT_DIR or utils.path_join { project_root, ".git" } - watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { - project_root = project_root, - }) + -- existing project known for this path + local project = find_project(path) + if project then + return project end - M.projects[project_root] = { - files = git_status, - dirs = git_utils.file_status_to_dir_status(git_status, project_root), - watcher = watcher, - } - return M.projects[project_root] + -- determine git directories + local toplevel, git_dir = git_utils.get_toplevel(path) + if not toplevel or not git_dir then + paths_no_project[path] = true + return nil + end + + -- exisiting project now known for this path + project = projects_by_toplevel[toplevel] + if project then + projects_by_path[path] = project + return project + end + + -- lazily create the new project + project = create_project(toplevel, git_dir) + projects_by_path[path] = project + return project end function M.purge_state() log.line("git", "purge_state") - M.projects = {} - M.cwd_to_project_root = {} + for _, project in pairs(projects_by_toplevel) do + if project.watcher then + project.watcher:destroy() + end + end + projects_by_toplevel = {} + projects_by_path = {} + paths_no_project = {} end --- Disable git integration permanently diff --git a/lua/nvim-tree/git/runner.lua b/lua/nvim-tree/git/runner.lua index 6b200b6dba4..915579d84dc 100644 --- a/lua/nvim-tree/git/runner.lua +++ b/lua/nvim-tree/git/runner.lua @@ -14,7 +14,7 @@ function Runner:_parse_status_output(status, path) path = path:gsub("/", "\\") end if #status > 0 and #path > 0 then - self.output[utils.path_remove_trailing(utils.path_join { self.project_root, path })] = status + self.output[utils.path_remove_trailing(utils.path_join { self.toplevel, path })] = status end end @@ -57,7 +57,7 @@ function Runner:_getopts(stdout_handle, stderr_handle) local ignored = (self.list_untracked and self.list_ignored) and "--ignored=matching" or "--ignored=no" return { args = { "--no-optional-locks", "status", "--porcelain=v1", "-z", ignored, untracked, self.path }, - cwd = self.project_root, + cwd = self.toplevel, stdio = { nil, stdout_handle, stderr_handle }, } end @@ -151,7 +151,7 @@ end function Runner:_finalise(opts) if self.rc == -1 then - log.line("git", "job timed out %s %s", opts.project_root, opts.path) + log.line("git", "job timed out %s %s", opts.toplevel, opts.path) timeouts = timeouts + 1 if timeouts == MAX_TIMEOUTS then notify.warn( @@ -164,9 +164,9 @@ function Runner:_finalise(opts) require("nvim-tree.git").disable_git_integration() end elseif self.rc ~= 0 then - log.line("git", "job fail rc %d %s %s", self.rc, opts.project_root, opts.path) + log.line("git", "job fail rc %d %s %s", self.rc, opts.toplevel, opts.path) else - log.line("git", "job success %s %s", opts.project_root, opts.path) + log.line("git", "job success %s %s", opts.toplevel, opts.path) end end @@ -176,7 +176,7 @@ end --- @return table|nil status by absolute path, nil if callback present function Runner.run(opts, callback) local self = setmetatable({ - project_root = opts.project_root, + toplevel = opts.toplevel, path = opts.path, list_untracked = opts.list_untracked, list_ignored = opts.list_ignored, @@ -186,7 +186,7 @@ function Runner.run(opts, callback) }, Runner) local async = callback ~= nil - local profile = log.profile_start("git %s job %s %s", async and "async" or "sync", opts.project_root, opts.path) + local profile = log.profile_start("git %s job %s %s", async and "async" or "sync", opts.toplevel, opts.path) if async and callback then -- async, always call back diff --git a/lua/nvim-tree/git/utils.lua b/lua/nvim-tree/git/utils.lua index b7e67c9a336..d507f0562bf 100644 --- a/lua/nvim-tree/git/utils.lua +++ b/lua/nvim-tree/git/utils.lua @@ -1,45 +1,68 @@ local M = {} local log = require "nvim-tree.log" +local utils = require "nvim-tree.utils" local has_cygpath = vim.fn.executable "cygpath" == 1 --- Retrieve the git toplevel directory --- @param cwd string path --- @return string|nil toplevel absolute path +--- @return string|nil git_dir absolute path function M.get_toplevel(cwd) - local profile = log.profile_start("git toplevel %s", cwd) + if not cwd then + return nil, nil + end + + local profile = log.profile_start("git toplevel git_dir %s", cwd) - local cmd = { "git", "-C", cwd, "rev-parse", "--show-toplevel" } - log.line("git", "%s", vim.inspect(cmd)) + -- both paths are absolute + local cmd = { "git", "-C", cwd, "rev-parse", "--show-toplevel", "--absolute-git-dir" } + log.line("git", "%s", table.concat(cmd, " ")) - local toplevel = vim.fn.system(cmd) + local out = vim.fn.system(cmd) - log.raw("git", toplevel) + log.raw("git", out) log.profile_end(profile) - if vim.v.shell_error ~= 0 or not toplevel or #toplevel == 0 or toplevel:match "fatal" then - return nil + if vim.v.shell_error ~= 0 or not out or #out == 0 or out:match "fatal" then + return nil, nil + end + + local toplevel, git_dir = out:match "([^\n]+)\n+([^\n]+)" + if not toplevel then + return nil, nil + end + if not git_dir then + git_dir = utils.path_join { toplevel, ".git" } end -- git always returns path with forward slashes if vim.fn.has "win32" == 1 then -- msys2 git support if has_cygpath then - toplevel = vim.fn.system("cygpath -w " .. vim.fn.shellescape(toplevel:sub(0, -2))) + toplevel = vim.fn.system("cygpath -w " .. vim.fn.shellescape(toplevel)) if vim.v.shell_error ~= 0 then - return nil + return nil, nil + end + git_dir = vim.fn.system("cygpath -w " .. vim.fn.shellescape(git_dir)) + if vim.v.shell_error ~= 0 then + return nil, nil end end toplevel = toplevel:gsub("/", "\\") + git_dir = git_dir:gsub("/", "\\") end - -- remove newline - return toplevel:sub(0, -2) + return toplevel, git_dir end local untracked = {} function M.should_show_untracked(cwd) + if not cwd then + return false + end + if untracked[cwd] ~= nil then return untracked[cwd] end @@ -47,7 +70,7 @@ function M.should_show_untracked(cwd) local profile = log.profile_start("git untracked %s", cwd) local cmd = { "git", "-C", cwd, "config", "status.showUntrackedFiles" } - log.line("git", vim.inspect(cmd)) + log.line("git", table.concat(cmd, " ")) local has_untracked = vim.fn.system(cmd)