Rev 2250: split out tag-merging code and add some tests in http://sourcefrog.net/bzr/tags

Martin Pool mbp at sourcefrog.net
Thu Feb 22 04:39:09 GMT 2007


At http://sourcefrog.net/bzr/tags

------------------------------------------------------------
revno: 2250
revision-id: mbp at sourcefrog.net-20070222043908-wt92joyurcxds96p
parent: mbp at sourcefrog.net-20070222030533-4e9nxag9ykjffq3s
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: tags
timestamp: Thu 2007-02-22 15:39:08 +1100
message:
  split out tag-merging code and add some tests
modified:
  BRANCH.TODO                    BRANCH.TODO-20060103052123-79ac4969351c03a9
  bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/tag.py                  tag.py-20070212110532-91cw79inah2cfozx-1
  bzrlib/tests/test_tag.py       test_tag.py-20070212110532-91cw79inah2cfozx-2
=== modified file 'BRANCH.TODO'
--- a/BRANCH.TODO	2007-02-21 05:19:10 +0000
+++ b/BRANCH.TODO	2007-02-22 04:39:08 +0000
@@ -46,11 +46,12 @@
  - rename TagStore and change to branch.tags
  - delete tags, and mark as deleted
  - command to show all tags
+ - make the experimental branch format available through a bzrdir format
 
 Plan
 ----
 
- - make the experimental branch format available through a bzrdir format
+ - move merge-tags stuff into pull, push, merge, etc - not in command routines
  - raise an exception if the tag name is unreasonable
    - see what happens if non-ascii byte string is given
  - if tags conflict when copying

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2007-02-22 03:04:54 +0000
+++ b/bzrlib/branch.py	2007-02-22 04:39:08 +0000
@@ -685,17 +685,9 @@
             checkout_branch.pull(self, stop_revision=revision_id)
         return checkout.create_workingtree(revision_id)
 
-    def copy_tags_to(self, to_branch):
-        """Copy tags to another branch.
-        """
-        # TODO: Allow for doing a smarter merge, etc
-        if self == to_branch:
-            return
-        to_branch.lock_write()
-        try:
-            to_branch.tags._set_tag_dict(self.tags.get_tag_dict())
-        finally:
-            to_branch.unlock()
+    def supports_tags(self):
+        return self._format.supports_tags()
+
 
 class BranchFormat(object):
     """An encapsulation of the initialization and open routines for a format.

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2007-02-21 05:34:56 +0000
+++ b/bzrlib/builtins.py	2007-02-22 04:39:08 +0000
@@ -549,6 +549,7 @@
     def run(self, location=None, remember=False, overwrite=False,
             revision=None, verbose=False,
             directory=None):
+        from bzrlib.tag import _merge_tags_if_possible
         # FIXME: too much stuff is in the command class
         if directory is None:
             directory = u'.'
@@ -601,7 +602,7 @@
                 delta.ChangeReporter(tree_to.inventory))
         else:
             count = branch_to.pull(branch_from, overwrite, rev_id)
-        _copy_tags_maybe(branch_from, branch_to)
+        _merge_tags_if_possible(branch_from, branch_to)
         note('%d revision(s) pulled.' % (count,))
 
         if verbose:
@@ -664,6 +665,7 @@
             directory=None):
         # FIXME: Way too big!  Put this into a function called from the
         # command.
+        from bzrlib.tag import _merge_tags_if_possible
         if directory is None:
             directory = '.'
         br_from = Branch.open_containing(directory)[0]
@@ -798,7 +800,7 @@
             except errors.DivergedBranches:
                 raise errors.BzrCommandError('These branches have diverged.'
                                         '  Try using "merge" and then "push".')
-        _copy_tags_maybe(br_from, br_to)
+        _merge_tags_if_possible(br_from, br_to)
         note('%d revision(s) pushed.' % (count,))
 
         if verbose:
@@ -828,6 +830,7 @@
     aliases = ['get', 'clone']
 
     def run(self, from_location, to_location=None, revision=None, basis=None):
+        from bzrlib.tag import _merge_tags_if_possible
         if revision is None:
             revision = [None]
         elif len(revision) > 1:
@@ -878,26 +881,12 @@
                 raise errors.BzrCommandError(msg)
             if name:
                 branch.control_files.put_utf8('branch-name', name)
-            _copy_tags_maybe(br_from, branch)
+            _merge_tags_if_possible(br_from, branch)
             note('Branched %d revision(s).' % branch.revno())
         finally:
             br_from.unlock()
 
 
-def _copy_tags_maybe(from_branch, to_branch):
-    """Copy tags between repositories if necessary and possible.
-    
-    This method has common command-line behaviour about handling 
-    error cases.
-    """
-    if not from_branch.tags.supports_tags():
-        # obviously nothing to copy
-        return
-    # TODO: give a warning if the source format supports tags and actually has
-    # tags, but the destination doesn't accept them.
-    from_branch.copy_tags_to(to_branch)
-
-
 class cmd_checkout(Command):
     """Create a new checkout of an existing branch.
 
@@ -2445,6 +2434,7 @@
             uncommitted=False, pull=False,
             directory=None,
             ):
+        from bzrlib.tag import _merge_tags_if_possible
         if merge_type is None:
             merge_type = _mod_merge.Merge3Merger
 
@@ -2507,7 +2497,7 @@
 
         # pull tags now... it's a bit inconsistent to do it ahead of copying
         # the history but that's done inside the merge code
-        _copy_tags_maybe(other_branch, tree.branch)
+        _merge_tags_if_possible(other_branch, tree.branch)
 
         if path != "":
             interesting_files = [path]

=== modified file 'bzrlib/tag.py'
--- a/bzrlib/tag.py	2007-02-22 03:04:54 +0000
+++ b/bzrlib/tag.py	2007-02-22 04:39:08 +0000
@@ -59,6 +59,10 @@
     lookup_tag = _not_supported
     delete_tag = _not_supported
 
+    def merge_to(self, to_tags):
+        # we never have anything to copy
+        pass
+
 
 class BasicTags(_Tags):
     """Tag storage in an unversioned branch control file.
@@ -150,3 +154,34 @@
         except ValueError, e:
             raise ValueError("failed to deserialize tag dictionary %r: %s"
                     % (tag_content, e))
+
+    def merge_to(self, to_tags, just_warn=False):
+        """Copy tags between repositories if necessary and possible.
+        
+        This method has common command-line behaviour about handling 
+        error cases.
+
+        :param to_tags: Branch to receive these tags
+        :param just_warn: If the destination doesn't support tags and the 
+            source does have tags, just give a warning.  Otherwise, raise
+            TagsNotSupported (default).
+        """
+        if self.branch == to_tags.branch:
+            return
+        if not self.supports_tags():
+            # obviously nothing to copy
+            return
+        td = self.get_tag_dict()
+        if not td:
+            # no tags in the source, and we don't want to clobber anything
+            # that's in the destination
+            return
+        to_tags.branch.lock_write()
+        try:
+            to_tags._set_tag_dict(td)
+        finally:
+            to_tags.branch.unlock()
+
+
+def _merge_tags_if_possible(from_branch, to_branch):
+    from_branch.tags.merge_to(to_branch.tags)

=== modified file 'bzrlib/tests/test_tag.py'
--- a/bzrlib/tests/test_tag.py	2007-02-20 07:50:00 +0000
+++ b/bzrlib/tests/test_tag.py	2007-02-22 04:39:08 +0000
@@ -15,9 +15,21 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
-from bzrlib import tag
-from bzrlib.tag import BasicTags
-from bzrlib.tests import TestCase
+from bzrlib import (
+    branch,
+    bzrdir,
+    errors,
+    tag,
+    )
+from bzrlib.tag import (
+    BasicTags,
+    _merge_tags_if_possible,
+    )
+from bzrlib.tests import (
+    TestCase,
+    TestCaseInTempDir,
+    )
+
 
 class TestTagSerialization(TestCase):
 
@@ -35,3 +47,36 @@
         self.assertEqualDiff(packed, expected)
         self.assertEqual(store._deserialize_tag_dict(packed), td)
 
+
+class TestTagMerging(TestCaseInTempDir):
+
+    def make_knit_branch(self, relpath):
+        old_bdf = bzrdir.format_registry.make_bzrdir('knit')
+        return bzrdir.BzrDir.create_branch_convenience(relpath, format=old_bdf)
+
+    def make_branch_supporting_tags(self, relpath):
+        new_bdf = bzrdir.format_registry.make_bzrdir('experimental-branch6')
+        return bzrdir.BzrDir.create_branch_convenience(relpath, format=new_bdf)
+
+    def test_merge_not_possible(self):
+        # test merging between branches which do and don't support tags
+        old_branch = self.make_knit_branch('old')
+        new_branch = self.make_branch_supporting_tags('new')
+        # just to make sure this test is valid
+        self.assertFalse(old_branch.supports_tags(),
+            "%s is expected to not support tags but does" % old_branch)
+        self.assertTrue(new_branch.supports_tags(),
+            "%s is expected to support tags but does not" % new_branch)
+        # there are no tags in the old one, and we can merge from it into the
+        # new one
+        _merge_tags_if_possible(old_branch, new_branch)
+        # we couldn't merge tags from the new branch to the old one, but as
+        # there are not any yet this isn't a problem
+        _merge_tags_if_possible(new_branch, old_branch)
+        # but if there is a tag in the new one, we get a warning when trying
+        # to move it back
+        new_branch.tags.set_tag(u'\u2040tag', 'revid')
+        _merge_tags_if_possible(old_branch, new_branch)
+        self.assertRaises(errors.TagsNotSupported,
+            _merge_tags_if_possible, new_branch, old_branch)
+




More information about the bazaar-commits mailing list