Skip to content

Get system user id in a lazy manner #1072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,16 @@ def _from_string(cls, string):
@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]
Copy link

@harupy harupy Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@athos-ribeiro @Byron
(not sure if I'm really correct) Is return missing here?

Copy link

@harupy harupy Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was investigating an error in our CI and found this patch which is included in gitpython==3.10.0 and seems related to the error.

https://github.com/mlflow/mlflow/pull/3584/checks?check_run_id=1297036420#step:4:1677

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess I messed up on this one :)

Will provide a follow-up fix right away.

I am sorry for this :(

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem :) Thanks for the quick reponse!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks everyone. With the next PR to fix this one you will get a new patch release right away.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing it in #1073.

Once again, sorry for the inconvenient


for attr, evar, cvar, default in (('name', env_name, cls.conf_name, default_name),
('email', env_email, cls.conf_email, default_email)):
Expand All @@ -592,10 +600,10 @@ def _main_actor(cls, env_name, env_email, config_reader=None):
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
Expand Down
25 changes: 24 additions & 1 deletion test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

import os
import pickle
import tempfile
import time
from unittest import skipIf
from unittest import mock, skipIf
from datetime import datetime

import ddt
Expand Down Expand Up @@ -215,6 +216,28 @@ def test_actor(self):
self.assertIsInstance(Actor.author(cr), Actor)
# END assure config reader is handled

@mock.patch("getpass.getuser")
def test_actor_get_uid_laziness_not_called(self, mock_get_uid):
env = {
"GIT_AUTHOR_NAME": "John Doe",
"GIT_AUTHOR_EMAIL": "jdoe@example.com",
"GIT_COMMITTER_NAME": "Jane Doe",
"GIT_COMMITTER_EMAIL": "jane@example.com",
}
os.environ.update(env)
for cr in (None, self.rorepo.config_reader()):
Actor.committer(cr)
Actor.author(cr)
self.assertFalse(mock_get_uid.called)

@mock.patch("getpass.getuser")
def test_actor_get_uid_laziness_called(self, mock_get_uid):
for cr in (None, self.rorepo.config_reader()):
Actor.committer(cr)
Actor.author(cr)
self.assertTrue(mock_get_uid.called)
self.assertEqual(mock_get_uid.call_count, 4)

def test_actor_from_string(self):
self.assertEqual(Actor._from_string("name"), Actor("name", None))
self.assertEqual(Actor._from_string("name <>"), Actor("name", ""))
Expand Down