diff --git a/news/12280.bugfix.rst b/news/12280.bugfix.rst new file mode 100644 index 000000000..77de283d3 --- /dev/null +++ b/news/12280.bugfix.rst @@ -0,0 +1 @@ +Fix crash when the git version number contains something else than digits and dots. diff --git a/src/pip/_internal/vcs/git.py b/src/pip/_internal/vcs/git.py index 8d1d49937..8c242cf89 100644 --- a/src/pip/_internal/vcs/git.py +++ b/src/pip/_internal/vcs/git.py @@ -101,7 +101,7 @@ class Git(VersionControl): if not match: logger.warning("Can't parse git version: %s", version) return () - return tuple(int(c) for c in match.groups()) + return (int(match.group(1)), int(match.group(2))) @classmethod def get_current_branch(cls, location: str) -> Optional[str]: diff --git a/tests/unit/test_vcs.py b/tests/unit/test_vcs.py index 3ecc69abf..fb6c3ea31 100644 --- a/tests/unit/test_vcs.py +++ b/tests/unit/test_vcs.py @@ -598,6 +598,21 @@ def test_get_git_version() -> None: assert git_version >= (1, 0, 0) +@pytest.mark.parametrize( + ("version", "expected"), + [ + ("git version 2.17", (2, 17)), + ("git version 2.18.1", (2, 18)), + ("git version 2.35.GIT", (2, 35)), # gh:12280 + ("oh my git version 2.37.GIT", ()), # invalid version + ("git version 2.GIT", ()), # invalid version + ], +) +def test_get_git_version_parser(version: str, expected: Tuple[int, int]) -> None: + with mock.patch("pip._internal.vcs.git.Git.run_command", return_value=version): + assert Git().get_git_version() == expected + + @pytest.mark.parametrize( "use_interactive,is_atty,expected", [