# HG changeset patch # User Paul Boddie # Date 2012-02-18 19:10:19 # Node ID 287f76b3f502552e0cfa3c087e6b69b812341a38 # Parent e8eecfe37d4e7a3ac6a8d7c5039208d0299acf47 hgweb: support multi-level repository indexes by enabling descend and collapse The descend option in hgweb can be used to display all reachable repositories within a directory hierarchy if set to True. However, all reachable repositories, regardless of their depth below the root of the hierarchy, are then listed at the same level - expanded - in the hgweb interface. This patch adds support for showing only each level of a directory hierarchy, with subrepositories being shown alongside their parent repositories only at the appropriate level (because there is no way to navigate to subrepositories from within repositories), and the contents of directories hidden - collapsed - behind a link for each directory. To enable this multi-level navigation, a new option called collapse must be set to True when the descend option is set to True. 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 @@ -245,12 +245,67 @@ class hgwebdir(object): def rawentries(subdir="", **map): descend = self.ui.configbool('web', 'descend', True) + collapse = self.ui.configbool('web', 'collapse', False) + seenrepos = set() + seendirs = set() for name, path in self.repos: if not name.startswith(subdir): continue name = name[len(subdir):] - if not descend and '/' in name: + directory = False + + if '/' in name: + if not descend: + continue + + nameparts = name.split('/') + rootname = nameparts[0] + + if not collapse: + pass + elif rootname in seendirs: + continue + elif rootname in seenrepos: + pass + else: + directory = True + name = rootname + + # redefine the path to refer to the directory + discarded = '/'.join(nameparts[1:]) + + # remove name parts plus accompanying slash + path = path[:-len(discarded) - 1] + + parts = [name] + if 'PATH_INFO' in req.env: + parts.insert(0, req.env['PATH_INFO'].rstrip('/')) + if req.env['SCRIPT_NAME']: + parts.insert(0, req.env['SCRIPT_NAME']) + url = re.sub(r'/+', '/', '/'.join(parts) + '/') + + # show either a directory entry or a repository + if directory: + # get the directory's time information + try: + d = (get_mtime(path), util.makedate()[1]) + except OSError: + continue + + row = dict(contact="", + contact_sort="", + name=name, + name_sort=name, + url=url, + description="", + description_sort="", + lastchange=d, + lastchange_sort=d[1]-d[0], + archives=[]) + + seendirs.add(name) + yield row continue u = self.ui.copy() @@ -268,13 +323,6 @@ class hgwebdir(object): if not self.read_allowed(u, req): continue - parts = [name] - if 'PATH_INFO' in req.env: - parts.insert(0, req.env['PATH_INFO'].rstrip('/')) - if req.env['SCRIPT_NAME']: - parts.insert(0, req.env['SCRIPT_NAME']) - url = re.sub(r'/+', '/', '/'.join(parts) + '/') - # update time with local timezone try: r = hg.repository(self.ui, path) @@ -302,6 +350,8 @@ class hgwebdir(object): lastchange=d, lastchange_sort=d[1]-d[0], archives=archivelist(u, "tip", url)) + + seenrepos.add(name) yield row sortdefault = None, False diff --git a/tests/test-hgwebdir.t b/tests/test-hgwebdir.t --- a/tests/test-hgwebdir.t +++ b/tests/test-hgwebdir.t @@ -31,6 +31,28 @@ create a nested repository $ hg --cwd c ci -Amc -d'3 0' adding c +create a subdirectory containing repositories and subrepositories + + $ mkdir notrepo + $ cd notrepo + $ hg init e + $ echo e > e/e + $ hg --cwd e ci -Ame -d'4 0' + adding e + $ hg init e/e2 + $ echo e2 > e/e2/e2 + $ hg --cwd e/e2 ci -Ame2 -d '4 0' + adding e2 + $ hg init f + $ echo f > f/f + $ hg --cwd f ci -Amf -d'4 0' + adding f + $ hg init f/f2 + $ echo f2 > f/f2/f2 + $ hg --cwd f/f2 ci -Amf2 -d '4 0' + adding f2 + $ cd .. + create repository without .hg/store $ hg init nostore @@ -119,20 +141,32 @@ should succeed, slashy names /coll/a/.hg/patches/ /coll/b/ /coll/c/ + /coll/notrepo/e/ + /coll/notrepo/f/ /rcoll/a/ /rcoll/a/.hg/patches/ /rcoll/b/ /rcoll/b/d/ /rcoll/c/ + /rcoll/notrepo/e/ + /rcoll/notrepo/e/e2/ + /rcoll/notrepo/f/ + /rcoll/notrepo/f/f2/ /star/webdir/a/ /star/webdir/a/.hg/patches/ /star/webdir/b/ /star/webdir/c/ + /star/webdir/notrepo/e/ + /star/webdir/notrepo/f/ /starstar/webdir/a/ /starstar/webdir/a/.hg/patches/ /starstar/webdir/b/ /starstar/webdir/b/d/ /starstar/webdir/c/ + /starstar/webdir/notrepo/e/ + /starstar/webdir/notrepo/e/e2/ + /starstar/webdir/notrepo/f/ + /starstar/webdir/notrepo/f/f2/ /astar/ /astar/.hg/patches/ @@ -217,6 +251,22 @@ should succeed, slashy names + coll/notrepo/e + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + coll/notrepo/f + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + rcoll/a unknown Foo Bar <foo.bar@example.com> @@ -257,6 +307,38 @@ should succeed, slashy names + rcoll/notrepo/e + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + rcoll/notrepo/e/e2 + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + rcoll/notrepo/f + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + rcoll/notrepo/f/f2 + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + star/webdir/a unknown Foo Bar <foo.bar@example.com> @@ -289,6 +371,22 @@ should succeed, slashy names + star/webdir/notrepo/e + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + star/webdir/notrepo/f + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + starstar/webdir/a unknown Foo Bar <foo.bar@example.com> @@ -329,6 +427,38 @@ should succeed, slashy names + starstar/webdir/notrepo/e + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + starstar/webdir/notrepo/e/e2 + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + starstar/webdir/notrepo/f + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + + starstar/webdir/notrepo/f/f2 + unknown + Foo Bar <foo.bar@example.com> + * (glob) + + + + astar unknown Foo Bar <foo.bar@example.com> @@ -489,6 +619,8 @@ Test [paths] '*' extension /coll/a/.hg/patches/ /coll/b/ /coll/c/ + /coll/notrepo/e/ + /coll/notrepo/f/ $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/coll/a/file/tip/a?style=raw' 200 Script output follows @@ -506,12 +638,140 @@ Test [paths] '**' extension /rcoll/b/ /rcoll/b/d/ /rcoll/c/ + /rcoll/notrepo/e/ + /rcoll/notrepo/e/e2/ + /rcoll/notrepo/f/ + /rcoll/notrepo/f/f2/ + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/b/d/file/tip/d?style=raw' + 200 Script output follows + + d + +Test collapse = True + + $ "$TESTDIR/killdaemons.py" + $ cat >> paths.conf < [web] + > collapse=true + > EOF + $ hg serve -p $HGPORT1 -d --pid-file=hg.pid --webdir-conf paths.conf \ + > -A access-paths.log -E error-paths-3.log + $ cat hg.pid >> $DAEMON_PIDS + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/coll/?style=raw' + 200 Script output follows + + + /coll/a/ + /coll/a/.hg/patches/ + /coll/b/ + /coll/c/ + /coll/notrepo/ + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/coll/a/file/tip/a?style=raw' + 200 Script output follows + + a + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/?style=raw' + 200 Script output follows + + + /rcoll/a/ + /rcoll/a/.hg/patches/ + /rcoll/b/ + /rcoll/b/d/ + /rcoll/c/ + /rcoll/notrepo/ $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/b/d/file/tip/d?style=raw' 200 Script output follows d +Test intermediate directories + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/notrepo/?style=raw' + 200 Script output follows + + + /rcoll/notrepo/e/ + /rcoll/notrepo/e/e2/ + /rcoll/notrepo/f/ + /rcoll/notrepo/f/f2/ + + +Test repositories inside intermediate directories + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/notrepo/e/file/tip/e?style=raw' + 200 Script output follows + + e + +Test subrepositories inside intermediate directories + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/notrepo/f/f2/file/tip/f2?style=raw' + 200 Script output follows + + f2 + +Test descend = False + + $ "$TESTDIR/killdaemons.py" + $ cat >> paths.conf < descend=false + > EOF + $ hg serve -p $HGPORT1 -d --pid-file=hg.pid --webdir-conf paths.conf \ + > -A access-paths.log -E error-paths-4.log + $ cat hg.pid >> $DAEMON_PIDS + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/coll/?style=raw' + 200 Script output follows + + + /coll/a/ + /coll/b/ + /coll/c/ + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/coll/a/file/tip/a?style=raw' + 200 Script output follows + + a + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/?style=raw' + 200 Script output follows + + + /rcoll/a/ + /rcoll/b/ + /rcoll/c/ + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/b/d/file/tip/d?style=raw' + 200 Script output follows + + d + +Test intermediate directories + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/notrepo/?style=raw' + 200 Script output follows + + + /rcoll/notrepo/e/ + /rcoll/notrepo/f/ + + +Test repositories inside intermediate directories + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/notrepo/e/file/tip/e?style=raw' + 200 Script output follows + + e + +Test subrepositories inside intermediate directories + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/rcoll/notrepo/f/f2/file/tip/f2?style=raw' + 200 Script output follows + + f2 + Test [paths] '*' in a repo root $ hg id http://localhost:$HGPORT1/astar @@ -523,15 +783,60 @@ Test [paths] '*' in a repo root > t/a = $root/a > t/b = $root/b > c = $root/c + > EOF + $ hg serve -p $HGPORT1 -d --pid-file=hg.pid --webdir-conf paths.conf \ + > -A access-paths.log -E error-paths-5.log + $ cat hg.pid >> $DAEMON_PIDS + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/?style=raw' + 200 Script output follows + + + /t/a/ + /t/b/ + /c/ + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/t/?style=raw' + 200 Script output follows + + + /t/a/ + /t/b/ + + +Test collapse = True + + $ "$TESTDIR/killdaemons.py" + $ cat >> paths.conf < [web] + > collapse=true + > EOF + $ hg serve -p $HGPORT1 -d --pid-file=hg.pid --webdir-conf paths.conf \ + > -A access-paths.log -E error-paths-6.log + $ cat hg.pid >> $DAEMON_PIDS + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/?style=raw' + 200 Script output follows + + + /t/ + /c/ + + $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/t/?style=raw' + 200 Script output follows + + + /t/a/ + /t/b/ + + +test descend = False + + $ "$TESTDIR/killdaemons.py" + $ cat >> paths.conf < descend=false > EOF $ hg serve -p $HGPORT1 -d --pid-file=hg.pid --webdir-conf paths.conf \ - > -A access-paths.log -E error-paths-3.log + > -A access-paths.log -E error-paths-7.log $ cat hg.pid >> $DAEMON_PIDS - -test descend = False - $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT1 '/?style=raw' 200 Script output follows @@ -552,7 +857,7 @@ test descend = False > inexistent = $root/inexistent > EOF $ hg serve -p $HGPORT1 -d --pid-file=hg.pid --webdir-conf paths.conf \ - > -A access-paths.log -E error-paths-4.log + > -A access-paths.log -E error-paths-8.log $ cat hg.pid >> $DAEMON_PIDS test inexistent and inaccessible repo should be ignored silently @@ -617,6 +922,8 @@ collections: should succeed /a/.hg/patches/ /b/ /c/ + /notrepo/e/ + /notrepo/f/ $ "$TESTDIR/get-with-headers.py" localhost:$HGPORT2 '/a/file/tip/a?style=raw' 200 Script output follows @@ -672,6 +979,26 @@ paths errors 3 $ cat error-paths-3.log +paths errors 4 + + $ cat error-paths-4.log + +paths errors 5 + + $ cat error-paths-5.log + +paths errors 6 + + $ cat error-paths-6.log + +paths errors 7 + + $ cat error-paths-7.log + +paths errors 8 + + $ cat error-paths-8.log + collections errors $ cat error-collections.log