diff --git a/pydriller/domain/__init__.py b/pydriller/domain/__init__.py index e69de29b..bd19aad0 100644 --- a/pydriller/domain/__init__.py +++ b/pydriller/domain/__init__.py @@ -0,0 +1,4 @@ +from pydriller.utils.check_git_version import CheckGitVersion + + +CheckGitVersion().check_git_version() diff --git a/pydriller/utils/check_git_version.py b/pydriller/utils/check_git_version.py new file mode 100644 index 00000000..43bb7cbb --- /dev/null +++ b/pydriller/utils/check_git_version.py @@ -0,0 +1,19 @@ +import subprocess +import re + + +class GitVersion(Exception): + def __init__(self, message): + super().__init__(message) + + +class CheckGitVersion: + def check_git_version(self): + git_version = ( + subprocess.check_output(["git", "--version"]).decode("ascii").strip() + ) + version_number = re.findall(r"[0-9]+\.[0-9]+", git_version)[0] + if float(version_number) < 2.38: + raise GitVersion( + f"Current git version is {version_number}. Minimum supported version is 2.38." + ) diff --git a/test-requirements.txt b/test-requirements.txt index de3869eb..71dfc80f 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,4 +1,5 @@ mock types-mock pytest -psutil \ No newline at end of file +psutil +pytest-mock \ No newline at end of file diff --git a/tests/test_check_git_version.py b/tests/test_check_git_version.py new file mode 100644 index 00000000..51c57b14 --- /dev/null +++ b/tests/test_check_git_version.py @@ -0,0 +1,24 @@ +from unittest.mock import patch +from pydriller.utils.check_git_version import CheckGitVersion, GitVersion +from contextlib import nullcontext as does_not_raise + +import pytest + + +@pytest.mark.parametrize( + "version_number,expectation", + [ + ("3.2.0", does_not_raise()), + ("2.38.1", does_not_raise()), + ("2.0.0", pytest.raises(GitVersion)), + ], +) +def test_extracts_correct_version(version_number, expectation): + with patch( + "pydriller.utils.check_git_version.subprocess.check_output" + ) as mock_git_version: + mock_git_version().decode.return_value.strip.return_value = ( + f"git version {version_number}" + ) + with expectation: + CheckGitVersion().check_git_version()