10
10
from packaging import version
11
11
from packaging .version import InvalidVersion , Version
12
12
from plumbum import TF , ProcessExecutionError , colors , local
13
- from plumbum .cmd import git
14
13
15
14
from .errors import DirtyLocalWarning , ShallowCloneWarning
16
15
from .types import OptBool , OptStr , StrOrPath
17
16
17
+
18
+ def get_git ():
19
+ """Gets `git` command, or fails if it's not available"""
20
+ return local ["git" ]
21
+
22
+
23
+ def get_git_version ():
24
+ git = get_git ()
25
+
26
+ return Version (re .findall (r"\d+\.\d+\.\d+" , git ("version" ))[0 ])
27
+
28
+
18
29
GIT_PREFIX = ("git@" , "git://" , "git+" , "https://github.com/" , "https://gitlab.com/" )
19
30
GIT_POSTFIX = ".git"
20
- GIT_VERSION = Version (re .findall (r"\d+\.\d+\.\d+" , git ("version" ))[0 ])
21
31
REPLACEMENTS = (
22
32
(re .compile (r"^gh:/?(.*\.git)$" ), r"https://github.com/\1" ),
23
33
(re .compile (r"^gh:/?(.*)$" ), r"https://github.com/\1.git" ),
@@ -30,15 +40,15 @@ def is_git_repo_root(path: StrOrPath) -> bool:
30
40
"""Indicate if a given path is a git repo root directory."""
31
41
try :
32
42
with local .cwd (Path (path , ".git" )):
33
- return git ("rev-parse" , "--is-inside-git-dir" ).strip () == "true"
43
+ return get_git () ("rev-parse" , "--is-inside-git-dir" ).strip () == "true"
34
44
except OSError :
35
45
return False
36
46
37
47
38
48
def is_in_git_repo (path : StrOrPath ) -> bool :
39
49
"""Indicate if a given path is in a git repo directory."""
40
50
try :
41
- git ("-C" , path , "rev-parse" , "--show-toplevel" )
51
+ get_git () ("-C" , path , "rev-parse" , "--show-toplevel" )
42
52
return True
43
53
except (OSError , ProcessExecutionError ):
44
54
return False
@@ -47,7 +57,10 @@ def is_in_git_repo(path: StrOrPath) -> bool:
47
57
def is_git_shallow_repo (path : StrOrPath ) -> bool :
48
58
"""Indicate if a given path is a git shallow repo directory."""
49
59
try :
50
- return git ("-C" , path , "rev-parse" , "--is-shallow-repository" ).strip () == "true"
60
+ return (
61
+ get_git ()("-C" , path , "rev-parse" , "--is-shallow-repository" ).strip ()
62
+ == "true"
63
+ )
51
64
except (OSError , ProcessExecutionError ):
52
65
return False
53
66
@@ -58,8 +71,8 @@ def is_git_bundle(path: Path) -> bool:
58
71
path = path .resolve ()
59
72
with TemporaryDirectory (prefix = f"{ __name__ } .is_git_bundle." ) as dirname :
60
73
with local .cwd (dirname ):
61
- git ("init" )
62
- return bool (git ["bundle" , "verify" , path ] & TF )
74
+ get_git () ("init" )
75
+ return bool (get_git () ["bundle" , "verify" , path ] & TF )
63
76
64
77
65
78
def get_repo (url : str ) -> OptStr :
@@ -107,6 +120,7 @@ def checkout_latest_tag(local_repo: StrOrPath, use_prereleases: OptBool = False)
107
120
use_prereleases:
108
121
If `False`, skip prerelease git tags.
109
122
"""
123
+ git = get_git ()
110
124
with local .cwd (local_repo ):
111
125
all_tags = filter (valid_version , git ("tag" ).split ())
112
126
if not use_prereleases :
@@ -140,10 +154,12 @@ def clone(url: str, ref: OptStr = None) -> str:
140
154
ref:
141
155
Reference to checkout. For Git repos, defaults to `HEAD`.
142
156
"""
157
+ git = get_git ()
158
+ git_version = get_git_version ()
143
159
location = mkdtemp (prefix = f"{ __name__ } .clone." )
144
160
_clone = git ["clone" , "--no-checkout" , url , location ]
145
161
# Faster clones if possible
146
- if GIT_VERSION >= Version ("2.27" ):
162
+ if git_version >= Version ("2.27" ):
147
163
url_match = re .match ("(file://)?(.*)" , url )
148
164
if url_match is not None :
149
165
file_url = url_match .groups ()[- 1 ]
0 commit comments