29
29
gpg_signing_command=name-of-program
30
30
log_format=name-of-format
32
in branches.conf, you specify the url of a branch and options for it.
32
in locations.conf, you specify the url of a branch and options for it.
33
33
Wildcards may be used - * and ? as normal in shell completion. Options
34
set in both bazaar.conf and branches.conf are overriden by the branches.conf
34
set in both bazaar.conf and locations.conf are overridden by the locations.conf
36
36
[/home/robertc/source]
37
37
recurse=False|True(default)
39
check_signatures= as abive
39
check_signatures= as above
40
40
create_signatures= as above.
42
42
explanation of options
49
49
create_signatures - this option controls whether bzr will always create
50
50
gpg signatures, never create them, or create them if the
51
51
branch is configured to require them.
52
NB: This option is planned, but not implemented yet.
53
52
log_format - This options set the default log format. Options are long,
54
53
short, line, or a plugin can register new formats
66
from fnmatch import fnmatch
69
from fnmatch import fnmatch
70
from StringIO import StringIO
73
73
import bzrlib.errors as errors
74
74
from bzrlib.osutils import pathjoin
75
from bzrlib.trace import mutter
75
from bzrlib.trace import mutter, warning
76
76
import bzrlib.util.configobj.configobj as configobj
77
from StringIO import StringIO
79
79
CHECK_IF_POSSIBLE=0
84
89
class ConfigObj(configobj.ConfigObj):
86
91
def get_bool(self, section, key):
106
111
def _get_signature_checking(self):
107
112
"""Template method to override signature checking policy."""
114
def _get_signing_policy(self):
115
"""Template method to override signature creation policy."""
109
117
def _get_user_option(self, option_name):
110
118
"""Template method to provide a user option."""
193
201
return CHECK_IF_POSSIBLE
203
def signing_policy(self):
204
"""What is the current policy for signature checking?."""
205
policy = self._get_signing_policy()
206
if policy is not None:
208
return SIGN_WHEN_REQUIRED
195
210
def signature_needed(self):
196
211
"""Is a signature needed when committing ?."""
197
policy = self._get_signature_checking()
198
if policy == CHECK_ALWAYS:
212
policy = self._get_signing_policy()
214
policy = self._get_signature_checking()
215
if policy is not None:
216
warning("Please use create_signatures, not check_signatures "
217
"to set signing policy.")
218
if policy == CHECK_ALWAYS:
220
elif policy == SIGN_ALWAYS:
233
261
return self._string_to_signature_policy(policy)
263
def _get_signing_policy(self):
264
"""See Config._get_signing_policy"""
265
policy = self._get_user_option('create_signatures')
267
return self._string_to_signing_policy(policy)
235
269
def _get_user_id(self):
236
270
"""Get the user id from the 'email' key in the current section."""
237
271
return self._get_user_option('email')
272
306
raise errors.BzrError("Invalid signatures policy '%s'"
273
307
% signature_string)
309
def _string_to_signing_policy(self, signature_string):
310
"""Convert a string to a signing policy."""
311
if signature_string.lower() == 'when-required':
312
return SIGN_WHEN_REQUIRED
313
if signature_string.lower() == 'never':
315
if signature_string.lower() == 'always':
317
raise errors.BzrError("Invalid signing policy '%s'"
275
320
def _get_alias(self, value):
277
322
return self._get_parser().get_value("ALIASES",
289
337
def __init__(self):
290
338
super(GlobalConfig, self).__init__(config_filename)
340
def set_user_option(self, option, value):
341
"""Save option and its value in the configuration."""
342
# FIXME: RBC 20051029 This should refresh the parser and also take a
343
# file lock on bazaar.conf.
344
conf_dir = os.path.dirname(self._get_filename())
345
ensure_config_dir_exists(conf_dir)
346
if 'DEFAULT' not in self._get_parser():
347
self._get_parser()['DEFAULT'] = {}
348
self._get_parser()['DEFAULT'][option] = value
349
f = open(self._get_filename(), 'wb')
350
self._get_parser().write(f)
293
354
class LocationConfig(IniBasedConfig):
294
355
"""A configuration object that gives the policy for a location."""
296
357
def __init__(self, location):
297
super(LocationConfig, self).__init__(branches_config_filename)
298
self._global_config = None
358
name_generator = locations_config_filename
359
if (not os.path.exists(name_generator()) and
360
os.path.exists(branches_config_filename())):
361
if sys.platform == 'win32':
362
warning('Please rename %s to %s'
363
% (branches_config_filename(),
364
locations_config_filename()))
366
warning('Please rename ~/.bazaar/branches.conf'
367
' to ~/.bazaar/locations.conf')
368
name_generator = branches_config_filename
369
super(LocationConfig, self).__init__(name_generator)
299
370
self.location = location
301
def _get_global_config(self):
302
if self._global_config is None:
303
self._global_config = GlobalConfig()
304
return self._global_config
306
372
def _get_section(self):
307
373
"""Get the section we should look in for config items.
344
410
matches.sort(reverse=True)
345
411
return matches[0][1]
347
def _gpg_signing_command(self):
348
"""See Config.gpg_signing_command."""
349
command = super(LocationConfig, self)._gpg_signing_command()
350
if command is not None:
352
return self._get_global_config()._gpg_signing_command()
354
def _log_format(self):
355
"""See Config.log_format."""
356
command = super(LocationConfig, self)._log_format()
357
if command is not None:
359
return self._get_global_config()._log_format()
361
def _get_user_id(self):
362
user_id = super(LocationConfig, self)._get_user_id()
363
if user_id is not None:
365
return self._get_global_config()._get_user_id()
367
def _get_user_option(self, option_name):
368
"""See Config._get_user_option."""
369
option_value = super(LocationConfig,
370
self)._get_user_option(option_name)
371
if option_value is not None:
373
return self._get_global_config()._get_user_option(option_name)
375
def _get_signature_checking(self):
376
"""See Config._get_signature_checking."""
377
check = super(LocationConfig, self)._get_signature_checking()
378
if check is not None:
380
return self._get_global_config()._get_signature_checking()
382
def _post_commit(self):
383
"""See Config.post_commit."""
384
hook = self._get_user_option('post_commit')
387
return self._get_global_config()._post_commit()
389
413
def set_user_option(self, option, value):
390
414
"""Save option and its value in the configuration."""
391
415
# FIXME: RBC 20051029 This should refresh the parser and also take a
392
# file lock on branches.conf.
416
# file lock on locations.conf.
393
417
conf_dir = os.path.dirname(self._get_filename())
394
418
ensure_config_dir_exists(conf_dir)
395
419
location = self.location
407
431
class BranchConfig(Config):
408
432
"""A configuration object giving the policy for a branch."""
434
def _get_branch_data_config(self):
435
if self._branch_data_config is None:
436
self._branch_data_config = TreeConfig(self.branch)
437
return self._branch_data_config
410
439
def _get_location_config(self):
411
440
if self._location_config is None:
412
441
self._location_config = LocationConfig(self.branch.base)
413
442
return self._location_config
444
def _get_global_config(self):
445
if self._global_config is None:
446
self._global_config = GlobalConfig()
447
return self._global_config
449
def _get_best_value(self, option_name):
450
"""This returns a user option from local, tree or global config.
452
They are tried in that order. Use get_safe_value if trusted values
455
for source in self.option_sources:
456
value = getattr(source(), option_name)()
457
if value is not None:
461
def _get_safe_value(self, option_name):
462
"""This variant of get_best_value never returns untrusted values.
464
It does not return values from the branch data, because the branch may
465
not be controlled by the user.
467
We may wish to allow locations.conf to control whether branches are
468
trusted in the future.
470
for source in (self._get_location_config, self._get_global_config):
471
value = getattr(source(), option_name)()
472
if value is not None:
415
476
def _get_user_id(self):
416
477
"""Return the full user id for the branch.
426
487
except errors.NoSuchFile, e:
429
return self._get_location_config()._get_user_id()
490
return self._get_best_value('_get_user_id')
431
492
def _get_signature_checking(self):
432
493
"""See Config._get_signature_checking."""
433
return self._get_location_config()._get_signature_checking()
494
return self._get_best_value('_get_signature_checking')
496
def _get_signing_policy(self):
497
"""See Config._get_signing_policy."""
498
return self._get_best_value('_get_signing_policy')
435
500
def _get_user_option(self, option_name):
436
501
"""See Config._get_user_option."""
437
return self._get_location_config()._get_user_option(option_name)
502
for source in self.option_sources:
503
value = source()._get_user_option(option_name)
504
if value is not None:
508
def set_user_option(self, name, value, local=False):
510
self._get_location_config().set_user_option(name, value)
512
self._get_branch_data_config().set_option(value, name)
439
515
def _gpg_signing_command(self):
440
516
"""See Config.gpg_signing_command."""
441
return self._get_location_config()._gpg_signing_command()
517
return self._get_safe_value('_gpg_signing_command')
443
519
def __init__(self, branch):
444
520
super(BranchConfig, self).__init__()
445
521
self._location_config = None
522
self._branch_data_config = None
523
self._global_config = None
446
524
self.branch = branch
525
self.option_sources = (self._get_location_config,
526
self._get_branch_data_config,
527
self._get_global_config)
448
529
def _post_commit(self):
449
530
"""See Config.post_commit."""
450
return self._get_location_config()._post_commit()
531
return self._get_safe_value('_post_commit')
533
def _get_nickname(self):
534
value = self._get_explicit_nickname()
535
if value is not None:
537
return self.branch.base.split('/')[-2]
539
def has_explicit_nickname(self):
540
"""Return true if a nickname has been explicitly assigned."""
541
return self._get_explicit_nickname() is not None
543
def _get_explicit_nickname(self):
544
return self._get_best_value('_get_nickname')
452
546
def _log_format(self):
453
547
"""See Config.log_format."""
454
return self._get_location_config()._log_format()
548
return self._get_best_value('_log_format')
457
551
def ensure_config_dir_exists(path=None):
487
581
base = os.environ.get('HOME', None)
489
raise BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
583
raise errors.BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
490
584
return pathjoin(base, 'bazaar', '2.0')
492
586
# cygwin, linux, and darwin all have a $HOME directory
525
624
uid = os.getuid()
526
625
w = pwd.getpwuid(uid)
529
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
530
username = w.pw_name.decode(bzrlib.user_encoding)
531
except UnicodeDecodeError:
532
# We're using pwd, therefore we're on Unix, so /etc/passwd is ok.
533
raise errors.BzrError("Can't decode username in " \
534
"/etc/passwd as %s." % bzrlib.user_encoding)
627
# we try utf-8 first, because on many variants (like Linux),
628
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
629
# false positives. (many users will have their user encoding set to
630
# latin-1, which cannot raise UnicodeError.)
632
gecos = w.pw_gecos.decode('utf-8')
636
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
637
encoding = bzrlib.user_encoding
639
raise errors.BzrCommandError('Unable to determine your name. '
640
'Use "bzr whoami" to set it.')
642
username = w.pw_name.decode(encoding)
644
raise errors.BzrCommandError('Unable to determine your name. '
645
'Use "bzr whoami" to set it.')
536
647
comma = gecos.find(',')
568
679
"a reasonable email address" % e)
569
680
return m.group(0)
571
class TreeConfig(object):
683
class TreeConfig(IniBasedConfig):
572
684
"""Branch configuration data associated with its contents, not location"""
573
685
def __init__(self, branch):
574
686
self.branch = branch
688
def _get_parser(self, file=None):
690
return IniBasedConfig._get_parser(file)
691
return self._get_config()
576
693
def _get_config(self):
578
695
obj = ConfigObj(self.branch.control_files.get('branch.conf'),