changeset 1004:b2d89ba6b871

layouts: pull out code for detecting layout from subversion This is the start of an effort to refactor the code for how we handle subversion layoout to mercurial layout mapping. Specifically, it pulls out the logic for guessing what layout to use based on the contents of subversion. It also sets up a bit of the structure for the new library.
author David Schleimer <dschleimer@fb.com>
date Wed, 17 Apr 2013 14:10:47 -0700
parents 6945d3359456
children 5bba4d1becde
files hgsubversion/layouts/__init__.py hgsubversion/layouts/detect.py hgsubversion/wrappers.py
diffstat 3 files changed, 56 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/hgsubversion/layouts/__init__.py
@@ -0,0 +1,16 @@
+"""Code for dealing with subversion layouts
+
+This package is intended to encapsulate everything about subversion
+layouts.  This includes detecting the layout based on looking at
+subversion, mapping subversion paths to hg branches, and doing any
+other path translation necessary.
+
+NB: this has a long way to go before it does everything it claims to
+
+"""
+
+import detect
+
+__all__ = [
+    "detect",
+    ]
new file mode 100644
--- /dev/null
+++ b/hgsubversion/layouts/detect.py
@@ -0,0 +1,36 @@
+""" Layout detection for subversion repos.
+
+Figure out what layout we should be using, based on config, command
+line flags, subversion contents, and anything else we decide to base
+it on.
+
+"""
+
+from mercurial import util as hgutil
+
+import hgsubversion.svnwrap
+
+def layout_from_subversion(svn, revision=None, ui=None):
+    """ Guess what layout to use based on directories under the svn root.
+
+    This is intended for use during bootstrapping.  It guesses which
+    layout to use based on the presence or absence of the conventional
+    trunk, branches, tags dirs immediately under the path your are
+    cloning.
+
+    Additionally, this will write the layout in use to the ui object
+    passed, if any.
+
+    """
+
+    try:
+        rootlist = svn.list_dir('', revision=revision)
+    except svnwrap.SubversionException, e:
+        err = "%s (subversion error: %d)" % (e.args[0], e.args[1])
+        raise hgutil.Abort(err)
+    if sum(map(lambda x: x in rootlist, ('branches', 'tags', 'trunk'))):
+        layout = 'standard'
+    else:
+        layout = 'single'
+    ui.setconfig('hgsubversion', 'layout', layout)
+    return layout
--- a/hgsubversion/wrappers.py
+++ b/hgsubversion/wrappers.py
@@ -14,6 +14,7 @@ from mercurial import i18n
 from mercurial import extensions
 from mercurial import repair
 
+import layouts
 import os
 import replay
 import pushmod
@@ -356,16 +357,9 @@ def pull(repo, source, heads=[], force=F
         if not layout in ('auto', 'single', 'standard'):
             raise hgutil.Abort("unknown layout '%s'" % layout)
         if layout == 'auto':
-            try:
-                rootlist = svn.list_dir('', revision=(stopat_rev or None))
-            except svnwrap.SubversionException, e:
-                err = "%s (subversion error: %d)" % (e.args[0], e.args[1])
-                raise hgutil.Abort(err)
-            if sum(map(lambda x: x in rootlist, ('branches', 'tags', 'trunk'))):
-                layout = 'standard'
-            else:
-                layout = 'single'
-            repo.ui.setconfig('hgsubversion', 'layout', layout)
+            layout = layouts.detect.layout_from_subversion(svn,
+                                                           (stopat_rev or None),
+                                                           repo.ui)
             repo.ui.note('using %s layout\n' % layout)
 
         branch = repo.ui.config('hgsubversion', 'branch')