[PATCH] Make hgwebdir handle paths the same whether part of a config file or init param

Jeremy Whitlock jcscoobyrs at gmail.com
Mon May 18 18:49:20 CDT 2009


# HG changeset patch
# User Jeremy Whitlock <jcscoobyrs at gmail.com>
# Date 1242690247 21600
# Node ID 4eb100e787af13f3c4e6457decd8dc619f62824c
# Parent  97184c58d0b83365a1b8de7a227e713d230f2dcd
Make hgwebdir handle paths the same whether part of a config file or init param.

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.

diff -r 97184c58d0b8 -r 4eb100e787af mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py	Mon May 18 13:54:21 2009 -0500
+++ b/mercurial/hgweb/hgwebdir_mod.py	Mon May 18 17:44:07 2009 -0600
@@ -19,6 +19,31 @@
 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 list((k, v) for k, v in repos.items())
+
 class hgwebdir(object):
     refreshinterval = 20
 
@@ -39,14 +64,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,28 +71,15 @@
             self.stripecount = int(self.stripecount)
         self._baseurl = self.ui.config('web', 'baseurl')
 
-        if self.repos:
-            return
+        if not isinstance(self.conf, (dict, list, tuple)):
+            self.ui.readconfig(self.conf, remap={'paths': 'hgweb-paths'}, 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):
diff -r 97184c58d0b8 -r 4eb100e787af tests/test-hgwebdir-paths.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-hgwebdir-paths.py	Mon May 18 17:44:07 2009 -0600
@@ -0,0 +1,59 @@
+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')
+config_file = open(config, 'w')
+
+config_file.write('[paths]\n')
+
+for k, v in paths.items():
+    config_file.write('%s = %s\n' % (k, v))
+
+config_file.close()
+
+config_hgwebdir = hgwebdir(config)
+dict_hgwebdir = hgwebdir(paths)
+
+if not len(config_hgwebdir.repos) == len(dict_hgwebdir.repos):
+    print 'Identical paths configurations should yield the same number of repos'
+
+if not len(config_hgwebdir.repos) == 9:
+    print 'There should be 9 repositories, found %d' % len(config_hgwebdir.repos)
+
+found_repos = {}
+
+for repo in config_hgwebdir.repos:
+    found_repos[repo[0]] = repo[1]
+
+for repo in dict_hgwebdir.repos:
+    if not found_repos.has_key(repo[0]):
+        print 'The repository name was not found'
+
+    if found_repos[repo[0]] != repo[1]:
+        print 'The repository paths should be the same'


More information about the Mercurial-devel mailing list