Download:
child 14:1ee9475523f1
parent 12:928e6b6d811d
13:0fbc8c1732ed
Anton Shestakov <av6@dwimlabs.net>, Fri, 09 Sep 2016 12:24:59 +0800
hglib: Client.runcommand()

2 файлов изменено, 63 вставок(+), 0 удалений(-) [+]
hglib.lua file | annotate | diff | comparison | revisions
spec/hglib_spec.lua file | annotate | diff | comparison | revisions
--- a/hglib.lua Fri Sep 09 12:21:28 2016 +0800
+++ b/hglib.lua Fri Sep 09 12:24:59 2016 +0800
@@ -2,6 +2,15 @@
local sizes = { i4 = 4, u4 = 4, c = 1 }
+local function write_u4(wh, value)
+ local bytes = ''
+ for _ = 1, sizes.u4 do
+ bytes = string.char(value % (2 ^ 8)) .. bytes
+ value = math.floor(value / (2 ^ 8))
+ end
+ wh:write(bytes)
+end
+
local function read_u4(rh)
local value = 0
for i = 1, sizes.u4 do
@@ -37,6 +46,12 @@
return value
end
+local function write_block(wh, ...)
+ local block = table.concat({...}, '\0')
+ write_u4(wh, #block)
+ wh:write(block)
+end
+
local Client = {}
Client.__index = Client
@@ -111,10 +126,38 @@
end
end
+function Client:runcommand(...)
+ if not self.capabilities.runcommand then
+ return nil, '', 'runcommand is not supported by this command server', ''
+ end
+ self.wh:write('runcommand\n')
+ write_block(self.wh, ...)
+ self.wh:flush()
+ local o = ''
+ local e = ''
+ local d = ''
+ while true do
+ local channel, message = read_channel(self.rh)
+ if channel == 'r' then
+ return decode_i4(message), o, e, d
+ elseif channel == 'o' then
+ o = o .. message
+ elseif channel == 'e' then
+ e = e .. message
+ elseif channel == 'd' then
+ d = d .. message
+ elseif channel:lower() ~= channel then
+ return nil, o, e, d
+ end
+ end
+end
+
return {
+ write_u4 = write_u4,
read_u4 = read_u4,
read_c = read_c,
read_channel = read_channel,
decode_i4 = decode_i4,
+ write_block = write_block,
Client = Client
}
--- a/spec/hglib_spec.lua Fri Sep 09 12:21:28 2016 +0800
+++ b/spec/hglib_spec.lua Fri Sep 09 12:24:59 2016 +0800
@@ -48,4 +48,24 @@
assert.is_nil(err)
assert.are_equal('string', type(encoding))
end)
+
+ it('can use runcommand to request version', function()
+ local client = hglib.Client.open()
+ local code, o, e, d = client:runcommand('version')
+ client:close()
+ assert.are_equal(0, code)
+ assert.is_not_nil(o:find('Mercurial Distributed SCM'))
+ assert.are_equal('', e)
+ assert.are_equal('', d)
+ end)
+
+ it('can use runcommand to run nonexistent command', function()
+ local client = hglib.Client.open()
+ local code, o, e, d = client:runcommand('extirpate')
+ client:close()
+ assert.are_equal(255, code)
+ assert.is_not_nil(o:find('use "hg help" for the full list of commands'))
+ assert.is_not_nil(e:find('unknown command'))
+ assert.are_equal('', d)
+ end)
end)