hg.lua

Wed, 24 Jun 2009 05:21:45 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 24 Jun 2009 05:21:45 +0100
changeset 0
327a60008b02
permissions
-rw-r--r--

Initial commit


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

mercurial