# HG changeset patch # User Matthew Wild # Date 1245817305 -3600 # Node ID 327a60008b022dfb16bac4fab763a12d3698d1c8 Initial commit diff -r 000000000000 -r 327a60008b02 hg.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hg.lua Wed Jun 24 05:21:45 2009 +0100 @@ -0,0 +1,68 @@ + +module("hg", package.seeall) + +__index = _M; + +function open(repo_path) + return setmetatable({ path = repo_path }, _M); +end + +function exec(repo, command, ...) + local params = {}; + for i=1,select('#', ...) do + local param = select(i, ...); + if param then + --TODO: Shell escape + params[i] = param; + end + end + params = table.concat(params, " "); + return os.execute("hg -y -q -R '"..repo.path.."' "..command.." "..params); +end + +function execr(repo, command, ...) + local params = {}; + for i=1,select('#', ...) do + local param = select(i, ...); + if param then + --TODO: Shell escape + params[i] = tostring(param); + end + end + params = table.concat(params, " "); + + local cmd = io.popen("hg -y -R '"..repo.path.."' "..command.." "..params); + if cmd then + return cmd:read("*a"); + end +end + +function update(repo, rev, force) + repo:exec("up", rev and ("-r"..rev), force and "-C"); +end + +function identify(repo) + local ret = repo:execr("id", "-n -i"); + if ret then + local id, num = ret:match("(%S+)%s(%S+)"); + return id, tonumber(num); + end +end + +function log(repo, rev, limit) + local ret = repo:execr("log", rev and ("-r"..rev), limit and ("-l"..limit)); + if ret then + local results, curr = {}, 1; + for line in ret:gmatch("(.-\n)\n") do + if not results[curr] then + results[curr] = {}; + end + for k,v in line:gmatch("(%w+):%s*(.-)%s*\n") do + + results[curr][k] = v; + end + curr = curr + 1; + end + return results; + end +end