Sitelet https://github.com/gitpython-developers/GitPython/pull/2221/files
Skip to content
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
26 changes: 23 additions & 3 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
Sequence,
Tuple,
TYPE_CHECKING,
Type,
TypeVar,
Union,
cast,
Expand Down Expand Up @@ -108,6 +109,7 @@

T_IterableObj = TypeVar("T_IterableObj", bound=Union["IterableObj", "Has_id_attribute"], covariant=True)
# So IterableList[Head] is subtype of IterableList[IterableObj].
T_Actor = TypeVar("T_Actor", bound="Actor")

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -853,11 +855,27 @@ def update(self, *args: Any, **kwargs: Any) -> None:
self._callable(*args, **kwargs)


class _DeprecatedActorNameEmailRegex:
_pattern = re.compile(r"(.*) <(.*?)>")

def __get__(self, _instance: Any, _owner: Any) -> Pattern[str]:
warnings.warn(
"Actor.name_email_regex is deprecated and will be removed in GitPython 4.0.0 because searching long "
"malformed strings with it can take quadratic time. Use Actor.from_string() to parse actor identities, "
"or Actor(name, email) when the fields are already separate.",
DeprecationWarning,
stacklevel=2,
)
return self._pattern


class Actor:
"""Actors hold information about a person acting on the repository. They can be
committers and authors or anything with a name and an email as mentioned in the git
log entries."""

name_email_regex = _DeprecatedActorNameEmailRegex()

# ENVIRONMENT VARIABLES
# These are read when creating new commits.
env_author_name = "GIT_AUTHOR_NAME"
Expand Down Expand Up @@ -891,7 +909,7 @@ def __repr__(self) -> str:
return '<git.Actor "%s <%s>">' % (self.name, self.email)

@classmethod
def _from_string(cls, string: str) -> "Actor":
def from_string(cls: Type[T_Actor], string: str) -> T_Actor:
"""Create an :class:`Actor` from a string.

:param string:
Expand All @@ -906,10 +924,12 @@ def _from_string(cls, string: str) -> "Actor":
left_bracket = line.find("<")
right_bracket = line.find(">", left_bracket + 1)
if left_bracket >= 0 and right_bracket >= 0:
return Actor(line[:left_bracket].rstrip(), line[left_bracket + 1 : right_bracket])
return cls(line[:left_bracket].rstrip(), line[left_bracket + 1 : right_bracket])

# Assume the best and use the whole string as name.
return Actor(string, None)
return cls(string, None)

_from_string = from_string

@classmethod
def _main_actor(
Expand Down
35 changes: 26 additions & 9 deletions test/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# This module is part of GitPython and is released under the
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/

from git import Actor
from unittest import mock

from git import Actor
from test.lib import TestBase


class TestActor(TestBase):
def test_from_string_should_separate_name_and_email(self):
a = Actor._from_string("Michael Trier <mtrier@example.com>")
a = Actor.from_string("Michael Trier <mtrier@example.com>")
self.assertEqual("Michael Trier", a.name)
self.assertEqual("mtrier@example.com", a.email)

Expand All @@ -23,18 +24,34 @@ def test_from_string_should_separate_name_and_email(self):
assert len(m) == 1

def test_from_string_should_handle_just_name(self):
a = Actor._from_string("Michael Trier")
a = Actor.from_string("Michael Trier")
self.assertEqual("Michael Trier", a.name)
self.assertEqual(None, a.email)

def test_from_string_constructs_subclass(self):
class DerivedActor(Actor):
pass

self.assertIsInstance(DerivedActor.from_string("name <email>"), DerivedActor)

def test_from_string_handles_unterminated_email_without_regex_backtracking(self):
value = "A" * 20_000 + " <unterminated"
actor = Actor._from_string(value)
self.assertNotIn("name_email_regex", vars(Actor))
with mock.patch.object(Actor, "name_email_regex", None):
actor = Actor.from_string(value)
self.assertEqual(actor, Actor(value, None))

def test_name_email_regex_is_available_but_deprecated(self):
with self.assertWarns(DeprecationWarning) as context:
match = Actor.name_email_regex.match("Michael Trier <mtrier@example.com>")

message = str(context.warning)
self.assertIn("Actor.from_string()", message)
self.assertIn("Actor(name, email)", message)
assert match is not None
self.assertEqual(match.groups(), ("Michael Trier", "mtrier@example.com"))

def test_from_string_does_not_parse_across_lines(self):
self.assertEqual(Actor._from_string("x <a>\n y <b>"), Actor("x", "a"))
self.assertEqual(Actor.from_string("x <a>\n y <b>"), Actor("x", "a"))

def test_from_string_uses_git_delimiters(self):
for value, expected in (
Expand All @@ -45,12 +62,12 @@ def test_from_string_uses_git_delimiters(self):
("Name <email", Actor("Name <email", None)),
("Name email>", Actor("Name email>", None)),
):
self.assertEqual(Actor._from_string(value), expected)
self.assertEqual(Actor.from_string(value), expected)

def test_should_display_representation(self):
a = Actor._from_string("Michael Trier <mtrier@example.com>")
a = Actor.from_string("Michael Trier <mtrier@example.com>")
self.assertEqual('<git.Actor "Michael Trier <mtrier@example.com>">', repr(a))

def test_str_should_alias_name(self):
a = Actor._from_string("Michael Trier <mtrier@example.com>")
a = Actor.from_string("Michael Trier <mtrier@example.com>")
self.assertEqual(a.name, str(a))
Loading