[RFC PATCH 03/13] b4: add cross-platform username resolution in _setup_user_config
Adrian Neftali Sanchez <[email protected]>
| Newsgroups | org.kernel.linux.tools |
|---|---|
| Message-ID | <[email protected]> |
On Unix-like systems, obtain the user's display name from pwd.getpwuid() when GIT_COMMITTER_NAME and GIT_AUTHOR_NAME are not set. Parse pw_gecos to extract only the full name field (before the first comma) per POSIX conventions. On Windows, where pwd and os.getuid() are unavailable, fall back to the USERNAME environment variable. Use explicit sys.platform != 'win32' guards so static type checkers (mypy, pyright, ruff) correctly recognize platform- specific APIs without 'possibly unbound' or 'attr-defined' diagnostics. This enables b4 to initialize USER_CONFIG['name'] reliably across all supported platforms without raising ImportError or AttributeError. Signed-off-by: Adrian Neftali Sanchez <[email protected]> --- src/b4/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/b4/__init__.py b/src/b4/__init__.py index 0530fec..e878d34 100644 --- a/src/b4/__init__.py +++ b/src/b4/__init__.py @@ -17,7 +17,6 @@ import logging import mailbox import os import pathlib -import pwd import re import shlex import shutil @@ -4007,9 +4006,15 @@ def _setup_user_config(cmdargs: argparse.Namespace) -> None: USER_CONFIG['name'] = os.environ['GIT_COMMITTER_NAME'] elif 'GIT_AUTHOR_NAME' in os.environ: USER_CONFIG['name'] = os.environ['GIT_AUTHOR_NAME'] - else: + elif sys.platform != 'win32': + # Unix-like: pwd and os.getuid() are guaranteed in this branch + import pwd udata = pwd.getpwuid(os.getuid()) - USER_CONFIG['name'] = udata.pw_gecos.strip(',') + # pw_gecos is typically "Full Name,Office,Phone,...". Extract just the name. + USER_CONFIG['name'] = udata.pw_gecos.split(',', 1)[0].strip() + else: + # Windows fallback + USER_CONFIG['name'] = os.environ.get('USERNAME', 'unknown') if 'email' not in USER_CONFIG: if 'GIT_COMMITTER_EMAIL' in os.environ: USER_CONFIG['email'] = os.environ['GIT_COMMITTER_EMAIL'] -- 2.45.0.windows.1