hg.lua

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

mercurial