feat/chore: rewrite git with job and some other fixes (#743)

* feat/chore: rewrite git with job and some other fixes

* fix: fs clear window, rename echo_warning -> warn

also fix renaming and add an event blocker to avoid running many events
at the same time
This commit is contained in:
Kiyan
2021-11-27 16:02:54 +01:00
committed by GitHub
parent b853e1083c
commit 6662b60a2b
15 changed files with 453 additions and 354 deletions

View File

@@ -0,0 +1,86 @@
local git_utils = require'nvim-tree.git.utils'
local Runner = require'nvim-tree.git.runner'
local M = {
config = nil,
projects = {},
cwd_to_project_root = {}
}
function M.reload(callback)
local num_projects = vim.tbl_count(M.projects)
if not M.config.enable or num_projects == 0 then
return callback({})
end
local done = 0
for project_root in pairs(M.projects) do
M.projects[project_root] = {}
Runner.run {
project_root = project_root,
list_untracked = git_utils.should_show_untracked(project_root),
list_ignored = M.config.ignore,
timeout = M.config.timeout,
on_end = function(git_status)
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
}
done = done + 1
if done == num_projects then
callback(M.projects)
end
end
}
end
end
function M.get_project_root(cwd)
if M.cwd_to_project_root[cwd] then
return M.cwd_to_project_root[cwd]
end
if M.cwd_to_project_root[cwd] == false then
return nil
end
local project_root = git_utils.get_toplevel(cwd)
return project_root
end
function M.load_project_status(cwd, callback)
if not M.config.enable then
return callback({})
end
local project_root = M.get_project_root(cwd)
if not project_root then
M.cwd_to_project_root[cwd] = false
return callback({})
end
local status = M.projects[project_root]
if status then
return callback(status)
end
Runner.run {
project_root = project_root,
list_untracked = git_utils.should_show_untracked(project_root),
list_ignored = M.config.ignore,
timeout = M.config.timeout,
on_end = function(git_status)
M.projects[project_root] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
}
callback(M.projects[project_root])
end
}
end
function M.setup(opts)
M.config = opts.git
end
return M

View File

@@ -0,0 +1,99 @@
local uv = vim.loop
local utils = require'nvim-tree.utils'
local Runner = {}
Runner.__index = Runner
function Runner:_parse_status_output(line)
local status = line:sub(1, 2)
-- removing `"` when git is returning special file status containing spaces
local path = line:sub(4, -2):gsub('^"', ''):gsub('"$', '')
if #status > 0 and #path > 0 then
self.output[utils.path_remove_trailing(utils.path_join({self.project_root,path}))] = status
end
return #line
end
function Runner:_handle_incoming_data(prev_output, incoming)
if incoming and utils.str_find(incoming, '\n') then
local prev = prev_output..incoming
local i = 1
for line in prev:gmatch('[^\n]*\n') do
i = i + self:_parse_status_output(line)
end
return prev:sub(i, -1)
end
if incoming then
return prev_output..incoming
end
for line in prev_output:gmatch('[^\n]*\n') do
self._parse_status_output(line)
end
return nil
end
function Runner:_getopts(stdout_handle)
local untracked = self.list_untracked and '-u' or nil
local ignored = self.list_ignored and '--ignored=matching' or '--ignored=no'
return {
args = {"status", "--porcelain=v1", ignored, untracked},
cwd = self.project_root,
stdio = { nil, stdout_handle, nil },
}
end
function Runner:_run_git_job()
local handle, pid
local stdout = uv.new_pipe(false)
local timer = uv.new_timer()
local function on_finish(output)
if timer:is_closing() or stdout:is_closing() or handle:is_closing() then
return
end
timer:stop()
timer:close()
stdout:read_stop()
stdout:close()
handle:close()
pcall(uv.kill, pid)
self.on_end(output or self.output)
end
handle, pid = uv.spawn(
"git",
self:_getopts(stdout),
vim.schedule_wrap(function() on_finish() end)
)
timer:start(self.timeout, 0, vim.schedule_wrap(function() on_finish({}) end))
local output_leftover = ''
local function manage_output(err, data)
if err then return end
output_leftover = self:_handle_incoming_data(output_leftover, data)
end
uv.read_start(stdout, vim.schedule_wrap(manage_output))
end
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
function Runner.run(opts)
local self = setmetatable({
project_root = opts.project_root,
list_untracked = opts.list_untracked,
list_ignored = opts.list_ignored,
timeout = opts.timeout or 400,
output = {},
on_end = opts.on_end,
}, Runner)
self:_run_git_job()
end
return Runner

View File

@@ -0,0 +1,54 @@
local M = {}
function M.get_toplevel(cwd)
local cmd = "git -C " .. vim.fn.shellescape(cwd) .. " rev-parse --show-toplevel"
local toplevel = vim.fn.system(cmd)
if not toplevel or #toplevel == 0 or toplevel:match('fatal') then
return nil
end
-- git always returns path with forward slashes
if vim.fn.has('win32') == 1 then
toplevel = toplevel:gsub("/", "\\")
end
-- remove newline
return toplevel:sub(0, -2)
end
local untracked = {}
function M.should_show_untracked(cwd)
if untracked[cwd] ~= nil then
return untracked[cwd]
end
local cmd = "git -C "..cwd.." config --type=bool status.showUntrackedFiles"
local has_untracked = vim.fn.system(cmd)
untracked[cwd] = vim.trim(has_untracked) ~= 'false'
return untracked[cwd]
end
function M.file_status_to_dir_status(status, cwd)
local dirs = {}
for p, s in pairs(status) do
if s ~= '!!' then
local modified = vim.fn.fnamemodify(p, ':h')
dirs[modified] = 'dirty'
end
end
for dirname, _ in pairs(dirs) do
local modified = dirname
while modified ~= cwd and modified ~= '/' do
modified = vim.fn.fnamemodify(modified, ':h')
dirs[modified] = 'dirty'
end
end
return dirs
end
return M