[PATCH 2 of 7 V2 of F2 series] store: add _cutdirs function

Adrian Buehlmann adrian at cadifra.com
Sun Oct 7 05:54:54 CDT 2012


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1349606353 -7200
# Node ID 54f0c9b50738d53fe4516fd184f7241b0112c1e5
# Parent  2f8dcee6ee70474d18488183a6e93bbf99c5d27e
store: add _cutdirs function

diff --git a/mercurial/store.py b/mercurial/store.py
--- a/mercurial/store.py
+++ b/mercurial/store.py
@@ -185,6 +185,41 @@
 _dirprefixlen = 8
 _maxshortdirslen = 8 * (_dirprefixlen + 1) - 4
 
+_encchar = ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
+            "~!~#$%&'()~+,-~~0123456789~;~=~~"
+            "@abcdefghijklmnopqrstuvwxyz[~]^_"
+            "`abcdefghijklmnopqrstuvwxyz{~}~~"
+            "~abcdefghijklmnopqrstuvwxyz{~}~~"
+            "~!~#$%&'()~+,-~~0123456789~;~=~~"
+            "@abcdefghijklmnopqrstuvwxyz[~]^_"
+            "`abcdefghijklmnopqrstuvwxyz{~}~~")
+
+def _foldencode(f): # preserves size
+    f = ''.join([_encchar[ord(c)] for c in f])
+    l = len(f)
+    if l == 3 and f in _winres3:
+        f = f[:2] + '~'
+    if (l == 4 and f[3] <= '9' and f[3] >= '0'
+               and f[:3] in _winres4):
+        f = f[:3] + '~'
+    return f
+
+def _pycutdirs(path):
+    spaceleft = _maxstorepathlen - 45
+    parts = []
+    for s in path.split('/'):
+        if len(s) > 8:
+            s = s[:8]
+        if parts:
+            spaceleft -= 1 # for '/'
+        spaceleft -= len(s)
+        if spaceleft < 0:
+            break
+        parts.append(s)
+    return '/'.join(map(_foldencode, parts))
+
+_cutdirs = getattr(parsers, 'cutdirs', _pycutdirs)
+
 def _hashencode(path, dotencode):
     digest = _sha(path).hexdigest()
     le = lowerencode(path).split('/')[1:]


More information about the Mercurial-devel mailing list