diff --git a/lua/nvim-tree/actions/reloaders/reloaders.lua b/lua/nvim-tree/actions/reloaders/reloaders.lua index 662f76c7abe..ffa7d073868 100644 --- a/lua/nvim-tree/actions/reloaders/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders/reloaders.lua @@ -12,8 +12,8 @@ local function refresh_nodes(node, projects, 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 toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path) + explorer_module.reload(n, projects[toplevel] or {}, unloaded_bufnr) end end) :recursor(function(n) @@ -23,8 +23,8 @@ local function refresh_nodes(node, projects, unloaded_bufnr) 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 {} + local toplevel = git.get_toplevel(parent_node.absolute_path) + local status = projects[toplevel] or {} for _, node in ipairs(parent_node.nodes) do explorer_node.update_git_status(node, explorer_node.is_git_ignored(parent_node), status) if node.nodes and #node.nodes > 0 then diff --git a/lua/nvim-tree/explorer/reload.lua b/lua/nvim-tree/explorer/reload.lua index aee26dc266c..cd1aa458170 100644 --- a/lua/nvim-tree/explorer/reload.lua +++ b/lua/nvim-tree/explorer/reload.lua @@ -22,10 +22,10 @@ local function update_status(nodes_by_path, node_ignored, status) end local function reload_and_get_git_project(path, callback) - local project_root = git.get_project_root(path) + local toplevel = git.get_toplevel(path) - git.reload_project(project_root, path, function() - callback(project_root, git.get_project(project_root) or {}) + git.reload_project(toplevel, path, function() + callback(toplevel, git.get_project(toplevel) or {}) end) end @@ -38,7 +38,7 @@ local function update_parent_statuses(node, project, root) break end - root = git.get_project_root(node.parent.absolute_path) + root = git.get_toplevel(node.parent.absolute_path) -- stop when no more projects if not root then @@ -174,10 +174,10 @@ function M.refresh_node(node, callback) local parent_node = utils.get_parent_of_group(node) - reload_and_get_git_project(node.absolute_path, function(project_root, project) + reload_and_get_git_project(node.absolute_path, function(toplevel, project) require("nvim-tree.explorer.reload").reload(parent_node, project) - update_parent_statuses(parent_node, project, project_root) + update_parent_statuses(parent_node, project, toplevel) callback() end) @@ -211,11 +211,11 @@ 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 toplevel = git.get_toplevel(node.absolute_path) + local project = git.get_project(toplevel) or {} M.reload(node, project) - update_parent_statuses(node, project, project_root) + update_parent_statuses(node, project, toplevel) end log.profile_end(profile) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 8c4117b4f56..d2c180c9f58 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -8,8 +8,15 @@ local explorer_node = require "nvim-tree.explorer.node" local M = { config = {}, - projects = {}, - cwd_to_project_root = {}, + + -- all projects keyed by toplevel + _projects_by_toplevel = {}, + + -- index of paths inside toplevels, false when not inside a project + _toplevels_by_path = {}, + + -- git dirs by toplevel + _git_dirs_by_toplevel = {}, } -- Files under .git that should result in a reload when changed. @@ -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,28 +63,34 @@ local function path_ignored_in_project(path, project) return false end +--- Reload all projects +--- @return table projects maybe empty function M.reload() if not M.config.git.enable then return {} end - for project_root in pairs(M.projects) do - M.reload_project(project_root) + for toplevel in pairs(M._projects_by_toplevel) do + M.reload_project(toplevel) end - return M.projects + return M._projects_by_toplevel end -function M.reload_project(project_root, path, callback) - local project = M.projects[project_root] - if not project or not M.config.git.enable then +--- Reload one project. Does nothing when no project or path is ignored +--- @param toplevel string|nil +--- @param path string|nil optional path to update only +--- @param callback function|nil +function M.reload_project(toplevel, path, callback) + local project = M._projects_by_toplevel[toplevel] + if not toplevel or not project or not M.config.git.enable then if callback then callback() end return end - if path and (path:find(project_root, 1, true) ~= 1 or path_ignored_in_project(path, project)) then + if path and (path:find(toplevel, 1, true) ~= 1 or path_ignored_in_project(path, project)) then if callback then callback() end @@ -85,56 +98,71 @@ function M.reload_project(project_root, path, callback) end local opts = { - project_root = project_root, + toplevel = toplevel, path = path, - list_untracked = git_utils.should_show_untracked(project_root), + 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) + reload_git_status(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_root, path, project, git_status) + reload_git_status(toplevel, path, project, git_status) end end -function M.get_project(project_root) - return M.projects[project_root] +--- Retrieve a known project +--- @return table|nil project +function M.get_project(toplevel) + return M._projects_by_toplevel[toplevel] end -function M.get_project_root(cwd) +--- Retrieve the toplevel for a path. nil on: +--- git disabled +--- not part of a project +--- not a directory +--- path in git.disable_for_dirs +--- @param path string absolute +--- @return string|nil +function M.get_toplevel(path) if not M.config.git.enable then return nil end - if M.cwd_to_project_root[cwd] then - return M.cwd_to_project_root[cwd] + if M._toplevels_by_path[path] then + return M._toplevels_by_path[path] end - if M.cwd_to_project_root[cwd] == false then + if M._toplevels_by_path[path] == false then return nil end - local stat, _ = vim.loop.fs_stat(cwd) + local stat, _ = vim.loop.fs_stat(path) if not stat or stat.type ~= "directory" then 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 + for root, project in pairs(M._projects_by_toplevel) do + if project and path_ignored_in_project(path, project) then + M._toplevels_by_path[path] = root return root end end - local toplevel = git_utils.get_toplevel(cwd) + -- attempt to fetch toplevel + local toplevel, git_dir = git_utils.get_toplevel(path) + if not toplevel or not git_dir then + return nil + end + + -- ignore disabled paths 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") @@ -143,23 +171,24 @@ function M.get_project_root(cwd) end end - M.cwd_to_project_root[cwd] = toplevel - return M.cwd_to_project_root[cwd] + M._toplevels_by_path[path] = toplevel + M._git_dirs_by_toplevel[toplevel] = git_dir + return M._toplevels_by_path[path] end -local function reload_tree_at(project_root) - if not M.config.git.enable then +local function reload_tree_at(toplevel) + if not M.config.git.enable or not toplevel then return nil end - log.line("watcher", "git event executing '%s'", project_root) - local root_node = utils.get_node_from_path(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 - M.reload_project(project_root, nil, function() - local git_status = M.get_project(project_root) + M.reload_project(toplevel, nil, function() + local git_status = M.get_project(toplevel) Iterator.builder(root_node.nodes) :hidden() @@ -176,25 +205,29 @@ local function reload_tree_at(project_root) end) end -function M.load_project_status(cwd) +--- Load the project status for a path. Does nothing when no toplevel for path. +--- Only fetches project status when unknown, otherwise returns existing. +--- @param path string absolute +--- @return table project maybe empty +function M.load_project_status(path) 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 + local toplevel = M.get_toplevel(path) + if not toplevel then + M._toplevels_by_path[path] = false return {} end - local status = M.projects[project_root] + local status = M._projects_by_toplevel[toplevel] if status then return status end local git_status = Runner.run { - project_root = project_root, - list_untracked = git_utils.should_show_untracked(project_root), + toplevel = toplevel, + list_untracked = git_utils.should_show_untracked(toplevel), list_ignored = true, timeout = M.config.git.timeout, } @@ -204,33 +237,41 @@ function M.load_project_status(cwd) log.line("watcher", "git start") 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() + 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.project_root) + reload_tree_at(w.toplevel) end) end - local git_dir = vim.env.GIT_DIR or utils.path_join { project_root, ".git" } + local git_dir = vim.env.GIT_DIR or M._git_dirs_by_toplevel[toplevel] or utils.path_join { toplevel, ".git" } watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { - project_root = project_root, + toplevel = toplevel, }) end - M.projects[project_root] = { + M._projects_by_toplevel[toplevel] = { files = git_status, - dirs = git_utils.file_status_to_dir_status(git_status, project_root), + dirs = git_utils.file_status_to_dir_status(git_status, toplevel), watcher = watcher, } - return M.projects[project_root] + return M._projects_by_toplevel[toplevel] end function M.purge_state() log.line("git", "purge_state") - M.projects = {} - M.cwd_to_project_root = {} + + for _, project in pairs(M._projects_by_toplevel) do + if project.watcher then + project.watcher:destroy() + end + end + + M._projects_by_toplevel = {} + M._toplevels_by_path = {} + M._git_dirs_by_toplevel = {} 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..9d5c58b908f 100644 --- a/lua/nvim-tree/git/utils.lua +++ b/lua/nvim-tree/git/utils.lua @@ -1,40 +1,55 @@ 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) + 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, nil + end + git_dir = vim.fn.system("cygpath -w " .. vim.fn.shellescape(git_dir)) if vim.v.shell_error ~= 0 then - return nil + 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 = {} @@ -47,7 +62,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)