55
55
turns on create_signatures.
56
56
create_signatures - this option controls whether bzr will always create
57
57
gpg signatures or not on commits. There is an unused
58
option which in future is expected to work if
58
option which in future is expected to work if
59
59
branch settings require signatures.
60
60
log_format - this option sets the default log format. Possible values are
61
61
long, short, line, or a plugin can register new formats.
196
195
return self[section][name]
199
# FIXME: Until we can guarantee that each config file is loaded once and
200
# only once for a given bzrlib session, we don't want to re-read the file every
201
# time we query for an option so we cache the value (bad ! watch out for tests
202
# needing to restore the proper value). -- vila 20110219
203
_expand_default_value = None
204
def _get_expand_default_value():
205
global _expand_default_value
206
if _expand_default_value is not None:
207
return _expand_default_value
208
conf = GlobalConfig()
209
# Note that we must not use None for the expand value below or we'll run
210
# into infinite recursion. Using False really would be quite silly ;)
211
expand = conf.get_user_option_as_bool('bzr.config.expand', expand=True)
213
# This is an opt-in feature, you *really* need to clearly say you want
216
_expand_default_value = expand
220
198
class Config(object):
221
199
"""A configuration policy - what username, editor, gpg needs etc."""
227
205
"""Returns a unique ID for the config."""
228
206
raise NotImplementedError(self.config_id)
230
@deprecated_method(deprecated_in((2, 4, 0)))
231
def get_editor(self):
232
"""Get the users pop up editor."""
233
raise NotImplementedError
235
208
def get_change_editor(self, old_tree, new_tree):
236
209
from bzrlib import diff
237
210
cmd = self._get_change_editor()
240
213
return diff.DiffFromTool.from_string(cmd, old_tree, new_tree,
243
def get_mail_client(self):
244
"""Get a mail client to use"""
245
selected_client = self.get_user_option('mail_client')
246
_registry = mail_client.mail_client_registry
248
mail_client_class = _registry.get(selected_client)
250
raise errors.UnknownMailClient(selected_client)
251
return mail_client_class(self)
253
216
def _get_signature_checking(self):
254
217
"""Template method to override signature checking policy."""
384
347
"""Template method to provide a user option."""
387
def get_user_option(self, option_name, expand=None):
350
def get_user_option(self, option_name, expand=True):
388
351
"""Get a generic option - no special process, no default.
390
353
:param option_name: The queried option.
648
609
for (oname, value, section, conf_id, parser) in self._get_options():
649
610
if oname.startswith('bzr.mergetool.'):
650
611
tool_name = oname[len('bzr.mergetool.'):]
651
tools[tool_name] = self.get_user_option(oname)
612
tools[tool_name] = self.get_user_option(oname, False)
652
613
trace.mutter('loaded merge tools: %r' % tools)
1090
1051
conf._create_from_string(str_or_unicode, save)
1093
@deprecated_method(deprecated_in((2, 4, 0)))
1094
def get_editor(self):
1095
return self._get_user_option('editor')
1097
1054
@needs_write_lock
1098
1055
def set_user_option(self, option, value):
1099
1056
"""Save option and its value in the configuration."""
1533
1492
TODO: Global option --config-dir to override this.
1535
base = os.environ.get('BZR_HOME', None)
1494
base = osutils.path_from_environ('BZR_HOME')
1536
1495
if sys.platform == 'win32':
1537
# environ variables on Windows are in user encoding/mbcs. So decode
1539
if base is not None:
1540
base = base.decode('mbcs')
1542
base = win32utils.get_appdata_location_unicode()
1544
base = os.environ.get('HOME', None)
1545
if base is not None:
1546
base = base.decode('mbcs')
1548
raise errors.BzrError('You must have one of BZR_HOME, APPDATA,'
1497
base = win32utils.get_appdata_location()
1499
base = win32utils.get_home_location()
1500
# GZ 2012-02-01: Really the two level subdirs only make sense inside
1501
# APPDATA, but hard to move. See bug 348640 for more.
1550
1502
return osutils.pathjoin(base, 'bazaar', '2.0')
1552
if base is not None:
1553
base = base.decode(osutils._fs_enc)
1554
if sys.platform == 'darwin':
1556
# this takes into account $HOME
1557
base = os.path.expanduser("~")
1558
return osutils.pathjoin(base, '.bazaar')
1561
xdg_dir = os.environ.get('XDG_CONFIG_HOME', None)
1504
# GZ 2012-02-01: What should OSX use instead of XDG if anything?
1505
if sys.platform != 'darwin':
1506
xdg_dir = osutils.path_from_environ('XDG_CONFIG_HOME')
1562
1507
if xdg_dir is None:
1563
xdg_dir = osutils.pathjoin(os.path.expanduser("~"), ".config")
1508
xdg_dir = osutils.pathjoin(osutils._get_home_dir(), ".config")
1564
1509
xdg_dir = osutils.pathjoin(xdg_dir, 'bazaar')
1565
1510
if osutils.isdir(xdg_dir):
1567
1512
"Using configuration in XDG directory %s." % xdg_dir)
1569
base = os.path.expanduser("~")
1570
return osutils.pathjoin(base, ".bazaar")
1514
base = osutils._get_home_dir()
1515
return osutils.pathjoin(base, ".bazaar")
1573
1518
def config_filename():
2198
2143
It may be set to a location, or None.
2200
This policy affects all branches contained by this bzrdir, except for
2201
those under repositories.
2145
This policy affects all branches contained by this control dir, except
2146
for those under repositories.
2203
2148
if self._config is None:
2204
2149
raise errors.BzrError("Cannot set configuration in %s" % self._bzrdir)
2396
2341
raise AssertionError('%r is not supported as a default value'
2398
2343
self.default_from_env = default_from_env
2400
2345
self.from_unicode = from_unicode
2401
2346
self.unquote = unquote
2402
2347
if invalid and invalid not in ('warning', 'error'):
2403
2348
raise AssertionError("%s not supported for 'invalid'" % (invalid,))
2404
2349
self.invalid = invalid
2406
2355
def convert_from_unicode(self, store, unicode_value):
2407
2356
if self.unquote and store is not None and unicode_value is not None:
2408
2357
unicode_value = store.unquote(unicode_value)
2503
class RegistryOption(Option):
2504
"""Option for a choice from a registry."""
2506
def __init__(self, name, registry, default_from_env=None,
2507
help=None, invalid=None):
2508
"""A registry based Option definition.
2510
This overrides the base class so the conversion from a unicode string
2511
can take quoting into account.
2513
super(RegistryOption, self).__init__(
2514
name, default=lambda: unicode(registry.default_key),
2515
default_from_env=default_from_env,
2516
from_unicode=self.from_unicode, help=help,
2517
invalid=invalid, unquote=False)
2518
self.registry = registry
2520
def from_unicode(self, unicode_str):
2521
if not isinstance(unicode_str, basestring):
2524
return self.registry.get(unicode_str)
2527
"Invalid value %s for %s."
2528
"See help for a list of possible values." % (unicode_str,
2533
ret = [self._help, "\n\nThe following values are supported:\n"]
2534
for key in self.registry.keys():
2535
ret.append(" %s - %s\n" % (key, self.registry.get_help(key)))
2551
2539
class OptionRegistry(registry.Registry):
2552
2540
"""Register config options by their name.
2644
2632
Whether revisions associated with tags should be fetched.
2634
option_registry.register_lazy(
2635
'bzr.transform.orphan_policy', 'bzrlib.transform', 'opt_transform_orphan')
2646
2636
option_registry.register(
2647
2637
Option('bzr.workingtree.worth_saving_limit', default=10,
2648
2638
from_unicode=int_from_store, invalid='warning',
2656
2646
a file has been touched.
2658
2648
option_registry.register(
2649
Option('bugtracker', default=None,
2651
Default bug tracker to use.
2653
This bug tracker will be used for example when marking bugs
2654
as fixed using ``bzr commit --fixes``, if no explicit
2655
bug tracker was specified.
2657
option_registry.register(
2659
2658
Option('check_signatures', default=CHECK_IF_POSSIBLE,
2660
2659
from_unicode=signature_policy_from_unicode,
2763
2762
Standard log formats are ``long``, ``short`` and ``line``. Additional formats
2764
2763
may be provided by plugins.
2765
option_registry.register_lazy('mail_client', 'bzrlib.mail_client',
2766
2767
option_registry.register(
2767
2768
Option('output_encoding',
2768
2769
help= 'Unicode encoding for output'
2865
2866
option_registry.register(
2866
2867
Option('submit_to',
2867
2868
help='''Where submissions from this branch are mailed to.'''))
2869
option_registry.register(
2870
ListOption('suppress_warnings',
2872
help="List of warning classes to suppress."))
2873
option_registry.register(
2874
Option('validate_signatures_in_log', default=False,
2875
from_unicode=bool_from_store, invalid='warning',
2876
help='''Whether to validate signatures in bzr log.'''))
2869
2877
option_registry.register_lazy('ssl.ca_certs',
2870
2878
'bzrlib.transport.http._urllib2_wrappers', 'opt_ssl_ca_certs')
3045
3053
# get_mutable_section() call below.
3047
3055
# Apply the changes from the preserved dirty sections
3048
for dirty in dirty_sections:
3049
clean = self.get_mutable_section(dirty.id)
3056
for section_id, dirty in dirty_sections.iteritems():
3057
clean = self.get_mutable_section(section_id)
3050
3058
clean.apply_changes(dirty, self)
3051
3059
# Everything is clean now
3052
self.dirty_sections = []
3060
self.dirty_sections = {}
3054
3062
def save_changes(self):
3055
3063
"""Saves the Store to persistent storage if changes occurred.
3231
3238
except errors.NoSuchFile:
3232
3239
# The file doesn't exist, let's pretend it was empty
3233
3240
self._load_from_string('')
3241
if section_id in self.dirty_sections:
3242
# We already created a mutable section for this id
3243
return self.dirty_sections[section_id]
3234
3244
if section_id is None:
3235
3245
section = self._config_obj
3237
3247
section = self._config_obj.setdefault(section_id, {})
3238
3248
mutable_section = self.mutable_section_class(section_id, section)
3239
3249
# All mutable sections can become dirty
3240
self.dirty_sections.append(mutable_section)
3250
self.dirty_sections[section_id] = mutable_section
3241
3251
return mutable_section
3243
3253
def quote(self, value):
3382
3392
self.branch = branch
3383
3393
self.id = 'branch'
3385
def lock_write(self, token=None):
3386
return self.branch.lock_write(token)
3389
return self.branch.unlock()
3393
# We need to be able to override the undecorated implementation
3394
self.save_without_locking()
3396
def save_without_locking(self):
3397
super(BranchStore, self).save()
3400
3396
class ControlStore(LockableIniFileStore):
3623
3619
self.store = store
3624
3620
self.mutable_section_id = mutable_section_id
3626
def get(self, name, expand=None):
3622
def iter_sections(self):
3623
"""Iterate all the defined sections."""
3624
# Ensuring lazy loading is achieved by delaying section matching (which
3625
# implies querying the persistent storage) until it can't be avoided
3626
# anymore by using callables to describe (possibly empty) section
3628
for sections in self.sections_def:
3629
for store, section in sections():
3630
yield store, section
3632
def get(self, name, expand=True, convert=True):
3627
3633
"""Return the *first* option value found in the sections.
3629
3635
This is where we guarantee that sections coming from Store are loaded
3637
3643
:param expand: Whether options references should be expanded.
3645
:param convert: Whether the option value should be converted from
3646
unicode (do nothing for non-registered options).
3639
3648
:returns: The value of the option.
3641
3650
# FIXME: No caching of options nor sections yet -- vila 20110503
3643
expand = _get_expand_default_value()
3645
3652
found_store = None # Where the option value has been found
3646
3653
# If the option is registered, it may provide additional info about
3674
3681
value = opt.get_override()
3675
3682
value = expand_and_convert(value)
3676
3683
if value is None:
3677
# Ensuring lazy loading is achieved by delaying section matching
3678
# (which implies querying the persistent storage) until it can't be
3679
# avoided anymore by using callables to describe (possibly empty)
3681
for sections in self.sections_def:
3682
for store, section in sections():
3683
value = section.get(name)
3684
if value is not None:
3684
for store, section in self.iter_sections():
3685
value = section.get(name)
3687
3686
if value is not None:
3689
3689
value = expand_and_convert(value)
3690
3690
if opt is not None and value is None:
3937
3937
self.branch = branch
3939
def lock_write(self, token=None):
3940
return self.branch.lock_write(token)
3943
return self.branch.unlock()
3946
def set(self, name, value):
3947
super(BranchStack, self).set(name, value)
3948
# Unlocking the branch will trigger a store.save_changes() so the last
3949
# unlock saves all the changes.
3952
def remove(self, name):
3953
super(BranchStack, self).remove(name)
3954
# Unlocking the branch will trigger a store.save_changes() so the last
3955
# unlock saves all the changes.
3940
3958
class RemoteControlStack(_CompatibleStack):
3941
3959
"""Remote control-only options stack."""
3967
3985
self.branch = branch
3970
# Use a an empty dict to initialize an empty configobj avoiding all
3971
# parsing and encoding checks
3972
_quoting_config = configobj.ConfigObj(
3973
{}, encoding='utf-8', interpolation=False, list_values=True)
3987
def lock_write(self, token=None):
3988
return self.branch.lock_write(token)
3991
return self.branch.unlock()
3994
def set(self, name, value):
3995
super(BranchOnlyStack, self).set(name, value)
3996
# Force a write to persistent storage
3997
self.store.save_changes()
4000
def remove(self, name):
4001
super(BranchOnlyStack, self).remove(name)
4002
# Force a write to persistent storage
4003
self.store.save_changes()
3975
4006
class cmd_config(commands.Command):
3976
4007
__doc__ = """Display, set or remove a configuration option.
4038
4069
# Set the option value
4039
4070
self._set_config_option(name, value, directory, scope)
4041
def _get_stack(self, directory, scope=None):
4072
def _get_stack(self, directory, scope=None, write_access=False):
4042
4073
"""Get the configuration stack specified by ``directory`` and ``scope``.
4044
4075
:param directory: Where the configurations are derived from.
4046
4077
:param scope: A specific config to start from.
4079
:param write_access: Whether a write access to the stack will be
4048
4082
# FIXME: scope should allow access to plugin-specific stacks (even
4049
4083
# reduced to the plugin-specific store), related to
4065
4101
controldir.ControlDir.open_containing_tree_or_branch(
4104
self.add_cleanup(br.lock_write().unlock)
4067
4105
return br.get_config_stack()
4068
4106
except errors.NotBranchError:
4069
4107
return LocationStack(directory)
4109
def _quote_multiline(self, value):
4111
value = '"""' + value + '"""'
4071
4114
def _show_value(self, name, directory, scope):
4072
4115
conf = self._get_stack(directory, scope)
4073
value = conf.get(name, expand=True)
4116
value = conf.get(name, expand=True, convert=False)
4074
4117
if value is not None:
4075
4118
# Quote the value appropriately
4076
value = _quoting_config._quote(value)
4119
value = self._quote_multiline(value)
4077
4120
self.outf.write('%s\n' % (value,))
4079
4122
raise errors.NoSuchConfigOption(name)
4087
4130
cur_store_id = None
4088
4131
cur_section = None
4089
4132
conf = self._get_stack(directory, scope)
4090
for sections in conf.sections_def:
4091
for store, section in sections():
4092
for oname in section.iter_option_names():
4093
if name.search(oname):
4094
if cur_store_id != store.id:
4095
# Explain where the options are defined
4096
self.outf.write('%s:\n' % (store.id,))
4097
cur_store_id = store.id
4099
if (section.id is not None
4100
and cur_section != section.id):
4101
# Display the section id as it appears in the store
4102
# (None doesn't appear by definition)
4103
self.outf.write(' [%s]\n' % (section.id,))
4104
cur_section = section.id
4105
value = section.get(oname, expand=False)
4106
# Since we don't use the stack, we need to restore a
4109
opt = option_registry.get(oname)
4110
value = opt.convert_from_unicode(store, value)
4112
value = store.unquote(value)
4113
value = _quoting_config._quote(value)
4114
self.outf.write(' %s = %s\n' % (oname, value))
4133
for store, section in conf.iter_sections():
4134
for oname in section.iter_option_names():
4135
if name.search(oname):
4136
if cur_store_id != store.id:
4137
# Explain where the options are defined
4138
self.outf.write('%s:\n' % (store.id,))
4139
cur_store_id = store.id
4141
if (section.id is not None and cur_section != section.id):
4142
# Display the section id as it appears in the store
4143
# (None doesn't appear by definition)
4144
self.outf.write(' [%s]\n' % (section.id,))
4145
cur_section = section.id
4146
value = section.get(oname, expand=False)
4147
# Quote the value appropriately
4148
value = self._quote_multiline(value)
4149
self.outf.write(' %s = %s\n' % (oname, value))
4116
4151
def _set_config_option(self, name, value, directory, scope):
4117
conf = self._get_stack(directory, scope)
4152
conf = self._get_stack(directory, scope, write_access=True)
4118
4153
conf.set(name, value)
4120
4155
def _remove_config_option(self, name, directory, scope):
4121
4156
if name is None:
4122
4157
raise errors.BzrCommandError(
4123
4158
'--remove expects an option to remove.')
4124
conf = self._get_stack(directory, scope)
4159
conf = self._get_stack(directory, scope, write_access=True)
4126
4161
conf.remove(name)
4127
4162
except KeyError: