27
27
check_signatures=require|ignore|check-available(default)
28
28
create_signatures=always|never|when-required(default)
29
29
gpg_signing_command=name-of-program
30
log_format=name-of-format
31
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.
32
33
Wildcards may be used - * and ? as normal in shell completion. Options
33
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
35
36
[/home/robertc/source]
36
37
recurse=False|True(default)
38
check_signatures= as abive
39
check_signatures= as above
39
40
create_signatures= as above.
41
42
explanation of options
48
49
create_signatures - this option controls whether bzr will always create
49
50
gpg signatures, never create them, or create them if the
50
51
branch is configured to require them.
51
NB: This option is planned, but not implemented yet.
52
log_format - this option sets the default log format. Possible values are
53
long, short, line, or a plugin can register new formats.
55
In bazaar.conf you can also define aliases in the ALIASES sections, example
58
lastlog=log --line -r-10..-1
59
ll=log --line -r-10..-1
54
from ConfigParser import ConfigParser
67
from bzrlib.lazy_import import lazy_import
68
lazy_import(globals(), """
56
70
from fnmatch import fnmatch
72
from StringIO import StringIO
61
import bzrlib.errors as errors
80
import bzrlib.util.configobj.configobj as configobj
83
from bzrlib.trace import mutter, warning
64
86
CHECK_IF_POSSIBLE=0
95
140
"""See gpg_signing_command()."""
143
def log_format(self):
144
"""What log format should be used"""
145
result = self._log_format()
150
def _log_format(self):
151
"""See log_format()."""
98
154
def __init__(self):
99
155
super(Config, self).__init__()
157
def post_commit(self):
158
"""An ordered list of python functions to call.
160
Each function takes branch, rev_id as parameters.
162
return self._post_commit()
164
def _post_commit(self):
165
"""See Config.post_commit."""
101
168
def user_email(self):
102
169
"""Return just the email component of a username."""
104
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
106
raise BzrError("%r doesn't seem to contain "
107
"a reasonable email address" % e)
170
return extract_email_address(self.username())
110
172
def username(self):
111
173
"""Return email-style username.
113
175
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
115
$BZREMAIL can be set to override this, then
177
$BZR_EMAIL can be set to override this (as well as the
178
deprecated $BZREMAIL), then
116
179
the concrete policy type is checked, and finally
118
but if none is found, a reasonable default is (hopefully)
181
If none is found, a reasonable default is (hopefully)
121
184
TODO: Check it's reasonably well-formed.
186
v = os.environ.get('BZR_EMAIL')
188
return v.decode(bzrlib.user_encoding)
123
189
v = os.environ.get('BZREMAIL')
191
warning('BZREMAIL is deprecated in favor of BZR_EMAIL. Please update your configuration.')
125
192
return v.decode(bzrlib.user_encoding)
127
194
v = self._get_user_id()
146
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
148
222
def signature_needed(self):
149
223
"""Is a signature needed when committing ?."""
150
policy = self._get_signature_checking()
151
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:
236
def get_alias(self, value):
237
return self._get_alias(value)
239
def _get_alias(self, value):
242
def get_nickname(self):
243
return self._get_nickname()
245
def _get_nickname(self):
156
249
class IniBasedConfig(Config):
157
250
"""A configuration policy that draws from ini files."""
159
252
def _get_parser(self, file=None):
160
253
if self._parser is not None:
161
254
return self._parser
162
parser = ConfigParser()
166
parser.read([self._get_filename()])
167
self._parser = parser
256
input = self._get_filename()
260
self._parser = ConfigObj(input, encoding='utf-8')
261
except configobj.ConfigObjError, e:
262
raise errors.ParseConfigError(e.errors, e.config.filename)
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, '')]
170
277
def _get_section(self):
171
278
"""Override this to define the section used by the config."""
174
281
def _get_signature_checking(self):
175
282
"""See Config._get_signature_checking."""
176
section = self._get_section()
179
if self._get_parser().has_option(section, 'check_signatures'):
180
return self._string_to_signature_policy(
181
self._get_parser().get(section, 'check_signatures'))
283
policy = self._get_user_option('check_signatures')
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)
183
293
def _get_user_id(self):
184
294
"""Get the user id from the 'email' key in the current section."""
185
section = self._get_section()
186
if section is not None:
187
if self._get_parser().has_option(section, 'email'):
188
return self._get_parser().get(section, 'email')
295
return self._get_user_option('email')
190
297
def _get_user_option(self, option_name):
191
298
"""See Config._get_user_option."""
192
section = self._get_section()
193
if section is not None:
194
if self._get_parser().has_option(section, option_name):
195
return self._get_parser().get(section, option_name)
299
for (section, extra_path) in self._get_matching_sections():
301
return self._get_parser().get_value(section, option_name)
197
307
def _gpg_signing_command(self):
198
308
"""See Config.gpg_signing_command."""
199
section = self._get_section()
200
if section is not None:
201
if self._get_parser().has_option(section, 'gpg_signing_command'):
202
return self._get_parser().get(section, 'gpg_signing_command')
309
return self._get_user_option('gpg_signing_command')
311
def _log_format(self):
312
"""See Config.log_format."""
313
return self._get_user_option('log_format')
204
315
def __init__(self, get_filename):
205
316
super(IniBasedConfig, self).__init__()
206
317
self._get_filename = get_filename
207
318
self._parser = None
320
def _post_commit(self):
321
"""See Config.post_commit."""
322
return self._get_user_option('post_commit')
209
324
def _string_to_signature_policy(self, signature_string):
210
325
"""Convert a string to a signing policy."""
217
332
raise errors.BzrError("Invalid signatures policy '%s'"
218
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'"
346
def _get_alias(self, value):
348
return self._get_parser().get_value("ALIASES",
353
def _get_nickname(self):
354
return self.get_user_option('nickname')
221
357
class GlobalConfig(IniBasedConfig):
222
358
"""The configuration that should be used for a specific location."""
224
360
def get_editor(self):
225
if self._get_parser().has_option(self._get_section(), 'editor'):
226
return self._get_parser().get(self._get_section(), 'editor')
361
return self._get_user_option('editor')
228
363
def __init__(self):
229
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)
232
380
class LocationConfig(IniBasedConfig):
233
381
"""A configuration object that gives the policy for a location."""
235
383
def __init__(self, location):
236
super(LocationConfig, self).__init__(branches_config_filename)
237
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)
238
401
self.location = location
240
def _get_global_config(self):
241
if self._global_config is None:
242
self._global_config = GlobalConfig()
243
return self._global_config
245
def _get_section(self):
246
"""Get the section we should look in for config items.
248
Returns None if none exists.
249
TODO: perhaps return a NullSection that thunks through to the
252
sections = self._get_parser().sections()
403
def _get_matching_sections(self):
404
"""Return an ordered list of section names matching this location."""
405
sections = self._get_parser()
253
406
location_names = self.location.split('/')
254
407
if self.location.endswith('/'):
255
408
del location_names[-1]
257
410
for section in sections:
258
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('/')
259
420
if section.endswith('/'):
260
421
del section_names[-1]
261
422
names = zip(location_names, section_names)
273
434
# if path is longer, and recurse is not true, no match
274
435
if len(section_names) < len(location_names):
275
if (self._get_parser().has_option(section, 'recurse')
276
and not self._get_parser().getboolean(section, 'recurse')):
278
matches.append((len(section_names), section))
437
if not self._get_parser()[section].as_bool('recurse'):
441
matches.append((len(section_names), section,
442
'/'.join(location_names[len(section_names):])))
281
443
matches.sort(reverse=True)
284
def _gpg_signing_command(self):
285
"""See Config.gpg_signing_command."""
286
command = super(LocationConfig, self)._gpg_signing_command()
287
if command is not None:
289
return self._get_global_config()._gpg_signing_command()
291
def _get_user_id(self):
292
user_id = super(LocationConfig, self)._get_user_id()
293
if user_id is not None:
295
return self._get_global_config()._get_user_id()
297
def _get_user_option(self, option_name):
298
"""See Config._get_user_option."""
299
option_value = super(LocationConfig,
300
self)._get_user_option(option_name)
301
if option_value is not None:
303
return self._get_global_config()._get_user_option(option_name)
305
def _get_signature_checking(self):
306
"""See Config._get_signature_checking."""
307
check = super(LocationConfig, self)._get_signature_checking()
308
if check is not None:
310
return self._get_global_config()._get_signature_checking()
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'):
455
def set_user_option(self, option, value):
456
"""Save option and its value in the configuration."""
457
# FIXME: RBC 20051029 This should refresh the parser and also take a
458
# file lock on locations.conf.
459
conf_dir = os.path.dirname(self._get_filename())
460
ensure_config_dir_exists(conf_dir)
461
location = self.location
462
if location.endswith('/'):
463
location = location[:-1]
464
if (not location in self._get_parser() and
465
not location + '/' in self._get_parser()):
466
self._get_parser()[location]={}
467
elif location + '/' in self._get_parser():
468
location = location + '/'
469
self._get_parser()[location][option]=value
470
self._get_parser().write(file(self._get_filename(), 'wb'))
313
473
class BranchConfig(Config):
314
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
316
481
def _get_location_config(self):
317
482
if self._location_config is None:
318
483
self._location_config = LocationConfig(self.branch.base)
319
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:
321
518
def _get_user_id(self):
322
519
"""Return the full user id for the branch.
325
522
This is looked up in the email controlfile for the branch.
328
return (self.branch.controlfile("email", "r")
525
return (self.branch.control_files.get_utf8("email")
330
527
.decode(bzrlib.user_encoding)
332
529
except errors.NoSuchFile, e:
335
return self._get_location_config()._get_user_id()
532
return self._get_best_value('_get_user_id')
337
534
def _get_signature_checking(self):
338
535
"""See Config._get_signature_checking."""
339
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')
341
542
def _get_user_option(self, option_name):
342
543
"""See Config._get_user_option."""
343
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)
345
557
def _gpg_signing_command(self):
346
558
"""See Config.gpg_signing_command."""
347
return self._get_location_config()._gpg_signing_command()
559
return self._get_safe_value('_gpg_signing_command')
349
561
def __init__(self, branch):
350
562
super(BranchConfig, self).__init__()
351
563
self._location_config = None
564
self._branch_data_config = None
565
self._global_config = None
352
566
self.branch = branch
567
self.option_sources = (self._get_location_config,
568
self._get_branch_data_config,
569
self._get_global_config)
571
def _post_commit(self):
572
"""See 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')
588
def _log_format(self):
589
"""See Config.log_format."""
590
return self._get_best_value('_log_format')
593
def ensure_config_dir_exists(path=None):
594
"""Make sure a configuration directory exists.
595
This makes sure that the directory exists.
596
On windows, since configuration directories are 2 levels deep,
597
it makes sure both the directory and the parent directory exists.
601
if not os.path.isdir(path):
602
if sys.platform == 'win32':
603
parent_dir = os.path.dirname(path)
604
if not os.path.isdir(parent_dir):
605
mutter('creating config parent directory: %r', parent_dir)
607
mutter('creating config directory: %r', path)
355
611
def config_dir():
360
616
TODO: Global option --config-dir to override this.
362
return os.path.join(os.path.expanduser("~"), ".bazaar")
618
base = os.environ.get('BZR_HOME', None)
619
if sys.platform == 'win32':
621
base = os.environ.get('APPDATA', None)
623
base = os.environ.get('HOME', None)
625
raise errors.BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
626
return osutils.pathjoin(base, 'bazaar', '2.0')
628
# cygwin, linux, and darwin all have a $HOME directory
630
base = os.path.expanduser("~")
631
return osutils.pathjoin(base, ".bazaar")
365
634
def config_filename():
366
635
"""Return per-user configuration ini file filename."""
367
return os.path.join(config_dir(), 'bazaar.conf')
636
return osutils.pathjoin(config_dir(), 'bazaar.conf')
370
639
def branches_config_filename():
371
640
"""Return per-user configuration ini file filename."""
372
return os.path.join(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')
375
654
def _auto_user_id():
392
671
uid = os.getuid()
393
672
w = pwd.getpwuid(uid)
394
gecos = w.pw_gecos.decode(bzrlib.user_encoding)
395
username = w.pw_name.decode(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.')
396
694
comma = gecos.find(',')
421
723
m = re.search(r'[\w+.-]+@[\w+.-]+', e)
423
raise BzrError("%r doesn't seem to contain "
424
"a reasonable email address" % e)
725
raise errors.NoEmailInUsername(e)
425
726
return m.group(0)
729
class TreeConfig(IniBasedConfig):
730
"""Branch configuration data associated with its contents, not location"""
731
def __init__(self, branch):
734
def _get_parser(self, file=None):
736
return IniBasedConfig._get_parser(file)
737
return self._get_config()
739
def _get_config(self):
741
obj = ConfigObj(self.branch.control_files.get('branch.conf'),
743
except errors.NoSuchFile:
744
obj = ConfigObj(encoding='utf=8')
747
def get_option(self, name, section=None, default=None):
748
self.branch.lock_read()
750
obj = self._get_config()
752
if section is not None:
761
def set_option(self, value, name, section=None):
762
"""Set a per-branch configuration option"""
763
self.branch.lock_write()
765
cfg_obj = self._get_config()
770
obj = cfg_obj[section]
772
cfg_obj[section] = {}
773
obj = cfg_obj[section]
775
out_file = StringIO()
776
cfg_obj.write(out_file)
778
self.branch.control_files.put('branch.conf', out_file)