Skip to content

fix(#1720): .git watch only FETCH_HEAD, HEAD, HEAD.lock, config, index #1732

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

Merged
merged 2 commits into from
Nov 12, 2022
Merged
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
2 changes: 1 addition & 1 deletion lua/nvim-tree/explorer/watch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function M.create_watcher(absolute_path)
end

M.uid = M.uid + 1
return Watcher:new(absolute_path, callback, {
return Watcher:new(absolute_path, nil, callback, {
context = "explorer:watch:" .. absolute_path .. ":" .. M.uid,
})
end
Expand Down
12 changes: 11 additions & 1 deletion lua/nvim-tree/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ local M = {
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 = {
"FETCH_HEAD", -- remote ref
"HEAD", -- local ref
"HEAD.lock", -- HEAD will not always be updated e.g. revert
"config", -- user config
"index", -- staging area
}

function M.reload()
if not M.config.git.enable then
return {}
Expand Down Expand Up @@ -149,7 +159,7 @@ function M.load_project_status(cwd)
end)
end

watcher = Watcher:new(utils.path_join { project_root, ".git" }, callback, {
watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, {
project_root = project_root,
})
end
Expand Down
13 changes: 8 additions & 5 deletions lua/nvim-tree/watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function Event:start()
else
log.line("watcher", "event_cb '%s' '%s'", self._path, filename)
for _, listener in ipairs(self._listeners) do
listener()
listener(filename)
end
end
end)
Expand Down Expand Up @@ -101,14 +101,15 @@ function Event:destroy(message)
Event._events[self._path] = nil
end

function Watcher:new(path, callback, data)
log.line("watcher", "Watcher:new '%s'", path)
function Watcher:new(path, files, callback, data)
log.line("watcher", "Watcher:new '%s' %s", path, vim.inspect(files))

local w = setmetatable(data, Watcher)

w._event = Event._events[path] or Event:new(path)
w._listener = nil
w._path = path
w._files = files
w._callback = callback

if not w._event then
Expand All @@ -123,8 +124,10 @@ function Watcher:new(path, callback, data)
end

function Watcher:start()
self._listener = function()
self._callback(self)
self._listener = function(filename)
if not self._files or vim.tbl_contains(self._files, filename) then
self._callback(self)
end
end

self._event:add(self._listener)
Expand Down