changeset 8529:a767998f0a78

hgweb: make hgwebdir handle dict/list paths the same as config paths Before this patch, the only way to get hgwebdir to honor the recursive paths was to create a config object or a config file with the recursive paths in it. This patch makes hgwebdir treat paths the same whether passed in as a list, tuple, config or however hgwebdir supports passing paths.
author Jeremy Whitlock <jcscoobyrs@gmail.com>
date Wed, 20 May 2009 16:04:37 +0200
parents 4ddffb793d18
children 03196ac9a8b9
files mercurial/hgweb/hgwebdir_mod.py tests/test-hgwebdir-paths.py
diffstat 2 files changed, 71 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/hgwebdir_mod.py	Wed May 20 02:08:53 2009 +0200
+++ b/mercurial/hgweb/hgwebdir_mod.py	Wed May 20 16:04:37 2009 +0200
@@ -19,6 +19,28 @@
 def cleannames(items):
     return [(util.pconvert(name).strip('/'), path) for name, path in items]
 
+def findrepos(paths):
+    repos = {}
+    for prefix, root in cleannames(paths):
+        roothead, roottail = os.path.split(root)
+        # "foo = /bar/*" makes every subrepo of /bar/ to be
+        # mounted as foo/subrepo
+        # and "foo = /bar/**" also recurses into the subdirectories,
+        # remember to use it without working dir.
+        try:
+            recurse = {'*': False, '**': True}[roottail]
+        except KeyError:
+            repos[prefix] = root
+            continue
+        roothead = os.path.normpath(roothead)
+        for path in util.walkrepos(roothead, followsym=True, recurse=recurse):
+            path = os.path.normpath(path)
+            name = util.pconvert(path[len(roothead):]).strip('/')
+            if prefix:
+                name = prefix + '/' + name
+            repos[name] = path
+    return repos.items()
+
 class hgwebdir(object):
     refreshinterval = 20
 
@@ -39,14 +61,6 @@
             self.ui.setconfig('ui', 'report_untrusted', 'off')
             self.ui.setconfig('ui', 'interactive', 'off')
 
-        if isinstance(self.conf, (list, tuple)):
-            self.repos = cleannames(conf)
-        elif isinstance(self.conf, dict):
-            self.repos = sorted(cleannames(self.conf.items()))
-        else:
-            self.ui.readconfig(self.conf, remap={'paths': 'hgweb-paths'}, trust=True)
-            self.repos = []
-
         self.motd = self.ui.config('web', 'motd')
         self.style = self.ui.config('web', 'style', 'paper')
         self.stripecount = self.ui.config('web', 'stripes', 1)
@@ -54,29 +68,16 @@
             self.stripecount = int(self.stripecount)
         self._baseurl = self.ui.config('web', 'baseurl')
 
-        if self.repos:
-            return
+        if not isinstance(self.conf, (dict, list, tuple)):
+            map = {'paths': 'hgweb-paths'}
+            self.ui.readconfig(self.conf, remap=map, trust=True)
+            paths = self.ui.configitems('hgweb-paths')
+        elif isinstance(self.conf, (list, tuple)):
+            paths = self.conf
+        elif isinstance(self.conf, dict):
+            paths = self.conf.items()
 
-        for prefix, root in cleannames(self.ui.configitems('hgweb-paths')):
-            roothead, roottail = os.path.split(root)
-            # "foo = /bar/*" makes every subrepo of /bar/ to be
-            # mounted as foo/subrepo
-            # and "foo = /bar/**" also recurses into the subdirectories,
-            # remember to use it without working dir.
-            try:
-                recurse = {'*': False, '**': True}[roottail]
-            except KeyError:
-                self.repos.append((prefix, root))
-                continue
-            roothead = os.path.normpath(roothead)
-            for path in util.walkrepos(roothead, followsym=True,
-                                       recurse=recurse):
-                path = os.path.normpath(path)
-                name = util.pconvert(path[len(roothead):]).strip('/')
-                if prefix:
-                    name = prefix + '/' + name
-                self.repos.append((name, path))
-
+        self.repos = findrepos(paths)
         for prefix, root in self.ui.configitems('collections'):
             for path in util.walkrepos(root, followsym=True):
                 repo = os.path.normpath(path)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-hgwebdir-paths.py	Wed May 20 16:04:37 2009 +0200
@@ -0,0 +1,40 @@
+import os
+from mercurial import hg, ui
+from mercurial.hgweb.hgwebdir_mod import hgwebdir
+
+os.mkdir('webdir')
+os.chdir('webdir')
+
+webdir = os.path.realpath('.')
+
+u = ui.ui()
+hg.repository(u, 'a', create=1)
+hg.repository(u, 'b', create=1)
+os.chdir('b')
+hg.repository(u, 'd', create=1)
+os.chdir('..')
+hg.repository(u, 'c', create=1)
+os.chdir('..')
+
+paths = {'t/a/': '%s/a' % webdir,
+         'b': '%s/b' % webdir,
+         'coll': '%s/*' % webdir,
+         'rcoll': '%s/**' % webdir}
+
+config = os.path.join(webdir, 'hgwebdir.conf')
+configfile = open(config, 'w')
+configfile.write('[paths]\n')
+for k, v in paths.items():
+    configfile.write('%s = %s\n' % (k, v))
+configfile.close()
+
+confwd = hgwebdir(config)
+dictwd = hgwebdir(paths)
+
+assert len(confwd.repos) == len(dictwd.repos), 'different numbers'
+assert len(confwd.repos) == 9, 'expected 9 repos, found %d' % len(confwd.repos)
+
+found = dict(confwd.repos)
+for key, path in dictwd.repos:
+    assert key in found, 'repository %s was not found' % key
+    assert found[key] == path, 'different paths for repo %s' % key