From 0339e7c306f1561147138a84215d6f5485d0edf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Luis=20Cano=20Rodr=C3=ADguez?= Date: Sat, 26 Aug 2023 15:43:56 +0200 Subject: [PATCH] Replace GIT_VERSION constant with lazy function --- copier/vcs.py | 15 +++++---------- tests/test_vcs.py | 4 ++-- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/copier/vcs.py b/copier/vcs.py index 68684c1e1..e1516d0f4 100644 --- a/copier/vcs.py +++ b/copier/vcs.py @@ -20,16 +20,10 @@ def get_git(): return local["git"] -try: - GIT_VERSION = Version(re.findall(r"\d+\.\d+\.\d+", get_git()("version"))[0]) -except OSError: - import warnings - - warnings.warn( - "git command not found, some functionality might not work", UserWarning - ) +def get_git_version(): + git = get_git() - GIT_VERSION = None # type: ignore + return Version(re.findall(r"\d+\.\d+\.\d+", git("version"))[0]) GIT_PREFIX = ("git@", "git://", "git+", "https://github.com/", "https://gitlab.com/") @@ -161,10 +155,11 @@ def clone(url: str, ref: OptStr = None) -> str: Reference to checkout. For Git repos, defaults to `HEAD`. """ git = get_git() + git_version = get_git_version() location = mkdtemp(prefix=f"{__name__}.clone.") _clone = git["clone", "--no-checkout", url, location] # Faster clones if possible - if GIT_VERSION >= Version("2.27"): + if git_version >= Version("2.27"): url_match = re.match("(file://)?(.*)", url) if url_match is not None: file_url = url_match.groups()[-1] diff --git a/tests/test_vcs.py b/tests/test_vcs.py index ce414a5d6..b6cede801 100644 --- a/tests/test_vcs.py +++ b/tests/test_vcs.py @@ -11,7 +11,7 @@ from copier import Worker, run_copy, run_update from copier.errors import ShallowCloneWarning -from copier.vcs import GIT_VERSION, checkout_latest_tag, clone, get_repo +from copier.vcs import get_git_version, checkout_latest_tag, clone, get_repo def test_get_repo() -> None: @@ -93,7 +93,7 @@ def test_shallow_clone(tmp_path: Path, recwarn: pytest.WarningsRecorder) -> None git("clone", "--depth=2", "https://github.com/copier-org/autopretty.git", src_path) assert Path(src_path, "README.md").exists() - if GIT_VERSION >= Version("2.27"): + if get_git_version() >= Version("2.27"): with pytest.warns(ShallowCloneWarning): local_tmp = clone(str(src_path)) else: