[PATCH 1 of 2] Descend empty directoriries in web view, non-optional, optimized

Ry4an Brase ry4an-hg at ry4an.org
Thu Oct 23 14:01:04 CDT 2008


# HG changeset patch
# User Ry4an Brase <ry4an-hg at ry4an.org>
# Date 1224788391 18000
# Node ID 794aa36724be01b71054c4aea9fc5baf1eed0161
# Parent  12a90281d83d6704028b198e82294a9ee329bdea
Descend empty directoriries in web view, non-optional, optimized

diff -r 12a90281d83d -r 794aa36724be mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py	Thu Oct 23 19:27:25 2008 +0200
+++ b/mercurial/hgweb/webcommands.py	Thu Oct 23 13:59:51 2008 -0500
@@ -259,6 +259,16 @@
 
 rev = changeset
 
+def _decendempties(parentpath, hash):
+    if len(hash) == 1:
+        key, val = hash.items().pop()
+        if isinstance(val, dict):
+            name = key + "/"
+            path = parentpath + name
+            return [{"name": name, "path": path}] + _decendempties(path, val)
+    return []
+
+
 def manifest(web, req, tmpl):
     ctx = webutil.changectx(web.repo, req)
     path = webutil.cleanpath(web.repo, req.form.get('file', [''])[0])
@@ -266,6 +276,7 @@
     node = ctx.node()
 
     files = {}
+    dirs = {}
     parity = paritygen(web.stripecount)
 
     if path and path[-1] != "/":
@@ -277,20 +288,25 @@
         if f[:l] != path:
             continue
         remain = f[l:]
-        idx = remain.find('/')
-        if idx != -1:
-            remain = remain[:idx+1]
-            n = None
-        files[remain] = (f, n)
+        elements = remain.split('/')
+        if len(elements) == 1:
+            files[remain] = f
+        else:
+            h = dirs
+            for elem in elements[0:len(elements)-1]:
+                if elem not in h:
+                    h[elem] = {}
+                h = h[elem]
+                if len(h) > 1:
+                    break
+            h[None] = 1
 
-    if not files:
+    if not files and not dirs:
         raise ErrorResponse(HTTP_NOT_FOUND, 'path not found: ' + path)
 
     def filelist(**map):
         for f in util.sort(files):
-            full, fnode = files[f]
-            if not fnode:
-                continue
+            full = files[f]
 
             fctx = ctx.filectx(full)
             yield {"file": full,
@@ -301,14 +317,13 @@
                    "permissions": mf.flags(full)}
 
     def dirlist(**map):
-        for f in util.sort(files):
-            full, fnode = files[f]
-            if fnode:
-                continue
+        for d in util.sort(dirs):
 
+            path = "%s%s" % (abspath, d)
             yield {"parity": parity.next(),
-                   "path": "%s%s" % (abspath, f),
-                   "basename": f[:-1]}
+                   "path": path,
+                   "emptydirs": _decendempties(path + '/', dirs[d]),
+                   "basename": d}
 
     return tmpl("manifest",
                 rev=ctx.rev(),


More information about the Mercurial-devel mailing list