Rev 4468: Fix bug #206577 by adding a --strict option to send. in file:///home/vila/src/bzr/bugs/206577-send-strict/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Jun 26 19:13:42 BST 2009


At file:///home/vila/src/bzr/bugs/206577-send-strict/

------------------------------------------------------------
revno: 4468
revision-id: v.ladeuil+lp at free.fr-20090626181341-grhq7i0h0gv7xlcb
parent: v.ladeuil+lp at free.fr-20090626153640-ay59jpmdn1u07xtq
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 206577-send-strict
timestamp: Fri 2009-06-26 20:13:41 +0200
message:
  Fix bug #206577 by adding a --strict option to send.
  
  * bzrlib/tests/blackbox/test_send.py:
  (load_tests): Test send --strict for uncommitted changes and
  pending merges.
  (TestSendBase): Factor out some common helpers.
  (TestSendStrict): Helpers for --strict option tests.
  (TestSendStrictWithoutChanges, TestSendStrictWithChanges): Test
  the --strict option.
  
  * bzrlib/send.py:
  (send): Handle the strict option.
  
  * bzrlib/help_topics/en/configuration.txt:
  (send_strict): Document.
  
  * bzrlib/builtins.py:
  (cmd_send): Add a '--strict' option.
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2009-06-19 09:06:56 +0000
+++ b/NEWS	2009-06-26 18:13:41 +0000
@@ -13,9 +13,16 @@
 ************
 
 * ``bzr push`` now checks if uncommitted changes are present in the working
-  tree if the ``--strict`` option is used.
+  tree if the ``--strict`` option is used. The ``push_strict`` option can
+  be declared in a configuration file.
   (Vincent Ladeuil, #284038)
 
+* ``bzr send`` now aborts if uncommitted changes (including pending merges)
+  are present in the working tree and no revision is speficied. The
+  ``--no-strict`` option can be used to force the sending. The
+  ``send_strict`` option can be declared in a configuration file.
+  (Vincent Ladeuil, #206577)
+
 
 Bug Fixes
 *********

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2009-06-19 09:06:56 +0000
+++ b/bzrlib/builtins.py	2009-06-26 18:13:41 +0000
@@ -4886,24 +4886,29 @@
                help='Write merge directive to this file; '
                     'use - for stdout.',
                type=unicode),
+        Option('strict',
+               help='Refuse to send if there are uncommitted changes in'
+               ' the working tree.'),
         Option('mail-to', help='Mail the request to this address.',
                type=unicode),
         'revision',
         'message',
         Option('body', help='Body for the email.', type=unicode),
         RegistryOption('format',
-                       help='Use the specified output format.', 
-                       lazy_registry=('bzrlib.send', 'format_registry'))
+                       help='Use the specified output format.',
+                       lazy_registry=('bzrlib.send', 'format_registry')),
         ]
 
     def run(self, submit_branch=None, public_branch=None, no_bundle=False,
             no_patch=False, revision=None, remember=False, output=None,
-            format=None, mail_to=None, message=None, body=None, **kwargs):
+            format=None, mail_to=None, message=None, body=None,
+            strict=None, **kwargs):
         from bzrlib.send import send
         return send(submit_branch, revision, public_branch, remember,
-                         format, no_bundle, no_patch, output,
-                         kwargs.get('from', '.'), mail_to, message, body,
-                         self.outf)
+                    format, no_bundle, no_patch, output,
+                    kwargs.get('from', '.'), mail_to, message, body,
+                    self.outf,
+                    strict=strict)
 
 
 class cmd_bundle_revisions(cmd_send):

=== modified file 'bzrlib/help_topics/en/configuration.txt'
--- a/bzrlib/help_topics/en/configuration.txt	2009-06-10 13:18:48 +0000
+++ b/bzrlib/help_topics/en/configuration.txt	2009-06-26 18:13:41 +0000
@@ -421,3 +421,10 @@
 
 If set to "True", the branch should act as a checkout, and push each commit to
 the bound_location.  This option is normally set by ``bind``/``unbind``.
+
+send_strict
+~~~~~~~~~~~
+
+If present, defines the ``--strict`` option default value for checking
+uncommitted changes before sending a merge directive.
+

=== modified file 'bzrlib/send.py'
--- a/bzrlib/send.py	2009-05-28 16:14:16 +0000
+++ b/bzrlib/send.py	2009-06-26 18:13:41 +0000
@@ -37,8 +37,8 @@
 
 
 def send(submit_branch, revision, public_branch, remember, format,
-         no_bundle, no_patch, output, from_, mail_to, message, body, 
-         to_file):
+         no_bundle, no_patch, output, from_, mail_to, message, body,
+         to_file, strict=None):
     tree, branch = bzrdir.BzrDir.open_containing_tree_or_branch(from_)[:2]
     # we may need to write data into branch's repository to calculate
     # the data to send.
@@ -107,6 +107,21 @@
             if len(revision) == 2:
                 base_revision_id = revision[0].as_revision_id(branch)
         if revision_id is None:
+            if strict is None:
+                strict = branch.get_config().get_user_option('send_strict')
+                if strict is not None:
+                    # FIXME: This should be better supported by config
+                    # -- vila 20090626
+                    bools = dict(yes=True, no=False, on=True, off=False,
+                                 true=True, false=False)
+                    try:
+                        strict = bools[strict.lower()]
+                    except KeyError:
+                        strict = None
+            if strict is None or strict: # Default to True
+                changes = tree.changes_from(tree.basis_tree())
+                if changes.has_changed() or len(tree.get_parent_ids()) > 1:
+                    raise errors.UncommittedChanges(tree)
             revision_id = branch.last_revision()
         if revision_id == NULL_REVISION:
             raise errors.BzrCommandError('No revisions to submit.')

=== modified file 'bzrlib/tests/blackbox/test_send.py'
--- a/bzrlib/tests/blackbox/test_send.py	2009-06-26 15:36:40 +0000
+++ b/bzrlib/tests/blackbox/test_send.py	2009-06-26 18:13:41 +0000
@@ -28,7 +28,48 @@
 from bzrlib.bundle import serializer
 
 
-class TestSend(tests.TestCaseWithTransport):
+def load_tests(standard_tests, module, loader):
+    """Multiply tests for the send command."""
+    result = loader.suiteClass()
+
+    # one for each king of change
+    changes_tests, remaining_tests = tests.split_suite_by_condition(
+        standard_tests, tests.condition_isinstance((
+                TestSendStrictWithChanges,
+                )))
+    changes_scenarios = [
+        ('uncommitted',
+         dict(_do_changes= TestSendStrictWithChanges.do_uncommitted_changes)),
+        ('pending_merges',
+         dict(_do_changes= TestSendStrictWithChanges.do_pending_merges)),
+        ]
+    tests.multiply_tests(changes_tests, changes_scenarios, result)
+    # No parametrization for the remaining tests
+    result.addTests(remaining_tests)
+
+    return result
+
+
+class TestSendBase(tests.TestCaseWithTransport):
+
+    def run_send(self, args, cmd=None, rc=0, wd='branch', err_re=None):
+        if cmd is None: cmd = ['send', '-o-']
+        if err_re is None: err_re = []
+        return self.run_bzr(cmd + args, retcode=rc,
+                            working_dir=wd,
+                            error_regexes=err_re)
+
+    def get_MD(self, args, cmd=None, wd='branch'):
+        out = StringIO(self.run_send(args, cmd=cmd, wd=wd)[0])
+        return merge_directive.MergeDirective.from_lines(out.readlines())
+
+    def assertBundleContains(self, revs, args, cmd=None, wd='branch'):
+        md = self.get_MD(args, cmd=cmd, wd=wd)
+        br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
+        self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
+
+
+class TestSend(TestSendBase):
 
     def setUp(self):
         super(TestSend, self).setUp()
@@ -46,20 +87,6 @@
         self.build_tree_contents([('branch/file1', 'branch')])
         branch_tree.commit('last commit', rev_id='rev3')
 
-    def run_send(self, args, cmd=None, rc=0, wd='branch'):
-        if cmd is None:
-            cmd = ['send', '-o-']
-        return self.run_bzr(cmd + args, retcode=rc, working_dir=wd)
-
-    def get_MD(self, args, cmd=None, wd='branch'):
-        out = StringIO(self.run_send(args, cmd=cmd, wd=wd)[0])
-        return merge_directive.MergeDirective.from_lines(out.readlines())
-
-    def assertBundleContains(self, revs, args, cmd=None, wd='branch'):
-        md = self.get_MD(args, cmd=cmd, wd=wd)
-        br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
-        self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
-
     def assertFormatIs(self, fmt_string, md):
         self.assertEqual(fmt_string, md.get_raw_bundle().splitlines()[0])
 
@@ -254,3 +281,130 @@
         out, err = self.run_bzr(["send", "--from", location], retcode=3)
         self.assertEqual(out, '')
         self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)
+
+
+class TestSendStrict(TestSendBase):
+
+    def make_parent_and_local_branches(self):
+        # Create a 'parent' branch as the base
+        self.parent_tree = bzrdir.BzrDir.create_standalone_workingtree('parent')
+        self.build_tree_contents([('parent/file', 'parent')])
+        self.parent_tree.add('file')
+        self.parent_tree.commit('first commit', rev_id='parent')
+        # Branch 'local' from parent and do a change
+        local_bzrdir = self.parent_tree.bzrdir.sprout('local')
+        self.local_tree = local_bzrdir.open_workingtree()
+        self.build_tree_contents([('local/file', 'local')])
+        self.local_tree.commit('second commit', rev_id='local')
+
+    def run_send(self, args, cmd=None, rc=0, wd='local', err_re=None):
+        if cmd is None: cmd = ['send', '../parent', '-o-']
+        if err_re is None: err_re = []
+        return super(TestSendStrict, self).run_send(
+            args, cmd=cmd, rc=rc, wd=wd, err_re=err_re)
+
+    def set_config_send_strict(self, value):
+        # set config var (any of bazaar.conf, locations.conf, branch.conf
+        # should do)
+        conf = self.local_tree.branch.get_config()
+        conf.set_user_option('send_strict', value)
+
+    def assertSendFails(self, args):
+        self.run_send(args, rc=3,
+                      err_re=['Working tree ".*/local/"'
+                              ' has uncommitted changes.$',])
+
+    def assertSendSucceeds(self, revs, args):
+        out, err = self.run_send(args)
+        self.assertEquals('Bundling 1 revision(s).\n', err)
+        md = merge_directive.MergeDirective.from_lines(
+                StringIO(out).readlines())
+        self.assertEqual('parent', md.base_revision_id)
+        br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
+        self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
+
+
+class TestSendStrictWithoutChanges(TestSendStrict):
+
+    def setUp(self):
+        super(TestSendStrictWithoutChanges, self).setUp()
+        self.make_parent_and_local_branches()
+
+    def test_send_default(self):
+        self.assertSendSucceeds(['local'], [])
+
+    def test_send_strict(self):
+        self.assertSendSucceeds(['local'], ['--strict'])
+
+    def test_send_no_strict(self):
+        self.assertSendSucceeds(['local'], ['--no-strict'])
+
+    def test_send_config_var_strict(self):
+        self.set_config_send_strict('true')
+        self.assertSendSucceeds(['local'], [])
+
+    def test_send_config_var_no_strict(self):
+        self.set_config_send_strict('false')
+        self.assertSendSucceeds(['local'], [])
+
+
+class TestSendStrictWithChanges(TestSendStrict):
+
+    _do_changes = None # Set by load_tests
+
+    def setUp(self):
+        super(TestSendStrictWithChanges, self).setUp()
+        # FIXME: The following is a bit ugly, but I don't know how to bind the
+        # method properly, yet I want to define the method to call in
+        # load_tests(). -- vila 20090626
+        self._do_changes(self)
+
+    def do_uncommitted_changes(self):
+        self.make_parent_and_local_branches()
+        # Make a change without committing it
+        self.build_tree_contents([('local/file', 'modified')])
+
+    def do_pending_merges(self):
+        self.make_parent_and_local_branches()
+        # Create 'other' branch containing a new file
+        other_bzrdir = self.parent_tree.bzrdir.sprout('other')
+        other_tree = other_bzrdir.open_workingtree()
+        self.build_tree_contents([('other/other-file', 'other')])
+        other_tree.add('other-file')
+        other_tree.commit('other commit', rev_id='other')
+        # Merge and revert, leabing a pending merge
+        self.local_tree.merge_from_branch(other_tree.branch)
+        self.local_tree.revert(filenames=['other-file'], backups=False)
+
+    def test_send_default(self):
+        self.assertSendFails([])
+
+    def test_send_with_revision(self):
+        self.assertSendSucceeds(['local'], ['-r', 'revid:local'])
+
+    def test_send_no_strict(self):
+        self.assertSendSucceeds(['local'], ['--no-strict'])
+
+    def test_send_strict_with_changes(self):
+        self.assertSendFails(['--strict'])
+
+    def test_send_respect_config_var_strict(self):
+        self.set_config_send_strict('true')
+        self.assertSendFails([])
+        self.assertSendSucceeds(['local'], ['--no-strict'])
+
+
+    def test_send_bogus_config_var_ignored(self):
+        self.set_config_send_strict("I'm unsure")
+        self.assertSendFails([])
+
+
+    def test_send_no_strict_command_line_override_config(self):
+        self.set_config_send_strict('true')
+        self.assertSendFails([])
+        self.assertSendSucceeds(['local'], ['--no-strict'])
+
+    def test_push_strict_command_line_override_config(self):
+        self.set_config_send_strict('false')
+        self.assertSendSucceeds(['local'], [])
+        self.assertSendFails(['--strict'])



More information about the bazaar-commits mailing list