4:33d49b3bcab5
Anton Shestakov <av6@dwimlabs.net>, Thu, 08 Sep 2016 13:42:51 +0800
pipelines: try this

next change 5:da6e34b39574
previous change 3:48f6ea626b5c

hglib.lua

Permissions: -rw-r--r--

Other formats: Feeds:
local lpc = require('lpc')
local sizes = { i4 = 4, u4 = 4, c = 1 }
local function read_u4(rh)
local value = 0
for i = 1, sizes.u4 do
local byte = rh:read(1):byte()
value = value + byte * (2 ^ ((sizes.u4 - i) * 8))
end
return value
end
local function read_c(rh)
return rh:read(sizes.c)
end
local function read_channel(rh)
local channel = read_c(rh)
local length = read_u4(rh)
if channel == 'I' or channel == 'L' then
return channel, length
else
return channel, rh:read(length)
end
end
local Client = {}
Client.__index = Client
function Client.connect(rh, wh)
local client = {}
setmetatable(client, Client)
client.rh = rh
client.wh = wh
client:read_hello()
return client
end
function Client.open(repo)
local cmd = { 'hg', 'serve', '--cmdserver', 'pipe', '--config', 'ui.interactive=True' }
if repo ~= nil then
table.insert(cmd, '-R')
table.insert(cmd, repo)
end
local pid, wh, rh = lpc.run(unpack(cmd))
local client = Client.connect(rh, wh)
client.lpcpid = pid
return client
end
function Client:close()
if self.wh ~= nil then self.wh:close() end
if self.rh ~= nil then self.rh:close() end
if self.lpcpid ~= nil then lpc.wait(self.lpcpid) end
end
function Client:read_hello()
local channel, message = read_channel(self.rh)
assert(channel == 'o', 'channel for the hello message must be "o"')
local index = 1
while index <= #message do
local nexteol = message:find('\n', index + 1, true)
if nexteol == nil then nexteol = #message + 1 end
local line = message:sub(index, nexteol - 1)
local ci = line:find(': ', 1, true)
if ci ~= nil then
local key, value = line:sub(1, ci - 1), line:sub(ci + 2)
if key == 'capabilities' then
self.capabilities = {}
for cap in value:gmatch('%S+') do
self.capabilities[cap] = true
end
elseif key == 'encoding' then
self.encoding = value
elseif key == 'pid' then
self.pid = tonumber(value)
elseif key == 'pgid' then
self.pgid = tonumber(value)
end
end
index = nexteol + 1
end
end
function Client:getencoding()
if not self.capabilities.getencoding then
return nil, 'getencoding is not supported by this command server'
end
self.wh:write('getencoding\n')
self.wh:flush()
local channel, message = read_channel(self.rh)
if channel == 'r' then
return message
elseif channel == 'e' then
return nil, message
end
end
return {
read_u4 = read_u4,
read_c = read_c,
read_channel = read_channel,
Client = Client
}