Rev 5452: Implement ``bzr config option=value``. in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Sun Oct 3 18:01:18 BST 2010


At file:///home/vila/src/bzr/experimental/config/

------------------------------------------------------------
revno: 5452
revision-id: v.ladeuil+lp at free.fr-20101003170118-3yhu3oguq1i9lqn6
parent: v.ladeuil+lp at free.fr-20101002210835-nqy0fqas09e7ijj1
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-modify
timestamp: Sun 2010-10-03 19:01:18 +0200
message:
  Implement ``bzr config option=value``.
  
  * bzrlib/tests/test_config.py:
  (TestWithConfigs.setUp): Slightly reorder.
  
  * bzrlib/tests/blackbox/test_config.py:
  (TestConfigDisplay): Inherits from TestWithConfigs to reduce code
  duplication.
  (TestConfigSet): Check the command behaviour.
  
  * bzrlib/config.py:
  (cmd_config): Implement setting a config option.
  (cmd_config._show_config, cmd_config.set_config_option): Split
  set/show option.
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2010-10-02 21:08:35 +0000
+++ b/NEWS	2010-10-03 17:01:18 +0000
@@ -22,9 +22,10 @@
 * Add ``annotate`` revision specifier, which selects the revision that
   introduced a specified line of a file.  (Aaron Bentley)
 
-* ``bzr config`` is a new command that displays the configuration options
-  for a given directory. It accepts a glob to match against multiple
-  options at once. (Vincent Ladeuil)
+* ``bzr config`` is a new command that displays the configuration options for
+  a given directory. It accepts a glob to match against multiple options at
+  once. It can also be used to set a configuration option in any configuration
+  file. (Vincent Ladeuil)
 
 * ``bzr status`` now displays a summary of existing shelves after
   the other status information. This is done using a ``post_status``

=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2010-10-02 21:08:35 +0000
+++ b/bzrlib/config.py	2010-10-03 17:01:18 +0000
@@ -1719,16 +1719,33 @@
 
     takes_options = [
         'directory',
+        # FIXME: This should be a registry option so that plugins can register
+        # their own config files (or not) -- vila 20101002
+        commands.Option('force', help='Force the configuration file',
+                        short_name='f', type=unicode),
         ]
 
     @commands.display_command
-    def run(self, matching=None, directory=None):
+    def run(self, matching=None, directory=None, force=None):
+        if directory is None:
+            directory = '.'
+        directory = urlutils.normalize_url(directory)
         if matching is None:
             matching = '*'
-        if directory is None:
-            directory = '.'
+            self._show_config('*', directory, force)
+        else:
+            pos = matching.find('=')
+            if pos == -1:
+                self._show_config(matching, directory, force)
+            else:
+                self._set_config_option(matching[:pos], matching[pos+1:],
+                                        directory, force)
+
+    def _show_config(self, matching, directory, force):
+        # FIXME: force must be None
         try:
-            (_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(directory)
+            (_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
+                directory)
             confs = [br.get_config()]
         except errors.NotBranchError:
             confs = [LocationConfig(directory), GlobalConfig()]
@@ -1743,3 +1760,28 @@
                         cur_conf_id = conf_id
                     self.outf.write('  %s = %s\n' % (name, value))
 
+    def _set_config_option(self, name, value, directory, force):
+        confs = []
+        if force is not None:
+            if force == 'bazaar':
+                confs = [GlobalConfig()]
+            elif force == 'locations':
+                confs = [LocationConfig(directory)]
+            elif force == 'branch':
+                (_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
+                    directory)
+                confs = [br.get_config()]
+        else:
+            try:
+                (_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
+                    directory)
+                confs = [br.get_config()]
+            except errors.NotBranchError:
+                confs = [LocationConfig(directory), GlobalConfig()]
+        trace.mutter('confs: %r' % (confs,))
+        if not confs:
+            raise errors.BzrError('%s is not a known configuration'
+                                  % (force,))
+        # We use the first config to set the option
+        confs[0].set_user_option(name, value)
+

=== modified file 'bzrlib/tests/blackbox/test_config.py'
--- a/bzrlib/tests/blackbox/test_config.py	2010-10-01 09:52:08 +0000
+++ b/bzrlib/tests/blackbox/test_config.py	2010-10-03 17:01:18 +0000
@@ -21,10 +21,15 @@
 
 from bzrlib import (
     config,
+    errors,
     tests,
     )
-from bzrlib.tests import script
-
+from bzrlib.tests import (
+    script,
+    # FIXME: Importing the 'other' test_config to use TestWithConfigs hints
+    # that we really need a fixture here -- vila 20101002
+    test_config as _t_config,
+    )
 
 class TestEmptyConfig(tests.TestCaseWithTransport):
 
@@ -39,17 +44,7 @@
         self.assertEquals('', err)
 
 
-class TestConfigDisplay(tests.TestCaseWithTransport):
-
-    def setUp(self):
-        super(TestConfigDisplay, self).setUp()
-        # All the test can chose to test in a branch or outside of it
-        # (defaulting to location='.' by using '-d tree'. We create the 3
-        # canonnical configs and each test can set whatever option it sees fit.
-        self.tree = self.make_branch_and_tree('tree')
-        self.branch_config = config.BranchConfig(self.tree.branch)
-        self.locations_config = config.LocationConfig(self.tree.basedir)
-        self.global_config = config.GlobalConfig()
+class TestConfigDisplay(_t_config.TestWithConfigs):
 
     def test_global_config(self):
         self.global_config.set_user_option('hello', 'world')
@@ -78,3 +73,49 @@
 bazaar:
   hello = world
 ''')
+
+
+class TestConfigSet(_t_config.TestWithConfigs):
+
+    def test_unknown_config(self):
+        self.run_bzr(['config', '--force', 'moon', 'hello=world'], retcode=3)
+
+    def test_global_config_outside_branch(self):
+        script.run_script(self, '''
+$ bzr config --force bazaar hello=world
+$ bzr config -d tree hello
+bazaar:
+  hello = world
+''')
+
+    def test_global_config_inside_branch(self):
+        script.run_script(self, '''
+$ bzr config -d tree --force bazaar hello=world
+$ bzr config -d tree hello
+bazaar:
+  hello = world
+''')
+
+    def test_locations_config_inside_branch(self):
+        script.run_script(self, '''
+$ bzr config -d tree --force locations hello=world
+$ bzr config -d tree hello
+locations:
+  hello = world
+''')
+
+    def test_branch_config_default(self):
+        script.run_script(self, '''
+$ bzr config -d tree hello=world
+$ bzr config -d tree hello
+branch:
+  hello = world
+''')
+
+    def test_branch_config_forcing_branch(self):
+        script.run_script(self, '''
+$ bzr config -d tree --force branch hello=world
+$ bzr config -d tree hello
+branch:
+  hello = world
+''')

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2010-10-02 21:08:35 +0000
+++ b/bzrlib/tests/test_config.py	2010-10-03 17:01:18 +0000
@@ -1475,10 +1475,10 @@
 
     def setUp(self):
         super(TestWithConfigs, self).setUp()
-        self.global_config = config.GlobalConfig()
         self.tree = self.make_branch_and_tree('tree')
+        self.locations_config = config.LocationConfig(self.tree.basedir)
         self.branch_config = config.BranchConfig(self.tree.branch)
-        self.locations_config = config.LocationConfig(self.tree.basedir)
+        self.global_config = config.GlobalConfig()
 
 
 class TestConfigGetOptions(TestWithConfigs):



More information about the bazaar-commits mailing list