Skip to content

Commit bcb2a5a

Browse files
authored
fix(#1720): .git watch only FETCH_HEAD, HEAD, HEAD.lock, config, index (#1732)
* fix(#1720): .git watch only HEAD, config and index * fix(#1720): .git watch only FETCH_HEAD, HEAD, HEAD.lock, config, index
1 parent 7e89276 commit bcb2a5a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

lua/nvim-tree/explorer/watch.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function M.create_watcher(absolute_path)
7777
end
7878

7979
M.uid = M.uid + 1
80-
return Watcher:new(absolute_path, callback, {
80+
return Watcher:new(absolute_path, nil, callback, {
8181
context = "explorer:watch:" .. absolute_path .. ":" .. M.uid,
8282
})
8383
end

lua/nvim-tree/git/init.lua

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ local M = {
1111
cwd_to_project_root = {},
1212
}
1313

14+
-- Files under .git that should result in a reload when changed.
15+
-- Utilities (like watchman) can also write to this directory (often) and aren't useful for us.
16+
local WATCHED_FILES = {
17+
"FETCH_HEAD", -- remote ref
18+
"HEAD", -- local ref
19+
"HEAD.lock", -- HEAD will not always be updated e.g. revert
20+
"config", -- user config
21+
"index", -- staging area
22+
}
23+
1424
function M.reload()
1525
if not M.config.git.enable then
1626
return {}
@@ -149,7 +159,7 @@ function M.load_project_status(cwd)
149159
end)
150160
end
151161

152-
watcher = Watcher:new(utils.path_join { project_root, ".git" }, callback, {
162+
watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, {
153163
project_root = project_root,
154164
})
155165
end

lua/nvim-tree/watcher.lua

+8-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function Event:start()
5858
else
5959
log.line("watcher", "event_cb '%s' '%s'", self._path, filename)
6060
for _, listener in ipairs(self._listeners) do
61-
listener()
61+
listener(filename)
6262
end
6363
end
6464
end)
@@ -101,14 +101,15 @@ function Event:destroy(message)
101101
Event._events[self._path] = nil
102102
end
103103

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

107107
local w = setmetatable(data, Watcher)
108108

109109
w._event = Event._events[path] or Event:new(path)
110110
w._listener = nil
111111
w._path = path
112+
w._files = files
112113
w._callback = callback
113114

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

125126
function Watcher:start()
126-
self._listener = function()
127-
self._callback(self)
127+
self._listener = function(filename)
128+
if not self._files or vim.tbl_contains(self._files, filename) then
129+
self._callback(self)
130+
end
128131
end
129132

130133
self._event:add(self._listener)

0 commit comments

Comments
 (0)