--- a/hglib.lua Fri Sep 16 15:50:45 2016 +0800
+++ b/hglib.lua Fri Sep 16 16:07:39 2016 +0800
-function Client:runcommand(command)
+function Client:runcommand(command, input)
if not self.capabilities.runcommand then
return nil, '', 'runcommand is not supported by this command server', ''
+ local function write_input(length)
+ write_block(self.wh, input:sub(1, length))
+ input = input:sub(length + 1)
local channel, message = read_channel(self.rh)
elseif channel == 'd' then
+ elseif channel == 'I' and input ~= nil then
+ elseif channel == 'L' and input ~= nil then
+ write_input(math.min(input:find('\n') or message, message))
elseif channel:lower() ~= channel then
e = e .. '\nhglib: unexpected data on required channel "' .. channel .. '"'
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/data/foo.patch Fri Sep 16 16:07:39 2016 +0800
+# User John Smith <john@example.com>
+# Date 1474010779 -28800
+# Fri Sep 16 15:26:19 2016 +0800
+# Node ID 39d84331d692c8b0beb94da7cba0c5af83ee070d
+# Parent 0000000000000000000000000000000000000000
+First patch ever, I don't know what I'm doing!
--- a/spec/hglib_spec.lua Fri Sep 16 15:50:45 2016 +0800
+++ b/spec/hglib_spec.lua Fri Sep 16 16:07:39 2016 +0800
assert.are_equal(0, code)
+ it('can use runcommand to import a patch and run summary', function()
+ local client = hglib.Client.open()
+ local code, o, e, d = client:runcommand({'init', 'testrepo'})
+ finally(function() os.execute('rm -r testrepo') end)
+ assert.are_equal('', o)
+ assert.are_equal('', e)
+ assert.are_equal('', d)
+ assert.are_equal(0, code)
+ local pf = assert(io.open('spec/data/foo.patch', 'rb'))
+ local patch = pf:read('*all')
+ local code, o, e, d = client:runcommand({'-R', 'testrepo', 'import', '-'}, patch)
+ assert.is_not_nil(o:find('applying patch from stdin'))
+ assert.are_equal('', e)
+ assert.are_equal('', d)
+ assert.are_equal(0, code)
+ local code, o, e, d = client:runcommand({'-R', 'testrepo', 'summary'})
+ assert.is_nil(o:find('empty repository'))
+ assert.is_not_nil(o:find('parent: 0:[0-9a-f]+ tip'))
+ assert.are_equal('', e)
+ assert.are_equal('', d)
+ assert.are_equal(0, code)