44
44
--- NOTE: async functions can't have optional parameters so wrap it into another function without '_'
45
45
local _run_cmd = async .wrap (function (args , cwd , callback )
46
46
local result = CmdResult :new ()
47
+ local logger = logging .get (" gitlinker" )
48
+ logger :debug (string.format (" |_run_cmd| args:%s, cwd:%s" , vim .inspect (args ), vim .inspect (cwd )))
47
49
48
50
spawn .run (args , {
49
51
cwd = cwd ,
@@ -70,10 +72,11 @@ local function run_cmd(args, cwd)
70
72
end
71
73
72
74
--- @package
75
+ --- @param cwd string ?
73
76
--- @return string[] | nil
74
- local function _get_remote ()
77
+ local function _get_remote (cwd )
75
78
local args = { " git" , " remote" }
76
- local result = run_cmd (args )
79
+ local result = run_cmd (args , cwd )
77
80
if type (result .stdout ) ~= " table" or # result .stdout == 0 then
78
81
result :print_err (" fatal: git repo has no remote" )
79
82
return nil
@@ -87,11 +90,12 @@ local function _get_remote()
87
90
end
88
91
89
92
--- @param remote string
93
+ --- @param cwd string ?
90
94
--- @return string ?
91
- local function get_remote_url (remote )
95
+ local function get_remote_url (remote , cwd )
92
96
assert (remote , " remote cannot be nil" )
93
97
local args = { " git" , " remote" , " get-url" , remote }
94
- local result = run_cmd (args )
98
+ local result = run_cmd (args , cwd )
95
99
if not result :has_out () then
96
100
result :print_err (" fatal: failed to get remote url by remote '" .. remote .. " '" )
97
101
return nil
@@ -106,10 +110,11 @@ end
106
110
107
111
--- @package
108
112
--- @param revspec string ?
113
+ --- @param cwd string ?
109
114
--- @return string ?
110
- local function _get_rev (revspec )
115
+ local function _get_rev (revspec , cwd )
111
116
local args = { " git" , " rev-parse" , revspec }
112
- local result = run_cmd (args )
117
+ local result = run_cmd (args , cwd )
113
118
-- logger.debug(
114
119
-- "|git._get_rev| running %s: %s (error:%s)",
115
120
-- vim.inspect(args),
@@ -121,10 +126,11 @@ end
121
126
122
127
--- @package
123
128
--- @param revspec string
129
+ --- @param cwd string ?
124
130
--- @return string ?
125
- local function _get_rev_name (revspec )
131
+ local function _get_rev_name (revspec , cwd )
126
132
local args = { " git" , " rev-parse" , " --abbrev-ref" , revspec }
127
- local result = run_cmd (args )
133
+ local result = run_cmd (args , cwd )
128
134
if not result :has_out () then
129
135
result :print_err (" fatal: git branch has no remote" )
130
136
return nil
@@ -139,10 +145,11 @@ end
139
145
140
146
--- @param file string
141
147
--- @param revspec string
148
+ --- @param cwd string ?
142
149
--- @return boolean
143
- local function is_file_in_rev (file , revspec )
150
+ local function is_file_in_rev (file , revspec , cwd )
144
151
local args = { " git" , " cat-file" , " -e" , revspec .. " :" .. file }
145
- local result = run_cmd (args )
152
+ local result = run_cmd (args , cwd )
146
153
if result :has_err () then
147
154
result :print_err (" fatal: '" .. file .. " ' does not exist in remote '" .. revspec .. " '" )
148
155
return false
@@ -157,10 +164,11 @@ end
157
164
158
165
--- @param file string
159
166
--- @param rev string
167
+ --- @param cwd string ?
160
168
--- @return boolean
161
- local function file_has_changed (file , rev )
169
+ local function file_has_changed (file , rev , cwd )
162
170
local args = { " git" , " diff" , rev , " --" , file }
163
- local result = run_cmd (args )
171
+ local result = run_cmd (args , cwd )
164
172
-- logger.debug(
165
173
-- "|git.has_file_changed| running %s: %s",
166
174
-- vim.inspect(args),
@@ -172,10 +180,11 @@ end
172
180
--- @package
173
181
--- @param revspec string
174
182
--- @param remote string
183
+ --- @param cwd string ?
175
184
--- @return boolean
176
- local function _is_rev_in_remote (revspec , remote )
185
+ local function _is_rev_in_remote (revspec , remote , cwd )
177
186
local args = { " git" , " branch" , " --remotes" , " --contains" , revspec }
178
- local result = run_cmd (args )
187
+ local result = run_cmd (args , cwd )
179
188
-- logger.debug(
180
189
-- "|git._is_rev_in_remote| running %s: %s (error:%s)",
181
190
-- vim.inspect(args),
@@ -193,10 +202,11 @@ end
193
202
194
203
--- @package
195
204
--- @param remote string
205
+ --- @param cwd string ?
196
206
--- @return boolean
197
- local function _has_remote_fetch_config (remote )
207
+ local function _has_remote_fetch_config (remote , cwd )
198
208
local args = { " git" , " config" , string.format (" remote.%s.fetch" , remote ) }
199
- local result = run_cmd (args )
209
+ local result = run_cmd (args , cwd )
200
210
-- logger.debug(
201
211
-- "|git._has_remote_fetch_config| running %s: %s (error:%s)",
202
212
-- vim.inspect(args),
@@ -258,13 +268,14 @@ local function resolve_host(host)
258
268
end
259
269
260
270
--- @param remote string
271
+ --- @param cwd string ?
261
272
--- @return string ?
262
- local function get_closest_remote_compatible_rev (remote )
273
+ local function get_closest_remote_compatible_rev (remote , cwd )
263
274
local logger = logging .get (" gitlinker" )
264
275
assert (remote , " remote cannot be nil" )
265
276
266
277
-- try upstream branch HEAD (a.k.a @{u})
267
- local upstream_rev = _get_rev (" @{u}" )
278
+ local upstream_rev = _get_rev (" @{u}" , cwd )
268
279
-- logger.debug(
269
280
-- "|git.get_closest_remote_compatible_rev| running _get_rev:%s",
270
281
-- vim.inspect(upstream_rev)
@@ -277,14 +288,14 @@ local function get_closest_remote_compatible_rev(remote)
277
288
278
289
-- try HEAD
279
290
if remote_fetch_configured then
280
- if _is_rev_in_remote (" HEAD" , remote ) then
281
- local head_rev = _get_rev (" HEAD" )
291
+ if _is_rev_in_remote (" HEAD" , remote , cwd ) then
292
+ local head_rev = _get_rev (" HEAD" , cwd )
282
293
if head_rev then
283
294
return head_rev
284
295
end
285
296
end
286
297
else
287
- local head_rev = _get_rev (" HEAD" )
298
+ local head_rev = _get_rev (" HEAD" , cwd )
288
299
if head_rev then
289
300
return head_rev
290
301
end
@@ -294,8 +305,8 @@ local function get_closest_remote_compatible_rev(remote)
294
305
if remote_fetch_configured then
295
306
for i = 1 , 50 do
296
307
local revspec = " HEAD~" .. i
297
- if _is_rev_in_remote (revspec , remote ) then
298
- local rev = _get_rev (revspec )
308
+ if _is_rev_in_remote (revspec , remote , cwd ) then
309
+ local rev = _get_rev (revspec , cwd )
299
310
if rev then
300
311
return rev
301
312
end
@@ -304,15 +315,15 @@ local function get_closest_remote_compatible_rev(remote)
304
315
else
305
316
for i = 1 , 50 do
306
317
local revspec = " HEAD~" .. i
307
- local rev = _get_rev (revspec )
318
+ local rev = _get_rev (revspec , cwd )
308
319
if rev then
309
320
return rev
310
321
end
311
322
end
312
323
end
313
324
314
325
-- try remote HEAD
315
- local remote_rev = _get_rev (remote )
326
+ local remote_rev = _get_rev (remote , cwd )
316
327
if remote_rev then
317
328
return remote_rev
318
329
end
@@ -321,12 +332,11 @@ local function get_closest_remote_compatible_rev(remote)
321
332
return nil
322
333
end
323
334
335
+ --- @param cwd string ?
324
336
--- @return string ?
325
- local function get_root ()
326
- local buf_path = vim .api .nvim_buf_get_name (0 )
327
- local buf_dir = vim .fn .fnamemodify (buf_path , " :p:h" )
337
+ local function get_root (cwd )
328
338
local args = { " git" , " rev-parse" , " --show-toplevel" }
329
- local result = run_cmd (args , buf_dir )
339
+ local result = run_cmd (args , cwd )
330
340
-- logger.debug(
331
341
-- "|git.get_root| buf_path:%s, buf_dir:%s, result:%s",
332
342
-- vim.inspect(buf_path),
@@ -346,11 +356,12 @@ local function get_root()
346
356
return result .stdout [1 ]
347
357
end
348
358
359
+ --- @param cwd string ?
349
360
--- @return string ?
350
- local function get_branch_remote ()
361
+ local function get_branch_remote (cwd )
351
362
local logger = logging .get (" gitlinker" )
352
363
-- origin/upstream
353
- local remotes = _get_remote ()
364
+ local remotes = _get_remote (cwd )
354
365
if not remotes then
355
366
return nil
356
367
end
@@ -360,7 +371,7 @@ local function get_branch_remote()
360
371
end
361
372
362
373
-- origin/linrongbin16/add-rule2
363
- local upstream_branch = _get_rev_name (" @{u}" )
374
+ local upstream_branch = _get_rev_name (" @{u}" , cwd )
364
375
if not upstream_branch then
365
376
return nil
366
377
end
@@ -393,11 +404,12 @@ local function get_branch_remote()
393
404
end
394
405
395
406
--- @param remote string
407
+ --- @param cwd string ?
396
408
--- @return string ?
397
- local function get_default_branch (remote )
409
+ local function get_default_branch (remote , cwd )
398
410
local logger = logging .get (" gitlinker" )
399
411
local args = { " git" , " rev-parse" , " --abbrev-ref" , string.format (" %s/HEAD" , remote ) }
400
- local result = run_cmd (args )
412
+ local result = run_cmd (args , cwd )
401
413
if type (result .stdout ) ~= " table" or # result .stdout == 0 then
402
414
return nil
403
415
end
@@ -412,11 +424,12 @@ local function get_default_branch(remote)
412
424
return splits [# splits ]
413
425
end
414
426
427
+ --- @param cwd string ?
415
428
--- @return string ?
416
- local function get_current_branch ()
429
+ local function get_current_branch (cwd )
417
430
local logger = logging .get (" gitlinker" )
418
431
local args = { " git" , " rev-parse" , " --abbrev-ref" , " HEAD" }
419
- local result = run_cmd (args )
432
+ local result = run_cmd (args , cwd )
420
433
if type (result .stdout ) ~= " table" or # result .stdout == 0 then
421
434
return nil
422
435
end
0 commit comments