8
8
import os
9
9
import subprocess
10
10
from pathlib import Path
11
+ from typing import Any , List
11
12
12
13
from system .temporary_directory import TemporaryDirectory
13
14
14
15
15
16
class GitRepository :
17
+ dir : str
16
18
"""
17
19
This class checks out a Git repository at a particular ref into an empty named directory (or temporary a directory if no named directory is given).
18
20
Temporary directories will be automatically deleted when the GitRepository object goes out of scope; named directories will be left alone.
19
21
Clients can obtain the actual commit ID by querying the "sha" attribute, and the temp directory name with "dir".
20
22
"""
21
23
22
- def __init__ (self , url , ref , directory = None , working_subdirectory = None ):
24
+ def __init__ (self , url : str , ref : str , directory : str = None , working_subdirectory : str = None ) -> None :
23
25
self .url = url
24
26
self .ref = ref
25
27
if directory is None :
@@ -32,15 +34,14 @@ def __init__(self, url, ref, directory=None, working_subdirectory=None):
32
34
self .working_subdirectory = working_subdirectory
33
35
self .__checkout__ ()
34
36
35
- def __enter__ (self ):
37
+ def __enter__ (self ) -> 'GitRepository' :
36
38
return self
37
39
38
- def __exit__ (self , exc_type , exc_value , exc_traceback ) :
40
+ def __exit__ (self , exc_type : Any , exc_value : Any , exc_traceback : Any ) -> None :
39
41
if self .temp_dir :
40
42
self .temp_dir .__exit__ (exc_type , exc_value , exc_traceback )
41
43
42
- def __checkout__ (self ):
43
- # Check out the repository
44
+ def __checkout__ (self ) -> None :
44
45
self .execute_silent ("git init" , self .dir )
45
46
self .execute_silent (f"git remote add origin { self .url } " , self .dir )
46
47
self .execute_silent (f"git fetch --depth 1 origin { self .ref } " , self .dir )
@@ -49,18 +50,18 @@ def __checkout__(self):
49
50
logging .info (f"Checked out { self .url } @{ self .ref } into { self .dir } at { self .sha } " )
50
51
51
52
@property
52
- def working_directory (self ):
53
+ def working_directory (self ) -> str :
53
54
if self .working_subdirectory :
54
55
return os .path .join (self .dir , self .working_subdirectory )
55
56
else :
56
57
return self .dir
57
58
58
59
@classmethod
59
- def stable_ref (self , url , ref ) :
60
+ def stable_ref (self , url : str , ref : str ) -> List [ str ] :
60
61
results = subprocess .check_output (f"git ls-remote { url } { ref } " , shell = True ).decode ().strip ().split ("\t " )
61
62
return results if len (results ) > 1 else [ref , ref ]
62
63
63
- def execute_silent (self , command , cwd = None ):
64
+ def execute_silent (self , command : str , cwd : str = None ) -> None :
64
65
cwd = cwd or self .working_directory
65
66
logging .info (f'Executing "{ command } " in { cwd } ' )
66
67
subprocess .check_call (
@@ -71,17 +72,17 @@ def execute_silent(self, command, cwd=None):
71
72
stderr = subprocess .DEVNULL ,
72
73
)
73
74
74
- def output (self , command , cwd = None ):
75
+ def output (self , command : str , cwd : str = None ) -> str :
75
76
cwd = cwd or self .working_directory
76
77
logging .info (f'Executing "{ command } " in { cwd } ' )
77
78
return subprocess .check_output (command , cwd = cwd , shell = True ).decode ().strip ()
78
79
79
- def execute (self , command , cwd = None ):
80
+ def execute (self , command : str , cwd : str = None ) -> None :
80
81
cwd = cwd or self .working_directory
81
82
logging .info (f'Executing "{ command } " in { cwd } ' )
82
83
subprocess .check_call (command , cwd = cwd , shell = True )
83
84
84
- def path (self , subdirname = None ):
85
+ def path (self , subdirname : str = None ) -> Path :
85
86
dirname = self .dir
86
87
if subdirname :
87
88
dirname = os .path .join (self .dir , subdirname )
0 commit comments