##// END OF EJS Templates
hgweb: Changed file page to list format syntax
Josef "Jeff" Sipek -
r976:5d5ab159 default
parent child Browse files
Show More
@@ -1,830 +1,830
1 1 # hgweb.py - web interface to a mercurial repository
2 2 #
3 3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
4 4 # Copyright 2005 Matt Mackall <mpm@selenic.com>
5 5 #
6 6 # This software may be used and distributed according to the terms
7 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 9 import os, cgi, time, re, difflib, socket, sys, zlib
10 10 from mercurial.hg import *
11 11 from mercurial.ui import *
12 12
13 13 def templatepath():
14 14 for f in "templates", "../templates":
15 15 p = os.path.join(os.path.dirname(__file__), f)
16 16 if os.path.isdir(p): return p
17 17
18 18 def age(t):
19 19 def plural(t, c):
20 20 if c == 1: return t
21 21 return t + "s"
22 22 def fmt(t, c):
23 23 return "%d %s" % (c, plural(t, c))
24 24
25 25 now = time.time()
26 26 delta = max(1, int(now - t))
27 27
28 28 scales = [["second", 1],
29 29 ["minute", 60],
30 30 ["hour", 3600],
31 31 ["day", 3600 * 24],
32 32 ["week", 3600 * 24 * 7],
33 33 ["month", 3600 * 24 * 30],
34 34 ["year", 3600 * 24 * 365]]
35 35
36 36 scales.reverse()
37 37
38 38 for t, s in scales:
39 39 n = delta / s
40 40 if n >= 2 or s == 1: return fmt(t, n)
41 41
42 42 def nl2br(text):
43 43 return text.replace('\n', '<br/>\n')
44 44
45 45 def obfuscate(text):
46 46 return ''.join([ '&#%d;' % ord(c) for c in text ])
47 47
48 48 def up(p):
49 49 if p[0] != "/": p = "/" + p
50 50 if p[-1] == "/": p = p[:-1]
51 51 up = os.path.dirname(p)
52 52 if up == "/":
53 53 return "/"
54 54 return up + "/"
55 55
56 56 def httphdr(type):
57 57 sys.stdout.write('Content-type: %s\n\n' % type)
58 58
59 59 def write(*things):
60 60 for thing in things:
61 61 if hasattr(thing, "__iter__"):
62 62 for part in thing:
63 63 write(part)
64 64 else:
65 65 sys.stdout.write(str(thing))
66 66
67 67 class templater:
68 68 def __init__(self, mapfile, filters = {}, defaults = {}):
69 69 self.cache = {}
70 70 self.map = {}
71 71 self.base = os.path.dirname(mapfile)
72 72 self.filters = filters
73 73 self.defaults = defaults
74 74
75 75 for l in file(mapfile):
76 76 m = re.match(r'(\S+)\s*=\s*"(.*)"$', l)
77 77 if m:
78 78 self.cache[m.group(1)] = m.group(2)
79 79 else:
80 80 m = re.match(r'(\S+)\s*=\s*(\S+)', l)
81 81 if m:
82 82 self.map[m.group(1)] = os.path.join(self.base, m.group(2))
83 83 else:
84 84 raise "unknown map entry '%s'" % l
85 85
86 86 def __call__(self, t, **map):
87 87 m = self.defaults.copy()
88 88 m.update(map)
89 89 try:
90 90 tmpl = self.cache[t]
91 91 except KeyError:
92 92 tmpl = self.cache[t] = file(self.map[t]).read()
93 93 return self.template(tmpl, self.filters, **m)
94 94
95 95 def template(self, tmpl, filters = {}, **map):
96 96 while tmpl:
97 97 m = re.search(r"#([a-zA-Z0-9]+)((%[a-zA-Z0-9]+)*)((\|[a-zA-Z0-9]+)*)#", tmpl)
98 98 if m:
99 99 yield tmpl[:m.start(0)]
100 100 v = map.get(m.group(1), "")
101 101 v = callable(v) and v(**map) or v
102 102
103 103 format = m.group(2)
104 104 fl = m.group(4)
105 105
106 106 if format:
107 107 q = v.__iter__
108 108 for i in q():
109 109 lm = map.copy()
110 110 lm.update(i)
111 111 yield self(format[1:], **lm)
112 112
113 113 v = ""
114 114
115 115 elif fl:
116 116 for f in fl.split("|")[1:]:
117 117 v = filters[f](v)
118 118
119 119 yield v
120 120 tmpl = tmpl[m.end(0):]
121 121 else:
122 122 yield tmpl
123 123 return
124 124
125 125 def rfc822date(x):
126 126 return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(x))
127 127
128 128 class hgweb:
129 129 maxchanges = 10
130 130 maxfiles = 10
131 131
132 132 def __init__(self, path, name, templates = ""):
133 133 self.templates = templates
134 134 self.reponame = name
135 135 self.path = path
136 136 self.mtime = -1
137 137 self.viewonly = 0
138 138
139 139 self.filters = {
140 140 "escape": cgi.escape,
141 141 "age": age,
142 142 "date": (lambda x: time.asctime(time.gmtime(x))),
143 143 "addbreaks": nl2br,
144 144 "obfuscate": obfuscate,
145 145 "short": (lambda x: x[:12]),
146 146 "firstline": (lambda x: x.splitlines(1)[0]),
147 147 "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"),
148 148 "rfc822date": rfc822date,
149 149 }
150 150
151 151 def refresh(self):
152 152 s = os.stat(os.path.join(self.path, ".hg", "00changelog.i"))
153 153 if s.st_mtime != self.mtime:
154 154 self.mtime = s.st_mtime
155 155 self.repo = repository(ui(), self.path)
156 156
157 157 def date(self, cs):
158 158 return time.asctime(time.gmtime(float(cs[2].split(' ')[0])))
159 159
160 160 def listfiles(self, files, mf):
161 161 for f in files[:self.maxfiles]:
162 162 yield self.t("filenodelink", node = hex(mf[f]), file = f)
163 163 if len(files) > self.maxfiles:
164 164 yield self.t("fileellipses")
165 165
166 166 def listfilediffs(self, files, changeset):
167 167 for f in files[:self.maxfiles]:
168 168 yield self.t("filedifflink", node = hex(changeset), file = f)
169 169 if len(files) > self.maxfiles:
170 170 yield self.t("fileellipses")
171 171
172 172 def parents(self, t1, nodes=[], rev=None,**args):
173 173 if not rev: rev = lambda x: ""
174 174 for node in nodes:
175 175 if node != nullid:
176 176 yield self.t(t1, node = hex(node), rev = rev(node), **args)
177 177
178 178 def showtag(self, t1, node=nullid, **args):
179 179 for t in self.repo.nodetags(node):
180 180 yield self.t(t1, tag = t, **args)
181 181
182 182 def diff(self, node1, node2, files):
183 183 def filterfiles(list, files):
184 184 l = [ x for x in list if x in files ]
185 185
186 186 for f in files:
187 187 if f[-1] != os.sep: f += os.sep
188 188 l += [ x for x in list if x.startswith(f) ]
189 189 return l
190 190
191 191 parity = [0]
192 192 def diffblock(diff, f, fn):
193 193 yield self.t("diffblock",
194 194 lines = prettyprintlines(diff),
195 195 parity = parity[0],
196 196 file = f,
197 197 filenode = hex(fn or nullid))
198 198 parity[0] = 1 - parity[0]
199 199
200 200 def prettyprintlines(diff):
201 201 for l in diff.splitlines(1):
202 202 if l.startswith('+'):
203 203 yield self.t("difflineplus", line = l)
204 204 elif l.startswith('-'):
205 205 yield self.t("difflineminus", line = l)
206 206 elif l.startswith('@'):
207 207 yield self.t("difflineat", line = l)
208 208 else:
209 209 yield self.t("diffline", line = l)
210 210
211 211 r = self.repo
212 212 cl = r.changelog
213 213 mf = r.manifest
214 214 change1 = cl.read(node1)
215 215 change2 = cl.read(node2)
216 216 mmap1 = mf.read(change1[0])
217 217 mmap2 = mf.read(change2[0])
218 218 date1 = self.date(change1)
219 219 date2 = self.date(change2)
220 220
221 221 c, a, d, u = r.changes(node1, node2)
222 222 if files:
223 223 c, a, d = map(lambda x: filterfiles(x, files), (c, a, d))
224 224
225 225 for f in c:
226 226 to = r.file(f).read(mmap1[f])
227 227 tn = r.file(f).read(mmap2[f])
228 228 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
229 229 for f in a:
230 230 to = None
231 231 tn = r.file(f).read(mmap2[f])
232 232 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
233 233 for f in d:
234 234 to = r.file(f).read(mmap1[f])
235 235 tn = None
236 236 yield diffblock(mdiff.unidiff(to, date1, tn, date2, f), f, tn)
237 237
238 238 def changelog(self, pos):
239 239 def changenav(**map):
240 240 def seq(factor = 1):
241 241 yield 1 * factor
242 242 yield 3 * factor
243 243 #yield 5 * factor
244 244 for f in seq(factor * 10):
245 245 yield f
246 246
247 247 l = []
248 248 for f in seq():
249 249 if f < self.maxchanges / 2: continue
250 250 if f > count: break
251 251 r = "%d" % f
252 252 if pos + f < count: l.append(("+" + r, pos + f))
253 253 if pos - f >= 0: l.insert(0, ("-" + r, pos - f))
254 254
255 255 yield {"rev": 0, "label": "(0)"}
256 256
257 257 for label, rev in l:
258 258 yield {"label": label, "rev": rev}
259 259
260 260 yield {"label": "tip", "rev": ""}
261 261
262 262 def changelist(**map):
263 263 parity = (start - end) & 1
264 264 cl = self.repo.changelog
265 265 l = [] # build a list in forward order for efficiency
266 266 for i in range(start, end):
267 267 n = cl.node(i)
268 268 changes = cl.read(n)
269 269 hn = hex(n)
270 270 t = float(changes[2].split(' ')[0])
271 271
272 272 l.insert(0, {
273 273 "parity": parity,
274 274 "author": changes[1],
275 275 "parent": self.parents("changelogparent",
276 276 cl.parents(n), cl.rev),
277 277 "changelogtag": self.showtag("changelogtag",n),
278 278 "manifest": hex(changes[0]),
279 279 "desc": changes[4],
280 280 "date": t,
281 281 "files": self.listfilediffs(changes[3], n),
282 282 "rev": i,
283 283 "node": hn})
284 284 parity = 1 - parity
285 285
286 286 for e in l: yield e
287 287
288 288 cl = self.repo.changelog
289 289 mf = cl.read(cl.tip())[0]
290 290 count = cl.count()
291 291 start = max(0, pos - self.maxchanges + 1)
292 292 end = min(count, start + self.maxchanges)
293 293 pos = end - 1
294 294
295 295 yield self.t('changelog',
296 296 changenav = changenav,
297 297 manifest = hex(mf),
298 298 rev = pos, changesets = count, entries = changelist)
299 299
300 300 def search(self, query):
301 301
302 302 def changelist(**map):
303 303 cl = self.repo.changelog
304 304 count = 0
305 305 qw = query.lower().split()
306 306
307 307 def revgen():
308 308 for i in range(cl.count() - 1, 0, -100):
309 309 l = []
310 310 for j in range(max(0, i - 100), i):
311 311 n = cl.node(j)
312 312 changes = cl.read(n)
313 313 l.insert(0, (n, j, changes))
314 314 for e in l:
315 315 yield e
316 316
317 317 for n, i, changes in revgen():
318 318 miss = 0
319 319 for q in qw:
320 320 if not (q in changes[1].lower() or
321 321 q in changes[4].lower() or
322 322 q in " ".join(changes[3][:20]).lower()):
323 323 miss = 1
324 324 break
325 325 if miss: continue
326 326
327 327 count += 1
328 328 hn = hex(n)
329 329 t = float(changes[2].split(' ')[0])
330 330
331 331 yield self.t(
332 332 'searchentry',
333 333 parity = count & 1,
334 334 author = changes[1],
335 335 parent = self.parents("changelogparent",
336 336 cl.parents(n), cl.rev),
337 337 changelogtag = self.showtag("changelogtag",n),
338 338 manifest = hex(changes[0]),
339 339 desc = changes[4],
340 340 date = t,
341 341 files = self.listfilediffs(changes[3], n),
342 342 rev = i,
343 343 node = hn)
344 344
345 345 if count >= self.maxchanges: break
346 346
347 347 cl = self.repo.changelog
348 348 mf = cl.read(cl.tip())[0]
349 349
350 350 yield self.t('search',
351 351 query = query,
352 352 manifest = hex(mf),
353 353 entries = changelist)
354 354
355 355 def changeset(self, nodeid):
356 356 n = bin(nodeid)
357 357 cl = self.repo.changelog
358 358 changes = cl.read(n)
359 359 p1 = cl.parents(n)[0]
360 360 t = float(changes[2].split(' ')[0])
361 361
362 362 files = []
363 363 mf = self.repo.manifest.read(changes[0])
364 364 for f in changes[3]:
365 365 files.append(self.t("filenodelink",
366 366 filenode = hex(mf.get(f, nullid)), file = f))
367 367
368 368 def diff(**map):
369 369 yield self.diff(p1, n, None)
370 370
371 371 yield self.t('changeset',
372 372 diff = diff,
373 373 rev = cl.rev(n),
374 374 node = nodeid,
375 375 parent = self.parents("changesetparent",
376 376 cl.parents(n), cl.rev),
377 377 changesettag = self.showtag("changesettag",n),
378 378 manifest = hex(changes[0]),
379 379 author = changes[1],
380 380 desc = changes[4],
381 381 date = t,
382 382 files = files)
383 383
384 384 def filelog(self, f, filenode):
385 385 cl = self.repo.changelog
386 386 fl = self.repo.file(f)
387 387 count = fl.count()
388 388
389 389 def entries(**map):
390 390 l = []
391 391 parity = (count - 1) & 1
392 392
393 393 for i in range(count):
394 394
395 395 n = fl.node(i)
396 396 lr = fl.linkrev(n)
397 397 cn = cl.node(lr)
398 398 cs = cl.read(cl.node(lr))
399 399 t = float(cs[2].split(' ')[0])
400 400
401 401 l.insert(0, self.t("filelogentry",
402 402 parity = parity,
403 403 filenode = hex(n),
404 404 filerev = i,
405 405 file = f,
406 406 node = hex(cn),
407 407 author = cs[1],
408 408 date = t,
409 409 parent = self.parents("filelogparent",
410 410 fl.parents(n), fl.rev, file=f),
411 411 desc = cs[4]))
412 412 parity = 1 - parity
413 413
414 414 yield l
415 415
416 416 yield self.t("filelog",
417 417 file = f,
418 418 filenode = filenode,
419 419 entries = entries)
420 420
421 421 def filerevision(self, f, node):
422 422 fl = self.repo.file(f)
423 423 n = bin(node)
424 424 text = fl.read(n)
425 425 changerev = fl.linkrev(n)
426 426 cl = self.repo.changelog
427 427 cn = cl.node(changerev)
428 428 cs = cl.read(cn)
429 429 t = float(cs[2].split(' ')[0])
430 430 mfn = cs[0]
431 431
432 432 def lines():
433 433 for l, t in enumerate(text.splitlines(1)):
434 yield self.t("fileline", line = t,
435 linenumber = "% 6d" % (l + 1),
436 parity = l & 1)
434 yield {"line": t,
435 "linenumber": "% 6d" % (l + 1),
436 "parity": l & 1}
437 437
438 438 yield self.t("filerevision", file = f,
439 439 filenode = node,
440 440 path = up(f),
441 441 text = lines(),
442 442 rev = changerev,
443 443 node = hex(cn),
444 444 manifest = hex(mfn),
445 445 author = cs[1],
446 446 date = t,
447 447 parent = self.parents("filerevparent",
448 448 fl.parents(n), fl.rev, file=f),
449 449 permissions = self.repo.manifest.readflags(mfn)[f])
450 450
451 451 def fileannotate(self, f, node):
452 452 bcache = {}
453 453 ncache = {}
454 454 fl = self.repo.file(f)
455 455 n = bin(node)
456 456 changerev = fl.linkrev(n)
457 457
458 458 cl = self.repo.changelog
459 459 cn = cl.node(changerev)
460 460 cs = cl.read(cn)
461 461 t = float(cs[2].split(' ')[0])
462 462 mfn = cs[0]
463 463
464 464 def annotate(**map):
465 465 parity = 1
466 466 last = None
467 467 for r, l in fl.annotate(n):
468 468 try:
469 469 cnode = ncache[r]
470 470 except KeyError:
471 471 cnode = ncache[r] = self.repo.changelog.node(r)
472 472
473 473 try:
474 474 name = bcache[r]
475 475 except KeyError:
476 476 cl = self.repo.changelog.read(cnode)
477 477 name = cl[1]
478 478 f = name.find('@')
479 479 if f >= 0:
480 480 name = name[:f]
481 481 f = name.find('<')
482 482 if f >= 0:
483 483 name = name[f+1:]
484 484 bcache[r] = name
485 485
486 486 if last != cnode:
487 487 parity = 1 - parity
488 488 last = cnode
489 489
490 490 yield self.t("annotateline",
491 491 parity = parity,
492 492 node = hex(cnode),
493 493 rev = r,
494 494 author = name,
495 495 file = f,
496 496 line = l)
497 497
498 498 yield self.t("fileannotate",
499 499 file = f,
500 500 filenode = node,
501 501 annotate = annotate,
502 502 path = up(f),
503 503 rev = changerev,
504 504 node = hex(cn),
505 505 manifest = hex(mfn),
506 506 author = cs[1],
507 507 date = t,
508 508 parent = self.parents("fileannotateparent",
509 509 fl.parents(n), fl.rev, file=f),
510 510 permissions = self.repo.manifest.readflags(mfn)[f])
511 511
512 512 def manifest(self, mnode, path):
513 513 mf = self.repo.manifest.read(bin(mnode))
514 514 rev = self.repo.manifest.rev(bin(mnode))
515 515 node = self.repo.changelog.node(rev)
516 516 mff=self.repo.manifest.readflags(bin(mnode))
517 517
518 518 files = {}
519 519
520 520 p = path[1:]
521 521 l = len(p)
522 522
523 523 for f,n in mf.items():
524 524 if f[:l] != p:
525 525 continue
526 526 remain = f[l:]
527 527 if "/" in remain:
528 528 short = remain[:remain.find("/") + 1] # bleah
529 529 files[short] = (f, None)
530 530 else:
531 531 short = os.path.basename(remain)
532 532 files[short] = (f, n)
533 533
534 534 def filelist(**map):
535 535 parity = 0
536 536 fl = files.keys()
537 537 fl.sort()
538 538 for f in fl:
539 539 full, fnode = files[f]
540 540 if fnode:
541 541 yield self.t("manifestfileentry",
542 542 file = full,
543 543 manifest = mnode,
544 544 filenode = hex(fnode),
545 545 parity = parity,
546 546 basename = f,
547 547 permissions = mff[full])
548 548 else:
549 549 yield self.t("manifestdirentry",
550 550 parity = parity,
551 551 path = os.path.join(path, f),
552 552 manifest = mnode, basename = f[:-1])
553 553 parity = 1 - parity
554 554
555 555 yield self.t("manifest",
556 556 manifest = mnode,
557 557 rev = rev,
558 558 node = hex(node),
559 559 path = path,
560 560 up = up(path),
561 561 entries = filelist)
562 562
563 563 def tags(self):
564 564 cl = self.repo.changelog
565 565 mf = cl.read(cl.tip())[0]
566 566
567 567 i = self.repo.tagslist()
568 568 i.reverse()
569 569
570 570 def entries(**map):
571 571 parity = 0
572 572 for k,n in i:
573 573 yield {"parity": parity,
574 574 "tag": k,
575 575 "node": hex(n)}
576 576 parity = 1 - parity
577 577
578 578 yield self.t("tags",
579 579 manifest = hex(mf),
580 580 entries = entries)
581 581
582 582 def filediff(self, file, changeset):
583 583 n = bin(changeset)
584 584 cl = self.repo.changelog
585 585 p1 = cl.parents(n)[0]
586 586 cs = cl.read(n)
587 587 mf = self.repo.manifest.read(cs[0])
588 588
589 589 def diff(**map):
590 590 yield self.diff(p1, n, file)
591 591
592 592 yield self.t("filediff",
593 593 file = file,
594 594 filenode = hex(mf.get(file, nullid)),
595 595 node = changeset,
596 596 rev = self.repo.changelog.rev(n),
597 597 parent = self.parents("filediffparent",
598 598 cl.parents(n), cl.rev),
599 599 diff = diff)
600 600
601 601 # add tags to things
602 602 # tags -> list of changesets corresponding to tags
603 603 # find tag, changeset, file
604 604
605 605 def run(self):
606 606 def header(**map):
607 607 yield self.t("header", **map)
608 608
609 609 def footer(**map):
610 610 yield self.t("footer", **map)
611 611
612 612 self.refresh()
613 613 args = cgi.parse()
614 614
615 615 t = self.templates or self.repo.ui.config("web", "templates",
616 616 templatepath())
617 617 m = os.path.join(t, "map")
618 618 if args.has_key('style'):
619 619 b = os.path.basename("map-" + args['style'][0])
620 620 p = os.path.join(self.templates, b)
621 621 if os.path.isfile(p): m = p
622 622
623 623 port = os.environ["SERVER_PORT"]
624 624 port = port != "80" and (":" + port) or ""
625 625 uri = os.environ["REQUEST_URI"]
626 626 if "?" in uri: uri = uri.split("?")[0]
627 627 url = "http://%s%s%s" % (os.environ["SERVER_NAME"], port, uri)
628 628
629 629 name = self.reponame or self.repo.ui.config("web", "name", os.getcwd())
630 630
631 631 self.t = templater(m, self.filters,
632 632 {"url":url,
633 633 "repo":name,
634 634 "header":header,
635 635 "footer":footer,
636 636 })
637 637
638 638 if not args.has_key('cmd'):
639 639 args['cmd'] = [self.t.cache['default'],]
640 640
641 641 if args['cmd'][0] == 'changelog':
642 642 c = self.repo.changelog.count() - 1
643 643 hi = c
644 644 if args.has_key('rev'):
645 645 hi = args['rev'][0]
646 646 try:
647 647 hi = self.repo.changelog.rev(self.repo.lookup(hi))
648 648 except RepoError:
649 649 write(self.search(hi))
650 650 return
651 651
652 652 write(self.changelog(hi))
653 653
654 654 elif args['cmd'][0] == 'changeset':
655 655 write(self.changeset(args['node'][0]))
656 656
657 657 elif args['cmd'][0] == 'manifest':
658 658 write(self.manifest(args['manifest'][0], args['path'][0]))
659 659
660 660 elif args['cmd'][0] == 'tags':
661 661 write(self.tags())
662 662
663 663 elif args['cmd'][0] == 'filediff':
664 664 write(self.filediff(args['file'][0], args['node'][0]))
665 665
666 666 elif args['cmd'][0] == 'file':
667 667 write(self.filerevision(args['file'][0], args['filenode'][0]))
668 668
669 669 elif args['cmd'][0] == 'annotate':
670 670 write(self.fileannotate(args['file'][0], args['filenode'][0]))
671 671
672 672 elif args['cmd'][0] == 'filelog':
673 673 write(self.filelog(args['file'][0], args['filenode'][0]))
674 674
675 675 elif args['cmd'][0] == 'heads':
676 676 httphdr("application/mercurial-0.1")
677 677 h = self.repo.heads()
678 678 sys.stdout.write(" ".join(map(hex, h)) + "\n")
679 679
680 680 elif args['cmd'][0] == 'branches':
681 681 httphdr("application/mercurial-0.1")
682 682 nodes = []
683 683 if args.has_key('nodes'):
684 684 nodes = map(bin, args['nodes'][0].split(" "))
685 685 for b in self.repo.branches(nodes):
686 686 sys.stdout.write(" ".join(map(hex, b)) + "\n")
687 687
688 688 elif args['cmd'][0] == 'between':
689 689 httphdr("application/mercurial-0.1")
690 690 nodes = []
691 691 if args.has_key('pairs'):
692 692 pairs = [ map(bin, p.split("-"))
693 693 for p in args['pairs'][0].split(" ") ]
694 694 for b in self.repo.between(pairs):
695 695 sys.stdout.write(" ".join(map(hex, b)) + "\n")
696 696
697 697 elif args['cmd'][0] == 'changegroup':
698 698 httphdr("application/mercurial-0.1")
699 699 nodes = []
700 700 if self.viewonly:
701 701 return
702 702
703 703 if args.has_key('roots'):
704 704 nodes = map(bin, args['roots'][0].split(" "))
705 705
706 706 z = zlib.compressobj()
707 707 f = self.repo.changegroup(nodes)
708 708 while 1:
709 709 chunk = f.read(4096)
710 710 if not chunk: break
711 711 sys.stdout.write(z.compress(chunk))
712 712
713 713 sys.stdout.write(z.flush())
714 714
715 715 else:
716 716 write(self.t("error"))
717 717
718 718 def create_server(path, name, templates, address, port, use_ipv6 = False,
719 719 accesslog = sys.stdout, errorlog = sys.stderr):
720 720
721 721 def openlog(opt, default):
722 722 if opt and opt != '-':
723 723 return open(opt, 'w')
724 724 return default
725 725
726 726 u = ui()
727 727 repo = repository(u, path)
728 728 if not address:
729 729 address = u.config("web", "address", "")
730 730 if not port:
731 731 print port
732 732 port = int(u.config("web", "port", 8000))
733 733 if not use_ipv6:
734 734 use_ipv6 = u.configbool("web", "ipv6")
735 735
736 736 accesslog = openlog(accesslog or u.config("web", "accesslog", "-"),
737 737 sys.stdout)
738 738 errorlog = openlog(errorlog or u.config("web", "errorlog", "-"),
739 739 sys.stderr)
740 740
741 741 import BaseHTTPServer
742 742
743 743 class IPv6HTTPServer(BaseHTTPServer.HTTPServer):
744 744 address_family = getattr(socket, 'AF_INET6', None)
745 745
746 746 def __init__(self, *args, **kwargs):
747 747 if self.address_family is None:
748 748 raise RepoError('IPv6 not available on this system')
749 749 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
750 750
751 751 class hgwebhandler(BaseHTTPServer.BaseHTTPRequestHandler):
752 752 def log_error(self, format, *args):
753 753 errorlog.write("%s - - [%s] %s\n" % (self.address_string(),
754 754 self.log_date_time_string(),
755 755 format % args))
756 756
757 757 def log_message(self, format, *args):
758 758 accesslog.write("%s - - [%s] %s\n" % (self.address_string(),
759 759 self.log_date_time_string(),
760 760 format % args))
761 761
762 762 def do_POST(self):
763 763 try:
764 764 self.do_hgweb()
765 765 except socket.error, inst:
766 766 if inst.args[0] != 32: raise
767 767
768 768 def do_GET(self):
769 769 self.do_POST()
770 770
771 771 def do_hgweb(self):
772 772 query = ""
773 773 p = self.path.find("?")
774 774 if p:
775 775 query = self.path[p + 1:]
776 776 query = query.replace('+', ' ')
777 777
778 778 env = {}
779 779 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
780 780 env['REQUEST_METHOD'] = self.command
781 781 env['SERVER_NAME'] = self.server.server_name
782 782 env['SERVER_PORT'] = str(self.server.server_port)
783 783 env['REQUEST_URI'] = "/"
784 784 if query:
785 785 env['QUERY_STRING'] = query
786 786 host = self.address_string()
787 787 if host != self.client_address[0]:
788 788 env['REMOTE_HOST'] = host
789 789 env['REMOTE_ADDR'] = self.client_address[0]
790 790
791 791 if self.headers.typeheader is None:
792 792 env['CONTENT_TYPE'] = self.headers.type
793 793 else:
794 794 env['CONTENT_TYPE'] = self.headers.typeheader
795 795 length = self.headers.getheader('content-length')
796 796 if length:
797 797 env['CONTENT_LENGTH'] = length
798 798 accept = []
799 799 for line in self.headers.getallmatchingheaders('accept'):
800 800 if line[:1] in "\t\n\r ":
801 801 accept.append(line.strip())
802 802 else:
803 803 accept = accept + line[7:].split(',')
804 804 env['HTTP_ACCEPT'] = ','.join(accept)
805 805
806 806 os.environ.update(env)
807 807
808 808 save = sys.argv, sys.stdin, sys.stdout, sys.stderr
809 809 try:
810 810 sys.stdin = self.rfile
811 811 sys.stdout = self.wfile
812 812 sys.argv = ["hgweb.py"]
813 813 if '=' not in query:
814 814 sys.argv.append(query)
815 815 self.send_response(200, "Script output follows")
816 816 hg.run()
817 817 finally:
818 818 sys.argv, sys.stdin, sys.stdout, sys.stderr = save
819 819
820 820 hg = hgweb(path, name, templates)
821 821 if use_ipv6:
822 822 return IPv6HTTPServer((address, port), hgwebhandler)
823 823 else:
824 824 return BaseHTTPServer.HTTPServer((address, port), hgwebhandler)
825 825
826 826 def server(path, name, templates, address, port, use_ipv6 = False,
827 827 accesslog = sys.stdout, errorlog = sys.stderr):
828 828 httpd = create_server(path, name, templates, address, port, use_ipv6,
829 829 accesslog, errorlog)
830 830 httpd.serve_forever()
@@ -1,41 +1,41
1 1 #header#
2 2 <title>#repo|escape#:#file#</title>
3 3 </head>
4 4 <body>
5 5
6 6 <div class="buttons">
7 7 <a href="?cmd=changelog;rev=#rev#">changelog</a>
8 8 <a href="?cmd=tags">tags</a>
9 9 <a href="?cmd=changeset;node=#node#">changeset</a>
10 10 <a href="?cmd=manifest;manifest=#manifest#;path=#path#">manifest</a>
11 11 <a href="?cmd=filelog;file=#file#;filenode=#filenode#">revisions</a>
12 12 <a href="?cmd=annotate;file=#file#;filenode=#filenode#">annotate</a>
13 13 <a href="?cmd=file;file=#file#;filenode=#filenode#;style=raw">raw</a>
14 14 </div>
15 15
16 16 <h2>#file# (revision #filenode|short#)</h2>
17 17
18 18 <table>
19 19 <tr>
20 20 <td class="metatag">changeset #rev#:</td>
21 21 <td><a href="?cmd=changeset;node=#node#">#node|short#</a></td></tr>
22 22 #parent#
23 23 <tr>
24 24 <td class="metatag">manifest:</td>
25 25 <td><a href="?cmd=manifest;manifest=#manifest#;path=/">#manifest|short#</a></td></tr>
26 26 <tr>
27 27 <td class="metatag">author:</td>
28 28 <td>#author|obfuscate#</td></tr>
29 29 <tr>
30 30 <td class="metatag">date:</td>
31 31 <td>#date|date# (#date|age# ago)</td></tr>
32 32 <tr>
33 33 <td class="metatag">permissions:</td>
34 34 <td>#permissions|permissions#</td></tr>
35 35 </table>
36 36
37 37 <pre>
38 #text#
38 #text%fileline#
39 39 </pre>
40 40
41 41 #footer#
General Comments 0
You need to be logged in to leave comments. Login now