88from gitdb .exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614
99from git .compat import safe_decode
1010
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+ # ------------------------------------------------------------------
1118
1219class GitError (Exception ):
1320""" Base class for all package exceptions """
@@ -37,7 +44,9 @@ class CommandError(GitError):
3744#: "'%s' failed%s"
3845_msg = "Cmd('%s') failed%s"
3946
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 :
4150if not isinstance (command , (tuple ,list )):
4251command = command .split ()
4352self .command = command
@@ -53,28 +62,33 @@ def __init__(self, command, status=None, stderr=None, stdout=None):
5362status = "'%s'" % s if isinstance (status ,str )else s
5463
5564self ._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 )
5766self ._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 ''
6069
61- def __str__ (self ):
70+ def __str__ (self )-> str :
6271return (self ._msg + "\n cmdline: %s%s%s" )% (
6372self ._cmd ,self ._cause ,self ._cmdline ,self .stdout ,self .stderr )
6473
6574
6675class GitCommandNotFound (CommandError ):
6776"""Thrown if we cannot find the `git` executable in the PATH or at the path given by
6877 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 :
7080super (GitCommandNotFound ,self ).__init__ (command ,cause )
7181self ._msg = "Cmd('%s') not found%s"
7282
7383
7484class GitCommandError (CommandError ):
7585""" Thrown if execution of the git command fails with non-zero status code. """
7686
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 :
7892super (GitCommandError ,self ).__init__ (command ,status ,stderr ,stdout )
7993
8094
@@ -92,13 +106,13 @@ class CheckoutError(GitError):
92106 were checked out successfully and hence match the version stored in the
93107 index"""
94108
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 :
96110Exception .__init__ (self ,message )
97111self .failed_files = failed_files
98112self .failed_reasons = failed_reasons
99113self .valid_files = valid_files
100114
101- def __str__ (self ):
115+ def __str__ (self )-> str :
102116return Exception .__str__ (self )+ ":%s" % self .failed_files
103117
104118
@@ -116,17 +130,18 @@ class HookExecutionError(CommandError):
116130"""Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned
117131 via standard output"""
118132
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 :
120135super (HookExecutionError ,self ).__init__ (command ,status ,stderr ,stdout )
121136self ._msg = "Hook('%s') failed%s"
122137
123138
124139class RepositoryDirtyError (GitError ):
125140"""Thrown whenever an operation on a repository fails as it has uncommitted changes that would be overwritten"""
126141
127- def __init__ (self ,repo ,message ) :
142+ def __init__ (self ,repo : Repo ,message : str ) -> None :
128143self .repo = repo
129144self .message = message
130145
131- def __str__ (self ):
146+ def __str__ (self )-> str :
132147return "Operation cannot be performed on %r: %s" % (self .repo ,self .message )