From 2a64948d015b71d86dd6bb4e2f9881ef16278686 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 12:22:41 +1000 Subject: [PATCH 1/9] fix(#2382): use --absolute-git-dir when available --- lua/nvim-tree/git/init.lua | 8 ++++++-- lua/nvim-tree/git/utils.lua | 39 +++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 8c4117b4f56..ee40f4077d7 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -10,6 +10,7 @@ local M = { config = {}, projects = {}, cwd_to_project_root = {}, + project_root_to_git_dir = {}, } -- Files under .git that should result in a reload when changed. @@ -134,7 +135,7 @@ function M.get_project_root(cwd) end end - local toplevel = git_utils.get_toplevel(cwd) + local toplevel, git_dir = 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") @@ -144,6 +145,9 @@ function M.get_project_root(cwd) end M.cwd_to_project_root[cwd] = toplevel + if toplevel then + M.project_root_to_git_dir[toplevel] = git_dir + end return M.cwd_to_project_root[cwd] end @@ -213,7 +217,7 @@ function M.load_project_status(cwd) 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.project_root_to_git_dir[project_root] or utils.path_join { project_root, ".git" } watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { project_root = project_root, }) diff --git a/lua/nvim-tree/git/utils.lua b/lua/nvim-tree/git/utils.lua index b7e67c9a336..52cd715397c 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) From bb8dcd7abc60e5bb65e4ccc352d29cd76f22bfc2 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 12:23:51 +1000 Subject: [PATCH 2/9] fix(#2382): use --absolute-git-dir when available --- lua/nvim-tree/git/init.lua | 4 +++- lua/nvim-tree/git/utils.lua | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index ee40f4077d7..f9fdfa07298 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -217,7 +217,9 @@ function M.load_project_status(cwd) end) end - local git_dir = vim.env.GIT_DIR or M.project_root_to_git_dir[project_root] or utils.path_join { project_root, ".git" } + local git_dir = vim.env.GIT_DIR + or M.project_root_to_git_dir[project_root] + or utils.path_join { project_root, ".git" } watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { project_root = project_root, }) diff --git a/lua/nvim-tree/git/utils.lua b/lua/nvim-tree/git/utils.lua index 52cd715397c..9d5c58b908f 100644 --- a/lua/nvim-tree/git/utils.lua +++ b/lua/nvim-tree/git/utils.lua @@ -24,7 +24,7 @@ function M.get_toplevel(cwd) return nil, nil end - local toplevel, git_dir = out:match("([^\n]+)\n+([^\n]+)") + local toplevel, git_dir = out:match "([^\n]+)\n+([^\n]+)" if not toplevel then return nil, nil end From 9f6dc6e7105dedef318d39df78418544510fb476 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 13:41:51 +1000 Subject: [PATCH 3/9] fix(#2382): git get_project_root, get_project -> get_project --- lua/nvim-tree/actions/reloaders/reloaders.lua | 23 ++++--- lua/nvim-tree/explorer/explore.lua | 4 +- lua/nvim-tree/explorer/init.lua | 4 +- lua/nvim-tree/explorer/node.lua | 6 +- lua/nvim-tree/explorer/reload.lua | 26 ++++---- lua/nvim-tree/git/init.lua | 64 ++++++++++--------- 6 files changed, 65 insertions(+), 62 deletions(-) diff --git a/lua/nvim-tree/actions/reloaders/reloaders.lua b/lua/nvim-tree/actions/reloaders/reloaders.lua index 662f76c7abe..b51956ed0bd 100644 --- a/lua/nvim-tree/actions/reloaders/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders/reloaders.lua @@ -8,12 +8,12 @@ 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) or {} + explorer_module.reload(n, project, unloaded_bufnr) end end) :recursor(function(n) @@ -22,13 +22,12 @@ 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) or {} 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 +41,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 +55,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..55f102a4dd3 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,7 +66,7 @@ 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] diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index a47ac4d7423..08e83174aad 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.load_project_status(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..03a9e935e2b 100644 --- a/lua/nvim-tree/explorer/node.lua +++ b/lua/nvim-tree/explorer/node.lua @@ -32,7 +32,7 @@ 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) local get_status if node.nodes then get_status = get_dir_git_status @@ -41,11 +41,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..09546dfdfb1 100644 --- a/lua/nvim-tree/explorer/reload.lua +++ b/lua/nvim-tree/explorer/reload.lua @@ -22,32 +22,31 @@ 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 project = git.get_project(path) or {} - git.reload_project(project_root, path, function() - callback(project_root, git.get_project(project_root) or {}) + git.reload_project(project.toplevel, path, function() + callback(project) 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.toplevel, node.absolute_path, nil) end -- update status @@ -174,10 +173,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(project) require("nvim-tree.explorer.reload").reload(parent_node, project) - update_parent_statuses(parent_node, project, project_root) + update_parent_statuses(parent_node, project) callback() end) @@ -211,11 +210,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) or {} 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 f9fdfa07298..4b320f5eea6 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -8,11 +8,12 @@ local explorer_node = require "nvim-tree.explorer.node" local M = { config = {}, - projects = {}, - cwd_to_project_root = {}, project_root_to_git_dir = {}, } +local projects = {} +local cwd_to_project_root = {} + -- 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 = { @@ -62,15 +63,15 @@ function M.reload() return {} end - for project_root in pairs(M.projects) do + for project_root in pairs(projects) do M.reload_project(project_root) end - return M.projects + return projects end function M.reload_project(project_root, path, callback) - local project = M.projects[project_root] + local project = projects[project_root] if not project or not M.config.git.enable then if callback then callback() @@ -105,20 +106,16 @@ function M.reload_project(project_root, path, callback) end end -function M.get_project(project_root) - return M.projects[project_root] -end - -function M.get_project_root(cwd) +local function get_project_root(cwd) 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 cwd_to_project_root[cwd] then + return cwd_to_project_root[cwd] end - if M.cwd_to_project_root[cwd] == false then + if cwd_to_project_root[cwd] == false then return nil end @@ -128,9 +125,9 @@ function M.get_project_root(cwd) end -- short-circuit any known ignored paths - for root, project in pairs(M.projects) do + for root, project in pairs(projects) do if project and path_ignored_in_project(cwd, project) then - M.cwd_to_project_root[cwd] = root + cwd_to_project_root[cwd] = root return root end end @@ -144,11 +141,19 @@ function M.get_project_root(cwd) end end - M.cwd_to_project_root[cwd] = toplevel + cwd_to_project_root[cwd] = toplevel if toplevel then M.project_root_to_git_dir[toplevel] = git_dir end - return M.cwd_to_project_root[cwd] + return cwd_to_project_root[cwd] +end + +--- Retrieve the project containing a path +--- @param path string absolute +--- @return table|nil project +function M.get_project(path) + local project_root = get_project_root(path) + return projects[project_root] end local function reload_tree_at(project_root) @@ -163,13 +168,13 @@ local function reload_tree_at(project_root) end M.reload_project(project_root, nil, function() - local git_status = M.get_project(project_root) + local project = projects[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) + explorer_node.update_git_status(node, parent_ignored, project) end) :recursor(function(node) return node.nodes and #node.nodes > 0 and node.nodes @@ -185,17 +190,19 @@ function M.load_project_status(cwd) return {} end - local project_root = M.get_project_root(cwd) + local project_root = get_project_root(cwd) if not project_root then - M.cwd_to_project_root[cwd] = false + cwd_to_project_root[cwd] = false return {} end - local status = M.projects[project_root] + local status = projects[project_root] if status then return status end + local git_dir = vim.env.GIT_DIR or M.project_root_to_git_dir[project_root] or utils.path_join { project_root, ".git" } + local git_status = Runner.run { project_root = project_root, list_untracked = git_utils.should_show_untracked(project_root), @@ -217,26 +224,25 @@ function M.load_project_status(cwd) end) end - local git_dir = vim.env.GIT_DIR - or M.project_root_to_git_dir[project_root] - or utils.path_join { project_root, ".git" } watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { project_root = project_root, }) end - M.projects[project_root] = { + projects[project_root] = { + toplevel = project_root, + git_dir = git_dir, files = git_status, dirs = git_utils.file_status_to_dir_status(git_status, project_root), watcher = watcher, } - return M.projects[project_root] + return projects[project_root] end function M.purge_state() log.line("git", "purge_state") - M.projects = {} - M.cwd_to_project_root = {} + projects = {} + cwd_to_project_root = {} end --- Disable git integration permanently From 21b86084e266ee8fba0e9fbff467faa16695afb9 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 14:55:17 +1000 Subject: [PATCH 4/9] fix(#2382): git reload_project takes a project --- lua/nvim-tree/explorer/reload.lua | 17 +++++------------ lua/nvim-tree/git/init.lua | 25 ++++++++++++++----------- lua/nvim-tree/git/utils.lua | 8 ++++++++ 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/lua/nvim-tree/explorer/reload.lua b/lua/nvim-tree/explorer/reload.lua index 09546dfdfb1..766e1949eb9 100644 --- a/lua/nvim-tree/explorer/reload.lua +++ b/lua/nvim-tree/explorer/reload.lua @@ -21,14 +21,6 @@ local function update_status(nodes_by_path, node_ignored, status) end end -local function reload_and_get_git_project(path, callback) - local project = git.get_project(path) or {} - - git.reload_project(project.toplevel, path, function() - callback(project) - end) -end - local function update_parent_statuses(node, project) while project and node do -- step up to the containing project @@ -46,7 +38,7 @@ local function update_parent_statuses(node, project) end -- update the containing project - git.reload_project(project.toplevel, node.absolute_path, nil) + git.reload_project(project, node.absolute_path, nil) end -- update status @@ -171,10 +163,11 @@ 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) - 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) @@ -210,7 +203,7 @@ 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 = git.get_project(node.absolute_path) or {} + local project = git.get_project(node.absolute_path) M.reload(node, project) update_parent_statuses(node, project) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 4b320f5eea6..451579229cd 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -11,6 +11,7 @@ local M = { project_root_to_git_dir = {}, } +-- TODO 2382 remove the toplevel key local projects = {} local cwd_to_project_root = {} @@ -58,20 +59,22 @@ local function path_ignored_in_project(path, project) return false end +--- Reload all git projects function M.reload() if not M.config.git.enable then return {} end - for project_root in pairs(projects) do - M.reload_project(project_root) + for _, project in pairs(projects) do + M.reload_project(project, nil, nil) end - - return projects end -function M.reload_project(project_root, path, callback) - local project = projects[project_root] +--- Reload the git project. +--- @param project table|nil +--- @param path string|nil only reload this path, NOP if 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() @@ -79,7 +82,7 @@ function M.reload_project(project_root, path, callback) 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(project.toplevel, 1, true) ~= 1 or path_ignored_in_project(path, project)) then if callback then callback() end @@ -87,22 +90,22 @@ function M.reload_project(project_root, path, callback) end local opts = { - project_root = project_root, + project_root = project.toplevel, path = path, - list_untracked = git_utils.should_show_untracked(project_root), + list_untracked = git_utils.should_show_untracked(project.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(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_root, path, project, git_status) + reload_git_status(project.toplevel, path, project, git_status) end end diff --git a/lua/nvim-tree/git/utils.lua b/lua/nvim-tree/git/utils.lua index 9d5c58b908f..d507f0562bf 100644 --- a/lua/nvim-tree/git/utils.lua +++ b/lua/nvim-tree/git/utils.lua @@ -9,6 +9,10 @@ local has_cygpath = vim.fn.executable "cygpath" == 1 --- @return string|nil toplevel absolute path --- @return string|nil git_dir absolute path function M.get_toplevel(cwd) + if not cwd then + return nil, nil + end + local profile = log.profile_start("git toplevel git_dir %s", cwd) -- both paths are absolute @@ -55,6 +59,10 @@ 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 From e69c18b7720562b14a358b5de9673e006312286c Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 16:52:46 +1000 Subject: [PATCH 5/9] fix(#2382): lazily create git projects --- lua/nvim-tree/actions/reloaders/reloaders.lua | 11 +- lua/nvim-tree/explorer/explore.lua | 4 +- lua/nvim-tree/explorer/init.lua | 3 +- lua/nvim-tree/explorer/node.lua | 4 + lua/nvim-tree/git/init.lua | 249 +++++++++--------- 5 files changed, 139 insertions(+), 132 deletions(-) diff --git a/lua/nvim-tree/actions/reloaders/reloaders.lua b/lua/nvim-tree/actions/reloaders/reloaders.lua index b51956ed0bd..8c2c9683fe6 100644 --- a/lua/nvim-tree/actions/reloaders/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders/reloaders.lua @@ -12,8 +12,10 @@ local function refresh_nodes(node, unloaded_bufnr) Iterator.builder({ node }) :applier(function(n) if n.open and n.nodes then - local project = git.get_project(n.cwd or n.link_to or n.absolute_path) or {} - explorer_module.reload(n, project, 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) @@ -23,7 +25,10 @@ local function refresh_nodes(node, unloaded_bufnr) end function M.reload_node_status(parent_node) - local project = git.get_project(parent_node.absolute_path) or {} + 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), project) if node.nodes and #node.nodes > 0 then diff --git a/lua/nvim-tree/explorer/explore.lua b/lua/nvim-tree/explorer/explore.lua index 55f102a4dd3..72e88b35810 100644 --- a/lua/nvim-tree/explorer/explore.lua +++ b/lua/nvim-tree/explorer/explore.lua @@ -72,9 +72,9 @@ function M.explore(node, project) 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 08e83174aad..1fbd207f429 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -24,7 +24,7 @@ end function Explorer:_load(node) local cwd = node.link_to or node.absolute_path - local project = git.load_project_status(cwd) + local project = git.get_project(cwd) M.explore(node, project) end @@ -56,3 +56,4 @@ end M.Explorer = Explorer return M + diff --git a/lua/nvim-tree/explorer/node.lua b/lua/nvim-tree/explorer/node.lua index 03a9e935e2b..1ca5d6e43bd 100644 --- a/lua/nvim-tree/explorer/node.lua +++ b/lua/nvim-tree/explorer/node.lua @@ -33,6 +33,10 @@ function M.has_one_child_folder(node) end 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 diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 451579229cd..ed3ddec1210 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -8,12 +8,10 @@ local explorer_node = require "nvim-tree.explorer.node" local M = { config = {}, - project_root_to_git_dir = {}, } --- TODO 2382 remove the toplevel key -local projects = {} -local cwd_to_project_root = {} +local projects_by_toplevel = {} +local projects_by_path = {} -- false when 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. @@ -59,20 +57,117 @@ local function path_ignored_in_project(path, project) return false end +local function reload_tree_at(project_root) + if not M.config.git.enable then + return nil + 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 + end + + M.reload_project(project_root, nil, function() + local project = projects_by_toplevel[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, project) + end) + :recursor(function(node) + return node.nodes and #node.nodes > 0 and node.nodes + end) + :iterate() + + require("nvim-tree.renderer").draw() + end) +end + +--- 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 { + project_root = toplevel, + list_untracked = git_utils.should_show_untracked(toplevel), + list_ignored = true, + timeout = M.config.git.timeout, + } + + 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 + + if M.config.filesystem_watchers.enable then + 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() + if w.destroyed then + return + end + reload_tree_at(w.toplevel) + end) + end + + project.watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { + toplevel = toplevel, + }) + end + + return project +end + +--- Find an project known for a path. +--- @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 + + -- ignore non-directories + local stat, _ = vim.loop.fs_stat(path) + if not stat or stat.type ~= "directory" then + projects_by_path[path] = false + return nil + end + + -- short-circuit any known ignored paths + 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 + + return nil +end + --- Reload all git projects function M.reload() if not M.config.git.enable then return {} end - for _, project in pairs(projects) do + for _, project in pairs(projects_by_toplevel) do M.reload_project(project, nil, nil) end end --- Reload the git project. --- @param project table|nil ---- @param path string|nil only reload this path, NOP if ignored +--- @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 @@ -109,143 +204,45 @@ function M.reload_project(project, path, callback) end end -local function get_project_root(cwd) - if not M.config.git.enable then - return nil - end - - if cwd_to_project_root[cwd] then - return cwd_to_project_root[cwd] - end - - if cwd_to_project_root[cwd] == false then - return nil - end - - local stat, _ = vim.loop.fs_stat(cwd) - if not stat or stat.type ~= "directory" then - return nil - end - - -- short-circuit any known ignored paths - for root, project in pairs(projects) do - if project and path_ignored_in_project(cwd, project) then - cwd_to_project_root[cwd] = root - return root - end - end - - local toplevel, git_dir = 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 - end - end - - cwd_to_project_root[cwd] = toplevel - if toplevel then - M.project_root_to_git_dir[toplevel] = git_dir - end - return cwd_to_project_root[cwd] -end - ---- Retrieve the project containing a path +--- Retrieve the project containing a path, creating and populating if necessary. --- @param path string absolute --- @return table|nil project function M.get_project(path) - local project_root = get_project_root(path) - return projects[project_root] -end - -local function reload_tree_at(project_root) if not M.config.git.enable then return nil 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 - end - - M.reload_project(project_root, nil, function() - local project = projects[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, project) - 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 = get_project_root(cwd) - if not project_root then - cwd_to_project_root[cwd] = false - return {} + -- existing project known for this path + local project = find_project(path) + if project then + return project end - local status = projects[project_root] - if status then - return status + -- determine git directories + local toplevel, git_dir = git_utils.get_toplevel(path) + if not toplevel or not git_dir then + projects_by_path[path] = false + return nil end - local git_dir = vim.env.GIT_DIR or M.project_root_to_git_dir[project_root] or utils.path_join { project_root, ".git" } - - local git_status = Runner.run { - project_root = project_root, - list_untracked = git_utils.should_show_untracked(project_root), - list_ignored = true, - timeout = M.config.git.timeout, - } - - local watcher = nil - if M.config.filesystem_watchers.enable then - 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() - if w.destroyed then - return - end - reload_tree_at(w.project_root) - end) - end - - watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { - project_root = project_root, - }) + -- exisiting project unknown for this path + project = projects_by_toplevel[toplevel] + if project then + projects_by_path[path] = project + return project end - projects[project_root] = { - toplevel = project_root, - git_dir = git_dir, - files = git_status, - dirs = git_utils.file_status_to_dir_status(git_status, project_root), - watcher = watcher, - } - return projects[project_root] + -- 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") - projects = {} - cwd_to_project_root = {} + -- TODO 2382 we never tore down the watcher + projects_by_toplevel = {} + projects_by_path = {} end --- Disable git integration permanently From e084a53df8bb7830aa4ae9543066ade3c481821c Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 16:59:17 +1000 Subject: [PATCH 6/9] fix(#2382): lazily create git projects --- lua/nvim-tree/explorer/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 1fbd207f429..93455bca9e7 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -56,4 +56,3 @@ end M.Explorer = Explorer return M - From 2fda3c09a4a72a369e3c22f5c7d7fe519a88ee6e Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 17:06:51 +1000 Subject: [PATCH 7/9] fix(#2382): lazily create git projects --- lua/nvim-tree/git/init.lua | 22 +++++++++++----------- lua/nvim-tree/git/runner.lua | 14 +++++++------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index ed3ddec1210..e0a2f67a5be 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -23,7 +23,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 @@ -35,7 +35,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? @@ -57,19 +57,19 @@ local function path_ignored_in_project(path, project) return false end -local function reload_tree_at(project_root) +local function reload_tree_at(toplevel) if not M.config.git.enable 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 project = projects_by_toplevel[project_root] + M.reload_project(toplevel, nil, function() + local project = projects_by_toplevel[toplevel] Iterator.builder(root_node.nodes) :hidden() @@ -92,7 +92,7 @@ end --- @return table project local function create_project(toplevel, git_dir) local git_status = Runner.run { - project_root = toplevel, + toplevel = toplevel, list_untracked = git_utils.should_show_untracked(toplevel), list_ignored = true, timeout = M.config.git.timeout, @@ -110,8 +110,8 @@ local function create_project(toplevel, git_dir) 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 @@ -185,7 +185,7 @@ function M.reload_project(project, path, callback) end local opts = { - project_root = project.toplevel, + toplevel = project.toplevel, path = path, list_untracked = git_utils.should_show_untracked(project.toplevel), list_ignored = true, 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 From 9d52cfef504024c351337291ffb9b3afd9deb53d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 17:14:06 +1000 Subject: [PATCH 8/9] fix(#2382): destroy git watchers on purge --- lua/nvim-tree/git/init.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index e0a2f67a5be..ba798a1eb8b 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -240,7 +240,11 @@ end function M.purge_state() log.line("git", "purge_state") - -- TODO 2382 we never tore down the watcher + for _, project in pairs(projects_by_toplevel) do + if project.watcher then + project.watcher:destroy() + end + end projects_by_toplevel = {} projects_by_path = {} end From 12768d4b91d7d00086e4070bdb8e3498afc57d1a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 27 Aug 2023 17:29:40 +1000 Subject: [PATCH 9/9] fix(#2382): destroy git watchers on purge --- lua/nvim-tree/git/init.lua | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index ba798a1eb8b..0a9cdb0e077 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -10,8 +10,14 @@ local M = { config = {}, } +-- all projects keyed by toplevel local projects_by_toplevel = {} -local projects_by_path = {} -- false when no project + +-- 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. @@ -128,6 +134,7 @@ local function create_project(toplevel, git_dir) 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) @@ -139,7 +146,7 @@ local function find_project(path) -- ignore non-directories local stat, _ = vim.loop.fs_stat(path) if not stat or stat.type ~= "directory" then - projects_by_path[path] = false + paths_no_project[path] = true return nil end @@ -208,7 +215,7 @@ end --- @param path string absolute --- @return table|nil project function M.get_project(path) - if not M.config.git.enable then + if not M.config.git.enable or paths_no_project[path] then return nil end @@ -221,11 +228,11 @@ function M.get_project(path) -- determine git directories local toplevel, git_dir = git_utils.get_toplevel(path) if not toplevel or not git_dir then - projects_by_path[path] = false + paths_no_project[path] = true return nil end - -- exisiting project unknown for this path + -- exisiting project now known for this path project = projects_by_toplevel[toplevel] if project then projects_by_path[path] = project @@ -247,6 +254,7 @@ function M.purge_state() end projects_by_toplevel = {} projects_by_path = {} + paths_no_project = {} end --- Disable git integration permanently