changeset 1012:e8cd211684c4

layouts: refactor out svn path to mercurial branch logic This pulls the logic for mapping from svn path to mercurial branch name out of svnmeta.py and into the new layouts library. It also sets up the structure for that library. This diff does not modify any call to svnmeta.localname, rather leaving it in place as a simple proxy to the new layout object.
author David Schleimer <dschleimer@fb.com>
date Wed, 24 Apr 2013 15:07:11 -0700
parents cc774e975aed
children d507c1a12dcb
files hgsubversion/layouts/__init__.py hgsubversion/layouts/base.py hgsubversion/layouts/single.py hgsubversion/layouts/standard.py hgsubversion/svnmeta.py
diffstat 5 files changed, 77 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/layouts/__init__.py
+++ b/hgsubversion/layouts/__init__.py
@@ -9,10 +9,36 @@ NB: this has a long way to go before it 
 
 """
 
+from mercurial import util as hgutil
+
 import detect
 import persist
+import single
+import standard
 
 __all__ = [
     "detect",
+    "layout_from_name",
     "persist",
     ]
+
+# This is the authoritative store of what layouts are available.
+# The intention is for extension authors who wish to build their own
+# layout to add it to this dict.
+NAME_TO_CLASS = {
+    "single": single.SingleLayout,
+    "standard": standard.StandardLayout,
+}
+
+
+def layout_from_name(name):
+    """Returns a layout module given the layout name
+
+    You should use one of the layout.detect.* functions to get the
+    name to pass to this function.
+
+    """
+
+    if name not in NAME_TO_CLASS:
+        raise hgutil.Abort('Unknown hgsubversion layout: %s' %name)
+    return NAME_TO_CLASS[name]()
new file mode 100644
--- /dev/null
+++ b/hgsubversion/layouts/base.py
@@ -0,0 +1,20 @@
+"""Module to hold the base API for layout classes.
+
+This module should not contain any implementation, just a definition
+of the API concrete layouts are expected to implement.
+
+"""
+
+from mercurial import util as hgutil
+
+class BaseLayout(object):
+
+    def __unimplemented(self, method_name):
+        raise NotImplementedError(
+            "Incomplete layout implementation: %s.%s doesn't implement %s" %
+            (self.__module__, self.__name__, method_name))
+
+    def localname(self, path):
+        """Compute the local name for a branch located at path.
+        """
+        self.__unimplemented('localname')
new file mode 100644
--- /dev/null
+++ b/hgsubversion/layouts/single.py
@@ -0,0 +1,9 @@
+
+
+import base
+
+class SingleLayout(base.BaseLayout):
+    """A layout with only the default branch"""
+
+    def localname(self, path):
+        return 'default'
new file mode 100644
--- /dev/null
+++ b/hgsubversion/layouts/standard.py
@@ -0,0 +1,14 @@
+
+
+import base
+
+
+class StandardLayout(base.BaseLayout):
+    """The standard trunk, branches, tags layout"""
+
+    def localname(self, path):
+        if path == 'trunk':
+            return None
+        elif path.startswith('branches/'):
+            return path[len('branches/'):]
+        return  '../%s' % path
--- a/hgsubversion/svnmeta.py
+++ b/hgsubversion/svnmeta.py
@@ -72,6 +72,7 @@ class SVNMeta(object):
             self.tag_locations = tag_locations
         self._layout = layouts.detect.layout_from_file(self.meta_data_dir,
                                                        ui=self.repo.ui)
+        self._layoutobj = None
         pickle_atomic(self.tag_locations, self.tag_locations_file)
         # ensure nested paths are handled properly
         self.tag_locations.sort()
@@ -107,6 +108,12 @@ class SVNMeta(object):
             layouts.persist.layout_to_file(self.meta_data_dir, self._layout)
         return self._layout
 
+    @property
+    def layoutobj(self):
+        if not self._layoutobj:
+            self._layoutobj = layouts.layout_from_name(self.layout)
+        return self._layoutobj
+
     @property
     def editor(self):
         if not hasattr(self, '_editor'):
@@ -215,13 +222,7 @@ class SVNMeta(object):
     def localname(self, path):
         """Compute the local name for a branch located at path.
         """
-        if self.layout == 'single':
-            return 'default'
-        if path == 'trunk':
-            return None
-        elif path.startswith('branches/'):
-            return path[len('branches/'):]
-        return  '../%s' % path
+        return self.layoutobj.localname(path)
 
     def remotename(self, branch):
         if self.layout == 'single':