feat(watcher): partial git refresh (#1373)

This commit is contained in:
Alexander Courtis
2022-06-27 11:12:28 +10:00
committed by GitHub
parent 247f80b633
commit 7a795d78fa
3 changed files with 24 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ local M = {}
local function reload_and_get_git_project(path) local function reload_and_get_git_project(path)
local project_root = git.get_project_root(path) local project_root = git.get_project_root(path)
git.reload_project(project_root) git.reload_project(project_root, path)
return project_root, git.get_project(project_root) or {} return project_root, git.get_project(project_root) or {}
end end

View File

@@ -22,25 +22,36 @@ function M.reload()
return M.projects return M.projects
end end
function M.reload_project(project_root) function M.reload_project(project_root, path)
local project = M.projects[project_root] local project = M.projects[project_root]
if not project or not M.config.enable then if not project or not M.config.enable then
return return
end end
local watcher = M.projects[project_root].watcher if path and not path:match("^" .. project_root) then
M.projects[project_root] = {} path = nil
end
local git_status = Runner.run { local git_status = Runner.run {
project_root = project_root, project_root = project_root,
path = path,
list_untracked = git_utils.should_show_untracked(project_root), list_untracked = git_utils.should_show_untracked(project_root),
list_ignored = true, list_ignored = true,
timeout = M.config.timeout, timeout = M.config.timeout,
} }
M.projects[project_root] = {
files = git_status, if path then
dirs = git_utils.file_status_to_dir_status(git_status, project_root), for p in pairs(project.files) do
watcher = watcher, if p:match("^" .. path) then
} project.files[p] = nil
end
end
project.files = vim.tbl_deep_extend("force", project.files, git_status)
else
project.files = git_status
end
project.dirs = git_utils.file_status_to_dir_status(project.files, project_root)
end end
function M.get_project(project_root) function M.get_project(project_root)

View File

@@ -45,7 +45,7 @@ function Runner:_getopts(stdout_handle, stderr_handle)
local untracked = self.list_untracked and "-u" or nil local untracked = self.list_untracked and "-u" or nil
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", ignored, untracked }, args = { "--no-optional-locks", "status", "--porcelain=v1", ignored, untracked, self.path },
cwd = self.project_root, cwd = self.project_root,
stdio = { nil, stdout_handle, stderr_handle }, stdio = { nil, stdout_handle, stderr_handle },
} }
@@ -129,10 +129,11 @@ end
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms -- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
function Runner.run(opts) function Runner.run(opts)
local ps = log.profile_start("git job %s", opts.project_root) local ps = log.profile_start("git job %s %s", opts.project_root, opts.path)
local self = setmetatable({ local self = setmetatable({
project_root = opts.project_root, project_root = opts.project_root,
path = opts.path,
list_untracked = opts.list_untracked, list_untracked = opts.list_untracked,
list_ignored = opts.list_ignored, list_ignored = opts.list_ignored,
timeout = opts.timeout or 400, timeout = opts.timeout or 400,
@@ -143,7 +144,7 @@ function Runner.run(opts)
self:_run_git_job() self:_run_git_job()
self:_wait() self:_wait()
log.profile_end(ps, "git job %s", opts.project_root) log.profile_end(ps, "git job %s %s", opts.project_root, opts.path)
if self.rc == -1 then if self.rc == -1 then
log.line("git", "job timed out") log.line("git", "job timed out")