From c96476be7f10616768584a95d06cd1bddfe6d404 Mon Sep 17 00:00:00 2001 From: Athos Ribeiro Date: Tue, 20 Oct 2020 14:51:31 +0200 Subject: Get system user id in a lazy manner Calling getpass.getuser may lead to breakage in environments where there is no entries in the /etc/passwd file for the current user. Setting the environment variables for the git user configurations should prevents GitPython from using values from /etc/passwd. However, doing so will not prevent reading /etc/passwd and looking for an entry with the current user UID. This patch changes the behavior described above so GitPython will perform a lazy evaluation of /etc/passwd, only doing so when the environment variables for the git user configuration are not available. Signed-off-by: Athos Ribeiro --- git/util.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'git/util.py') diff --git a/git/util.py b/git/util.py index 216703cb..44eb0e51 100644 --- a/git/util.py +++ b/git/util.py @@ -582,8 +582,16 @@ class Actor(object): @classmethod def _main_actor(cls, env_name, env_email, config_reader=None): actor = Actor('', '') - default_email = get_user_id() - default_name = default_email.split('@')[0] + user_id = None # We use this to avoid multiple calls to getpass.getuser() + + def default_email(): + nonlocal user_id + if not user_id: + user_id = get_user_id() + return user_id + + def default_name(): + default_email().split('@')[0] for attr, evar, cvar, default in (('name', env_name, cls.conf_name, default_name), ('email', env_email, cls.conf_email, default_email)): @@ -592,10 +600,10 @@ class Actor(object): setattr(actor, attr, val) except KeyError: if config_reader is not None: - setattr(actor, attr, config_reader.get_value('user', cvar, default)) + setattr(actor, attr, config_reader.get_value('user', cvar, default())) # END config-reader handling if not getattr(actor, attr): - setattr(actor, attr, default) + setattr(actor, attr, default()) # END handle name # END for each item to retrieve return actor -- cgit v1.2.1