Rev 990: Move scheme-related functions out of repository code. in file:///data/jelmer/bzr-svn/layout/

Jelmer Vernooij jelmer at samba.org
Fri May 2 20:07:24 BST 2008


At file:///data/jelmer/bzr-svn/layout/

------------------------------------------------------------
revno: 990
revision-id: jelmer at samba.org-20080502190723-sb0pvaduzeep7f2q
parent: jelmer at samba.org-20080502184810-1xkpqfeja1f8fheu
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: layout
timestamp: Fri 2008-05-02 21:07:23 +0200
message:
  Move scheme-related functions out of repository code.
modified:
  fetch.py                       fetch.py-20060625004942-x2lfaib8ra707a8p-1
  mapping.py                     mapping.py-20080128201303-6cp01phc0dmc0kiv-1
  mapping3/__init__.py           __init__.py-20080502174630-9324zh25kka98vlw-1
  repository.py                  repository.py-20060306123302-1f8c5069b3fe0265
  tests/test_repos.py            test_repos.py-20060508151940-ddc49a59257ca712
=== modified file 'fetch.py'
--- a/fetch.py	2008-05-02 18:48:10 +0000
+++ b/fetch.py	2008-05-02 19:07:23 +0000
@@ -32,10 +32,11 @@
 from logwalker import lazy_dict
 from bzrlib.plugins.svn.mapping import (SVN_PROP_BZR_ANCESTRY, SVN_PROP_BZR_MERGE, 
                      SVN_PROP_BZR_PREFIX, SVN_PROP_BZR_REVISION_INFO, 
-                     SVN_PROP_BZR_BRANCHING_SCHEME, SVN_PROP_BZR_REVISION_ID,
+                     SVN_PROP_BZR_REVISION_ID,
                      SVN_PROP_BZR_FILEIDS, SVN_REVPROP_BZR_SIGNATURE,
                      parse_merge_property,
                      parse_revision_metadata)
+from bzrlib.plugins.svn.mapping3 import SVN_PROP_BZR_BRANCHING_SCHEME
 from repository import SvnRepository, SvnRepositoryFormat
 from svk import SVN_PROP_SVK_MERGE
 from tree import (apply_txdelta_handler, parse_externals_description, 

=== modified file 'mapping.py'
--- a/mapping.py	2008-05-02 18:48:10 +0000
+++ b/mapping.py	2008-05-02 19:07:23 +0000
@@ -35,7 +35,6 @@
 SVN_PROP_BZR_MERGE = 'bzr:merge'
 SVN_PROP_BZR_REVISION_INFO = 'bzr:revision-info'
 SVN_PROP_BZR_REVISION_ID = 'bzr:revision-id:v%d-' % MAPPING_VERSION
-SVN_PROP_BZR_BRANCHING_SCHEME = 'bzr:branching-scheme'
 
 SVN_REVPROP_BZR_COMMITTER = 'bzr:committer'
 SVN_REVPROP_BZR_FILEIDS = 'bzr:file-ids'
@@ -262,6 +261,10 @@
             BzrSvnMapping._warned_experimental = True
 
     @classmethod
+    def from_repository(cls, repository, _hinted_branch_path=None):
+        return cls()
+
+    @classmethod
     def supports_roundtripping(cls):
         """Whether this mapping supports roundtripping.
         """

=== modified file 'mapping3/__init__.py'
--- a/mapping3/__init__.py	2008-05-02 18:48:10 +0000
+++ b/mapping3/__init__.py	2008-05-02 19:07:23 +0000
@@ -13,9 +13,65 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from bzrlib import osutils
+from bzrlib import osutils, ui
+from bzrlib.trace import mutter
 from bzrlib.plugins.svn import mapping
-from mapping3.scheme import BranchingScheme, guess_scheme_from_branch_path
+from mapping3.scheme import BranchingScheme, guess_scheme_from_branch_path, guess_scheme_from_history
+
+SVN_PROP_BZR_BRANCHING_SCHEME = 'bzr:branching-scheme'
+
+# Number of revisions to evaluate when guessing the branching scheme
+SCHEME_GUESS_SAMPLE_SIZE = 2000
+
+def get_stored_scheme(repository):
+    """Retrieve the stored branching scheme, either in the repository 
+    or in the configuration file.
+    """
+    scheme = repository.get_config().get_branching_scheme()
+    if scheme is not None:
+        return (scheme, repository.get_config().branching_scheme_is_mandatory())
+
+    last_revnum = repository.get_latest_revnum()
+    scheme = get_property_scheme(repository, last_revnum)
+    if scheme is not None:
+        return (scheme, True)
+
+    return (None, False)
+
+def get_property_scheme(repository, revnum=None):
+    if revnum is None:
+        revnum = repository.get_latest_revnum()
+    text = repository.branchprop_list.get_properties("", revnum).get(SVN_PROP_BZR_BRANCHING_SCHEME, None)
+    if text is None:
+        return None
+    return ListBranchingScheme(parse_list_scheme_text(text))
+
+def set_property_scheme(repository, scheme):
+    def done(revmetadata, pool):
+        pass
+    editor = repository.transport.get_commit_editor(
+            {svn.core.SVN_PROP_REVISION_LOG: "Updating branching scheme for Bazaar."},
+            done, None, False)
+    root = editor.open_root(-1)
+    editor.change_dir_prop(root, SVN_PROP_BZR_BRANCHING_SCHEME, 
+            "".join(map(lambda x: x+"\n", scheme.branch_list)).encode("utf-8"))
+    editor.close_directory(root)
+    editor.close()
+
+def repository_guess_scheme(repository, last_revnum, branch_path=None):
+    pb = ui.ui_factory.nested_progress_bar()
+    try:
+        scheme = guess_scheme_from_history(
+            repository._log.iter_changes("", last_revnum, max(0, last_revnum-SCHEME_GUESS_SAMPLE_SIZE), pb=pb), last_revnum, branch_path)
+    finally:
+        pb.finished()
+    mutter("Guessed branching scheme: %r" % scheme)
+    return scheme
+
+def set_branching_scheme(repository, scheme, mandatory=False):
+    repository.get_config().set_branching_scheme(str(scheme), mandatory=mandatory)
+
+
 
 class BzrSvnMappingv3(mapping.BzrSvnMapping):
     """The third version of the mappings as used in the 0.4.x series.
@@ -30,6 +86,24 @@
         self.scheme = scheme
         assert not isinstance(scheme, str)
 
+    @classmethod
+    def from_repository(cls, repository, _hinted_branch_path=None):
+        (scheme, mandatory) = get_stored_scheme(repository)
+        if mandatory:
+            return cls(scheme) 
+
+        if scheme is not None:
+            if (_hinted_branch_path is None or 
+                scheme.is_branch(_hinted_branch_path)):
+                return cls(scheme)
+
+        last_revnum = repository.get_latest_revnum()
+        scheme = repository_guess_scheme(repository, last_revnum, _hinted_branch_path)
+        if last_revnum > 20:
+            set_branching_scheme(repository, scheme, mandatory=False)
+
+        return cls(scheme)
+
     def __repr__(self):
         return "%s(%r)" % (self.__class__.__name__, self.scheme)
 
@@ -110,7 +184,6 @@
     def __eq__(self, other):
         return type(self) == type(other) and self.scheme == other.scheme
 
-
 class BzrSvnMappingv3FileProps(mapping.BzrSvnMappingFileProps, BzrSvnMappingv3):
     pass
 

=== modified file 'repository.py'
--- a/repository.py	2008-05-02 18:48:10 +0000
+++ b/repository.py	2008-05-02 19:07:23 +0000
@@ -42,7 +42,6 @@
 import errors
 import logwalker
 from bzrlib.plugins.svn.mapping import (SVN_PROP_BZR_REVISION_ID, SVN_REVPROP_BZR_SIGNATURE,
-                     SVN_PROP_BZR_BRANCHING_SCHEME, 
                      parse_revision_metadata, parse_revid_property, 
                      parse_merge_property, BzrSvnMapping,
                      get_default_mapping, parse_revision_id)
@@ -55,9 +54,6 @@
 from tree import SvnRevisionTree
 import urllib
 
-# Number of revisions to evaluate when guessing the branching scheme
-SCHEME_GUESS_SAMPLE_SIZE = 2000
-
 def svk_feature_to_revision_id(feature, mapping):
     """Convert a SVK feature to a revision id for this repository.
 
@@ -131,7 +127,7 @@
         self._log = logwalker.LogWalker(transport=transport)
         self.fileid_map = FileIdMap(simple_apply_changes, self)
         self.revmap = RevidMap(self)
-        self._scheme = None
+        self._default_mapping = None
         self._hinted_branch_path = branch_path
 
         cache = self.get_config().get_use_cache()
@@ -199,93 +195,16 @@
         self._cached_revnum = self.transport.get_latest_revnum()
         return self._cached_revnum
 
-    def get_stored_scheme(self):
-        """Retrieve the stored branching scheme, either in the repository 
-        or in the configuration file.
-        """
-        scheme = self.get_config().get_branching_scheme()
-        if scheme is not None:
-            return (scheme, self.get_config().branching_scheme_is_mandatory())
-
-        last_revnum = self.get_latest_revnum()
-        scheme = self._get_property_scheme(last_revnum)
-        if scheme is not None:
-            return (scheme, True)
-
-        return (None, False)
-
     def get_mapping(self):
-        return get_default_mapping()(self.get_scheme())
+        if self._default_mapping is not None:
+            return self._default_mapping
+        return get_default_mapping().from_repository(self, self._hinted_branch_path)
 
     def _make_parents_provider(self):
         return CachingParentsProvider(self)
 
     def get_layout(self):
-        return self.get_scheme()
-
-    def get_scheme(self):
-        """Determine the branching scheme to use for this repository.
-
-        :return: Branching scheme.
-        """
-        # First, try to use the branching scheme we already know
-        if self._scheme is not None:
-            return self._scheme
-
-        (scheme, mandatory) = self.get_stored_scheme()
-        if mandatory:
-            self._scheme = scheme
-            return scheme
-
-        if scheme is not None:
-            if (self._hinted_branch_path is None or 
-                scheme.is_branch(self._hinted_branch_path)):
-                self._scheme = scheme
-                return scheme
-
-        last_revnum = self.get_latest_revnum()
-        self.set_branching_scheme(
-            self._guess_scheme(last_revnum, self._hinted_branch_path),
-            store=(last_revnum > 20),
-            mandatory=False)
-
-        return self._scheme
-
-    def _get_property_scheme(self, revnum=None):
-        if revnum is None:
-            revnum = self.get_latest_revnum()
-        text = self.branchprop_list.get_properties("", revnum).get(SVN_PROP_BZR_BRANCHING_SCHEME, None)
-        if text is None:
-            return None
-        return ListBranchingScheme(parse_list_scheme_text(text))
-
-    def set_property_scheme(self, scheme):
-        def done(revmetadata, pool):
-            pass
-        editor = self.transport.get_commit_editor(
-                {svn.core.SVN_PROP_REVISION_LOG: "Updating branching scheme for Bazaar."},
-                done, None, False)
-        root = editor.open_root(-1)
-        editor.change_dir_prop(root, SVN_PROP_BZR_BRANCHING_SCHEME, 
-                "".join(map(lambda x: x+"\n", scheme.branch_list)).encode("utf-8"))
-        editor.close_directory(root)
-        editor.close()
-
-    def _guess_scheme(self, last_revnum, branch_path=None):
-        pb = ui.ui_factory.nested_progress_bar()
-        try:
-            scheme = guess_scheme_from_history(
-                self._log.iter_changes("", last_revnum, max(0, last_revnum-SCHEME_GUESS_SAMPLE_SIZE), pb=pb), last_revnum, branch_path)
-        finally:
-            pb.finished()
-        mutter("Guessed branching scheme: %r" % scheme)
-        return scheme
-
-    def set_branching_scheme(self, scheme, store=True, mandatory=False):
-        self._scheme = scheme
-        if store:
-            self.get_config().set_branching_scheme(str(scheme), 
-                                                   mandatory=mandatory)
+        return self.get_mapping().scheme
 
     def _warn_if_deprecated(self):
         # This class isn't deprecated
@@ -565,19 +484,19 @@
 
         return self.get_revmap().get_revision_id(revnum, path, mapping, revprops, changed_fileprops)
 
-    def lookup_revision_id(self, revid, scheme=None):
+    def lookup_revision_id(self, revid, layout=None):
         """Parse an existing Subversion-based revision id.
 
         :param revid: The revision id.
-        :param scheme: Optional repository layout to use when searching for 
+        :param layout: Optional repository layout to use when searching for 
                        revisions
         :raises: NoSuchRevision
         :return: Tuple with branch path, revision number and mapping.
         """
         # If there is no entry in the map, walk over all branches:
-        if scheme is None:
-            scheme = self.get_scheme()
-        return self.get_revmap().get_branch_revnum(revid, scheme)
+        if layout is None:
+            layout = self.get_layout()
+        return self.get_revmap().get_branch_revnum(revid, layout)
 
     def get_inventory_xml(self, revision_id):
         """See Repository.get_inventory_xml()."""

=== modified file 'tests/test_repos.py'
--- a/tests/test_repos.py	2008-05-02 18:37:05 +0000
+++ b/tests/test_repos.py	2008-05-02 19:07:23 +0000
@@ -35,7 +35,8 @@
 
 import format
 from mapping import (escape_svn_path, unescape_svn_path, 
-                     SVN_PROP_BZR_REVISION_ID, SVN_PROP_BZR_BRANCHING_SCHEME)
+                     SVN_PROP_BZR_REVISION_ID)
+from mapping3 import SVN_PROP_BZR_BRANCHING_SCHEME
 from mapping3.scheme import (TrunkBranchingScheme, NoBranchingScheme, 
                     ListBranchingScheme, SingleBranchingScheme)
 from transport import SvnRaTransport




More information about the bazaar-commits mailing list