441
441
def set_user_option(self, option, value):
442
442
"""Save option and its value in the configuration."""
443
self._set_option(option, value, 'DEFAULT')
445
def get_aliases(self):
446
"""Return the aliases section."""
447
if 'ALIASES' in self._get_parser():
448
return self._get_parser()['ALIASES']
452
def set_alias(self, alias_name, alias_command):
453
"""Save the alias in the configuration."""
454
self._set_option(alias_name, alias_command, 'ALIASES')
456
def unset_alias(self, alias_name):
457
"""Unset an existing alias."""
458
aliases = self._get_parser().get('ALIASES')
459
if not aliases or alias_name not in aliases:
460
raise errors.NoSuchAlias(alias_name)
461
del aliases[alias_name]
462
self._write_config_file()
464
def _set_option(self, option, value, section):
443
465
# FIXME: RBC 20051029 This should refresh the parser and also take a
444
466
# file lock on bazaar.conf.
445
467
conf_dir = os.path.dirname(self._get_filename())
446
468
ensure_config_dir_exists(conf_dir)
447
if 'DEFAULT' not in self._get_parser():
448
self._get_parser()['DEFAULT'] = {}
449
self._get_parser()['DEFAULT'][option] = value
469
self._get_parser().setdefault(section, {})[option] = value
470
self._write_config_file()
472
def _write_config_file(self):
450
473
f = open(self._get_filename(), 'wb')
451
474
self._get_parser().write(f)
641
664
def _get_user_id(self):
642
665
"""Return the full user id for the branch.
644
e.g. "John Hacker <jhacker@foo.org>"
667
e.g. "John Hacker <jhacker@example.com>"
645
668
This is looked up in the email controlfile for the branch.
648
return (self.branch.control_files._transport.get_bytes("email")
671
return (self.branch._transport.get_bytes("email")
649
672
.decode(bzrlib.user_encoding)
651
674
except errors.NoSuchFile, e:
895
918
# XXX: Really needs a better name, as this is not part of the tree! -- mbp 20080507
897
920
def __init__(self, branch):
898
transport = branch.control_files._transport
899
self._config = TransportConfig(transport, 'branch.conf')
921
# XXX: Really this should be asking the branch for its configuration
922
# data, rather than relying on a Transport, so that it can work
923
# more cleanly with a RemoteBranch that has no transport.
924
self._config = TransportConfig(branch._transport, 'branch.conf')
900
925
self.branch = branch
902
927
def _get_parser(self, file=None):
1090
1115
credentials = self.get_credentials(scheme, host, port, user, path)
1091
1116
if credentials is not None:
1092
1117
password = credentials['password']
1118
if password is not None and scheme is 'ssh':
1119
trace.warning('password ignored in section [%s],'
1120
' use an ssh agent instead'
1121
% credentials['name'])
1094
1124
password = None
1095
1125
# Prompt user only if we could't find a password
1096
1126
if password is None:
1097
1127
if prompt is None:
1098
# Create a default prompt suitable for most of the cases
1128
# Create a default prompt suitable for most cases
1099
1129
prompt = '%s' % scheme.upper() + ' %(user)s@%(host)s password'
1100
1130
# Special handling for optional fields in the prompt
1101
1131
if port is not None:
1110
1140
return credentials
1143
class BzrDirConfig(object):
1145
def __init__(self, transport):
1146
self._config = TransportConfig(transport, 'control.conf')
1148
def set_default_stack_on(self, value):
1149
"""Set the default stacking location.
1151
It may be set to a location, or None.
1153
This policy affects all branches contained by this bzrdir, except for
1154
those under repositories.
1157
self._config.set_option('', 'default_stack_on')
1159
self._config.set_option(value, 'default_stack_on')
1161
def get_default_stack_on(self):
1162
"""Return the default stacking location.
1164
This will either be a location, or None.
1166
This policy affects all branches contained by this bzrdir, except for
1167
those under repositories.
1169
value = self._config.get_option('default_stack_on')
1113
1175
class TransportConfig(object):
1114
1176
"""A Config that reads/writes a config file on a Transport.