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
log_format - This options set the default log format. Options are long,
54
short, line, or a plugin can register new formats
52
log_format - this option sets the default log format. Possible values are
53
long, short, line, or a plugin can register new formats.
56
55
In bazaar.conf you can also define aliases in the ALIASES sections, example
67
from bzrlib.lazy_import import lazy_import
68
lazy_import(globals(), """
69
70
from fnmatch import fnmatch
72
from StringIO import StringIO
73
import bzrlib.errors as errors
74
from bzrlib.osutils import pathjoin
75
from bzrlib.trace import mutter
76
80
import bzrlib.util.configobj.configobj as configobj
77
from StringIO import StringIO
83
from bzrlib.trace import mutter, warning
79
86
CHECK_IF_POSSIBLE=0
84
96
class ConfigObj(configobj.ConfigObj):
86
98
def get_bool(self, section, key):
193
213
return CHECK_IF_POSSIBLE
215
def signing_policy(self):
216
"""What is the current policy for signature checking?."""
217
policy = self._get_signing_policy()
218
if policy is not None:
220
return SIGN_WHEN_REQUIRED
195
222
def signature_needed(self):
196
223
"""Is a signature needed when committing ?."""
197
policy = self._get_signature_checking()
198
if policy == CHECK_ALWAYS:
224
policy = self._get_signing_policy()
226
policy = self._get_signature_checking()
227
if policy is not None:
228
warning("Please use create_signatures, not check_signatures "
229
"to set signing policy.")
230
if policy == CHECK_ALWAYS:
232
elif policy == SIGN_ALWAYS:
222
262
raise errors.ParseConfigError(e.errors, e.config.filename)
223
263
return self._parser
265
def _get_matching_sections(self):
266
"""Return an ordered list of (section_name, extra_path) pairs.
268
If the section contains inherited configuration, extra_path is
269
a string containing the additional path components.
271
section = self._get_section()
272
if section is not None:
273
return [(section, '')]
225
277
def _get_section(self):
226
278
"""Override this to define the section used by the config."""
233
285
return self._string_to_signature_policy(policy)
287
def _get_signing_policy(self):
288
"""See Config._get_signing_policy"""
289
policy = self._get_user_option('create_signatures')
291
return self._string_to_signing_policy(policy)
235
293
def _get_user_id(self):
236
294
"""Get the user id from the 'email' key in the current section."""
237
295
return self._get_user_option('email')
239
297
def _get_user_option(self, option_name):
240
298
"""See Config._get_user_option."""
242
return self._get_parser().get_value(self._get_section(),
299
for (section, extra_path) in self._get_matching_sections():
301
return self._get_parser().get_value(section, option_name)
247
307
def _gpg_signing_command(self):
248
308
"""See Config.gpg_signing_command."""
272
332
raise errors.BzrError("Invalid signatures policy '%s'"
273
333
% signature_string)
335
def _string_to_signing_policy(self, signature_string):
336
"""Convert a string to a signing policy."""
337
if signature_string.lower() == 'when-required':
338
return SIGN_WHEN_REQUIRED
339
if signature_string.lower() == 'never':
341
if signature_string.lower() == 'always':
343
raise errors.BzrError("Invalid signing policy '%s'"
275
346
def _get_alias(self, value):
277
348
return self._get_parser().get_value("ALIASES",
289
363
def __init__(self):
290
364
super(GlobalConfig, self).__init__(config_filename)
366
def set_user_option(self, option, value):
367
"""Save option and its value in the configuration."""
368
# FIXME: RBC 20051029 This should refresh the parser and also take a
369
# file lock on bazaar.conf.
370
conf_dir = os.path.dirname(self._get_filename())
371
ensure_config_dir_exists(conf_dir)
372
if 'DEFAULT' not in self._get_parser():
373
self._get_parser()['DEFAULT'] = {}
374
self._get_parser()['DEFAULT'][option] = value
375
f = open(self._get_filename(), 'wb')
376
self._get_parser().write(f)
293
380
class LocationConfig(IniBasedConfig):
294
381
"""A configuration object that gives the policy for a location."""
296
383
def __init__(self, location):
297
super(LocationConfig, self).__init__(branches_config_filename)
298
self._global_config = None
384
name_generator = locations_config_filename
385
if (not os.path.exists(name_generator()) and
386
os.path.exists(branches_config_filename())):
387
if sys.platform == 'win32':
388
warning('Please rename %s to %s'
389
% (branches_config_filename(),
390
locations_config_filename()))
392
warning('Please rename ~/.bazaar/branches.conf'
393
' to ~/.bazaar/locations.conf')
394
name_generator = branches_config_filename
395
super(LocationConfig, self).__init__(name_generator)
396
# local file locations are looked up by local path, rather than
397
# by file url. This is because the config file is a user
398
# file, and we would rather not expose the user to file urls.
399
if location.startswith('file://'):
400
location = urlutils.local_path_from_url(location)
299
401
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
def _get_section(self):
307
"""Get the section we should look in for config items.
309
Returns None if none exists.
310
TODO: perhaps return a NullSection that thunks through to the
403
def _get_matching_sections(self):
404
"""Return an ordered list of section names matching this location."""
313
405
sections = self._get_parser()
314
406
location_names = self.location.split('/')
315
407
if self.location.endswith('/'):
316
408
del location_names[-1]
318
410
for section in sections:
319
section_names = section.split('/')
411
# location is a local path if possible, so we need
412
# to convert 'file://' urls to local paths if necessary.
413
# This also avoids having file:///path be a more exact
414
# match than '/path'.
415
if section.startswith('file://'):
416
section_path = urlutils.local_path_from_url(section)
418
section_path = section
419
section_names = section_path.split('/')
320
420
if section.endswith('/'):
321
421
del section_names[-1]
322
422
names = zip(location_names, section_names)
341
matches.append((len(section_names), section))
441
matches.append((len(section_names), section,
442
'/'.join(location_names[len(section_names):])))
344
443
matches.sort(reverse=True)
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()
445
for (length, section, extra_path) in matches:
446
sections.append((section, extra_path))
447
# should we stop looking for parent configs here?
449
if self._get_parser()[section].as_bool('ignore_parents'):
389
455
def set_user_option(self, option, value):
390
456
"""Save option and its value in the configuration."""
391
457
# FIXME: RBC 20051029 This should refresh the parser and also take a
392
# file lock on branches.conf.
458
# file lock on locations.conf.
393
459
conf_dir = os.path.dirname(self._get_filename())
394
460
ensure_config_dir_exists(conf_dir)
395
461
location = self.location
407
473
class BranchConfig(Config):
408
474
"""A configuration object giving the policy for a branch."""
476
def _get_branch_data_config(self):
477
if self._branch_data_config is None:
478
self._branch_data_config = TreeConfig(self.branch)
479
return self._branch_data_config
410
481
def _get_location_config(self):
411
482
if self._location_config is None:
412
483
self._location_config = LocationConfig(self.branch.base)
413
484
return self._location_config
486
def _get_global_config(self):
487
if self._global_config is None:
488
self._global_config = GlobalConfig()
489
return self._global_config
491
def _get_best_value(self, option_name):
492
"""This returns a user option from local, tree or global config.
494
They are tried in that order. Use get_safe_value if trusted values
497
for source in self.option_sources:
498
value = getattr(source(), option_name)()
499
if value is not None:
503
def _get_safe_value(self, option_name):
504
"""This variant of get_best_value never returns untrusted values.
506
It does not return values from the branch data, because the branch may
507
not be controlled by the user.
509
We may wish to allow locations.conf to control whether branches are
510
trusted in the future.
512
for source in (self._get_location_config, self._get_global_config):
513
value = getattr(source(), option_name)()
514
if value is not None:
415
518
def _get_user_id(self):
416
519
"""Return the full user id for the branch.
426
529
except errors.NoSuchFile, e:
429
return self._get_location_config()._get_user_id()
532
return self._get_best_value('_get_user_id')
431
534
def _get_signature_checking(self):
432
535
"""See Config._get_signature_checking."""
433
return self._get_location_config()._get_signature_checking()
536
return self._get_best_value('_get_signature_checking')
538
def _get_signing_policy(self):
539
"""See Config._get_signing_policy."""
540
return self._get_best_value('_get_signing_policy')
435
542
def _get_user_option(self, option_name):
436
543
"""See Config._get_user_option."""
437
return self._get_location_config()._get_user_option(option_name)
544
for source in self.option_sources:
545
value = source()._get_user_option(option_name)
546
if value is not None:
550
def set_user_option(self, name, value, local=False):
552
self._get_location_config().set_user_option(name, value)
554
self._get_branch_data_config().set_option(value, name)
439
557
def _gpg_signing_command(self):
440
558
"""See Config.gpg_signing_command."""
441
return self._get_location_config()._gpg_signing_command()
559
return self._get_safe_value('_gpg_signing_command')
443
561
def __init__(self, branch):
444
562
super(BranchConfig, self).__init__()
445
563
self._location_config = None
564
self._branch_data_config = None
565
self._global_config = None
446
566
self.branch = branch
567
self.option_sources = (self._get_location_config,
568
self._get_branch_data_config,
569
self._get_global_config)
448
571
def _post_commit(self):
449
572
"""See Config.post_commit."""
450
return self._get_location_config()._post_commit()
573
return self._get_safe_value('_post_commit')
575
def _get_nickname(self):
576
value = self._get_explicit_nickname()
577
if value is not None:
579
return self.branch.base.split('/')[-2]
581
def has_explicit_nickname(self):
582
"""Return true if a nickname has been explicitly assigned."""
583
return self._get_explicit_nickname() is not None
585
def _get_explicit_nickname(self):
586
return self._get_best_value('_get_nickname')
452
588
def _log_format(self):
453
589
"""See Config.log_format."""
454
return self._get_location_config()._log_format()
590
return self._get_best_value('_log_format')
457
593
def ensure_config_dir_exists(path=None):
487
623
base = os.environ.get('HOME', None)
489
raise BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
490
return pathjoin(base, 'bazaar', '2.0')
625
raise errors.BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
626
return osutils.pathjoin(base, 'bazaar', '2.0')
492
628
# cygwin, linux, and darwin all have a $HOME directory
494
630
base = os.path.expanduser("~")
495
return pathjoin(base, ".bazaar")
631
return osutils.pathjoin(base, ".bazaar")
498
634
def config_filename():
499
635
"""Return per-user configuration ini file filename."""
500
return pathjoin(config_dir(), 'bazaar.conf')
636
return osutils.pathjoin(config_dir(), 'bazaar.conf')
503
639
def branches_config_filename():
504
640
"""Return per-user configuration ini file filename."""
505
return pathjoin(config_dir(), 'branches.conf')
641
return osutils.pathjoin(config_dir(), 'branches.conf')
644
def locations_config_filename():
645
"""Return per-user configuration ini file filename."""
646
return osutils.pathjoin(config_dir(), 'locations.conf')
649
def user_ignore_config_filename():
650
"""Return the user default ignore filename"""
651
return osutils.pathjoin(config_dir(), 'ignore')
508
654
def _auto_user_id():
525
671
uid = os.getuid()
526
672
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)
674
# we try utf-8 first, because on many variants (like Linux),
675
# /etc/passwd "should" be in utf-8, and because it's unlikely to give
676
# false positives. (many users will have their user encoding set to
677
# latin-1, which cannot raise UnicodeError.)
679
gecos = w.pw_gecos.decode('utf-8')
683
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
684
encoding = bzrlib.user_encoding
686
raise errors.BzrCommandError('Unable to determine your name. '
687
'Use "bzr whoami" to set it.')
689
username = w.pw_name.decode(encoding)
691
raise errors.BzrCommandError('Unable to determine your name. '
692
'Use "bzr whoami" to set it.')
536
694
comma = gecos.find(',')
565
723
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
567
raise errors.BzrError("%r doesn't seem to contain "
568
"a reasonable email address" % e)
725
raise errors.NoEmailInUsername(e)
569
726
return m.group(0)
571
class TreeConfig(object):
729
class TreeConfig(IniBasedConfig):
572
730
"""Branch configuration data associated with its contents, not location"""
573
731
def __init__(self, branch):
574
732
self.branch = branch
734
def _get_parser(self, file=None):
736
return IniBasedConfig._get_parser(file)
737
return self._get_config()
576
739
def _get_config(self):
578
741
obj = ConfigObj(self.branch.control_files.get('branch.conf'),