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 @@ -19,6 +19,28 @@ import webutil 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 @@ class hgwebdir(object): 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 @@ class hgwebdir(object): 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) diff --git a/tests/test-hgwebdir-paths.py b/tests/test-hgwebdir-paths.py new file mode 100644 --- /dev/null +++ b/tests/test-hgwebdir-paths.py @@ -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