Skip to content

Commit

Permalink
Replace GIT_VERSION constant with lazy function
Browse files Browse the repository at this point in the history
  • Loading branch information
astrojuanlu committed Aug 26, 2023
1 parent fb395a5 commit 0339e7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
15 changes: 5 additions & 10 deletions copier/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0339e7c

Please sign in to comment.