Download:
child 35:9dcbc37ecb32
parent 33:40f83ed9b472
34:e0912d348cb1
Anton Shestakov <av6@dwimlabs.net>, Fri, 16 Sep 2016 16:07:39 +0800
hglib: commands now can receive input

3 файлов изменено, 55 вставок(+), 1 удалений(-) [+]
hglib.lua file | annotate | diff | comparison | revisions
spec/data/foo.patch file | annotate | diff | comparison | revisions
spec/hglib_spec.lua file | annotate | diff | comparison | revisions
--- a/hglib.lua Fri Sep 16 15:50:45 2016 +0800
+++ b/hglib.lua Fri Sep 16 16:07:39 2016 +0800
@@ -128,7 +128,7 @@
end
end
-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', ''
end
@@ -138,6 +138,11 @@
local o = ''
local e = ''
local d = ''
+ local function write_input(length)
+ write_block(self.wh, input:sub(1, length))
+ self.wh:flush()
+ input = input:sub(length + 1)
+ end
while true do
local channel, message = read_channel(self.rh)
if channel == 'r' then
@@ -148,6 +153,10 @@
e = e .. message
elseif channel == 'd' then
d = d .. message
+ elseif channel == 'I' and input ~= nil then
+ write_input(message)
+ 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 .. '"'
return nil, o, e, d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/data/foo.patch Fri Sep 16 16:07:39 2016 +0800
@@ -0,0 +1,14 @@
+# HG changeset patch
+# 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!
+
+diff --git a/foo b/foo
+new file mode 100644
+--- /dev/null
++++ b/foo
+@@ -0,0 +1,1 @@
++bar
--- 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
@@ -80,4 +80,35 @@
assert.are_equal('', d)
assert.are_equal(0, code)
end)
+
+ it('can use runcommand to import a patch and run summary', function()
+ local client = hglib.Client.open()
+ do
+ 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)
+ end
+ do
+ local pf = assert(io.open('spec/data/foo.patch', 'rb'))
+ local patch = pf:read('*all')
+ pf:close()
+ 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)
+ end
+ do
+ 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)
+ end
+ client:close()
+ end)
end)