Rev 2262: Tag command refuses to replace existing tags unless you force it. in http://sourcefrog.net/bzr/tags

Martin Pool mbp at sourcefrog.net
Mon Feb 26 05:29:07 GMT 2007


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

------------------------------------------------------------
revno: 2262
revision-id: mbp at sourcefrog.net-20070226052905-7mt8m6871e9dym8e
parent: mbp at sourcefrog.net-20070226050852-791ohdf63hd52yt1
committer: Martin Pool <mbp at sourcefrog.net>
branch nick: tags
timestamp: Mon 2007-02-26 16:29:05 +1100
message:
  Tag command refuses to replace existing tags unless you force it.
  
  Add Tags.has_tag.
modified:
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/tag.py                  tag.py-20070212110532-91cw79inah2cfozx-1
  bzrlib/tests/blackbox/test_tags.py test_tags.py-20070116132048-5h4qak2cm22jlb9e-1
  bzrlib/tests/branch_implementations/test_tags.py test_tags.py-20070212110545-w2s799hm2jlbsmg5-1
=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2007-02-26 05:08:52 +0000
+++ b/bzrlib/builtins.py	2007-02-26 05:29:05 +0000
@@ -3181,39 +3181,54 @@
 
     Tags are stored in the branch.  Tags are copied from one branch to another
     along when you branch, push, pull or merge.
+
+    It is an error to give a tag name that already exists unless you pass 
+    --force, in which case the tag is moved to point to the new revision.
     """
 
     takes_args = ['tag_name']
     takes_options = [
-        Option('directory',
-            help='branch in which to place the tag',
-            short_name='d',
-            type=unicode,
-            ),
         Option('delete',
-            help='Delete this tag rather than placing it',
+            help='Delete this tag rather than placing it.',
+            ),
+        Option('directory',
+            help='Branch in which to place the tag.',
+            short_name='d',
+            type=unicode,
+            ),
+        Option('force',
+            help='Replace existing tags',
             ),
         'revision',
         ]
 
-    def run(self, tag_name, directory='.',
+    def run(self, tag_name,
+            delete=None,
+            directory='.',
+            force=None,
             revision=None,
-            delete=None):
+            ):
         branch, relpath = Branch.open_containing(directory)
-        if delete:
-            branch.tags.delete_tag(tag_name)
-            self.outf.write('deleted tag %s' % tag_name)
-        else:
-            if revision:
-                if len(revision) != 1:
-                    raise errors.BzrCommandError(
-                        "Tags can only be placed on a single revision, "
-                        "not on a range")
-                revision_id = revision[0].in_history(branch).rev_id
+        branch.lock_write()
+        try:
+            if delete:
+                branch.tags.delete_tag(tag_name)
+                self.outf.write('Deleted tag %s.\n' % tag_name)
             else:
-                revision_id = branch.last_revision()
-            branch.tags.set_tag(tag_name, revision_id)
-            self.outf.write('created tag %s' % tag_name)
+                if revision:
+                    if len(revision) != 1:
+                        raise errors.BzrCommandError(
+                            "Tags can only be placed on a single revision, "
+                            "not on a range")
+                    revision_id = revision[0].in_history(branch).rev_id
+                else:
+                    revision_id = branch.last_revision()
+                if (not force) and branch.tags.has_tag(tag_name):
+                    raise errors.TagAlreadyExists(tag_name)
+                branch.tags.set_tag(tag_name, revision_id)
+                self.outf.write('Created tag %s.\n' % tag_name)
+        finally:
+            branch.unlock()
 
 
 class cmd_tags(Command):

=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2007-02-21 05:34:56 +0000
+++ b/bzrlib/errors.py	2007-02-26 05:29:05 +0000
@@ -1797,3 +1797,11 @@
 
     def __init__(self, branch):
         self.branch = branch
+
+        
+class TagAlreadyExists(BzrError):
+
+    _fmt = "Tag %(tag_name)s already exists."
+
+    def __init__(self, tag_name):
+        self.tag_name = tag_name

=== modified file 'bzrlib/tag.py'
--- a/bzrlib/tag.py	2007-02-22 05:49:03 +0000
+++ b/bzrlib/tag.py	2007-02-26 05:29:05 +0000
@@ -40,6 +40,9 @@
     def __init__(self, branch):
         self.branch = branch
 
+    def has_tag(self, tag_name):
+        return self.get_tag_dict().has_key(tag_name)
+
 
 class DisabledTags(_Tags):
     """Tag storage that refuses to store anything.

=== modified file 'bzrlib/tests/blackbox/test_tags.py'
--- a/bzrlib/tests/blackbox/test_tags.py	2007-02-26 05:08:33 +0000
+++ b/bzrlib/tests/blackbox/test_tags.py	2007-02-26 05:29:05 +0000
@@ -57,7 +57,7 @@
             rev_id='first-revid')
         # make a tag through the command line
         out, err = self.run_bzr('tag', '-d', 'branch', 'NEWTAG')
-        self.assertContainsRe(out, 'created tag NEWTAG')
+        self.assertContainsRe(out, 'Created tag NEWTAG.')
         # tag should be observable through the api
         self.assertEquals(t.branch.tags.get_tag_dict(),
                 dict(NEWTAG='first-revid'))
@@ -65,7 +65,12 @@
         self.run_bzr('tag', '-d', 'branch', 'tag2', '-r1')
         self.assertEquals(t.branch.tags.lookup_tag('tag2'), 'first-revid')
         # can also delete an existing tag
-        self.run_bzr('tag', '--delete', '-d', 'branch', 'tag2')
+        out, err = self.run_bzr('tag', '--delete', '-d', 'branch', 'tag2')
+        # cannot replace an existing tag normally
+        out, err = self.run_bzr('tag', '-d', 'branch', 'NEWTAG', retcode=3)
+        self.assertContainsRe(err, 'Tag NEWTAG already exists\\.')
+        # ... but can if you use --force
+        out, err = self.run_bzr('tag', '-d', 'branch', 'NEWTAG', '--force')
 
     def test_branch_push_pull_merge_copies_tags(self):
         t = self.make_branch_and_tree('branch1')

=== modified file 'bzrlib/tests/branch_implementations/test_tags.py'
--- a/bzrlib/tests/branch_implementations/test_tags.py	2007-02-22 05:49:03 +0000
+++ b/bzrlib/tests/branch_implementations/test_tags.py	2007-02-26 05:29:05 +0000
@@ -67,6 +67,9 @@
         # read one at a time
         result = b.tags.lookup_tag('tag-name')
         self.assertEqual(result, 'target-revid-1')
+        # and try has_tag
+        self.assertTrue(b.tags.has_tag('tag-name'))
+        self.assertFalse(b.tags.has_tag('imaginary'))
 
     def test_no_such_tag(self):
         b = self.make_branch('b')




More information about the bazaar-commits mailing list