From c83f4b01aa621f936edff4bcb58cf5497e2eb516 Mon Sep 17 00:00:00 2001 From: Peter Scheibel Date: Mon, 21 Jun 2021 17:53:12 -0700 Subject: Fetching: git on Mac OS (#24247) Extend the changes in #24163 to unit tests. --- lib/spack/spack/fetch_strategy.py | 15 ++++++++++++--- lib/spack/spack/test/cmd/is_git_repo.py | 2 +- lib/spack/spack/test/git_fetch.py | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/fetch_strategy.py b/lib/spack/spack/fetch_strategy.py index da0b12556f..b4ca8b3e70 100644 --- a/lib/spack/spack/fetch_strategy.py +++ b/lib/spack/spack/fetch_strategy.py @@ -766,6 +766,8 @@ class GitFetchStrategy(VCSFetchStrategy): optional_attrs = ['tag', 'branch', 'commit', 'submodules', 'get_full_repo', 'submodules_delete'] + git_version_re = r'git version (\S+)' + def __init__(self, **kwargs): # Discards the keywords in kwargs that may conflict with the next call # to __init__ @@ -780,9 +782,16 @@ class GitFetchStrategy(VCSFetchStrategy): @property def git_version(self): - output = self.git('--version', output=str, error=str) - match = re.search(r'git version (\S+)', output) - return Version(match.group(1)) if match else None + return GitFetchStrategy.version_from_git(self.git) + + @staticmethod + def version_from_git(git_exe): + """Given a git executable, return the Version (this will fail if + the output cannot be parsed into a valid Version). + """ + version_output = git_exe('--version', output=str) + m = re.search(GitFetchStrategy.git_version_re, version_output) + return Version(m.group(1)) @property def git(self): diff --git a/lib/spack/spack/test/cmd/is_git_repo.py b/lib/spack/spack/test/cmd/is_git_repo.py index 109f5030c7..fcfd1e010d 100644 --- a/lib/spack/spack/test/cmd/is_git_repo.py +++ b/lib/spack/spack/test/cmd/is_git_repo.py @@ -28,7 +28,7 @@ def check_git_version(): Refer: https://github.com/git/git/commit/cc73385cf6c5c229458775bc92e7dbbe24d11611 """ - git_version = ver(git('--version', output=str).lstrip('git version ')) + git_version = spack.fetch_strategy.GitFetchStrategy.version_from_git(git) return git_version >= ver(git_required_version) diff --git a/lib/spack/spack/test/git_fetch.py b/lib/spack/spack/test/git_fetch.py index ad010e3ca5..e06077df83 100644 --- a/lib/spack/spack/test/git_fetch.py +++ b/lib/spack/spack/test/git_fetch.py @@ -37,7 +37,8 @@ def git_version(request, monkeypatch): use the backward-compatibility code paths with newer git versions. """ git = which('git', required=True) - real_git_version = ver(git('--version', output=str).lstrip('git version ')) + real_git_version = ( + spack.fetch_strategy.GitFetchStrategy.version_from_git(git)) if request.param is None: # Don't patch; run with the real git_version method. -- cgit v1.2.3-60-g2f50