monotone

monotone Mtn Source Tree

Root/std_hooks.lua

1
2-- this is the standard set of lua hooks for monotone;
3-- user-provided files can override it or add to it.
4
5function temp_file()
6 local tdir
7 tdir = os.getenv("TMPDIR")
8 if tdir == nil then tdir = os.getenv("TMP") end
9 if tdir == nil then tdir = os.getenv("TEMP") end
10 if tdir == nil then tdir = "/tmp" end
11 return io.mkstemp(string.format("%s/mt.XXXXXX", tdir))
12end
13
14
15-- attributes are persistent metadata about files (such as execute
16-- bit, ACLs, various special flags) which we want to have set and
17-- re-set any time the files are modified. the attributes themselves
18-- are stored in a file .mt-attrs, in the working copy (and
19-- manifest). each (f,k,v) triple in an atribute file turns into a
20-- call to attr_functions[k](f,v) in lua.
21
22if (attr_functions == nil) then
23 attr_functions = {}
24end
25
26
27attr_functions["execute"] =
28 function(filename, value)
29 if (value == "true") then
30 os.execute(string.format("chmod +x %s", filename))
31 end
32 end
33
34
35function get_http_proxy(host, port)
36 val = os.getenv("HTTP_PROXY")
37 if (val == nil) then
38 val = os.getenv("http_proxy")
39 end
40 if (val == nil) then
41 return nil
42 end
43 val = string.gsub(val, "http://", "")
44 b, e = string.find(val, ":")
45 if (b ~= nil and b > 0) then
46 chost = string.sub(val, 0, b-1)
47 cport = string.sub(val, b+1)
48 return { chost, cport }
49 end
50 return { val, port }
51end
52
53function ignore_file(name)
54 if (string.find(name, "%.a$")) then return true end
55 if (string.find(name, "%.so$")) then return true end
56 if (string.find(name, "%.o$")) then return true end
57 if (string.find(name, "%.la$")) then return true end
58 if (string.find(name, "%.lo$")) then return true end
59 if (string.find(name, "%.aux$")) then return true end
60 if (string.find(name, "%.bak$")) then return true end
61 if (string.find(name, "%.orig$")) then return true end
62 if (string.find(name, "%.rej$")) then return true end
63 if (string.find(name, "%~$")) then return true end
64 if (string.find(name, "/core$")) then return true end
65 if (string.find(name, "^CVS/")) then return true end
66 if (string.find(name, "^SVN/")) then return true end
67 if (string.find(name, "/CVS/")) then return true end
68 if (string.find(name, "/SVN/")) then return true end
69 return false;
70end
71
72
73function edit_comment(basetext)
74 local exe = "vi"
75 local visual = os.getenv("VISUAL")
76 if (visual ~= nil) then exe = visual end
77 local editor = os.getenv("EDITOR")
78 if (editor ~= nil) then exe = editor end
79
80 local tmp, tname = temp_file()
81 if (tmp == nil) then return nil end
82 basetext = "MT: " .. string.gsub(basetext, "\n", "\nMT: ")
83 tmp:write(basetext)
84 io.close(tmp)
85
86 if (os.execute(string.format("%s %s", exe, tname)) ~= 0) then
87 os.remove(tname)
88 return nil
89 end
90
91 tmp = io.open(tname, "r")
92 if (tmp == nil) then os.remove(tname); return nil end
93 local res = ""
94 local line = tmp:read()
95 while(line ~= nil) do
96 if (not string.find(line, "^MT:")) then
97 res = res .. line .. "\n"
98 end
99 line = tmp:read()
100 end
101 io.close(tmp)
102 os.remove(tname)
103 return res
104end
105
106
107function non_blocking_rng_ok()
108 return true
109end
110
111
112function persist_phrase_ok()
113 return true
114end
115
116function get_mail_hostname(url)
117 return os.getenv("HOSTNAME")
118end
119
120function get_author(branchname)
121 local user = os.getenv("USER")
122 local host = os.getenv("HOSTNAME")
123 if ((user == nil) or (host == nil)) then return nil end
124 return string.format("%s@%s", user, host)
125end
126
127-- trust evaluation hooks
128
129function intersection(a,b)
130 local s={}
131 local t={}
132 for k,v in pairs(a) do s[v] = 1 end
133 for k,v in pairs(b) do if s[v] ~= nil then table.insert(t,v) end end
134 return t
135end
136
137function get_manifest_cert_trust(signers, id, name, val)
138 return true
139end
140
141function get_file_cert_trust(signers, id, name, val)
142 return true
143end
144
145function accept_testresult_change(old_results, new_results)
146 for test,res in pairs(old_results)
147 do
148 if res == true and new_results[test] ~= true
149 then
150 return false
151 end
152 end
153 return true
154end
155
156-- merger support
157
158function merge2_emacs_cmd(lfile, rfile, outfile)
159 local elisp = "'(ediff-merge-files \"%s\" \"%s\" nil \"%s\")'"
160 local cmd_fmt = "emacs -no-init-file -eval " .. elisp
161 return string.format(cmd_fmt, lfile, rfile, outfile)
162end
163
164function merge3_emacs_cmd(lfile, afile, rfile, outfile)
165 local elisp = "'(ediff-merge-files-with-ancestor \"%s\" \"%s\" \"%s\" nil \"%s\")'"
166 local cmd_fmt = "emacs -no-init-file -eval " .. elisp
167 return string.format(cmd_fmt, lfile, rfile, afile, outfile)
168end
169
170function merge2_xxdiff_cmd(lfile, rfile, outfile)
171 local cmd_fmt = "xxdiff %s %s "
172 local cmd_opts = " --title1 left --title2 right"
173 return string.format(cmd_fmt .. cmd_opts, lfile, rfile, outfile)
174end
175
176function merge3_xxdiff_cmd(lfile, afile, rfile, outfile)
177 local cmd_fmt = "xxdiff %s %s %s --merge --merged-filename %s "
178 local cmd_opts = " --title1 left --title2 ancestor --title3 right"
179 return string.format(cmd_fmt .. cmd_opts, lfile, afile, rfile, outfile)
180end
181
182function write_to_temporary_file(data)
183 tmp, filename = temp_file()
184 if (tmp == nil) then
185 return nil
186 end;
187 tmp:write(data)
188 io.close(tmp)
189 return filename
190end
191
192function read_contents_of_file(filename)
193 tmp = io.open(filename, "r")
194 if (tmp == nil) then
195 return nil
196 end
197 local data = tmp:read("*a")
198 io.close(tmp)
199 return data
200end
201
202function program_exists_in_path(program)
203 return os.execute(string.format("which %s", program)) == 0
204end
205
206function merge2(left, right)
207 local lfile = nil
208 local rfile = nil
209 local outfile = nil
210 local data = nil
211
212 lfile = write_to_temporary_file(left)
213 rfile = write_to_temporary_file(right)
214 outfile = write_to_temporary_file("")
215
216 if lfile ~= nil and
217 rfile ~= nil and
218 outfile ~= nil
219 then
220 local cmd = nil
221 if program_exists_in_path("xxdiff") then
222 cmd = merge2_xxdiff_cmd(lfile, rfile, outfile)
223 elseif program_exists_in_path("emacs") then
224 cmd = merge2_emacs_cmd(lfile, rfile, outfile)
225 end
226
227 if cmd ~= nil
228 then
229 io.write(string.format("executing external 2-way merge command: %s\n", cmd))
230 if os.execute(cmd) == 0
231 then
232 data = read_contents_of_file(outfile)
233 end
234 end
235 end
236
237 os.remove(lfile)
238 os.remove(rfile)
239 os.remove(outfile)
240
241 return data
242end
243
244function merge3(ancestor, left, right)
245 local afile = nil
246 local lfile = nil
247 local rfile = nil
248 local outfile = nil
249 local data = nil
250
251 lfile = write_to_temporary_file(left)
252 afile = write_to_temporary_file(ancestor)
253 rfile = write_to_temporary_file(right)
254 outfile = write_to_temporary_file("")
255
256 if lfile ~= nil and
257 rfile ~= nil and
258 afile ~= nil and
259 outfile ~= nil
260 then
261 local cmd = nil
262 if program_exists_in_path("xxdiff") then
263 cmd = merge3_xxdiff_cmd(lfile, afile, rfile, outfile)
264 elseif program_exists_in_path("emacs") then
265 cmd = merge3_emacs_cmd(lfile, afile, rfile, outfile)
266 end
267
268 if cmd ~= nil
269 then
270 io.write(string.format("executing external 3-way merge command: %s\n", cmd))
271 if os.execute(cmd) == 0
272 then
273 data = read_contents_of_file(outfile)
274 end
275 end
276 end
277
278 os.remove(lfile)
279 os.remove(rfile)
280 os.remove(afile)
281 os.remove(outfile)
282
283 return data
284end

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status