Mercurial > public > mercurial-scm > hg
comparison mercurial/hgweb/hgweb_mod.py @ 6392:2540521dc7c1
hgweb: separate out utility functions
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Fri, 28 Mar 2008 19:37:28 +0100 |
parents | a1007f7b9b7b |
children | 894875eae49b |
comparison
equal
deleted
inserted
replaced
6391:a1007f7b9b7b | 6392:2540521dc7c1 |
---|---|
13 from mercurial import revlog, templater, templatefilters, changegroup | 13 from mercurial import revlog, templater, templatefilters, changegroup |
14 from common import get_mtime, style_map, paritygen, countgen, get_contact | 14 from common import get_mtime, style_map, paritygen, countgen, get_contact |
15 from common import ErrorResponse | 15 from common import ErrorResponse |
16 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR | 16 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR |
17 from request import wsgirequest | 17 from request import wsgirequest |
18 import webcommands, protocol | 18 import webcommands, protocol, webutil |
19 | 19 |
20 shortcuts = { | 20 shortcuts = { |
21 'cl': [('cmd', ['changelog']), ('rev', None)], | 21 'cl': [('cmd', ['changelog']), ('rev', None)], |
22 'sl': [('cmd', ['shortlog']), ('rev', None)], | 22 'sl': [('cmd', ['shortlog']), ('rev', None)], |
23 'cs': [('cmd', ['changeset']), ('node', None)], | 23 'cs': [('cmd', ['changeset']), ('node', None)], |
337 def listfilediffs(self, tmpl, files, changeset): | 337 def listfilediffs(self, tmpl, files, changeset): |
338 for f in files[:self.maxfiles]: | 338 for f in files[:self.maxfiles]: |
339 yield tmpl("filedifflink", node=hex(changeset), file=f) | 339 yield tmpl("filedifflink", node=hex(changeset), file=f) |
340 if len(files) > self.maxfiles: | 340 if len(files) > self.maxfiles: |
341 yield tmpl("fileellipses") | 341 yield tmpl("fileellipses") |
342 | |
343 def siblings(self, siblings=[], hiderev=None, **args): | |
344 siblings = [s for s in siblings if s.node() != nullid] | |
345 if len(siblings) == 1 and siblings[0].rev() == hiderev: | |
346 return | |
347 for s in siblings: | |
348 d = {'node': hex(s.node()), 'rev': s.rev()} | |
349 if hasattr(s, 'path'): | |
350 d['file'] = s.path() | |
351 d.update(args) | |
352 yield d | |
353 | |
354 def renamelink(self, fl, node): | |
355 r = fl.renamed(node) | |
356 if r: | |
357 return [dict(file=r[0], node=hex(r[1]))] | |
358 return [] | |
359 | |
360 def nodetagsdict(self, node): | |
361 return [{"name": i} for i in self.repo.nodetags(node)] | |
362 | |
363 def nodebranchdict(self, ctx): | |
364 branches = [] | |
365 branch = ctx.branch() | |
366 # If this is an empty repo, ctx.node() == nullid, | |
367 # ctx.branch() == 'default', but branchtags() is | |
368 # an empty dict. Using dict.get avoids a traceback. | |
369 if self.repo.branchtags().get(branch) == ctx.node(): | |
370 branches.append({"name": branch}) | |
371 return branches | |
372 | |
373 def nodeinbranch(self, ctx): | |
374 branches = [] | |
375 branch = ctx.branch() | |
376 if branch != 'default' and self.repo.branchtags().get(branch) != ctx.node(): | |
377 branches.append({"name": branch}) | |
378 return branches | |
379 | |
380 def nodebranchnodefault(self, ctx): | |
381 branches = [] | |
382 branch = ctx.branch() | |
383 if branch != 'default': | |
384 branches.append({"name": branch}) | |
385 return branches | |
386 | |
387 def showtag(self, tmpl, t1, node=nullid, **args): | |
388 for t in self.repo.nodetags(node): | |
389 yield tmpl(t1, tag=t, **args) | |
390 | 342 |
391 def diff(self, tmpl, node1, node2, files): | 343 def diff(self, tmpl, node1, node2, files): |
392 def filterfiles(filters, files): | 344 def filterfiles(filters, files): |
393 l = [x for x in files if x in filters] | 345 l = [x for x in files if x in filters] |
394 | 346 |
460 cl = self.repo.changelog | 412 cl = self.repo.changelog |
461 l = [] # build a list in forward order for efficiency | 413 l = [] # build a list in forward order for efficiency |
462 for i in xrange(start, end): | 414 for i in xrange(start, end): |
463 ctx = self.repo.changectx(i) | 415 ctx = self.repo.changectx(i) |
464 n = ctx.node() | 416 n = ctx.node() |
465 showtags = self.showtag(tmpl, 'changelogtag', n) | 417 showtags = webutil.showtag(self.repo, tmpl, 'changelogtag', n) |
466 | 418 |
467 l.insert(0, {"parity": parity.next(), | 419 l.insert(0, {"parity": parity.next(), |
468 "author": ctx.user(), | 420 "author": ctx.user(), |
469 "parent": self.siblings(ctx.parents(), i - 1), | 421 "parent": webutil.siblings(ctx.parents(), i - 1), |
470 "child": self.siblings(ctx.children(), i + 1), | 422 "child": webutil.siblings(ctx.children(), i + 1), |
471 "changelogtag": showtags, | 423 "changelogtag": showtags, |
472 "desc": ctx.description(), | 424 "desc": ctx.description(), |
473 "date": ctx.date(), | 425 "date": ctx.date(), |
474 "files": self.listfilediffs(tmpl, ctx.files(), n), | 426 "files": self.listfilediffs(tmpl, ctx.files(), n), |
475 "rev": i, | 427 "rev": i, |
476 "node": hex(n), | 428 "node": hex(n), |
477 "tags": self.nodetagsdict(n), | 429 "tags": webutil.nodetagsdict(self.repo, n), |
478 "inbranch": self.nodeinbranch(ctx), | 430 "inbranch": webutil.nodeinbranch(self.repo, ctx), |
479 "branches": self.nodebranchdict(ctx)}) | 431 "branches": webutil.nodebranchdict(self.repo, ctx) |
432 }) | |
480 | 433 |
481 if limit > 0: | 434 if limit > 0: |
482 l = l[:limit] | 435 l = l[:limit] |
483 | 436 |
484 for e in l: | 437 for e in l: |
531 if miss: | 484 if miss: |
532 continue | 485 continue |
533 | 486 |
534 count += 1 | 487 count += 1 |
535 n = ctx.node() | 488 n = ctx.node() |
536 showtags = self.showtag(tmpl, 'changelogtag', n) | 489 showtags = webutil.showtag(self.repo, tmpl, 'changelogtag', n) |
537 | 490 |
538 yield tmpl('searchentry', | 491 yield tmpl('searchentry', |
539 parity=parity.next(), | 492 parity=parity.next(), |
540 author=ctx.user(), | 493 author=ctx.user(), |
541 parent=self.siblings(ctx.parents()), | 494 parent=webutil.siblings(ctx.parents()), |
542 child=self.siblings(ctx.children()), | 495 child=webutil.siblings(ctx.children()), |
543 changelogtag=showtags, | 496 changelogtag=showtags, |
544 desc=ctx.description(), | 497 desc=ctx.description(), |
545 date=ctx.date(), | 498 date=ctx.date(), |
546 files=self.listfilediffs(tmpl, ctx.files(), n), | 499 files=self.listfilediffs(tmpl, ctx.files(), n), |
547 rev=ctx.rev(), | 500 rev=ctx.rev(), |
548 node=hex(n), | 501 node=hex(n), |
549 tags=self.nodetagsdict(n), | 502 tags=webutil.nodetagsdict(self.repo, n), |
550 inbranch=self.nodeinbranch(ctx), | 503 inbranch=webutil.nodeinbranch(self.repo, ctx), |
551 branches=self.nodebranchdict(ctx)) | 504 branches=webutil.nodebranchdict(self.repo, ctx)) |
552 | 505 |
553 if count >= self.maxchanges: | 506 if count >= self.maxchanges: |
554 break | 507 break |
555 | 508 |
556 cl = self.repo.changelog | 509 cl = self.repo.changelog |
562 entries=changelist, | 515 entries=changelist, |
563 archives=self.archivelist("tip")) | 516 archives=self.archivelist("tip")) |
564 | 517 |
565 def changeset(self, tmpl, ctx): | 518 def changeset(self, tmpl, ctx): |
566 n = ctx.node() | 519 n = ctx.node() |
567 showtags = self.showtag(tmpl, 'changesettag', n) | 520 showtags = webutil.showtag(self.repo, tmpl, 'changesettag', n) |
568 parents = ctx.parents() | 521 parents = ctx.parents() |
569 p1 = parents[0].node() | 522 p1 = parents[0].node() |
570 | 523 |
571 files = [] | 524 files = [] |
572 parity = paritygen(self.stripecount) | 525 parity = paritygen(self.stripecount) |
580 | 533 |
581 return tmpl('changeset', | 534 return tmpl('changeset', |
582 diff=diff, | 535 diff=diff, |
583 rev=ctx.rev(), | 536 rev=ctx.rev(), |
584 node=hex(n), | 537 node=hex(n), |
585 parent=self.siblings(parents), | 538 parent=webutil.siblings(parents), |
586 child=self.siblings(ctx.children()), | 539 child=webutil.siblings(ctx.children()), |
587 changesettag=showtags, | 540 changesettag=showtags, |
588 author=ctx.user(), | 541 author=ctx.user(), |
589 desc=ctx.description(), | 542 desc=ctx.description(), |
590 date=ctx.date(), | 543 date=ctx.date(), |
591 files=files, | 544 files=files, |
592 archives=self.archivelist(hex(n)), | 545 archives=self.archivelist(hex(n)), |
593 tags=self.nodetagsdict(n), | 546 tags=webutil.nodetagsdict(self.repo, n), |
594 branch=self.nodebranchnodefault(ctx), | 547 branch=webutil.nodebranchnodefault(ctx), |
595 inbranch=self.nodeinbranch(ctx), | 548 inbranch=webutil.nodeinbranch(self.repo, ctx), |
596 branches=self.nodebranchdict(ctx)) | 549 branches=webutil.nodebranchdict(self.repo, ctx)) |
597 | 550 |
598 def filelog(self, tmpl, fctx): | 551 def filelog(self, tmpl, fctx): |
599 f = fctx.path() | 552 f = fctx.path() |
600 fl = fctx.filelog() | 553 fl = fctx.filelog() |
601 count = fl.count() | 554 count = fl.count() |
617 "filerev": i, | 570 "filerev": i, |
618 "file": f, | 571 "file": f, |
619 "node": hex(ctx.node()), | 572 "node": hex(ctx.node()), |
620 "author": ctx.user(), | 573 "author": ctx.user(), |
621 "date": ctx.date(), | 574 "date": ctx.date(), |
622 "rename": self.renamelink(fl, n), | 575 "rename": webutil.renamelink(fl, n), |
623 "parent": self.siblings(fctx.parents()), | 576 "parent": webutil.siblings(fctx.parents()), |
624 "child": self.siblings(fctx.children()), | 577 "child": webutil.siblings(fctx.children()), |
625 "desc": ctx.description()}) | 578 "desc": ctx.description()}) |
626 | 579 |
627 if limit > 0: | 580 if limit > 0: |
628 l = l[:limit] | 581 l = l[:limit] |
629 | 582 |
661 rev=fctx.rev(), | 614 rev=fctx.rev(), |
662 node=hex(fctx.node()), | 615 node=hex(fctx.node()), |
663 author=fctx.user(), | 616 author=fctx.user(), |
664 date=fctx.date(), | 617 date=fctx.date(), |
665 desc=fctx.description(), | 618 desc=fctx.description(), |
666 branch=self.nodebranchnodefault(fctx), | 619 branch=webutil.nodebranchnodefault(fctx), |
667 parent=self.siblings(fctx.parents()), | 620 parent=webutil.siblings(fctx.parents()), |
668 child=self.siblings(fctx.children()), | 621 child=webutil.siblings(fctx.children()), |
669 rename=self.renamelink(fl, n), | 622 rename=webutil.renamelink(fl, n), |
670 permissions=fctx.manifest().flags(f)) | 623 permissions=fctx.manifest().flags(f)) |
671 | 624 |
672 def fileannotate(self, tmpl, fctx): | 625 def fileannotate(self, tmpl, fctx): |
673 f = fctx.path() | 626 f = fctx.path() |
674 n = fctx.filenode() | 627 n = fctx.filenode() |
708 rev=fctx.rev(), | 661 rev=fctx.rev(), |
709 node=hex(fctx.node()), | 662 node=hex(fctx.node()), |
710 author=fctx.user(), | 663 author=fctx.user(), |
711 date=fctx.date(), | 664 date=fctx.date(), |
712 desc=fctx.description(), | 665 desc=fctx.description(), |
713 rename=self.renamelink(fl, n), | 666 rename=webutil.renamelink(fl, n), |
714 branch=self.nodebranchnodefault(fctx), | 667 branch=webutil.nodebranchnodefault(fctx), |
715 parent=self.siblings(fctx.parents()), | 668 parent=webutil.siblings(fctx.parents()), |
716 child=self.siblings(fctx.children()), | 669 child=webutil.siblings(fctx.children()), |
717 permissions=fctx.manifest().flags(f)) | 670 permissions=fctx.manifest().flags(f)) |
718 | 671 |
719 def manifest(self, tmpl, ctx, path): | 672 def manifest(self, tmpl, ctx, path): |
720 mf = ctx.manifest() | 673 mf = ctx.manifest() |
721 node = ctx.node() | 674 node = ctx.node() |
777 up=_up(abspath), | 730 up=_up(abspath), |
778 upparity=parity.next(), | 731 upparity=parity.next(), |
779 fentries=filelist, | 732 fentries=filelist, |
780 dentries=dirlist, | 733 dentries=dirlist, |
781 archives=self.archivelist(hex(node)), | 734 archives=self.archivelist(hex(node)), |
782 tags=self.nodetagsdict(node), | 735 tags=webutil.nodetagsdict(self.repo, node), |
783 inbranch=self.nodeinbranch(ctx), | 736 inbranch=webutil.nodeinbranch(self.repo, ctx), |
784 branches=self.nodebranchdict(ctx)) | 737 branches=webutil.nodebranchdict(self.repo, ctx)) |
785 | 738 |
786 def tags(self, tmpl): | 739 def tags(self, tmpl): |
787 i = self.repo.tagslist() | 740 i = self.repo.tagslist() |
788 i.reverse() | 741 i.reverse() |
789 parity = paritygen(self.stripecount) | 742 parity = paritygen(self.stripecount) |
858 author=ctx.user(), | 811 author=ctx.user(), |
859 desc=ctx.description(), | 812 desc=ctx.description(), |
860 date=ctx.date(), | 813 date=ctx.date(), |
861 rev=i, | 814 rev=i, |
862 node=hn, | 815 node=hn, |
863 tags=self.nodetagsdict(n), | 816 tags=webutil.nodetagsdict(self.repo, n), |
864 inbranch=self.nodeinbranch(ctx), | 817 inbranch=webutil.nodeinbranch(self.repo, ctx), |
865 branches=self.nodebranchdict(ctx))) | 818 branches=webutil.nodebranchdict(self.repo, ctx))) |
866 | 819 |
867 yield l | 820 yield l |
868 | 821 |
869 cl = self.repo.changelog | 822 cl = self.repo.changelog |
870 count = cl.count() | 823 count = cl.count() |
892 | 845 |
893 return tmpl("filediff", | 846 return tmpl("filediff", |
894 file=path, | 847 file=path, |
895 node=hex(n), | 848 node=hex(n), |
896 rev=fctx.rev(), | 849 rev=fctx.rev(), |
897 branch=self.nodebranchnodefault(fctx), | 850 branch=webutil.nodebranchnodefault(fctx), |
898 parent=self.siblings(parents), | 851 parent=webutil.siblings(parents), |
899 child=self.siblings(fctx.children()), | 852 child=webutil.siblings(fctx.children()), |
900 diff=diff) | 853 diff=diff) |
901 | 854 |
902 archive_specs = { | 855 archive_specs = { |
903 'bz2': ('application/x-tar', 'tbz2', '.tar.bz2', None), | 856 'bz2': ('application/x-tar', 'tbz2', '.tar.bz2', None), |
904 'gz': ('application/x-tar', 'tgz', '.tar.gz', None), | 857 'gz': ('application/x-tar', 'tgz', '.tar.gz', None), |
922 headers.append(('Content-Encoding', encoding)) | 875 headers.append(('Content-Encoding', encoding)) |
923 req.header(headers) | 876 req.header(headers) |
924 req.respond(HTTP_OK) | 877 req.respond(HTTP_OK) |
925 archival.archive(self.repo, req, cnode, artype, prefix=name) | 878 archival.archive(self.repo, req, cnode, artype, prefix=name) |
926 | 879 |
927 # add tags to things | |
928 # tags -> list of changesets corresponding to tags | |
929 # find tag, changeset, file | |
930 | |
931 def cleanpath(self, path): | |
932 path = path.lstrip('/') | |
933 return util.canonpath(self.repo.root, '', path) | |
934 | |
935 def changectx(self, req): | |
936 if 'node' in req.form: | |
937 changeid = req.form['node'][0] | |
938 elif 'manifest' in req.form: | |
939 changeid = req.form['manifest'][0] | |
940 else: | |
941 changeid = self.repo.changelog.count() - 1 | |
942 | |
943 try: | |
944 ctx = self.repo.changectx(changeid) | |
945 except RepoError: | |
946 man = self.repo.manifest | |
947 mn = man.lookup(changeid) | |
948 ctx = self.repo.changectx(man.linkrev(mn)) | |
949 | |
950 return ctx | |
951 | |
952 def filectx(self, req): | |
953 path = self.cleanpath(req.form['file'][0]) | |
954 if 'node' in req.form: | |
955 changeid = req.form['node'][0] | |
956 else: | |
957 changeid = req.form['filenode'][0] | |
958 try: | |
959 ctx = self.repo.changectx(changeid) | |
960 fctx = ctx.filectx(path) | |
961 except RepoError: | |
962 fctx = self.repo.filectx(path, fileid=changeid) | |
963 | |
964 return fctx | |
965 | |
966 def check_perm(self, req, op, default): | 880 def check_perm(self, req, op, default): |
967 '''check permission for operation based on user auth. | 881 '''check permission for operation based on user auth. |
968 return true if op allowed, else false. | 882 return true if op allowed, else false. |
969 default is policy to use if no config given.''' | 883 default is policy to use if no config given.''' |
970 | 884 |