# HG changeset patch # User Dirkjan Ochtman # Date 2007-12-03 18:19:12 # Node ID 74f65f44a9aaea7bf98df77582afb17a0ae90433 # Parent d676d0f35bd8d43b0e9abac305b8228ff7b39cb5 hgwebdir: refactor inner loop diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py --- a/mercurial/hgweb/hgwebdir_mod.py +++ b/mercurial/hgweb/hgwebdir_mod.py @@ -73,49 +73,58 @@ class hgwebdir(object): try: try: + virtual = req.env.get("PATH_INFO", "").strip('/') - if virtual.startswith('static/'): + + # a static file + if virtual.startswith('static/') or 'static' in req.form: static = os.path.join(templater.templatepath(), 'static') - fname = virtual[7:] + if virtual.startswith('static/'): + fname = virtual[7:] + else: + fname = req.form['static'][0] req.write(staticfile(static, fname, req)) - elif virtual: - repos = dict(self.repos) - while virtual: - real = repos.get(virtual) - if real: - req.env['REPO_NAME'] = virtual - try: - repo = hg.repository(self.parentui, real) - hgweb(repo).run_wsgi(req) - return - except IOError, inst: - raise ErrorResponse(500, inst.strerror) - except hg.RepoError, inst: - raise ErrorResponse(500, str(inst)) + return + + # top-level index + elif not virtual: + tmpl = self.templater(req) + self.makeindex(req, tmpl) + return - # browse subdirectories - subdir = virtual + '/' - if [r for r in repos if r.startswith(subdir)]: - tmpl = self.templater(req) - self.makeindex(req, tmpl, subdir) + # nested indexes and hgwebs + repos = dict(self.repos) + while virtual: + real = repos.get(virtual) + if real: + req.env['REPO_NAME'] = virtual + try: + repo = hg.repository(self.parentui, real) + hgweb(repo).run_wsgi(req) return - - up = virtual.rfind('/') - if up < 0: - break - virtual = virtual[:up] + except IOError, inst: + raise ErrorResponse(500, inst.strerror) + except hg.RepoError, inst: + raise ErrorResponse(500, str(inst)) - tmpl = self.templater(req) - req.respond(404, tmpl("notfound", repo=virtual)) - else: - if req.form.has_key('static'): - static = os.path.join(templater.templatepath(), "static") - fname = req.form['static'][0] - req.write(staticfile(static, fname, req)) - else: + # browse subdirectories + subdir = virtual + '/' + if [r for r in repos if r.startswith(subdir)]: tmpl = self.templater(req) - self.makeindex(req, tmpl) + self.makeindex(req, tmpl, subdir) + return + + up = virtual.rfind('/') + if up < 0: + break + virtual = virtual[:up] + + # prefixes not found + tmpl = self.templater(req) + req.respond(404, tmpl("notfound", repo=virtual)) + except ErrorResponse, err: + tmpl = self.templater(req) req.respond(err.code, tmpl('error', error=err.message or '')) finally: tmpl = None