Mercurial > hg-stable
annotate mercurial/hgweb.py @ 182:089594a5bbde
Pull from Jake's hg docs
author | mpm@selenic.com |
---|---|
date | Fri, 27 May 2005 13:09:56 -0800 |
parents | f25944662097 f25944662097 |
children | 767916673e16 |
rev | line source |
---|---|
131 | 1 #!/usr/bin/env python |
2 # | |
132 | 3 # hgweb.py - 0.2 - 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
131 | 4 # - web interface to a mercurial repository |
5 # | |
6 # This software may be used and distributed according to the terms | |
7 # of the GNU General Public License, incorporated herein by reference. | |
8 | |
9 # useful for debugging | |
10 import cgitb | |
11 cgitb.enable() | |
12 | |
13 import os, cgi, time, re, difflib, sys, zlib | |
138 | 14 from mercurial.hg import * |
15 | |
157
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
16 def templatepath(): |
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
17 for f in "templates/map", "../templates/map": |
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
18 p = os.path.join(os.path.dirname(__file__), f) |
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
19 if os.path.isfile(p): return p |
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
20 |
138 | 21 def age(t): |
22 def plural(t, c): | |
23 if c == 1: return t | |
24 return t + "s" | |
25 def fmt(t, c): | |
26 return "%d %s" % (c, plural(t, c)) | |
27 | |
28 now = time.time() | |
29 delta = max(1, int(now - t)) | |
30 | |
31 scales = [["second", 1], | |
32 ["minute", 60], | |
33 ["hour", 3600], | |
34 ["day", 3600 * 24], | |
35 ["week", 3600 * 24 * 7], | |
36 ["month", 3600 * 24 * 30], | |
37 ["year", 3600 * 24 * 365]] | |
38 | |
39 scales.reverse() | |
40 | |
41 for t, s in scales: | |
42 n = delta / s | |
43 if n >= 1: return fmt(t, n) | |
131 | 44 |
45 def nl2br(text): | |
138 | 46 return text.replace('\n', '<br/>') |
131 | 47 |
48 def obfuscate(text): | |
138 | 49 return ''.join([ '&#%d' % ord(c) for c in text ]) |
50 | |
51 def up(p): | |
52 if p[0] != "/": p = "/" + p | |
53 if p[-1] == "/": p = p[:-1] | |
54 up = os.path.dirname(p) | |
55 if up == "/": | |
56 return "/" | |
57 return up + "/" | |
131 | 58 |
59 def httphdr(type): | |
60 print 'Content-type: %s\n' % type | |
61 | |
135 | 62 def write(*things): |
63 for thing in things: | |
64 if hasattr(thing, "__iter__"): | |
65 for part in thing: | |
66 write(part) | |
67 else: | |
68 sys.stdout.write(str(thing)) | |
69 | |
138 | 70 def template(tmpl, **map): |
71 while tmpl: | |
72 m = re.search(r"#([a-zA-Z0-9]+)#", tmpl) | |
73 if m: | |
74 yield tmpl[:m.start(0)] | |
75 v = map.get(m.group(1), "") | |
76 yield callable(v) and v() or v | |
77 tmpl = tmpl[m.end(0):] | |
78 else: | |
79 yield tmpl | |
80 return | |
81 | |
82 class templater: | |
83 def __init__(self, mapfile): | |
84 self.cache = {} | |
85 self.map = {} | |
86 self.base = os.path.dirname(mapfile) | |
87 | |
88 for l in file(mapfile): | |
89 m = re.match(r'(\S+)\s*=\s*"(.*)"$', l) | |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
90 if m: |
138 | 91 self.cache[m.group(1)] = m.group(2) |
92 else: | |
93 m = re.match(r'(\S+)\s*=\s*(\S+)', l) | |
94 if m: | |
95 self.map[m.group(1)] = os.path.join(self.base, m.group(2)) | |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
96 else: |
138 | 97 raise "unknown map entry '%s'" % l |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
98 |
138 | 99 def __call__(self, t, **map): |
100 try: | |
101 tmpl = self.cache[t] | |
102 except KeyError: | |
103 tmpl = self.cache[t] = file(self.map[t]).read() | |
104 return template(tmpl, **map) | |
105 | |
106 class hgweb: | |
107 maxchanges = 20 | |
108 maxfiles = 10 | |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
109 |
157
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
110 def __init__(self, path, name, templatemap = ""): |
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
111 templatemap = templatemap or templatepath() |
2653740d8118
Install the templates where they can be found by hgweb.py
mpm@selenic.com
parents:
156
diff
changeset
|
112 |
138 | 113 self.reponame = name |
114 self.repo = repository(ui(), path) | |
115 self.t = templater(templatemap) | |
131 | 116 |
138 | 117 def date(self, cs): |
118 return time.asctime(time.gmtime(float(cs[2].split(' ')[0]))) | |
119 | |
120 def listfiles(self, files, mf): | |
121 for f in files[:self.maxfiles]: | |
122 yield self.t("filenodelink", node = hex(mf[f]), file = f) | |
123 if len(files) > self.maxfiles: | |
124 yield self.t("fileellipses") | |
125 | |
126 def listfilediffs(self, files, changeset): | |
127 for f in files[:self.maxfiles]: | |
128 yield self.t("filedifflink", node = hex(changeset), file = f) | |
129 if len(files) > self.maxfiles: | |
130 yield self.t("fileellipses") | |
131 | |
156 | 132 def parent(self, t1, node=nullid, rev=-1, **args): |
142 | 133 if node != hex(nullid): |
156 | 134 yield self.t(t1, node = node, rev = rev, **args) |
142 | 135 |
138 | 136 def diff(self, node1, node2, files): |
137 def filterfiles(list, files): | |
138 l = [ x for x in list if x in files ] | |
139 | |
140 for f in files: | |
141 if f[-1] != os.sep: f += os.sep | |
142 l += [ x for x in list if x.startswith(f) ] | |
143 return l | |
131 | 144 |
172
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
145 parity = [0] |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
146 def diffblock(diff, f, fn): |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
147 yield self.t("diffblock", |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
148 lines = prettyprintlines(diff), |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
149 parity = parity[0], |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
150 file = f, |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
151 filenode = hex(fn)) |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
152 parity[0] = 1 - parity[0] |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
153 |
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
154 def prettyprintlines(diff): |
138 | 155 for l in diff.splitlines(1): |
156 line = cgi.escape(l) | |
157 if line.startswith('+'): | |
158 yield self.t("difflineplus", line = line) | |
159 elif line.startswith('-'): | |
160 yield self.t("difflineminus", line = line) | |
161 elif line.startswith('@'): | |
162 yield self.t("difflineat", line = line) | |
163 else: | |
164 yield self.t("diffline", line = line) | |
131 | 165 |
138 | 166 r = self.repo |
167 cl = r.changelog | |
168 mf = r.manifest | |
169 change1 = cl.read(node1) | |
170 change2 = cl.read(node2) | |
171 mmap1 = mf.read(change1[0]) | |
172 mmap2 = mf.read(change2[0]) | |
173 date1 = self.date(change1) | |
174 date2 = self.date(change2) | |
131 | 175 |
138 | 176 c, a, d = r.diffrevs(node1, node2) |
177 c, a, d = map(lambda x: filterfiles(x, files), (c, a, d)) | |
131 | 178 |
138 | 179 for f in c: |
180 to = r.file(f).read(mmap1[f]) | |
181 tn = r.file(f).read(mmap2[f]) | |
172
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
182 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn) |
138 | 183 for f in a: |
184 to = "" | |
185 tn = r.file(f).read(mmap2[f]) | |
172
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
186 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn) |
138 | 187 for f in d: |
188 to = r.file(f).read(mmap1[f]) | |
189 tn = "" | |
172
e9b1147db448
hgweb: alternating colors for multifile diffs
mpm@selenic.com
parents:
168
diff
changeset
|
190 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn) |
131 | 191 |
142 | 192 def header(self): |
193 yield self.t("header", repo = self.reponame) | |
194 | |
195 def footer(self): | |
196 yield self.t("footer", repo = self.reponame) | |
197 | |
180 | 198 def changelog(self, pos): |
138 | 199 def changenav(): |
200 def seq(factor = 1): | |
201 yield 1 * factor | |
173
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
202 yield 3 * factor |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
203 #yield 5 * factor |
138 | 204 for f in seq(factor * 10): |
205 yield f | |
131 | 206 |
173
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
207 l = [] |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
208 for f in seq(): |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
209 if f < self.maxchanges / 2: continue |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
210 if f > count: break |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
211 r = "%d" % f |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
212 if pos + f < count - (f/2): l.append(("+" + r, pos + f)) |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
213 if pos - f >= 0 + (f/2): l.insert(0, ("-" + r, pos - f)) |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
214 |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
215 yield self.t("naventry", rev = 0, label="(0)") |
138 | 216 |
173
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
217 for label, rev in l: |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
218 yield self.t("naventry", label = label, rev = rev) |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
219 |
8da1df932c16
hgweb: make navigation of changesets a bit nicer
mpm@selenic.com
parents:
172
diff
changeset
|
220 yield self.t("naventry", rev = count - 1, label="tip") |
131 | 221 |
138 | 222 def changelist(): |
142 | 223 parity = (start - end) & 1 |
138 | 224 cl = self.repo.changelog |
225 l = [] # build a list in forward order for efficiency | |
226 for i in range(start, end + 1): | |
227 n = cl.node(i) | |
228 changes = cl.read(n) | |
229 hn = hex(n) | |
230 p1, p2 = cl.parents(n) | |
231 t = float(changes[2].split(' ')[0]) | |
131 | 232 |
138 | 233 l.insert(0, self.t( |
234 'changelogentry', | |
142 | 235 parity = parity, |
138 | 236 author = obfuscate(changes[1]), |
237 shortdesc = cgi.escape(changes[4].splitlines()[0]), | |
238 age = age(t), | |
142 | 239 parent1 = self.parent("changelogparent", |
240 hex(p1), cl.rev(p1)), | |
241 parent2 = self.parent("changelogparent", | |
242 hex(p2), cl.rev(p2)), | |
138 | 243 p1 = hex(p1), p2 = hex(p2), |
244 p1rev = cl.rev(p1), p2rev = cl.rev(p2), | |
245 manifest = hex(changes[0]), | |
246 desc = nl2br(cgi.escape(changes[4])), | |
247 date = time.asctime(time.gmtime(t)), | |
248 files = self.listfilediffs(changes[3], n), | |
249 rev = i, | |
250 node = hn)) | |
142 | 251 parity = 1 - parity |
138 | 252 |
253 yield l | |
131 | 254 |
168 | 255 cl = self.repo.changelog |
256 mf = cl.read(cl.tip())[0] | |
257 count = cl.count() | |
138 | 258 end = min(pos, count - 1) |
259 start = max(0, pos - self.maxchanges) | |
260 end = min(count - 1, start + self.maxchanges) | |
261 | |
142 | 262 yield self.t('changelog', |
263 header = self.header(), | |
264 footer = self.footer(), | |
265 repo = self.reponame, | |
266 changenav = changenav, | |
168 | 267 manifest = hex(mf), |
142 | 268 rev = pos, changesets = count, entries = changelist) |
131 | 269 |
138 | 270 def changeset(self, nodeid): |
271 n = bin(nodeid) | |
272 cl = self.repo.changelog | |
273 changes = cl.read(n) | |
274 p1, p2 = cl.parents(n) | |
275 p1rev, p2rev = cl.rev(p1), cl.rev(p2) | |
276 t = float(changes[2].split(' ')[0]) | |
277 | |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
278 files = [] |
138 | 279 mf = self.repo.manifest.read(changes[0]) |
131 | 280 for f in changes[3]: |
138 | 281 files.append(self.t("filenodelink", |
282 filenode = hex(mf[f]), file = f)) | |
283 | |
284 def diff(): | |
285 yield self.diff(p1, n, changes[3]) | |
131 | 286 |
138 | 287 yield self.t('changeset', |
142 | 288 header = self.header(), |
289 footer = self.footer(), | |
290 repo = self.reponame, | |
138 | 291 diff = diff, |
292 rev = cl.rev(n), | |
293 node = nodeid, | |
294 shortdesc = cgi.escape(changes[4].splitlines()[0]), | |
142 | 295 parent1 = self.parent("changesetparent", |
296 hex(p1), cl.rev(p1)), | |
297 parent2 = self.parent("changesetparent", | |
298 hex(p2), cl.rev(p2)), | |
138 | 299 p1 = hex(p1), p2 = hex(p2), |
300 p1rev = cl.rev(p1), p2rev = cl.rev(p2), | |
301 manifest = hex(changes[0]), | |
302 author = obfuscate(changes[1]), | |
303 desc = nl2br(cgi.escape(changes[4])), | |
304 date = time.asctime(time.gmtime(t)), | |
305 files = files) | |
131 | 306 |
138 | 307 def filelog(self, f, filenode): |
308 cl = self.repo.changelog | |
309 fl = self.repo.file(f) | |
310 count = fl.count() | |
311 | |
312 def entries(): | |
313 l = [] | |
142 | 314 parity = (count - 1) & 1 |
315 | |
138 | 316 for i in range(count): |
317 | |
318 n = fl.node(i) | |
319 lr = fl.linkrev(n) | |
320 cn = cl.node(lr) | |
321 cs = cl.read(cl.node(lr)) | |
322 p1, p2 = fl.parents(n) | |
323 t = float(cs[2].split(' ')[0]) | |
133
fb84d3e71042
added template support for some hgweb output, also, template files for
jake@edge2.net
parents:
132
diff
changeset
|
324 |
138 | 325 l.insert(0, self.t("filelogentry", |
142 | 326 parity = parity, |
138 | 327 filenode = hex(n), |
328 filerev = i, | |
329 file = f, | |
330 node = hex(cn), | |
331 author = obfuscate(cs[1]), | |
332 age = age(t), | |
333 date = time.asctime(time.gmtime(t)), | |
334 shortdesc = cgi.escape(cs[4].splitlines()[0]), | |
335 p1 = hex(p1), p2 = hex(p2), | |
336 p1rev = fl.rev(p1), p2rev = fl.rev(p2))) | |
142 | 337 parity = 1 - parity |
138 | 338 |
339 yield l | |
340 | |
341 yield self.t("filelog", | |
142 | 342 header = self.header(), |
343 footer = self.footer(), | |
344 repo = self.reponame, | |
138 | 345 file = f, |
346 filenode = filenode, | |
347 entries = entries) | |
131 | 348 |
138 | 349 def filerevision(self, f, node): |
350 fl = self.repo.file(f) | |
351 n = bin(node) | |
352 text = cgi.escape(fl.read(n)) | |
353 changerev = fl.linkrev(n) | |
354 cl = self.repo.changelog | |
355 cn = cl.node(changerev) | |
356 cs = cl.read(cn) | |
357 p1, p2 = fl.parents(n) | |
358 t = float(cs[2].split(' ')[0]) | |
359 mfn = cs[0] | |
142 | 360 |
361 def lines(): | |
362 for l, t in enumerate(text.splitlines(1)): | |
363 yield self.t("fileline", | |
364 line = t, | |
365 linenumber = "% 6d" % (l + 1), | |
366 parity = l & 1) | |
138 | 367 |
368 yield self.t("filerevision", file = f, | |
142 | 369 header = self.header(), |
370 footer = self.footer(), | |
371 repo = self.reponame, | |
138 | 372 filenode = node, |
373 path = up(f), | |
142 | 374 text = lines(), |
138 | 375 rev = changerev, |
376 node = hex(cn), | |
377 manifest = hex(mfn), | |
378 author = obfuscate(cs[1]), | |
379 age = age(t), | |
380 date = time.asctime(time.gmtime(t)), | |
381 shortdesc = cgi.escape(cs[4].splitlines()[0]), | |
142 | 382 parent1 = self.parent("filerevparent", |
156 | 383 hex(p1), fl.rev(p1), file=f), |
142 | 384 parent2 = self.parent("filerevparent", |
156 | 385 hex(p2), fl.rev(p2), file=f), |
138 | 386 p1 = hex(p1), p2 = hex(p2), |
387 p1rev = fl.rev(p1), p2rev = fl.rev(p2)) | |
388 | |
131 | 389 |
138 | 390 def fileannotate(self, f, node): |
391 bcache = {} | |
392 ncache = {} | |
393 fl = self.repo.file(f) | |
394 n = bin(node) | |
395 changerev = fl.linkrev(n) | |
396 | |
397 cl = self.repo.changelog | |
398 cn = cl.node(changerev) | |
399 cs = cl.read(cn) | |
400 p1, p2 = fl.parents(n) | |
401 t = float(cs[2].split(' ')[0]) | |
402 mfn = cs[0] | |
131 | 403 |
138 | 404 def annotate(): |
142 | 405 parity = 1 |
406 last = None | |
138 | 407 for r, l in fl.annotate(n): |
408 try: | |
409 cnode = ncache[r] | |
410 except KeyError: | |
411 cnode = ncache[r] = self.repo.changelog.node(r) | |
412 | |
413 try: | |
414 name = bcache[r] | |
415 except KeyError: | |
416 cl = self.repo.changelog.read(cnode) | |
417 name = cl[1] | |
418 f = name.find('@') | |
419 if f >= 0: | |
420 name = name[:f] | |
421 bcache[r] = name | |
131 | 422 |
142 | 423 if last != cnode: |
424 parity = 1 - parity | |
425 last = cnode | |
426 | |
138 | 427 yield self.t("annotateline", |
142 | 428 parity = parity, |
138 | 429 node = hex(cnode), |
430 rev = r, | |
431 author = name, | |
432 file = f, | |
433 line = cgi.escape(l)) | |
434 | |
435 yield self.t("fileannotate", | |
142 | 436 header = self.header(), |
437 footer = self.footer(), | |
438 repo = self.reponame, | |
138 | 439 file = f, |
440 filenode = node, | |
441 annotate = annotate, | |
442 path = up(f), | |
443 rev = changerev, | |
444 node = hex(cn), | |
445 manifest = hex(mfn), | |
446 author = obfuscate(cs[1]), | |
447 age = age(t), | |
448 date = time.asctime(time.gmtime(t)), | |
449 shortdesc = cgi.escape(cs[4].splitlines()[0]), | |
156 | 450 parent1 = self.parent("fileannotateparent", |
451 hex(p1), fl.rev(p1), file=f), | |
452 parent2 = self.parent("fileannotateparent", | |
453 hex(p2), fl.rev(p2), file=f), | |
138 | 454 p1 = hex(p1), p2 = hex(p2), |
455 p1rev = fl.rev(p1), p2rev = fl.rev(p2)) | |
136 | 456 |
138 | 457 def manifest(self, mnode, path): |
458 mf = self.repo.manifest.read(bin(mnode)) | |
459 rev = self.repo.manifest.rev(bin(mnode)) | |
460 node = self.repo.changelog.node(rev) | |
461 | |
462 files = {} | |
142 | 463 |
138 | 464 p = path[1:] |
465 l = len(p) | |
131 | 466 |
138 | 467 for f,n in mf.items(): |
468 if f[:l] != p: | |
469 continue | |
470 remain = f[l:] | |
471 if "/" in remain: | |
472 short = remain[:remain.find("/") + 1] # bleah | |
142 | 473 files[short] = (f, None) |
138 | 474 else: |
475 short = os.path.basename(remain) | |
476 files[short] = (f, n) | |
131 | 477 |
138 | 478 def filelist(): |
142 | 479 parity = 0 |
138 | 480 fl = files.keys() |
481 fl.sort() | |
482 for f in fl: | |
483 full, fnode = files[f] | |
142 | 484 if fnode: |
485 yield self.t("manifestfileentry", | |
486 file = full, | |
487 manifest = mnode, | |
488 filenode = hex(fnode), | |
489 parity = parity, | |
490 basename = f) | |
491 else: | |
492 yield self.t("manifestdirentry", | |
493 parity = parity, | |
494 path = os.path.join(path, f), | |
495 manifest = mnode, basename = f[:-1]) | |
496 parity = 1 - parity | |
138 | 497 |
498 yield self.t("manifest", | |
142 | 499 header = self.header(), |
500 footer = self.footer(), | |
501 repo = self.reponame, | |
138 | 502 manifest = mnode, |
503 rev = rev, | |
504 node = hex(node), | |
505 path = path, | |
506 up = up(path), | |
142 | 507 entries = filelist) |
131 | 508 |
168 | 509 def tags(self): |
510 cl = self.repo.changelog | |
511 mf = cl.read(cl.tip())[0] | |
512 | |
513 self.repo.lookup(0) # prime the cache | |
514 i = self.repo.tags.items() | |
515 i.sort() | |
516 | |
517 def entries(): | |
518 parity = 0 | |
519 for k,n in i: | |
520 yield self.t("tagentry", | |
521 parity = parity, | |
522 tag = k, | |
523 node = hex(n)) | |
524 parity = 1 - parity | |
525 | |
526 yield self.t("tags", | |
527 header = self.header(), | |
528 footer = self.footer(), | |
529 repo = self.reponame, | |
530 manifest = hex(mf), | |
531 entries = entries) | |
532 | |
138 | 533 def filediff(self, file, changeset): |
534 n = bin(changeset) | |
535 cl = self.repo.changelog | |
536 p1 = cl.parents(n)[0] | |
537 cs = cl.read(n) | |
538 mf = self.repo.manifest.read(cs[0]) | |
539 | |
540 def diff(): | |
541 yield self.diff(p1, n, file) | |
131 | 542 |
138 | 543 yield self.t("filediff", |
142 | 544 header = self.header(), |
545 footer = self.footer(), | |
546 repo = self.reponame, | |
138 | 547 file = file, |
548 filenode = hex(mf[file]), | |
549 node = changeset, | |
550 rev = self.repo.changelog.rev(n), | |
551 p1 = hex(p1), | |
552 p1rev = self.repo.changelog.rev(p1), | |
553 diff = diff) | |
554 | |
555 # add tags to things | |
556 # tags -> list of changesets corresponding to tags | |
557 # find tag, changeset, file | |
131 | 558 |
132 | 559 def run(self): |
560 args = cgi.parse() | |
561 | |
138 | 562 if not args.has_key('cmd') or args['cmd'][0] == 'changelog': |
563 hi = self.repo.changelog.count() | |
153
e8a360cd5a9f
changed pos to rev for changelog cmd, changed & to ;
jake@edge2.net
parents:
142
diff
changeset
|
564 if args.has_key('rev'): |
165 | 565 hi = args['rev'][0] |
166
39624c47060f
hgweb: don't blow up on search for unknown keys
mpm@selenic.com
parents:
165
diff
changeset
|
566 try: |
39624c47060f
hgweb: don't blow up on search for unknown keys
mpm@selenic.com
parents:
165
diff
changeset
|
567 hi = self.repo.changelog.rev(self.repo.lookup(hi)) |
39624c47060f
hgweb: don't blow up on search for unknown keys
mpm@selenic.com
parents:
165
diff
changeset
|
568 except KeyError: |
39624c47060f
hgweb: don't blow up on search for unknown keys
mpm@selenic.com
parents:
165
diff
changeset
|
569 hi = self.repo.changelog.count() |
131 | 570 |
138 | 571 write(self.changelog(hi)) |
132 | 572 |
138 | 573 elif args['cmd'][0] == 'changeset': |
574 write(self.changeset(args['node'][0])) | |
575 | |
576 elif args['cmd'][0] == 'manifest': | |
577 write(self.manifest(args['manifest'][0], args['path'][0])) | |
578 | |
168 | 579 elif args['cmd'][0] == 'tags': |
580 write(self.tags()) | |
581 | |
138 | 582 elif args['cmd'][0] == 'filediff': |
583 write(self.filediff(args['file'][0], args['node'][0])) | |
131 | 584 |
132 | 585 elif args['cmd'][0] == 'file': |
138 | 586 write(self.filerevision(args['file'][0], args['filenode'][0])) |
131 | 587 |
138 | 588 elif args['cmd'][0] == 'annotate': |
589 write(self.fileannotate(args['file'][0], args['filenode'][0])) | |
131 | 590 |
138 | 591 elif args['cmd'][0] == 'filelog': |
592 write(self.filelog(args['file'][0], args['filenode'][0])) | |
136 | 593 |
132 | 594 elif args['cmd'][0] == 'branches': |
595 httphdr("text/plain") | |
596 nodes = [] | |
597 if args.has_key('nodes'): | |
138 | 598 nodes = map(bin, args['nodes'][0].split(" ")) |
599 for b in self.repo.branches(nodes): | |
600 sys.stdout.write(" ".join(map(hex, b)) + "\n") | |
131 | 601 |
132 | 602 elif args['cmd'][0] == 'between': |
603 httphdr("text/plain") | |
604 nodes = [] | |
605 if args.has_key('pairs'): | |
138 | 606 pairs = [ map(bin, p.split("-")) |
132 | 607 for p in args['pairs'][0].split(" ") ] |
138 | 608 for b in self.repo.between(pairs): |
609 sys.stdout.write(" ".join(map(hex, b)) + "\n") | |
132 | 610 |
611 elif args['cmd'][0] == 'changegroup': | |
612 httphdr("application/hg-changegroup") | |
613 nodes = [] | |
614 if args.has_key('roots'): | |
138 | 615 nodes = map(bin, args['roots'][0].split(" ")) |
131 | 616 |
132 | 617 z = zlib.compressobj() |
138 | 618 for chunk in self.repo.changegroup(nodes): |
132 | 619 sys.stdout.write(z.compress(chunk)) |
620 | |
621 sys.stdout.write(z.flush()) | |
131 | 622 |
132 | 623 else: |
138 | 624 write(self.t("error")) |
131 | 625 |
158
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
626 def server(path, name, templates, address, port): |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
627 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
628 import BaseHTTPServer |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
629 import sys, os |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
630 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
631 class hgwebhandler(BaseHTTPServer.BaseHTTPRequestHandler): |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
632 def do_POST(self): |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
633 self.do_hgweb() |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
634 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
635 def do_GET(self): |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
636 self.do_hgweb() |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
637 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
638 def do_hgweb(self): |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
639 query = "" |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
640 p = self.path.find("?") |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
641 if p: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
642 query = self.path[p + 1:] |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
643 query = query.replace('+', ' ') |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
644 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
645 env = {} |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
646 env['GATEWAY_INTERFACE'] = 'CGI/1.1' |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
647 env['REQUEST_METHOD'] = self.command |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
648 if query: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
649 env['QUERY_STRING'] = query |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
650 host = self.address_string() |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
651 if host != self.client_address[0]: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
652 env['REMOTE_HOST'] = host |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
653 env['REMOTE_ADDR'] = self.client_address[0] |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
654 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
655 if self.headers.typeheader is None: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
656 env['CONTENT_TYPE'] = self.headers.type |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
657 else: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
658 env['CONTENT_TYPE'] = self.headers.typeheader |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
659 length = self.headers.getheader('content-length') |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
660 if length: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
661 env['CONTENT_LENGTH'] = length |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
662 accept = [] |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
663 for line in self.headers.getallmatchingheaders('accept'): |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
664 if line[:1] in "\t\n\r ": |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
665 accept.append(line.strip()) |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
666 else: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
667 accept = accept + line[7:].split(',') |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
668 env['HTTP_ACCEPT'] = ','.join(accept) |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
669 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
670 os.environ.update(env) |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
671 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
672 save = sys.argv, sys.stdin, sys.stdout, sys.stderr |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
673 try: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
674 sys.stdin = self.rfile |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
675 sys.stdout = self.wfile |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
676 sys.argv = ["hgweb.py"] |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
677 if '=' not in query: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
678 sys.argv.append(query) |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
679 self.send_response(200, "Script output follows") |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
680 hg.run() |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
681 finally: |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
682 sys.argv, sys.stdin, sys.stdout, sys.stderr = save |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
683 |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
684 hg = hgweb(path, name, templates) |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
685 httpd = BaseHTTPServer.HTTPServer((address, port), hgwebhandler) |
be7103467d2e
Add 'hg serve' command for stand-alone server
mpm@selenic.com
parents:
157
diff
changeset
|
686 httpd.serve_forever() |