=== added file bzrlib/tests/blackbox/test_whoami.py // file-id:test_whoami.py-2 ... 0060629025641-8h3m2ch7kutqx7ug-1 // last-changed:robey@lag.net-200606292034 ... 39-d32a68c74428c9db --- /dev/null +++ bzrlib/tests/blackbox/test_whoami.py @@ -0,0 +1,97 @@ +# Copyright (C) 2005 by Canonical Ltd +# -*- coding: utf-8 -*- + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +"""Black-box tests for bzr whoami. +""" + +import os + +import bzrlib +from bzrlib.branch import Branch +from bzrlib.tests.blackbox import ExternalBase + + +class TestWhoami(ExternalBase): + + def test_whoami(self): + # this should always identify something, if only "john@localhost" + out = self.run_bzr("whoami")[0] + self.assertTrue(len(out) > 0) + self.assertEquals(out.count('@'), 1) + + out = self.run_bzr("whoami", "--email")[0] + self.assertTrue(len(out) > 0) + self.assertEquals(out.count('@'), 1) + + def test_whoami_branch(self): + """branch specific user identity works.""" + self.run_bzr('init') + b = bzrlib.branch.Branch.open('.') + b.get_config().set_user_option('email', 'Branch Identity ') + bzr_email = os.environ.get('BZREMAIL') + if bzr_email is not None: + del os.environ['BZREMAIL'] + try: + whoami = self.run_bzr("whoami")[0] + whoami_email = self.run_bzr("whoami", "--email")[0] + self.assertTrue(whoami.startswith('Branch Identity ')) + self.assertTrue(whoami_email.startswith('branch@identi.ty')) + + # Verify that the environment variable overrides the value + # in the file + os.environ['BZREMAIL'] = 'Different ID ' + whoami = self.run_bzr("whoami")[0] + whoami_email = self.run_bzr("whoami", "--email")[0] + self.assertTrue(whoami.startswith('Different ID ')) + self.assertTrue(whoami_email.startswith('other@environ.ment')) + finally: + if bzr_email is not None: + os.environ['BZREMAIL'] = bzr_email + + def test_whoami_utf8(self): + """verify that an identity can be in utf-8.""" + self.run_bzr('init') + self.run_bzr('whoami', u'Branch Identity \u20ac ', encoding='utf-8') + bzr_email = os.environ.get('BZREMAIL') + if bzr_email is not None: + del os.environ['BZREMAIL'] + try: + whoami = self.run_bzr("whoami", encoding='utf-8')[0] + whoami_email = self.run_bzr("whoami", "--email", encoding='utf-8')[0] + self.assertTrue(whoami.startswith('Branch Identity \xe2\x82\xac ')) + self.assertTrue(whoami_email.startswith('branch@identi.ty')) + finally: + if bzr_email is not None: + os.environ['BZREMAIL'] = bzr_email + + def test_whoami_ascii(self): + """verify that whoami doesn't totally break when in utf-8, using an ascii encoding.""" + self.runbzr('init') + b = bzrlib.branch.Branch.open('.') + b.get_config().set_user_option('email', u'Branch Identity \u20ac ') + bzr_email = os.environ.get('BZREMAIL') + if bzr_email is not None: + del os.environ['BZREMAIL'] + try: + whoami = self.run_bzr("whoami", encoding='ascii')[0] + whoami_email = self.run_bzr("whoami", "--email", encoding='ascii')[0] + self.assertTrue(whoami.startswith('Branch Identity ? ')) + self.assertTrue(whoami_email.startswith('branch@identi.ty')) + finally: + if bzr_email is not None: + os.environ['BZREMAIL'] = bzr_email === modified file NEWS // last-changed:robey@lag.net-20060629232558-a7b7c4da6c4 ... 55841 --- NEWS +++ NEWS @@ -64,6 +64,9 @@ * 'branches.conf' has been changed to 'locations.conf', since it can apply to more locations than just branch locations. (Aaron Bentley) + + * 'bzr whoami' can now be used to set your identity from the command line, + for a branch or globally. (Robey Pointer) BUG FIXES: === modified file bzrlib/builtins.py // last-changed:robey@lag.net-200606292034 ... 17-d400dd001a1c1752 --- bzrlib/builtins.py +++ bzrlib/builtins.py @@ -1776,19 +1776,42 @@ class cmd_whoami(Command): - """Show bzr user id.""" - takes_options = ['email'] + """Show or set bzr user id. + + examples: + bzr whoami --email + bzr whoami 'Frank Chu ' + """ + takes_options = [ Option('email', + help='display email address only'), + Option('branch', + help='set identity for the current branch instead of ' + 'globally'), + ] + takes_args = ['name?'] + encoding_type = 'replace' @display_command - def run(self, email=False): - try: + def run(self, email=False, branch=False, name=None): + if name is None: + # use branch if we're inside one; otherwise global config + try: + c = WorkingTree.open_containing(u'.')[0].branch.get_config() + except NotBranchError: + c = config.GlobalConfig() + if email: + self.outf.write(c.user_email()) + else: + self.outf.write(c.username()) + self.outf.write('\n') + return + + # use global config unless --branch given + if branch: c = WorkingTree.open_containing(u'.')[0].branch.get_config() - except NotBranchError: + else: c = config.GlobalConfig() - if email: - print c.user_email() - else: - print c.username() + c.set_user_option('email', name) class cmd_nick(Command): === modified file bzrlib/config.py // last-changed:robey@lag.net-20060629051253 ... -f3c6c1306aebcb3d --- bzrlib/config.py +++ bzrlib/config.py @@ -337,6 +337,19 @@ def __init__(self): super(GlobalConfig, self).__init__(config_filename) + def set_user_option(self, option, value): + """Save option and its value in the configuration.""" + # FIXME: RBC 20051029 This should refresh the parser and also take a + # file lock on bazaar.conf. + conf_dir = os.path.dirname(self._get_filename()) + ensure_config_dir_exists(conf_dir) + if 'DEFAULT' not in self._get_parser(): + self._get_parser()['DEFAULT'] = {} + self._get_parser()['DEFAULT'][option] = value + f = open(self._get_filename(), 'wb') + self._get_parser().write(f) + f.close() + class LocationConfig(IniBasedConfig): """A configuration object that gives the policy for a location.""" @@ -597,13 +610,25 @@ uid = os.getuid() w = pwd.getpwuid(uid) - try: - gecos = w.pw_gecos.decode(bzrlib.user_encoding) - username = w.pw_name.decode(bzrlib.user_encoding) - except UnicodeDecodeError: - # We're using pwd, therefore we're on Unix, so /etc/passwd is ok. - raise errors.BzrError("Can't decode username in " \ - "/etc/passwd as %s." % bzrlib.user_encoding) + # we try utf-8 first, because on many variants (like Linux), + # /etc/passwd "should" be in utf-8, and because it's unlikely to give + # false positives. (many users will have their user encoding set to + # latin-1, which cannot raise UnicodeError.) + try: + gecos = w.pw_gecos.decode('utf-8') + encoding = 'utf-8' + except UnicodeError: + try: + gecos = w.pw_gecos.decode(bzrlib.user_encoding) + encoding = bzrlib.user_encoding + except UnicodeError: + raise errors.BzrCommandError('Unable to determine your name. ' + 'Use "bzr whoami" to set it.') + try: + username = w.pw_name.decode(encoding) + except UnicodeError: + raise errors.BzrCommandError('Unable to determine your name. ' + 'Use "bzr whoami" to set it.') comma = gecos.find(',') if comma == -1: === modified file bzrlib/tests/blackbox/__init__.py // last-changed:robey@lag.n ... et-20060629051326-55354a4283dc69da --- bzrlib/tests/blackbox/__init__.py +++ bzrlib/tests/blackbox/__init__.py @@ -83,6 +83,7 @@ 'bzrlib.tests.blackbox.test_update', 'bzrlib.tests.blackbox.test_upgrade', 'bzrlib.tests.blackbox.test_versioning', + 'bzrlib.tests.blackbox.test_whoami', ] test_encodings = [ 'bzrlib.tests.blackbox.test_non_ascii', === modified file bzrlib/tests/blackbox/test_too_much.py // last-changed:robey@ ... lag.net-20060629051326-55354a4283dc69da --- bzrlib/tests/blackbox/test_too_much.py +++ bzrlib/tests/blackbox/test_too_much.py @@ -57,36 +57,6 @@ class TestCommands(ExternalBase): - def test_whoami(self): - # this should always identify something, if only "john@localhost" - self.runbzr("whoami") - self.runbzr("whoami --email") - - self.assertEquals(self.runbzr("whoami --email", - backtick=True).count('@'), 1) - - def test_whoami_branch(self): - """branch specific user identity works.""" - self.runbzr('init') - b = bzrlib.branch.Branch.open('.') - b.control_files.put_utf8('email', 'Branch Identity ') - bzr_email = os.environ.get('BZREMAIL') - if bzr_email is not None: - del os.environ['BZREMAIL'] - whoami = self.runbzr("whoami",backtick=True) - whoami_email = self.runbzr("whoami --email",backtick=True) - self.assertTrue(whoami.startswith('Branch Identity ')) - self.assertTrue(whoami_email.startswith('branch@identi.ty')) - # Verify that the environment variable overrides the value - # in the file - os.environ['BZREMAIL'] = 'Different ID ' - whoami = self.runbzr("whoami",backtick=True) - whoami_email = self.runbzr("whoami --email",backtick=True) - self.assertTrue(whoami.startswith('Different ID ')) - self.assertTrue(whoami_email.startswith('other@environ.ment')) - if bzr_email is not None: - os.environ['BZREMAIL'] = bzr_email - def test_nick_command(self): """bzr nick for viewing, setting nicknames""" os.mkdir('me.dev') === modified file doc/README.1st // last-changed:robey@lag.net-20060629232548-2 ... 5aaee5fe809193c --- doc/README.1st +++ doc/README.1st @@ -1,6 +1,6 @@ These documents originally came from the Bazaar-NG wiki hosted at -http://bazaar-vcs.org. These documents, though maintained, These documenets -can fall behind when Bazaar-NG is under heavy development. +http://bazaar-vcs.org. These documents, though maintained, can fall behind +when Bazaar-NG is under heavy development. The documents on the wiki take precedence in the event of a discrepancy between what is documented here and what is documented on the wiki. === modified file doc/setting_up_email.txt // last-changed:robey@lag.net-200606 ... 29232631-73dee390d880c557 --- doc/setting_up_email.txt +++ doc/setting_up_email.txt @@ -31,14 +31,25 @@ % bzr whoami Joe Cool + +Setting email via the 'whoami' command +====================================== +You can use the whoami command to set your email globally:: + + % bzr whoami 'Joe Cool ' + +or only for the current branch:: + + % bzr whoami --branch 'Joe Cool ' + +These modify your global bazaar.conf or branch branch.conf file, respectively. Setting email via default ini file ================================== -The first method is using the default ini file. To use the default ini -method, create the file `$HOME/.bazaar/bazaar.conf` (on Windows this is -`%APPDATA%\bazaar\2.0\bazaar.conf`) and set an email address as shown -below. Please note that the word DEFAULT is case sensitive, and must be in -upper-case.:: +To use the default ini file, create the file `$HOME/.bazaar/bazaar.conf` (on +Windows this is `%APPDATA%\bazaar\2.0\bazaar.conf`) and set an email address +as shown below. Please note that the word DEFAULT is case sensitive, and +must be in upper-case.:: [DEFAULT] email=Your Name === modified file doc/tutorial.txt --- doc/tutorial.txt +++ doc/tutorial.txt @@ -117,6 +117,16 @@ address by looking up your username and hostname. If you don't like the guess that Bazaar-NG makes, then three options exist: + 1. Set an email address via ``bzr whoami``. This is the simplest way. + To set a global identity, use:: + + % bzr whoami 'Your Name ' + + If you'd like to use a different address for a specific branch, enter + the branch folder and use:: + + % bzr whoami --branch 'Your Name ' + 1. Setting the email address in the ``~/.bazaar/bazaar.conf`` by adding the following lines. Please note that ``[DEFAULT]`` is case sensitive:: @@ -124,9 +134,9 @@ [DEFAULT] email= Your Name - 1. Override the previous setting on a - branch by branch basis by creating a branch section in - ``~/.bazaar/locations.conf`` by adding the following lines:: + As above, you can override this settings on a branch by branch basis by + creating a branch section in ``~/.bazaar/locations.conf`` and adding the + following lines:: [/the/directory/to/the/branch] email=Your Name