hgweb: support multi-level repository indexes by enabling descend and collapse
authorPaul Boddie <paul@boddie.org.uk>
Sat, 18 Feb 2012 20:10:19 +0100
changeset 16239 287f76b3f502
parent 16238 e8eecfe37d4e
child 16240 95e45abe7e8e
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.
mercurial/hgweb/hgwebdir_mod.py
tests/test-hgwebdir.t
--- a/mercurial/hgweb/hgwebdir_mod.py	Tue Mar 06 18:43:05 2012 +0200
+++ b/mercurial/hgweb/hgwebdir_mod.py	Sat Feb 18 20:10:19 2012 +0100
@@ -245,12 +245,67 @@
         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 @@
                 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 @@
                            lastchange=d,
                            lastchange_sort=d[1]-d[0],
                            archives=archivelist(u, "tip", url))
+
+                seenrepos.add(name)
                 yield row
 
         sortdefault = None, False
--- a/tests/test-hgwebdir.t	Tue Mar 06 18:43:05 2012 +0200
+++ b/tests/test-hgwebdir.t	Sat Feb 18 20:10:19 2012 +0100
@@ -31,6 +31,28 @@
   $ 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 @@
   /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 @@
   </tr>
   
   <tr class="parity0">
+  <td><a href="/coll/notrepo/e/?style=paper">coll/notrepo/e</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity1">
+  <td><a href="/coll/notrepo/f/?style=paper">coll/notrepo/f</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity0">
   <td><a href="/rcoll/a/?style=paper">rcoll/a</a></td>
   <td>unknown</td>
   <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
@@ -257,6 +307,38 @@
   </tr>
   
   <tr class="parity1">
+  <td><a href="/rcoll/notrepo/e/?style=paper">rcoll/notrepo/e</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity0">
+  <td><a href="/rcoll/notrepo/e/e2/?style=paper">rcoll/notrepo/e/e2</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity1">
+  <td><a href="/rcoll/notrepo/f/?style=paper">rcoll/notrepo/f</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity0">
+  <td><a href="/rcoll/notrepo/f/f2/?style=paper">rcoll/notrepo/f/f2</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity1">
   <td><a href="/star/webdir/a/?style=paper">star/webdir/a</a></td>
   <td>unknown</td>
   <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
@@ -289,6 +371,22 @@
   </tr>
   
   <tr class="parity1">
+  <td><a href="/star/webdir/notrepo/e/?style=paper">star/webdir/notrepo/e</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity0">
+  <td><a href="/star/webdir/notrepo/f/?style=paper">star/webdir/notrepo/f</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity1">
   <td><a href="/starstar/webdir/a/?style=paper">starstar/webdir/a</a></td>
   <td>unknown</td>
   <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
@@ -329,6 +427,38 @@
   </tr>
   
   <tr class="parity0">
+  <td><a href="/starstar/webdir/notrepo/e/?style=paper">starstar/webdir/notrepo/e</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity1">
+  <td><a href="/starstar/webdir/notrepo/e/e2/?style=paper">starstar/webdir/notrepo/e/e2</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity0">
+  <td><a href="/starstar/webdir/notrepo/f/?style=paper">starstar/webdir/notrepo/f</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity1">
+  <td><a href="/starstar/webdir/notrepo/f/f2/?style=paper">starstar/webdir/notrepo/f/f2</a></td>
+  <td>unknown</td>
+  <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
+  <td class="age">*</td> (glob)
+  <td class="indexlinks"></td>
+  </tr>
+  
+  <tr class="parity0">
   <td><a href="/astar/?style=paper">astar</a></td>
   <td>unknown</td>
   <td>&#70;&#111;&#111;&#32;&#66;&#97;&#114;&#32;&#60;&#102;&#111;&#111;&#46;&#98;&#97;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;&#62;</td>
@@ -489,6 +619,8 @@
   /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 @@
   /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 <<EOF
+  > [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 <<EOF
+  > 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 @@
   > 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 <<EOF
   > [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 <<EOF
   > 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 @@
   > 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 @@
   /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 @@
 
   $ 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