Skip to content

fix(#1970): disable git integration after 10 timeouts #1972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ Git integration with icons and colors.

*nvim-tree.git.timeout*
Kills the git process after some time if it takes too long.
Git integration will be disabled after 10 git jobs exceed this timeout.
Type: `number`, Default: `400` (ms)

You will still need to set |renderer.icons.show.git| `= true` or
Expand Down
7 changes: 7 additions & 0 deletions lua/nvim-tree/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ function M.purge_state()
M.cwd_to_project_root = {}
end

--- Disable git integration permanently
function M.disable_git_integration()
log.line("git", "disabling git integration")
M.purge_state()
M.config.git.enable = false
end

function M.setup(opts)
M.config.git = opts.git
M.config.filesystem_watchers = opts.filesystem_watchers
Expand Down
63 changes: 43 additions & 20 deletions lua/nvim-tree/git/runner.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"

local Runner = {}
Runner.__index = Runner

local timeouts = 0
local MAX_TIMEOUTS = 5

function Runner:_parse_status_output(status, path)
-- replacing slashes if on windows
if vim.fn.has "win32" == 1 then
Expand Down Expand Up @@ -66,46 +70,53 @@ function Runner:_log_raw_output(output)
end

function Runner:_run_git_job()
local handle, pid
local handle
local stdout = vim.loop.new_pipe(false)
local stderr = vim.loop.new_pipe(false)
local timer = vim.loop.new_timer()

local function on_finish(rc)
local function on_finish(rc, signal)
log.line("git", "rc=%d, signal=%s", tostring(rc), tostring(signal))
self.rc = rc or 0
if timer:is_closing() or stdout:is_closing() or stderr:is_closing() or (handle and handle:is_closing()) then
return

if timer and not timer:is_closing() then
timer:stop()
timer:close()
end
if stdout and not stdout:is_closing() then
stdout:read_stop()
stdout:close()
end
timer:stop()
timer:close()
stdout:read_stop()
stderr:read_stop()
stdout:close()
stderr:close()
if handle then
if stderr and not stderr:is_closing() then
stderr:read_stop()
stderr:close()
end
if handle and not handle:is_closing() then
handle:close()
end

pcall(vim.loop.kill, pid)
end

local opts = self:_getopts(stdout, stderr)
log.line("git", "running job with timeout %dms", self.timeout)
log.line("git", "git %s", table.concat(utils.array_remove_nils(opts.args), " "))

handle, pid = vim.loop.spawn(
handle = vim.loop.spawn(
"git",
opts,
vim.schedule_wrap(function(rc)
on_finish(rc)
vim.schedule_wrap(function(rc, signal)
on_finish(rc, signal)
end)
)

timer:start(
self.timeout,
0,
vim.schedule_wrap(function()
on_finish(-1)
log.line("git", "timed out, killing")
self.timed_out = true
if handle then
handle:kill "sigkill"
end
end)
)

Expand All @@ -131,7 +142,7 @@ end

function Runner:_wait()
local function is_done()
return self.rc ~= nil
return self.rc ~= nil or self.timed_out
end

while not vim.wait(30, is_done) do
Expand All @@ -149,16 +160,28 @@ function Runner.run(opts)
list_ignored = opts.list_ignored,
timeout = opts.timeout or 400,
output = {},
rc = nil, -- -1 indicates timeout
rc = nil,
timed_out = false,
}, Runner)

self:_run_git_job()
self:_wait()

log.profile_end(profile)

if self.rc == -1 then
if self.timed_out then
log.line("git", "job timed out %s %s", opts.project_root, opts.path)
timeouts = timeouts + 1
if timeouts == MAX_TIMEOUTS then
notify.warn(
string.format(
"%d git jobs have timed out after %dms, disabling git integration. Please consider increasing git.timeout",
timeouts,
opts.timeout
)
)
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)
else
Expand Down