synthrepo: new filenames must not also be new directories, and vice-versa
authorMike Edgar <adgar@google.com>
Fri, 12 Dec 2014 17:42:14 +0000
changeset 23778 a5dbec255f14
parent 23777 a4951adebfb0
child 23779 427b8728536f
synthrepo: new filenames must not also be new directories, and vice-versa When generating many new files into a set of many possible new directories, there is the possibility that the same path is chosen as both file and directory. How likely this is depends on the size of the dictionary used, the generated directory structure and the number of generated files.
contrib/synthrepo.py
--- a/contrib/synthrepo.py	Thu Jan 08 13:29:06 2015 -0800
+++ b/contrib/synthrepo.py	Fri Dec 12 17:42:14 2014 +0000
@@ -323,15 +323,32 @@
     initcount = int(opts['initfiles'])
     if initcount and initdirs:
         pctx = repo[None].parents()[0]
+        dirs = set(pctx.dirs())
         files = {}
+
+        def validpath(path):
+            # Don't pick filenames which are already directory names.
+            if path in dirs:
+                return False
+            # Don't pick directories which were used as file names.
+            while path:
+                if path in files:
+                    return False
+                path = os.path.dirname(path)
+            return True
+
         for i in xrange(0, initcount):
             ui.progress(_synthesizing, i, unit=_files, total=initcount)
 
             path = pickpath()
-            while path in pctx.dirs():
+            while not validpath(path):
                 path = pickpath()
             data = '%s contents\n' % path
             files[path] = context.memfilectx(repo, path, data)
+            dir = os.path.dirname(path)
+            while dir and dir not in dirs:
+                dirs.add(dir)
+                dir = os.path.dirname(dir)
 
         def filectxfn(repo, memctx, path):
             return files[path]