hg.lua

1
module("hg", package.seeall)
2
 
3
__index = _M;
4
 
5
function open(repo_path)
6
	return setmetatable({ path = repo_path }, _M);
7
end
8
 
9
function exec(repo, command, ...)
10
	local params = {};
11
	for i=1,select('#', ...) do
12
		local param = select(i, ...);
13
		if param then
14
			--TODO: Shell escape
15
			params[i] = param;
16
		end
17
	end
18
	params = table.concat(params, " ");
19
	return os.execute("hg -y -q -R '"..repo.path.."' "..command.." "..params);
20
end
21
 
22
function execr(repo, command, ...)
23
	local params = {};
24
	for i=1,select('#', ...) do
25
		local param = select(i, ...);
26
		if param then
27
			--TODO: Shell escape
28
			params[i] = tostring(param);
29
		end
30
	end
31
	params = table.concat(params, " ");
32
	
33
	local cmd = io.popen("hg -y -R '"..repo.path.."' "..command.." "..params);
34
	if cmd then
35
		return cmd:read("*a");
36
	end
37
end
38
 
39
function update(repo, rev, force)
40
	repo:exec("up", rev and ("-r"..rev), force and "-C");
41
end
42
 
43
function identify(repo)
44
	local ret = repo:execr("id", "-n -i");
45
	if ret then
46
		local id, num =  ret:match("(%S+)%s(%S+)");
47
		return id, tonumber(num);
48
	end
49
end
50
 
51
function log(repo, rev, limit)
52
	local ret = repo:execr("log", rev and ("-r"..rev), limit and ("-l"..limit));
53
	if ret then
54
		local results, curr = {}, 1;
55
		for line in ret:gmatch("(.-\n)\n") do
56
			if not results[curr] then
57
				results[curr] = {};
58
			end
59
			for k,v in line:gmatch("(%w+):%s*(.-)%s*\n") do
60
			
61
				results[curr][k] = v;
62
			end
63
			curr = curr + 1;
64
		end
65
		return results;
66
	end
67
end