Rev 5213: bzr does not guess whoami anymore. (parthm, #549310) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed May 5 18:31:23 BST 2010


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5213 [merge]
revision-id: pqm at pqm.ubuntu.com-20100505173116-peb5afaw8y7b9i2o
parent: pqm at pqm.ubuntu.com-20100505144453-4ewd3ms50q5m9tgw
parent: parth.malwankar at gmail.com-20100505141113-c21oicoxzb3u6if6
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2010-05-05 18:31:16 +0100
message:
  bzr does not guess whoami anymore. (parthm, #549310)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/lockdir.py              lockdir.py-20060220222025-98258adf27fbdda3
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/blackbox/test_commit.py test_commit.py-20060212094538-ae88fc861d969db0
  bzrlib/tests/blackbox/test_init.py test_init.py-20060309032856-a292116204d86eb7
  bzrlib/tests/blackbox/test_shared_repository.py test_shared_repository.py-20060317053531-ed30c0d79325e483
  bzrlib/tests/blackbox/test_whoami.py test_whoami.py-20060629025641-8h3m2ch7kutqx7ug-1
  bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
=== modified file 'NEWS'
--- a/NEWS	2010-05-05 14:44:53 +0000
+++ b/NEWS	2010-05-05 17:31:16 +0000
@@ -13,6 +13,11 @@
 Compatibility Breaks
 ********************
 
+* ``bzr`` does not try to guess the username as ``username at hostname``
+  and requires it to be explictly set. This can be set using ``bzr
+  whoami``.
+  (Parth Malwankar, #549310)
+
 New Features
 ************
 

=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2010-04-24 09:08:27 +0000
+++ b/bzrlib/config.py	2010-04-28 04:52:16 +0000
@@ -257,8 +257,7 @@
 
         Something similar to 'Martin Pool <mbp at sourcefrog.net>'
 
-        $BZR_EMAIL can be set to override this (as well as the
-        deprecated $BZREMAIL), then
+        $BZR_EMAIL can be set to override this, then
         the concrete policy type is checked, and finally
         $EMAIL is examined.
         If none is found, a reasonable default is (hopefully)
@@ -278,11 +277,14 @@
         if v:
             return v.decode(osutils.get_user_encoding())
 
-        name, email = _auto_user_id()
-        if name:
-            return '%s <%s>' % (name, email)
-        else:
-            return email
+        raise errors.NoWhoami()
+
+    def ensure_username(self):
+        """Raise BzrCommandError if username is not set.
+
+        This method relies on the username() function raising the error.
+        """
+        self.username()
 
     def signature_checking(self):
         """What is the current policy for signature checking?."""
@@ -899,79 +901,6 @@
         return os.path.expanduser('~/.cache')
 
 
-def _auto_user_id():
-    """Calculate automatic user identification.
-
-    Returns (realname, email).
-
-    Only used when none is set in the environment or the id file.
-
-    This previously used the FQDN as the default domain, but that can
-    be very slow on machines where DNS is broken.  So now we simply
-    use the hostname.
-    """
-    import socket
-
-    if sys.platform == 'win32':
-        name = win32utils.get_user_name_unicode()
-        if name is None:
-            raise errors.BzrError("Cannot autodetect user name.\n"
-                                  "Please, set your name with command like:\n"
-                                  'bzr whoami "Your Name <name at domain.com>"')
-        host = win32utils.get_host_name_unicode()
-        if host is None:
-            host = socket.gethostname()
-        return name, (name + '@' + host)
-
-    try:
-        import pwd
-        uid = os.getuid()
-        try:
-            w = pwd.getpwuid(uid)
-        except KeyError:
-            raise errors.BzrCommandError('Unable to determine your name.  '
-                'Please use "bzr whoami" to set it.')
-
-        # 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:
-                encoding = osutils.get_user_encoding()
-                gecos = w.pw_gecos.decode(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:
-            realname = gecos
-        else:
-            realname = gecos[:comma]
-        if not realname:
-            realname = username
-
-    except ImportError:
-        import getpass
-        try:
-            user_encoding = osutils.get_user_encoding()
-            realname = username = getpass.getuser().decode(user_encoding)
-        except UnicodeDecodeError:
-            raise errors.BzrError("Can't decode username as %s." % \
-                    user_encoding)
-
-    return realname, (username + '@' + socket.gethostname())
-
-
 def parse_username(username):
     """Parse e-mail username and return a (name, address) tuple."""
     match = re.match(r'(.*?)\s*<?([\w+.-]+@[\w+.-]+)>?', username)

=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2010-04-21 11:27:04 +0000
+++ b/bzrlib/errors.py	2010-05-05 14:11:13 +0000
@@ -3134,3 +3134,10 @@
     def __init__(self, bzrdir):
         self.bzrdir = bzrdir
 
+class NoWhoami(BzrError):
+
+    _fmt = ('Unable to determine your name.\n'
+        "Please, set your name with the 'whoami' command.\n"
+        'E.g. bzr whoami "Your Name <name at example.com>"')
+
+

=== modified file 'bzrlib/lockdir.py'
--- a/bzrlib/lockdir.py	2010-03-03 22:59:21 +0000
+++ b/bzrlib/lockdir.py	2010-04-28 06:09:23 +0000
@@ -447,9 +447,9 @@
         # XXX: is creating this here inefficient?
         config = bzrlib.config.GlobalConfig()
         try:
-            user = config.user_email()
-        except errors.NoEmailInUsername:
             user = config.username()
+        except errors.NoWhoami:
+            user = osutils.getuser_unicode()
         s = rio.Stanza(hostname=get_host_name(),
                    pid=str(os.getpid()),
                    start_time=str(int(time.time())),

=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2010-05-03 06:25:46 +0000
+++ b/bzrlib/osutils.py	2010-05-05 14:11:13 +0000
@@ -26,6 +26,7 @@
 lazy_import(globals(), """
 from datetime import datetime
 import errno
+import getpass
 from ntpath import (abspath as _nt_abspath,
                     join as _nt_join,
                     normpath as _nt_normpath,
@@ -2302,3 +2303,15 @@
         return os.fdopen(os.open(filename, flags), mode, bufsize)
 else:
     open_file = open
+
+
+def getuser_unicode():
+    """Return the username as unicode.
+    """
+    try:
+        user_encoding = get_user_encoding()
+        username = getpass.getuser().decode(user_encoding)
+    except UnicodeDecodeError:
+        raise errors.BzrError("Can't decode username as %s." % \
+                user_encoding)
+    return username

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2010-05-05 02:35:02 +0000
+++ b/bzrlib/tests/__init__.py	2010-05-05 14:11:13 +0000
@@ -1524,7 +1524,7 @@
             'EDITOR': None,
             'BZR_EMAIL': None,
             'BZREMAIL': None, # may still be present in the environment
-            'EMAIL': None,
+            'EMAIL': 'jrandom at example.com', # set EMAIL as bzr does not guess
             'BZR_PROGRESS_BAR': None,
             'BZR_LOG': None,
             'BZR_PLUGIN_PATH': None,

=== modified file 'bzrlib/tests/blackbox/test_commit.py'
--- a/bzrlib/tests/blackbox/test_commit.py	2010-04-07 22:22:27 +0000
+++ b/bzrlib/tests/blackbox/test_commit.py	2010-04-27 17:09:48 +0000
@@ -710,3 +710,15 @@
         out, err = self.run_bzr_error(["empty commit message"],
             "commit tree/hello.txt", stdin="n\n")
         self.assertEqual(expected, tree.last_revision())
+
+    def test_commit_without_username(self):
+        """Ensure commit error if username is not set.
+        """
+        self.run_bzr(['init', 'foo'])
+        os.chdir('foo')
+        open('foo.txt', 'w').write('hello')
+        self.run_bzr(['add'])
+        osutils.set_or_unset_env('EMAIL', None)
+        osutils.set_or_unset_env('BZR_EMAIL', None)
+        out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
+        self.assertContainsRe(err, 'Unable to determine your name')

=== modified file 'bzrlib/tests/blackbox/test_init.py'
--- a/bzrlib/tests/blackbox/test_init.py	2010-04-23 07:15:23 +0000
+++ b/bzrlib/tests/blackbox/test_init.py	2010-05-05 14:02:53 +0000
@@ -202,3 +202,17 @@
         self.assertEqual(True, branch._get_append_revisions_only())
         self.run_bzr_error(['cannot be set to append-revisions-only'],
                            'init --append-revisions-only --knit knit')
+
+    def test_init_without_username(self):
+        """Ensure init works if username is not set.
+        """
+        # bzr makes user specified whoami mandatory for operations
+        # like commit as whoami is recorded. init however is not so final
+        # and uses whoami only in a lock file. Without whoami the login name
+        # is used. This test is to ensure that init passes even when whoami
+        # is not available.
+        osutils.set_or_unset_env('EMAIL', None)
+        osutils.set_or_unset_env('BZR_EMAIL', None)
+        out, err = self.run_bzr(['init', 'foo'])
+        self.assertEqual(err, '')
+        self.assertTrue(os.path.exists('foo'))

=== modified file 'bzrlib/tests/blackbox/test_shared_repository.py'
--- a/bzrlib/tests/blackbox/test_shared_repository.py	2010-03-25 18:02:45 +0000
+++ b/bzrlib/tests/blackbox/test_shared_repository.py	2010-05-05 14:02:53 +0000
@@ -18,6 +18,7 @@
 
 import os
 
+from bzrlib import osutils
 from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
 import bzrlib.errors as errors
 from bzrlib.tests import TestCaseInTempDir
@@ -145,3 +146,17 @@
         self.assertLength(0, calls)
         self.run_bzr("init-repository a")
         self.assertLength(1, calls)
+
+    def test_init_repo_without_username(self):
+        """Ensure init-repo works if username is not set.
+        """
+        # bzr makes user specified whoami mandatory for operations
+        # like commit as whoami is recorded. init-repo however is not so final
+        # and uses whoami only in a lock file. Without whoami the login name
+        # is used. This test is to ensure that init-repo passes even when whoami
+        # is not available.
+        osutils.set_or_unset_env('EMAIL', None)
+        osutils.set_or_unset_env('BZR_EMAIL', None)
+        out, err = self.run_bzr(['init-repo', 'foo'])
+        self.assertEqual(err, '')
+        self.assertTrue(os.path.exists('foo'))

=== modified file 'bzrlib/tests/blackbox/test_whoami.py'
--- a/bzrlib/tests/blackbox/test_whoami.py	2009-03-23 14:59:43 +0000
+++ b/bzrlib/tests/blackbox/test_whoami.py	2010-04-27 17:09:48 +0000
@@ -20,6 +20,7 @@
 import os
 
 import bzrlib
+from bzrlib import osutils
 from bzrlib.branch import Branch
 from bzrlib.tests.blackbox import ExternalBase
 
@@ -111,3 +112,11 @@
         self.assertEquals('"Branch Identity" does not seem to contain an '
                           'email address.  This is allowed, but not '
                           'recommended.\n', display)
+
+    def test_whoami_not_set(self):
+        """Ensure whoami error if username is not set.
+        """
+        osutils.set_or_unset_env('EMAIL', None)
+        osutils.set_or_unset_env('BZR_EMAIL', None)
+        out, err = self.run_bzr(['whoami'], 3)
+        self.assertContainsRe(err, 'Unable to determine your name')

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2010-04-27 07:52:08 +0000
+++ b/bzrlib/tests/test_osutils.py	2010-05-05 14:11:13 +0000
@@ -2018,3 +2018,14 @@
         self.assertEquals(self.path, 'test_file')
         self.assertEquals(self.uid, s.st_uid)
         self.assertEquals(self.gid, s.st_gid)
+
+class TestGetuserUnicode(tests.TestCase):
+
+    def test_ascii_user(self):
+        osutils.set_or_unset_env('LOGNAME', 'jrandom')
+        self.assertEqual(u'jrandom', osutils.getuser_unicode())
+
+    def test_unicode_user(self):
+        ue = osutils.get_user_encoding()
+        osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
+        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())




More information about the bazaar-commits mailing list