31
31
PRIMITIVES = (str , int , float , bool )
32
32
33
33
34
- def check_output (command : List [str ], suppress_stderr : bool = True ) -> str :
34
+ def check_output (command : List [str ], suppress_stderr : bool = True , ** kwargs ) -> str :
35
35
"""Runs subprocess.check_output and returns the result as a string.
36
36
37
37
:param command: A list of strings representing the command to run on the command line.
@@ -40,77 +40,79 @@ def check_output(command: List[str], suppress_stderr: bool = True) -> str:
40
40
"""
41
41
with open (os .devnull , 'w' ) as devnull :
42
42
devnull = devnull if suppress_stderr else None
43
- output = subprocess .check_output (command , stderr = devnull ).decode ('utf-8' ).strip ()
43
+ output = subprocess .check_output (command , stderr = devnull , ** kwargs ).decode ('utf-8' ).strip ()
44
44
return output
45
45
46
46
47
- def has_git () -> bool :
48
- """Returns whether git is installed.
47
+ class GitInfo :
48
+ """Class with helper methods for extracting information about a git repo."""
49
49
50
- :return: True if git is installed, False otherwise.
51
- """
52
- try :
53
- output = check_output (['git' , 'rev-parse' , '--is-inside-work-tree' ])
54
- return output == 'true'
55
- except (FileNotFoundError , subprocess .CalledProcessError ):
56
- return False
50
+ def __init__ (self , repo_path : str ):
51
+ self .repo_path = repo_path
57
52
53
+ def has_git (self ) -> bool :
54
+ """Returns whether git is installed.
58
55
59
- def get_git_root () -> str :
60
- """Gets the root directory of the git repo where the command is run.
61
-
62
- :return: The root directory of the current git repo.
63
- """
64
- return check_output ([ 'git' , 'rev-parse' , '--show-toplevel' ])
65
-
56
+ :return: True if git is installed, False otherwise.
57
+ """
58
+ try :
59
+ output = check_output ([ ' git' , 'rev-parse' , '--is-inside-work-tree' ], cwd = self . repo_path )
60
+ return output == 'true'
61
+ except ( FileNotFoundError , subprocess . CalledProcessError ):
62
+ return False
66
63
67
- def get_git_url ( commit_hash : bool = True ) -> str :
68
- """Gets the https url of the git repo where the command is run.
64
+ def get_git_root ( self ) -> str :
65
+ """Gets the root directory of the git repo where the command is run.
69
66
70
- :param commit_hash: If True, the url links to the latest local git commit hash.
71
- If False, the url links to the general git url.
72
- :return: The https url of the current git repo.
73
- """
74
- # Get git url (either https or ssh)
75
- try :
76
- url = check_output (['git' , 'remote' , 'get-url' , 'origin' ])
77
- except subprocess .CalledProcessError :
78
- # For git versions <2.0
79
- url = check_output (['git' , 'config' , '--get' , 'remote.origin.url' ])
67
+ :return: The root directory of the current git repo.
68
+ """
69
+ return check_output (['git' , 'rev-parse' , '--show-toplevel' ], cwd = self .repo_path )
80
70
81
- # Remove .git at end
82
- url = url [: - len ( '. git' )]
71
+ def get_git_url ( self , commit_hash : bool = True ) -> str :
72
+ """Gets the https url of the git repo where the command is run.
83
73
84
- # Convert ssh url to https url
85
- m = re .search ('git@(.+):' , url )
86
- if m is not None :
87
- domain = m .group (1 )
88
- path = url [m .span ()[1 ]:]
89
- url = f'https://{ domain } /{ path } '
74
+ :param commit_hash: If True, the url links to the latest local git commit hash.
75
+ If False, the url links to the general git url.
76
+ :return: The https url of the current git repo.
77
+ """
78
+ # Get git url (either https or ssh)
79
+ try :
80
+ url = check_output (['git' , 'remote' , 'get-url' , 'origin' ], cwd = self .repo_path )
81
+ except subprocess .CalledProcessError :
82
+ # For git versions <2.0
83
+ url = check_output (['git' , 'config' , '--get' , 'remote.origin.url' ], cwd = self .repo_path )
90
84
91
- if commit_hash :
92
- # Add tree and hash of current commit
93
- url = f'{ url } /tree/{ get_git_hash ()} '
85
+ # Remove .git at end
86
+ url = url [:- len ('.git' )]
94
87
95
- return url
88
+ # Convert ssh url to https url
89
+ m = re .search ('git@(.+):' , url )
90
+ if m is not None :
91
+ domain = m .group (1 )
92
+ path = url [m .span ()[1 ]:]
93
+ url = f'https://{ domain } /{ path } '
96
94
95
+ if commit_hash :
96
+ # Add tree and hash of current commit
97
+ url = f'{ url } /tree/{ self .get_git_hash ()} '
97
98
98
- def get_git_hash () -> str :
99
- """Gets the git hash of HEAD of the git repo where the command is run.
99
+ return url
100
100
101
- :return: The git hash of HEAD of the current git repo.
102
- """
103
- return check_output (['git' , 'rev-parse' , 'HEAD' ])
101
+ def get_git_hash (self ) -> str :
102
+ """Gets the git hash of HEAD of the git repo where the command is run.
104
103
104
+ :return: The git hash of HEAD of the current git repo.
105
+ """
106
+ return check_output (['git' , 'rev-parse' , 'HEAD' ], cwd = self .repo_path )
105
107
106
- def has_uncommitted_changes () -> bool :
107
- """Returns whether there are uncommitted changes in the git repo where the command is run.
108
+ def has_uncommitted_changes (self ) -> bool :
109
+ """Returns whether there are uncommitted changes in the git repo where the command is run.
108
110
109
- :return: True if there are uncommitted changes in the current git repo, False otherwise.
110
- """
111
- status = check_output (['git' , 'status' ])
111
+ :return: True if there are uncommitted changes in the current git repo, False otherwise.
112
+ """
113
+ status = check_output (['git' , 'status' ], cwd = self . repo_path )
112
114
113
- return not status .endswith (NO_CHANGES_STATUS )
115
+ return not status .endswith (NO_CHANGES_STATUS )
114
116
115
117
116
118
def type_to_str (type_annotation : Union [type , Any ]) -> str :
0 commit comments