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

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

mercurial