@@ -2,25 +2,35 @@ local log = require("nvim-tree.log")
2
2
local utils = require (" nvim-tree.utils" )
3
3
local notify = require (" nvim-tree.notify" )
4
4
5
- local Class = require (" nvim-tree.class " )
5
+ local Class = require (" nvim-tree.classic " )
6
6
7
- --- @class (exact ) GitRunnerOpts
7
+ --- @class (exact ) GitRunner : Class
8
+ --- @field private args GitRunnerArgs
9
+ --- @field private path_xy GitPathXY
10
+ --- @field private rc integer ? -- -1 indicates timeout
11
+ local GitRunner = Class :extend ()
12
+
13
+ --- @class GitRunner
14
+ --- @overload fun ( args : GitRunnerArgs ): GitRunner
15
+
16
+ --- @class (exact ) GitRunnerArgs
8
17
--- @field toplevel string absolute path
9
18
--- @field path string ? absolute path
10
19
--- @field list_untracked boolean
11
20
--- @field list_ignored boolean
12
21
--- @field timeout integer
13
22
--- @field callback fun ( path_xy : GitPathXY )?
14
23
15
- --- @class (exact ) GitRunner : Class
16
- --- @field private opts GitRunnerOpts
17
- --- @field private path_xy GitPathXY
18
- --- @field private rc integer ? -- -1 indicates timeout
19
- local GitRunner = Class :new ()
20
-
21
24
local timeouts = 0
22
25
local MAX_TIMEOUTS = 5
23
26
27
+ --- @private
28
+ --- @param args GitRunnerArgs
29
+ function GitRunner :new (args )
30
+ self .args = args
31
+ self .path_xy = {}
32
+ end
33
+
24
34
--- @private
25
35
--- @param status string
26
36
--- @param path string | nil
@@ -34,7 +44,7 @@ function GitRunner:parse_status_output(status, path)
34
44
path = path :gsub (" /" , " \\ " )
35
45
end
36
46
if # status > 0 and # path > 0 then
37
- self .path_xy [utils .path_remove_trailing (utils .path_join ({ self .opts .toplevel , path }))] = status
47
+ self .path_xy [utils .path_remove_trailing (utils .path_join ({ self .args .toplevel , path }))] = status
38
48
end
39
49
end
40
50
81
91
--- @param stderr_handle uv.uv_pipe_t
82
92
--- @return uv.spawn.options
83
93
function GitRunner :get_spawn_options (stdout_handle , stderr_handle )
84
- local untracked = self .opts .list_untracked and " -u" or nil
85
- local ignored = (self .opts .list_untracked and self .opts .list_ignored ) and " --ignored=matching" or " --ignored=no"
94
+ local untracked = self .args .list_untracked and " -u" or nil
95
+ local ignored = (self .args .list_untracked and self .args .list_ignored ) and " --ignored=matching" or " --ignored=no"
86
96
return {
87
- args = { " --no-optional-locks" , " status" , " --porcelain=v1" , " -z" , ignored , untracked , self .opts .path },
88
- cwd = self .opts .toplevel ,
97
+ args = { " --no-optional-locks" , " status" , " --porcelain=v1" , " -z" , ignored , untracked , self .args .path },
98
+ cwd = self .args .toplevel ,
89
99
stdio = { nil , stdout_handle , stderr_handle },
90
100
}
91
101
end
@@ -139,7 +149,7 @@ function GitRunner:run_git_job(callback)
139
149
end
140
150
141
151
local spawn_options = self :get_spawn_options (stdout , stderr )
142
- log .line (" git" , " running job with timeout %dms" , self .opts .timeout )
152
+ log .line (" git" , " running job with timeout %dms" , self .args .timeout )
143
153
log .line (" git" , " git %s" , table.concat (utils .array_remove_nils (spawn_options .args ), " " ))
144
154
145
155
handle , pid = vim .loop .spawn (
@@ -151,7 +161,7 @@ function GitRunner:run_git_job(callback)
151
161
)
152
162
153
163
timer :start (
154
- self .opts .timeout ,
164
+ self .args .timeout ,
155
165
0 ,
156
166
vim .schedule_wrap (function ()
157
167
on_finish (- 1 )
@@ -191,35 +201,35 @@ end
191
201
--- @private
192
202
function GitRunner :finalise ()
193
203
if self .rc == - 1 then
194
- log .line (" git" , " job timed out %s %s" , self .opts .toplevel , self .opts .path )
204
+ log .line (" git" , " job timed out %s %s" , self .args .toplevel , self .args .path )
195
205
timeouts = timeouts + 1
196
206
if timeouts == MAX_TIMEOUTS then
197
207
notify .warn (string.format (" %d git jobs have timed out after git.timeout %dms, disabling git integration." , timeouts ,
198
- self .opts .timeout ))
208
+ self .args .timeout ))
199
209
require (" nvim-tree.git" ).disable_git_integration ()
200
210
end
201
211
elseif self .rc ~= 0 then
202
- log .line (" git" , " job fail rc %d %s %s" , self .rc , self .opts .toplevel , self .opts .path )
212
+ log .line (" git" , " job fail rc %d %s %s" , self .rc , self .args .toplevel , self .args .path )
203
213
else
204
- log .line (" git" , " job success %s %s" , self .opts .toplevel , self .opts .path )
214
+ log .line (" git" , " job success %s %s" , self .args .toplevel , self .args .path )
205
215
end
206
216
end
207
217
208
218
--- Return nil when callback present
209
219
--- @private
210
220
--- @return GitPathXY ?
211
221
function GitRunner :execute ()
212
- local async = self .opts .callback ~= nil
213
- local profile = log .profile_start (" git %s job %s %s" , async and " async" or " sync" , self .opts .toplevel , self .opts .path )
222
+ local async = self .args .callback ~= nil
223
+ local profile = log .profile_start (" git %s job %s %s" , async and " async" or " sync" , self .args .toplevel , self .args .path )
214
224
215
- if async and self .opts .callback then
225
+ if async and self .args .callback then
216
226
-- async, always call back
217
227
self :run_git_job (function ()
218
228
log .profile_end (profile )
219
229
220
230
self :finalise ()
221
231
222
- self .opts .callback (self .path_xy )
232
+ self .args .callback (self .path_xy )
223
233
end )
224
234
else
225
235
-- sync, maybe call back
@@ -230,8 +240,8 @@ function GitRunner:execute()
230
240
231
241
self :finalise ()
232
242
233
- if self .opts .callback then
234
- self .opts .callback (self .path_xy )
243
+ if self .args .callback then
244
+ self .args .callback (self .path_xy )
235
245
else
236
246
return self .path_xy
237
247
end
@@ -240,15 +250,10 @@ end
240
250
241
251
--- Static method to run a git process, which will be killed if it takes more than timeout
242
252
--- Return nil when callback present
243
- --- @param opts GitRunnerOpts
253
+ --- @param args GitRunnerArgs
244
254
--- @return GitPathXY ?
245
- function GitRunner :run (opts )
246
- --- @type GitRunner
247
- local runner = {
248
- opts = opts ,
249
- path_xy = {},
250
- }
251
- runner = GitRunner :new (runner )
255
+ function GitRunner :run (args )
256
+ local runner = GitRunner (args )
252
257
253
258
return runner :execute ()
254
259
end
0 commit comments