11local logger = require (" gitlinker.logger" )
2+ local spawn = require (" gitlinker.spawn" )
23
34--- @class JobResult
45--- @field stdout string[]
56--- @field stderr string[]
7+ local JobResult = {}
8+
9+ --- @return JobResult
10+ function JobResult :new ()
11+ local o = {
12+ stdout = {},
13+ stderr = {},
14+ }
15+ setmetatable (o , self )
16+ self .__index = self
17+ return o
18+ end
619
7- --- @param result JobResult
820--- @return boolean
9- local function result_has_out (result )
10- return result [" stdout" ]
11- and type (result [" stdout" ]) == " table"
12- and # result [" stdout" ] > 0
21+ function JobResult :has_out ()
22+ return type (self .stdout ) == " table" and # self .stdout > 0
1323end
1424
15- --- @param result JobResult
1625--- @return boolean
17- local function result_has_err (result )
18- return result [" stderr" ] ~= nil
19- and type (result [" stderr" ]) == " table"
20- and # result [" stderr" ] > 0
26+ function JobResult :has_err ()
27+ return type (self .stderr ) == " table" and # self .stderr > 0
2128end
2229
23- --- @param result JobResult
24- --- @param default string | nil
25- --- @return nil
26- local function result_print_err ( result , default )
27- if result_has_err ( result ) and # result . stderr > 0 then
28- logger . err ( " %s " , result . stderr [ 1 ])
30+ --- @param default string
31+ function JobResult : print_err ( default )
32+ if self : has_err () then
33+ for _ , e in ipairs ( self . stderr ) do
34+ logger . err ( " %s " , e )
35+ end
2936 else
3037 logger .err (" fatal: %s" , default )
3138 end
3744--- @param cwd string | nil
3845--- @return JobResult
3946local function cmd (args , cwd )
40- local result = { stdout = {}, stderr = {} }
41- local job = vim .fn .jobstart (args , {
42- cwd = cwd ,
43- on_stdout = function (chanid , data , name )
44- logger .debug (
45- " |cmd.on_stdout| args(%s):%s, cwd(%s):%s, chanid(%s):%s, data(%s):%s, name(%s):%s" ,
46- vim .inspect (type (args )),
47- vim .inspect (args ),
48- vim .inspect (type (cwd )),
49- vim .inspect (cwd ),
50- vim .inspect (type (chanid )),
51- vim .inspect (chanid ),
52- vim .inspect (type (data )),
53- vim .inspect (data ),
54- vim .inspect (type (name )),
55- vim .inspect (name )
56- )
57- for _ , line in ipairs (data ) do
58- if string.len (line ) > 0 then
59- table.insert (result .stdout , line )
60- end
61- end
62- end ,
63- on_stderr = function (chanid , data , name )
64- logger .debug (
65- " |cmd.on_stderr| args(%s):%s, cwd(%s):%s, chanid(%s):%s, data(%s):%s, name(%s):%s" ,
66- vim .inspect (type (args )),
67- vim .inspect (args ),
68- vim .inspect (type (cwd )),
69- vim .inspect (cwd ),
70- vim .inspect (type (chanid )),
71- vim .inspect (chanid ),
72- vim .inspect (type (data )),
73- vim .inspect (data ),
74- vim .inspect (type (name )),
75- vim .inspect (name )
76- )
77- for _ , line in ipairs (data ) do
78- if string.len (line ) > 0 then
79- table.insert (result .stderr , line )
80- end
81- end
82- end ,
83- })
84- vim .fn .jobwait ({ job })
47+ local result = JobResult :new ()
48+
49+ local sp = spawn .Spawn :make (args , function (line )
50+ if type (line ) == " string" then
51+ table.insert (result .stdout , line )
52+ end
53+ end , function (line )
54+ if type (line ) == " string" then
55+ table.insert (result .stderr , line )
56+ end
57+ end ) --[[ @as Spawn]]
58+ sp :run ()
59+
8560 logger .debug (
8661 " |cmd| args(%s):%s, cwd(%s):%s, result(%s):%s" ,
8762 vim .inspect (type (args )),
12398
12499--- @package
125100--- @param revspec string | nil
126- --- @return string | nil
101+ --- @return string ?
127102local function get_rev (revspec )
128103 local result = cmd ({ " git" , " rev-parse" , revspec })
129104 logger .debug (
@@ -133,7 +108,7 @@ local function get_rev(revspec)
133108 vim .inspect (type (result )),
134109 vim .inspect (result )
135110 )
136- return result_has_out ( result ) and result .stdout [1 ] or nil
111+ return result : has_out ( ) and result .stdout [1 ] or nil
137112end
138113
139114--- @package
@@ -182,7 +157,7 @@ local function has_file_changed(file, rev)
182157 vim .inspect (type (result )),
183158 vim .inspect (result )
184159 )
185- return result_has_out ( result )
160+ return result : has_out ( )
186161end
187162
188163--- @package
@@ -271,14 +246,14 @@ local function get_root()
271246 return result
272247end
273248
274- --- @return string | nil
249+ --- @return string ?
275250local function get_branch_remote ()
276251 -- origin/upstream
277252 --- @type JobResult
278253 local remote_result = get_remote ()
279254
280255 if type (remote_result .stdout ) ~= " table" or # remote_result .stdout == 0 then
281- result_print_err ( remote_result , " git repository has no remote" )
256+ remote_result : print_err ( " git repository has no remote" )
282257 return nil
283258 end
284259
@@ -289,8 +264,8 @@ local function get_branch_remote()
289264 -- origin/linrongbin16/add-rule2
290265 --- @type JobResult
291266 local upstream_branch_result = get_rev_name (" @{u}" )
292- if not result_has_out ( upstream_branch_result ) then
293- result_print_err ( upstream_branch_result , " git branch has no remote" )
267+ if not upstream_branch_result : has_out ( ) then
268+ upstream_branch_result : print_err ( " git branch has no remote" )
294269 return nil
295270 end
296271
@@ -324,11 +299,7 @@ local function get_branch_remote()
324299 return nil
325300end
326301
327- --- @type table<string , function>
328302local M = {
329- result_has_out = result_has_out ,
330- result_has_err = result_has_err ,
331- result_print_err = result_print_err ,
332303 get_root = get_root ,
333304 get_remote_url = get_remote_url ,
334305 is_file_in_rev = is_file_in_rev ,
0 commit comments