8
8
from gitdb .exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614
9
9
from git .compat import safe_decode
10
10
11
+ # typing ----------------------------------------------------
12
+
13
+ from git .repo .base import Repo
14
+ from git .types import PathLike
15
+ from typing import IO , List , Optional , Sequence , Tuple , Union
16
+
17
+ # ------------------------------------------------------------------
11
18
12
19
class GitError (Exception ):
13
20
""" Base class for all package exceptions """
@@ -37,7 +44,9 @@ class CommandError(GitError):
37
44
#: "'%s' failed%s"
38
45
_msg = "Cmd('%s') failed%s"
39
46
40
- def __init__ (self , command , status = None , stderr = None , stdout = None ):
47
+ def __init__ (self , command : Union [List [str ], Tuple [str , ...], str ],
48
+ status : Union [str , None , Exception ] = None ,
49
+ stderr : Optional [IO [str ]] = None , stdout : Optional [IO [str ]] = None ) -> None :
41
50
if not isinstance (command , (tuple , list )):
42
51
command = command .split ()
43
52
self .command = command
@@ -53,28 +62,33 @@ def __init__(self, command, status=None, stderr=None, stdout=None):
53
62
status = "'%s'" % s if isinstance (status , str ) else s
54
63
55
64
self ._cmd = safe_decode (command [0 ])
56
- self ._cmdline = ' ' .join (safe_decode (i ) for i in command )
65
+ self ._cmdline = ' ' .join (str ( safe_decode (i ) ) for i in command )
57
66
self ._cause = status and " due to: %s" % status or "!"
58
- self .stdout = stdout and "\n stdout: '%s'" % safe_decode (stdout ) or ''
59
- self .stderr = stderr and "\n stderr: '%s'" % safe_decode (stderr ) or ''
67
+ self .stdout = stdout and "\n stdout: '%s'" % safe_decode (str ( stdout ) ) or ''
68
+ self .stderr = stderr and "\n stderr: '%s'" % safe_decode (str ( stderr ) ) or ''
60
69
61
- def __str__ (self ):
70
+ def __str__ (self ) -> str :
62
71
return (self ._msg + "\n cmdline: %s%s%s" ) % (
63
72
self ._cmd , self ._cause , self ._cmdline , self .stdout , self .stderr )
64
73
65
74
66
75
class GitCommandNotFound (CommandError ):
67
76
"""Thrown if we cannot find the `git` executable in the PATH or at the path given by
68
77
the GIT_PYTHON_GIT_EXECUTABLE environment variable"""
69
- def __init__ (self , command , cause ):
78
+
79
+ def __init__ (self , command : Union [List [str ], Tuple [str ], str ], cause : Union [str , Exception ]) -> None :
70
80
super (GitCommandNotFound , self ).__init__ (command , cause )
71
81
self ._msg = "Cmd('%s') not found%s"
72
82
73
83
74
84
class GitCommandError (CommandError ):
75
85
""" Thrown if execution of the git command fails with non-zero status code. """
76
86
77
- def __init__ (self , command , status , stderr = None , stdout = None ):
87
+ def __init__ (self , command : Union [List [str ], Tuple [str , ...], str ],
88
+ status : Union [str , None , Exception ] = None ,
89
+ stderr : Optional [IO [str ]] = None ,
90
+ stdout : Optional [IO [str ]] = None ,
91
+ ) -> None :
78
92
super (GitCommandError , self ).__init__ (command , status , stderr , stdout )
79
93
80
94
@@ -92,13 +106,13 @@ class CheckoutError(GitError):
92
106
were checked out successfully and hence match the version stored in the
93
107
index"""
94
108
95
- def __init__ (self , message , failed_files , valid_files , failed_reasons ) :
109
+ def __init__ (self , message : str , failed_files : List [ PathLike ] , valid_files : List [ PathLike ] , failed_reasons : List [ str ]) -> None :
96
110
Exception .__init__ (self , message )
97
111
self .failed_files = failed_files
98
112
self .failed_reasons = failed_reasons
99
113
self .valid_files = valid_files
100
114
101
- def __str__ (self ):
115
+ def __str__ (self ) -> str :
102
116
return Exception .__str__ (self ) + ":%s" % self .failed_files
103
117
104
118
@@ -116,17 +130,18 @@ class HookExecutionError(CommandError):
116
130
"""Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned
117
131
via standard output"""
118
132
119
- def __init__ (self , command , status , stderr = None , stdout = None ):
133
+ def __init__ (self , command : Union [List [str ], Tuple [str , ...], str ], status : Optional [str ],
134
+ stderr : Optional [IO [str ]] = None , stdout : Optional [IO [str ]] = None ) -> None :
120
135
super (HookExecutionError , self ).__init__ (command , status , stderr , stdout )
121
136
self ._msg = "Hook('%s') failed%s"
122
137
123
138
124
139
class RepositoryDirtyError (GitError ):
125
140
"""Thrown whenever an operation on a repository fails as it has uncommitted changes that would be overwritten"""
126
141
127
- def __init__ (self , repo , message ) :
142
+ def __init__ (self , repo : Repo , message : str ) -> None :
128
143
self .repo = repo
129
144
self .message = message
130
145
131
- def __str__ (self ):
146
+ def __str__ (self ) -> str :
132
147
return "Operation cannot be performed on %r: %s" % (self .repo , self .message )
0 commit comments