Debian Codegraph @

lua/gitsigns/git/repo/watcher.lua

raw
1 local debounce_trailing = require('gitsigns.debounce').debounce_trailing
2 local util = require('gitsigns.util')
3 local log = require('gitsigns.debug.log')
4 local Path = util.Path
5
6 --- vim.inspect but on one line
7 --- @param x any
8 --- @return string
9 local function inspect(x)
10 return vim.inspect(x, { indent = '', newline = ' ' })
11 end
12
13 --- @class Gitsigns.Repo.Watcher
14 --- @field private update_callbacks fun()[]
15 --- @field private notify_callbacks_debounced fun(weak_self:{ref:Gitsigns.Repo.Watcher})
16 --- @field private gitdir string
17 --- @field private commondir string
18 --- @field private handles table<string, uv.uv_fs_event_t> Map from watched dir -> handle
19 --- @field private head_ref_dir? string
20 --- @field private _gc userdata? Used for garbage collection
21 local Watcher = {}
22 Watcher.__index = Watcher
23
24 --- @param gitdir string
25 --- @param commondir? string
26 --- @return Gitsigns.Repo.Watcher
27 function Watcher.new(gitdir, commondir)
28 local self = setmetatable({}, Watcher)
29
30 self.update_callbacks = {}
31 self.gitdir = gitdir
32 self.commondir = commondir or gitdir
33 self.handles = {}
34 self.notify_callbacks_debounced = debounce_trailing(200, Watcher.notify_callbacks)
35
36 local weak_self = util.weak_ref(self)
37
38 self._gc = util.gc_proxy(function()
39 for _, handle in pairs(self.handles) do
40 handle:stop()
41 handle:close()
42 end
43 end)
44
45 -- Changes to nested refs (e.g. `refs/heads/main`) may not be reported when
46 -- only watching the gitdir root, so we add extra watches as needed.
47 self:_watch_dir(gitdir, weak_self)
48
49 if self.commondir ~= gitdir then
50 self:_watch_dir(self.commondir, weak_self)
51 end
52
53 local reftable_dir = Path.join(self.commondir, 'reftable')
54 self:_watch_dir(reftable_dir, weak_self)
55
56 return self
57 end
58
59 --- @private
60 --- @param dir string
61 --- @param weak_self {ref:Gitsigns.Repo.Watcher}
62 function Watcher:_watch_dir(dir, weak_self)
63 if self.handles[dir] or not Path.is_dir(dir) then
64 return
65 end
66
67 local handle = assert(vim.uv.new_fs_event())
68 self.handles[dir] = handle
69
70 log.dprintf('Starting git dir watcher on %s', dir)
71 handle:start(dir, {}, Watcher.handler(weak_self))
72 end
73
74 --- @private
75 --- @param dir string
76 function Watcher:_unwatch_dir(dir)
77 local handle = self.handles[dir]
78 if not handle then
79 return
80 end
81
82 handle:stop()
83 handle:close()
84 self.handles[dir] = nil
85 end
86
87 --- Watch the directory containing `head_ref` under commondir.
88 --- This ensures we see branch-tip moves which update the target ref file but
89 --- don't necessarily touch `gitdir/HEAD`.
90 --- @param head_ref? string
91 function Watcher:set_head_ref(head_ref)
92 local old_dir = self.head_ref_dir
93 local new_dir --- @type string?
94
95 if head_ref then
96 local rel_dir = vim.fs.dirname(head_ref)
97 if rel_dir and rel_dir ~= '.' then
98 new_dir = Path.join(self.commondir, rel_dir)
99 end
100 end
101
102 if old_dir and old_dir ~= new_dir then
103 self:_unwatch_dir(old_dir)
104 self.head_ref_dir = nil
105 end
106
107 if new_dir and not self.head_ref_dir then
108 self.head_ref_dir = new_dir
109 self:_watch_dir(new_dir, util.weak_ref(self))
110 end
111 end
112
113 --- @param callback fun() Callback function to be invoked on update.
114 --- @return fun() deregister Function to remove the callback from the watcher.
115 function Watcher:on_update(callback)
116 -- Make sure insertion order is preserved as pos 1 is used by the repo object
117 -- and must run before the buffer callbacks.
118 table.insert(self.update_callbacks, callback)
119 return function()
120 for i, cb in ipairs(self.update_callbacks) do
121 if cb == callback then
122 table.remove(self.update_callbacks, i)
123 break
124 end
125 end
126 end
127 end
128
129 --- @param weak_self {ref:Gitsigns.Repo.Watcher}
130 function Watcher.notify_callbacks(weak_self)
131 local self = weak_self.ref
132 if not self then
133 return -- garbage collected
134 end
135
136 vim.schedule(function()
137 for _, cb in ipairs(self.update_callbacks) do
138 local ok, err = pcall(cb)
139 if not ok then
140 log.eprintf('Repo watcher callback error: %s', err)
141 end
142 end
143 end)
144 end
145
146 --- @param weak_self {ref:Gitsigns.Repo.Watcher}
147 --- @return fun(err:string?, filename:string, events:{ change:boolean?, rename:boolean? })
148 function Watcher.handler(weak_self)
149 --- @param err string?
150 --- @param filename string
151 --- @param events { change: boolean?, rename: boolean? }
152 return function(err, filename, events)
153 local __FUNC__ = 'watcher.handler1'
154
155 local self = weak_self.ref
156 if not self then
157 log.dprint('watcher was garbage collected')
158 return
159 end
160
161 if err then
162 log.dprintf('Git dir update error: %s', err)
163 return
164 end
165
166 -- The luv docs say filename is passed as a string but it has been observed
167 -- to sometimes be nil.
168 -- https://github.com/lewis6991/gitsigns.nvim/issues/848
169 if not filename then
170 log.eprint('No filename')
171 else
172 for _, ex in ipairs({
173 '.watchman-cookie',
174 'index.lock',
175 }) do
176 if vim.startswith(filename, ex) then
177 log.dprintf("Git dir update: '%s' %s (ignoring)", filename, inspect(events))
178 return
179 end
180 end
181
182 log.dprintf("Git dir update: '%s' %s", filename, inspect(events))
183 end
184
185 self.notify_callbacks_debounced(weak_self)
186 end
187 end
188
189 return Watcher