fix(#2382): git watcher handles worktrees and submodules, via --absolute-git-dir when it is available (#2389)

* fix(#2382): use --absolute-git-dir when available

* fix(#2382): use --absolute-git-dir when available

* fix(#2382): rename private git members, destroy git watchers on purge

* fix(#2382): consistent naming of toplevel

* fix(#2382): more doc and safety

* fix(#2382): consistent naming of toplevel

* fix(#2382): consistent naming of toplevel
This commit is contained in:
Alexander Courtis
2023-09-02 12:05:34 +10:00
committed by GitHub
parent 00741206c2
commit 28c3980b25
5 changed files with 139 additions and 83 deletions

View File

@@ -12,8 +12,8 @@ local function refresh_nodes(node, projects, unloaded_bufnr)
Iterator.builder({ node }) Iterator.builder({ node })
:applier(function(n) :applier(function(n)
if n.open and n.nodes then if n.open and n.nodes then
local project_root = git.get_project_root(n.cwd or n.link_to or n.absolute_path) local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path)
explorer_module.reload(n, projects[project_root] or {}, unloaded_bufnr) explorer_module.reload(n, projects[toplevel] or {}, unloaded_bufnr)
end end
end) end)
:recursor(function(n) :recursor(function(n)
@@ -23,8 +23,8 @@ local function refresh_nodes(node, projects, unloaded_bufnr)
end end
function M.reload_node_status(parent_node, projects) function M.reload_node_status(parent_node, projects)
local project_root = git.get_project_root(parent_node.absolute_path) local toplevel = git.get_toplevel(parent_node.absolute_path)
local status = projects[project_root] or {} local status = projects[toplevel] or {}
for _, node in ipairs(parent_node.nodes) do 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), status)
if node.nodes and #node.nodes > 0 then if node.nodes and #node.nodes > 0 then

View File

@@ -22,10 +22,10 @@ local function update_status(nodes_by_path, node_ignored, status)
end end
local function reload_and_get_git_project(path, callback) 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() git.reload_project(toplevel, path, function()
callback(project_root, git.get_project(project_root) or {}) callback(toplevel, git.get_project(toplevel) or {})
end) end)
end end
@@ -38,7 +38,7 @@ local function update_parent_statuses(node, project, root)
break break
end end
root = git.get_project_root(node.parent.absolute_path) root = git.get_toplevel(node.parent.absolute_path)
-- stop when no more projects -- stop when no more projects
if not root then if not root then
@@ -174,10 +174,10 @@ function M.refresh_node(node, callback)
local parent_node = utils.get_parent_of_group(node) 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) 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() callback()
end) end)
@@ -211,11 +211,11 @@ function M.refresh_parent_nodes_for_path(path)
-- refresh in order; this will expand groups as needed -- refresh in order; this will expand groups as needed
for _, node in ipairs(parent_nodes) do for _, node in ipairs(parent_nodes) do
local project_root = git.get_project_root(node.absolute_path) local toplevel = git.get_toplevel(node.absolute_path)
local project = git.get_project(project_root) or {} local project = git.get_project(toplevel) or {}
M.reload(node, project) M.reload(node, project)
update_parent_statuses(node, project, project_root) update_parent_statuses(node, project, toplevel)
end end
log.profile_end(profile) log.profile_end(profile)

View File

@@ -8,8 +8,15 @@ local explorer_node = require "nvim-tree.explorer.node"
local M = { local M = {
config = {}, 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. -- Files under .git that should result in a reload when changed.
@@ -22,7 +29,7 @@ local WATCHED_FILES = {
"index", -- staging area "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 if path then
for p in pairs(project.files) do for p in pairs(project.files) do
if p:find(path, 1, true) == 1 then 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 project.files = git_status
end 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 end
--- Is this path in a known ignored directory? --- Is this path in a known ignored directory?
@@ -56,28 +63,34 @@ local function path_ignored_in_project(path, project)
return false return false
end end
--- Reload all projects
--- @return table projects maybe empty
function M.reload() function M.reload()
if not M.config.git.enable then if not M.config.git.enable then
return {} return {}
end end
for project_root in pairs(M.projects) do for toplevel in pairs(M._projects_by_toplevel) do
M.reload_project(project_root) M.reload_project(toplevel)
end end
return M.projects return M._projects_by_toplevel
end end
function M.reload_project(project_root, path, callback) --- Reload one project. Does nothing when no project or path is ignored
local project = M.projects[project_root] --- @param toplevel string|nil
if not project or not M.config.git.enable then --- @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 if callback then
callback() callback()
end end
return return
end 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 if callback then
callback() callback()
end end
@@ -85,56 +98,71 @@ function M.reload_project(project_root, path, callback)
end end
local opts = { local opts = {
project_root = project_root, toplevel = toplevel,
path = path, path = path,
list_untracked = git_utils.should_show_untracked(project_root), list_untracked = git_utils.should_show_untracked(toplevel),
list_ignored = true, list_ignored = true,
timeout = M.config.git.timeout, timeout = M.config.git.timeout,
} }
if callback then if callback then
Runner.run(opts, function(git_status) Runner.run(opts, function(git_status)
reload_git_status(project_root, path, project, git_status) reload_git_status(toplevel, path, project, git_status)
callback() callback()
end) end)
else else
-- TODO use callback once async/await is available -- TODO use callback once async/await is available
local git_status = Runner.run(opts) 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
end end
function M.get_project(project_root) --- Retrieve a known project
return M.projects[project_root] --- @return table|nil project
function M.get_project(toplevel)
return M._projects_by_toplevel[toplevel]
end 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 if not M.config.git.enable then
return nil return nil
end end
if M.cwd_to_project_root[cwd] then if M._toplevels_by_path[path] then
return M.cwd_to_project_root[cwd] return M._toplevels_by_path[path]
end end
if M.cwd_to_project_root[cwd] == false then if M._toplevels_by_path[path] == false then
return nil return nil
end end
local stat, _ = vim.loop.fs_stat(cwd) local stat, _ = vim.loop.fs_stat(path)
if not stat or stat.type ~= "directory" then if not stat or stat.type ~= "directory" then
return nil return nil
end end
-- short-circuit any known ignored paths -- short-circuit any known ignored paths
for root, project in pairs(M.projects) do for root, project in pairs(M._projects_by_toplevel) do
if project and path_ignored_in_project(cwd, project) then if project and path_ignored_in_project(path, project) then
M.cwd_to_project_root[cwd] = root M._toplevels_by_path[path] = root
return root return root
end end
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 for _, disabled_for_dir in ipairs(M.config.git.disable_for_dirs) do
local toplevel_norm = vim.fn.fnamemodify(toplevel, ":p") local toplevel_norm = vim.fn.fnamemodify(toplevel, ":p")
local disabled_norm = vim.fn.fnamemodify(disabled_for_dir, ":p") local disabled_norm = vim.fn.fnamemodify(disabled_for_dir, ":p")
@@ -143,23 +171,24 @@ function M.get_project_root(cwd)
end end
end end
M.cwd_to_project_root[cwd] = toplevel M._toplevels_by_path[path] = toplevel
return M.cwd_to_project_root[cwd] M._git_dirs_by_toplevel[toplevel] = git_dir
return M._toplevels_by_path[path]
end end
local function reload_tree_at(project_root) local function reload_tree_at(toplevel)
if not M.config.git.enable then if not M.config.git.enable or not toplevel then
return nil return nil
end end
log.line("watcher", "git event executing '%s'", project_root) log.line("watcher", "git event executing '%s'", toplevel)
local root_node = utils.get_node_from_path(project_root) local root_node = utils.get_node_from_path(toplevel)
if not root_node then if not root_node then
return return
end end
M.reload_project(project_root, nil, function() M.reload_project(toplevel, nil, function()
local git_status = M.get_project(project_root) local git_status = M.get_project(toplevel)
Iterator.builder(root_node.nodes) Iterator.builder(root_node.nodes)
:hidden() :hidden()
@@ -176,25 +205,29 @@ local function reload_tree_at(project_root)
end) end)
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 if not M.config.git.enable then
return {} return {}
end end
local project_root = M.get_project_root(cwd) local toplevel = M.get_toplevel(path)
if not project_root then if not toplevel then
M.cwd_to_project_root[cwd] = false M._toplevels_by_path[path] = false
return {} return {}
end end
local status = M.projects[project_root] local status = M._projects_by_toplevel[toplevel]
if status then if status then
return status return status
end end
local git_status = Runner.run { local git_status = Runner.run {
project_root = project_root, toplevel = toplevel,
list_untracked = git_utils.should_show_untracked(project_root), list_untracked = git_utils.should_show_untracked(toplevel),
list_ignored = true, list_ignored = true,
timeout = M.config.git.timeout, timeout = M.config.git.timeout,
} }
@@ -204,33 +237,41 @@ function M.load_project_status(cwd)
log.line("watcher", "git start") log.line("watcher", "git start")
local callback = function(w) local callback = function(w)
log.line("watcher", "git event scheduled '%s'", w.project_root) log.line("watcher", "git event scheduled '%s'", w.toplevel)
utils.debounce("git:watcher:" .. w.project_root, M.config.filesystem_watchers.debounce_delay, function() utils.debounce("git:watcher:" .. w.toplevel, M.config.filesystem_watchers.debounce_delay, function()
if w.destroyed then if w.destroyed then
return return
end end
reload_tree_at(w.project_root) reload_tree_at(w.toplevel)
end) end)
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, { watcher = Watcher:new(git_dir, WATCHED_FILES, callback, {
project_root = project_root, toplevel = toplevel,
}) })
end end
M.projects[project_root] = { M._projects_by_toplevel[toplevel] = {
files = git_status, 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, watcher = watcher,
} }
return M.projects[project_root] return M._projects_by_toplevel[toplevel]
end end
function M.purge_state() function M.purge_state()
log.line("git", "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 end
--- Disable git integration permanently --- Disable git integration permanently

View File

@@ -14,7 +14,7 @@ function Runner:_parse_status_output(status, path)
path = path:gsub("/", "\\") path = path:gsub("/", "\\")
end end
if #status > 0 and #path > 0 then 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
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" local ignored = (self.list_untracked and self.list_ignored) and "--ignored=matching" or "--ignored=no"
return { return {
args = { "--no-optional-locks", "status", "--porcelain=v1", "-z", ignored, untracked, self.path }, 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 }, stdio = { nil, stdout_handle, stderr_handle },
} }
end end
@@ -151,7 +151,7 @@ end
function Runner:_finalise(opts) function Runner:_finalise(opts)
if self.rc == -1 then 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 timeouts = timeouts + 1
if timeouts == MAX_TIMEOUTS then if timeouts == MAX_TIMEOUTS then
notify.warn( notify.warn(
@@ -164,9 +164,9 @@ function Runner:_finalise(opts)
require("nvim-tree.git").disable_git_integration() require("nvim-tree.git").disable_git_integration()
end end
elseif self.rc ~= 0 then 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 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
end end
@@ -176,7 +176,7 @@ end
--- @return table|nil status by absolute path, nil if callback present --- @return table|nil status by absolute path, nil if callback present
function Runner.run(opts, callback) function Runner.run(opts, callback)
local self = setmetatable({ local self = setmetatable({
project_root = opts.project_root, toplevel = opts.toplevel,
path = opts.path, path = opts.path,
list_untracked = opts.list_untracked, list_untracked = opts.list_untracked,
list_ignored = opts.list_ignored, list_ignored = opts.list_ignored,
@@ -186,7 +186,7 @@ function Runner.run(opts, callback)
}, Runner) }, Runner)
local async = callback ~= nil 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 if async and callback then
-- async, always call back -- async, always call back

View File

@@ -1,40 +1,55 @@
local M = {} local M = {}
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local has_cygpath = vim.fn.executable "cygpath" == 1 local has_cygpath = vim.fn.executable "cygpath" == 1
--- Retrieve the git toplevel directory --- Retrieve the git toplevel directory
--- @param cwd string path --- @param cwd string path
--- @return string|nil toplevel absolute path --- @return string|nil toplevel absolute path
--- @return string|nil git_dir absolute path
function M.get_toplevel(cwd) 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" } -- both paths are absolute
log.line("git", "%s", vim.inspect(cmd)) 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) log.profile_end(profile)
if vim.v.shell_error ~= 0 or not toplevel or #toplevel == 0 or toplevel:match "fatal" then if vim.v.shell_error ~= 0 or not out or #out == 0 or out:match "fatal" then
return nil 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 end
-- git always returns path with forward slashes -- git always returns path with forward slashes
if vim.fn.has "win32" == 1 then if vim.fn.has "win32" == 1 then
-- msys2 git support -- msys2 git support
if has_cygpath then 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 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
end end
toplevel = toplevel:gsub("/", "\\") toplevel = toplevel:gsub("/", "\\")
git_dir = git_dir:gsub("/", "\\")
end end
-- remove newline return toplevel, git_dir
return toplevel:sub(0, -2)
end end
local untracked = {} local untracked = {}
@@ -47,7 +62,7 @@ function M.should_show_untracked(cwd)
local profile = log.profile_start("git untracked %s", cwd) local profile = log.profile_start("git untracked %s", cwd)
local cmd = { "git", "-C", cwd, "config", "status.showUntrackedFiles" } 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) local has_untracked = vim.fn.system(cmd)